I have a header div
that I'd like to adopt the same width as its parent div
, with the content centered. As of now, the div
is shrunk to the width of its content, putting it off-center.
What did not work:
- According to this thread, inheriting the width from the parent div should be the default behavior or can be reinforcable with
width: auto
, but this does not seem to take effect on my site. - Setting
width: 100%
makes the header too wide. - Changing to
position: relative
is not an option for me because then the header will no longer be sticky. - Hard-coding the width to a fixed width is not ideal because I want the page to be resizable.
- Specifying a fixed
max-width
likewise makes the header shrink to its content width. width: inherit
doesn't have an effect, either.
Questions:
Why does the solution proposed in the other thread not apply in my case?
How to I make header
have the same width as container
?
body {
max-width: 1200px;
margin: auto;
}
nav {
height: 100%;
width: 250px;
position: fixed;
overflow-x: hidden;
margin-top: 100px;
}
#container {
margin-left: 250px;
}
#header {
position: fixed;
top: 0;
height: 100px;
background: white;
border: 1px solid lightgray;
}
<!DOCTYPE html>
<html id="top">
<head>
<link rel="stylesheet" href="style.css">
</script>
</head>
<body>
<nav id="nav">
<li><a href="#sec1">Section 1</a></li>
<li><a href="#sec2">Section 2</a></li>
<li><a href="#sec3">Section 3</a></li>
</nav>
<div id="container">
<div id="header">
<h1><a href="#top">Title</a></h1>
</div>
<div id="content">
<section id="sec1">
<h2><a href="#sec1">Section 1</a></h2>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p></section<
</div>
</div>
</body>
</html>