Learning CSS basics and was doing layouts with floats. I know CSS Grid and Flexbox are best practice and I intend to learn them, but wanted to understand the basics of floats before I move on.
body {
margin: 0px;
padding: 0px;
}
.wrapper {
width: 600px;
padding-left: 200px;
padding-right: 200px;
overflow: hidden;
}
.col-main {
float: left;
width: 100%;
}
.col-left {
position: relative;
float: left;
width: 200px;
margin-left: -100%;
right: 200px;
}
.col-right {
float: left;
width: 125px;
margin-right: -125px;
}
<body>
<div class="wrapper">
<div class="col-main">Main Content</div>
<div class="col-left">Left Content</div>
<div class="col-right">Right Content</div>
</div>
</body>
On an intuitive level I understand how .col-left
works. margin-left: -100%;
represents 600px width of the parent element placing it along side .col-main
and position: relative;
combined with right: 200px;
puts it flush within the reserved left padding of the parent element. In fact, it doesn't have to be -100%
it can be -200%
if you like and push .col-left
completely off the screen.
However, I cannot grasp how .col-right
works. Having a negative value in margin-right
that's equal to the absolute value in width
places the .col-right
on the same line, neatly sticking out on the right side of the parent element. It's great but I don't understand how or why. What's more, is that if you change margin-right
to -200px
or heck even -1000px
it will still be in the same position. Why?