I just started learning HTML and CSS. And while following a tutorial, I just thought of some experiment.
Question is:
Why do the bottom <div>
s goes outside the main <div>
?
Is it because of the floating property? I think it is, if it's really so, how do I contain these floating bottom divs to the red main container div?
There are similar questions, like this CSS Float Logic but it's kinda complicated for a beginner.
.clr {
clear: both;
}
.container {
width: 80%;
margin: auto;
border: 2px solid royalblue;
background-color: red;
}
.block {
float: left;
width: 33.3%;
border: 1px solid #ccc;
padding: 10px;
box-sizing: border-box;
}
#main-block {
float: left;
width: 70%;
}
#sidebar {
float: right;
width: 30%;
background-color: #333;
color: #ffffff;
padding: 15px;
box-sizing: border-box;
margin: 0;
}
<div class="container">
<div class="block">
<h3>Heading</h3>
<p>“Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.” The purpose of lorem ipsum is to create a natural looking block of text (sentence, paragraph, page, etc.) that doesn't distract
from the layout. A practice not without controversy, laying out pages with meaningless filler text can be very useful when the focus is meant to be on design, not content.</p>
</div>
<div class="block">
<h3>Heading</h3>
<p>“Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.” The purpose of lorem ipsum is to create a natural looking block of text (sentence, paragraph, page, etc.) that doesn't distract
from the layout. A practice not without controversy, laying out pages with meaningless filler text can be very useful when the focus is meant to be on design, not content.</p>
</div>
<div class="block">
<h3>Heading</h3>
<p>“Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.” The purpose of lorem ipsum is to create a natural looking block of text (sentence, paragraph, page, etc.) that doesn't distract
from the layout. A practice not without controversy, laying out pages with meaningless filler text can be very useful when the focus is meant to be on design, not content.</p>
</div>
<div class="clr"></div>
<div id="main-block">
<h3>Heading</h3>
<p>“Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.”
</p>
</div>
<div id="sidebar">
<p>“Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.” .
</p>
</div>
</div>