My query revolves around a header div
that I'm struggling to align with the same width as its parent div
, while keeping the content centered. Currently, the div
is only as wide as its content, causing it to be off-center.
https://i.stack.imgur.com/E1IiG.png
Issues faced:
- Despite information from this thread suggesting that inheriting the width from the parent
div
should happen by default or usingwidth: auto
, this doesn't seem to work for me. - Setting
width: 100%
results in the header being too wide. - Switching to
position: relative
isn't viable as it removes the sticky property of the header. - Assigning a fixed width value isn't preferred because I want the page to remain resizable.
- Even specifying a fixed
max-width
causes the header to shrink to content width. - Using
width: inherit
also has no impact.
Queries:
Why does the solution proposed in the aforementioned thread not work in my scenario?
How can I adjust the header
to have the same width as the 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>