Let's consider a scenario where my website is structured with a header, footer, and main column. The main column holds the article and its table of contents side by side, with the center alignment. The width of the main column isn't fixed due to the presence of the table of contents. I aim to align the header and footer content (enclosed within .main-column
) so that it matches the left edge of the centered main column.
Putting the header and footer inside the <main>
tag for centering is not an option because I want a border (like <hr>
) to span across the entire viewport width.
An alternative solution would involve using JavaScript to set the margin-left
of the header and footer. However, I prefer not to rely on JavaScript for layout purposes but rather reserve it for minor enhancements.
* {
margin: 0;
padding: 0;
}
header {
border-bottom: 1px solid black;
}
footer {
border-top: 1px solid black;
}
.main-column {
/* this adjusts alignment */
margin-left: 5rem;
}
main {
display: flex;
flex-direction: row;
justify-content: center;
align-items: flex-start;
}
article {
max-width: 20rem;
}
nav {
background: gray;
}
ul {
margin-left: 1.5em;
}
<html>
<body>
<header>
<div class="main-column">
<h1>Header here</h1>
</div>
</header>
<main>
<article>
<h1>This is some article</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris mattis leo ut metus egestas porttitor eu ac risus. Fusce convallis posuere diam ut dapibus. Mauris at velit ex. Ut malesuada sollicitudin magna, ac imperdiet ipsum elementum in. In
hac habitasse platea dictumst. Etiam egestas ligula sed erat vestibulum porta. Suspendisse potenti. Donec facilisis risus placerat tincidunt lacinia. Suspendisse potenti. Aliquam in pharetra lectus. Suspendisse sodales, erat iaculis auctor lacinia,
augue dolor lobortis dui, quis fermentum neque nulla ac lacus. Morbi id nulla iaculis, laoreet dui eget, vehicula nulla. Donec eget purus sit amet nibh mollis tempor. Proin hendrerit nisi quis orci placerat ullamcorper. Integer congue mauris dui,
vel tempor libero ultrices non. Aliquam in nisl magna.</p>
</article>
<nav>
<h3>Table of contents</h3>
<ul>
<li>Some</li>
<li>links</li>
<li>here</li>
</ul>
</nav>
</main>
<footer>
<div class="main-column">
<h1>Footer here</h1>
</div>
</footer>
</body>
</html>