Splitting sections horizontally
There are various methods to achieve this, with HTML
elements typically stacked on top of each other by default. However, if you want to keep a fixed header at the top and a persistent footer at the bottom even when scrolling, you can utilize the position: fixed
property with adjustments to top, left, bottom, and right
values based on your design requirements. In the example below, we fix the div
with the class header
to the top of the screen with top: 0
, spanning the full width with left: 0;
and right: 0;
properties. The same applies to the .footer
, which is fixed to the bottom using bottom: 0;
. The div
with class body
contains the remaining content, requiring a margin-top
equal to the height of .header
to prevent content from being hidden beneath it, as well as a similar adjustment for margin-bottom
according to the height of .footer
.
Dividing the body vertically (with responsiveness)
This can be achieved by setting element widths in percentages. For instance, if you want to divide the .body
into three columns, each should take up one-third (33.33%) of the total width via width: 33.333%
. To display inner div
s on the same line, set the display
property to inline
(or another inline value) while ensuring margin
is zero since it does not factor into the width calculation.
While there are numerous alternatives available, here's an example:
.header {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 70px;
background: #4286f4;
text-align: center;
}
.footer {
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 70px;
background: black;
color: white;
text-align: center;
}
.body {
background: green;
margin: 70px 0;
padding: 0;
width: 100%;
text-align: center;
}
.body_v1, .body_v2, .body_v3 {
height: 100px;
width: 33.333%;
border: 0;
display: inline-block;
margin: 0;
float: left;
padding: 0;
text-align: center;
}
.body_v1 {
background: #42f465;
}
.body_v2 {
background: #108928;
}
.body_v3 {
background: #034210;
}
<div class="header">header</div>
<div class="body">
<div class="body_v1">a</div>
<div class="body_v2">b</div>
<div class="body_v3">c</div>
</div>
<div class="footer">footer</div>
In conclusion, my recommendation would be to utilize a third-party framework for such layouts instead of starting from scratch. There are various options available online, enabling you to select the one that best suits your needs. Check out multiple examples and find the most suitable solution for you.