CSS - Float

by Rodwyn Moreno

HTML

<h1>CSS - Float</h1>
<p>
  The float property overwrites the default positioning and pushes an element to the left or the right of the page. This take it out of the document flow.  
</p>
<p>
  It's great for positioning an image in text and make sure that the text actually flows around the image.
</p>
<div class="container">
  <img class="logo" src="https://workylab.com/img/logo.svg" alt="workylab">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
<p>
  It's not as great for positioning block level elements because the text will respond to it, the other block element won't.
</p>
<p>
  <strong>Hack</strong><br>
  It's necessary keep its space reserved and tell the other block level elements that come after it that they shouldn't respect any previous floating. To do that is using the clear both property, which means clear floats on both sides, left and right.
</p>

<div class="parent">
  <div class="child">A</div>
  <div class="child">B</div>
  <div class="clearfix"></div>
  <div class="child">C</div>
</div>

CSS

*{
  font-family: sans-serif;
}

.parent {
  border: red solid 2px;
  margin-bottom: 20px;
}

.child {
  background-color: rgba(236, 120, 120, 0.4);
  width: 60%;
  padding: 8px 4px;
}

.child:nth-child(1) {
  background-color: rgba(180, 120, 120, 0.4);
}

.child:nth-child(2) {
  background-color: rgba(80, 120, 120, 0.4);
  float: right;
}

.clearfix {
  clear: both;
}

.container {
  border: #E0BC0A solid 2px;
  padding: 10px;
}

.logo {
  width: 70px;
  height: 26.31px;
  float: right;
}