DEV Community

Cover image for CSS (FLOAT, COMBINATORS AND OVERFLOW)
Maame Afia Fordjour
Maame Afia Fordjour

Posted on • Edited on

CSS (FLOAT, COMBINATORS AND OVERFLOW)

INTRODUCTION

Hey there guys! Still on my learning journey and currently on my w3schools CSS tutorials. I should have been far ahead and probably on JavaScript but the reality is, procrastination is a hell of a drug and everyone has a different learning curve. Today is day 7 of my #100daysofcode challenge and I studied about CSS float, combinators and overflow. I am first going to give an overall summary of the three, and then explain them singlehandedly using simple terms.

The CSS float property is there to specify how an element should float. It is mainly used to position and format content. The CSS combinators explain the relationship between selectors. There are four types of combinators in CSS which would be discussed earlier on. The CSS overflow property controls what happens to content that is too big to fit in an area.

FLOAT PROPERTY

The float property is used to wrap text around images and can have these values;
📌Inherit: Elements inherits the float values of its parents
📌None: Elements does not float (default)
📌Right: Elements float to the right of its container
📌Left: Elements float to the left of its container

FLOAT RIGHT

<!DOCTYPE html>
<html>
<head>
<style>
img {
  float: right;
}
</style>
</head>
<body>

<p>In this example, the image will float to the right in the paragraph, and the text in the paragraph will wrap around the image.</p>

<p><img src="pineapple.jpg" alt="Pineapple" style="width:170px;height:170px;margin-left:15px;">

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut aliquet. Mauris ante ligula, facilisis sed ornare eu, lobortis in odio. Praesent convallis urna a lacus interdum ut hendrerit risus congue. Nunc sagittis dictum nisi, sed ullamcorper ipsum dignissim ac. In at libero sed nunc venenatis imperdiet sed ornare turpis. Donec vitae dui eget tellus gravida venenatis. Integer fringilla congue eros non fermentum. Sed dapibus pulvinar nibh tempor porta. Cras ac leo purus. Mauris quis diam velit.</p>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

The block of code above would display a photo wrapped on the right side of the page with the text next to it. This would be displayed as;

Image description

FLOAT LEFT

<!DOCTYPE html>
<html>
<head>
<style>
img {
  float: left;
}
</style>
</head>
<body>

<p>In this example, the image will float to the left in the paragraph, and the text in the paragraph will wrap around the image.</p>

<p><img src="pineapple.jpg" alt="Pineapple" style="width:170px;height:170px;margin-right:15px;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut aliquet. Mauris ante ligula, facilisis sed ornare eu, lobortis in odio. Praesent convallis urna a lacus interdum ut hendrerit risus congue. Nunc sagittis dictum nisi, sed ullamcorper ipsum dignissim ac. In at libero sed nunc venenatis imperdiet sed ornare turpis. Donec vitae dui eget tellus gravida venenatis. Integer fringilla congue eros non fermentum. Sed dapibus pulvinar nibh tempor porta. Cras ac leo purus. Mauris quis diam velit.</p>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

The block of code above would display the image wrapped to the left side of the page with the text next to it. This would be displayed as;

Image description

NONE
The none value is the default. This would display the image where it occurs in the text.

<!DOCTYPE html>
<html>
<head>
<style>
img {
  float: none;
}
</style>
</head>
<body>

<p>In this example, the image will be displayed just where it occurs in the text (float: none;).</p>

<p><img src="pineapple.jpg" alt="Pineapple" style="width:170px;height:170px;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut aliquet. Mauris ante ligula, facilisis sed ornare eu, lobortis in odio. Praesent convallis urna a lacus interdum ut hendrerit risus congue. Nunc sagittis dictum nisi, sed ullamcorper ipsum dignissim ac. In at libero sed nunc venenatis imperdiet sed ornare turpis. Donec vitae dui eget tellus gravida venenatis. Integer fringilla congue eros non fermentum. Sed dapibus pulvinar nibh tempor porta. Cras ac leo purus. Mauris quis diam velit.</p>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

The above block of code would be displayed as;

Image description

Sometimes, when using the left and right values, the image can be taller than the text on the page. This makes the appearance of the page aesthetically displeasing. There is something known as 'The clearfix hack'. The clearfix hack is when the overflow: auto is added to fix the problem. For example;

<!DOCTYPE html>
<html>
<head>
<style>
div {
  border: 3px solid #4CAF50;
  padding: 5px;
}

.img1 {
  float: right;
}

.clearfix {
  overflow: auto;
}

.img2 {
  float: right;
}
</style>
</head>
<body>

<p>In this example, the image is taller than the element containing it, and it is floated, so it overflows outside of its container:</p>

<div>
  <img class="img1" src="pineapple.jpg" alt="Pineapple" width="170" height="170">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum...
</div>

<p style="clear:right">Add a clearfix class with overflow: auto; to the containing element, to fix this problem:</p>

<div class="clearfix">
  <img class="img2" src="pineapple.jpg" alt="Pineapple" width="170" height="170">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum...
</div>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

The clearfix hack would work when the class 'clearfix' which contains the overflow: auto is added to the div container. The above code would run and display as;

Image description

COMBINATORS

The combinators is quite self explanatory becaUse it explains the relationship between the selectors. In CSS, there are different combinators. These are listed out as;

📌 General sibling selector: This is represented by the (~) symbol. It selects all elements that are siblings of a specified element such as;

<!DOCTYPE html>
<html>
<head>
<style>
div ~ p {
  background-color: yellow;
}
</style>
</head>
<body>

<p>Paragraph 1.</p>

<div>
  <p>Paragraph 2.</p>
</div>

<p>Paragraph 3.</p>
<code>Some code.</code>
<p>Paragraph 4.</p>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

The block of code above would be displayed as;

Image description

📌 Descendant selector: This is represented by a space. It also matches all elements that are descendants of a specified element such as;

<!DOCTYPE html>
<html>
<head>
<style>
div p {
  background-color: yellow;
}
</style>
</head>
<body>

<div>
  <p>Paragraph 1 in the div.</p>
  <p>Paragraph 2 in the div.</p>
  <section><p>Paragraph 3 in the div.</p></section>
</div>

<p>Paragraph 4. Not in a div.</p>
<p>Paragraph 5. Not in a div.</p>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

The block of code above would be displayed as;

Image description

📌Child selector: This is represented by a (>) symbol. It selects all the elements that are children of a specified element.

<!DOCTYPE html>
<html>
<head>
<style>
div > p {
  background-color: yellow;
}
</style>
</head>
<body>

<div>
  <p>Paragraph 1 in the div.</p>
  <p>Paragraph 2 in the div.</p>
  <section><p>Paragraph 3 in the div.</p></section> <!-- not Child but Descendant -->
  <p>Paragraph 4 in the div.</p>
</div>

<p>Paragraph 5. Not in a div.</p>
<p>Paragraph 6. Not in a div.</p>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

The block of code above would be displayed as;

Image description

📌Adjacent sibling selector: This is represented by a (+) symbol. It selects all elements that are adjacent(next to) siblings of a specified element such as;

<!DOCTYPE html>
<html>
<head>
<style>
div + p {
  background-color: yellow;
}
</style>
</head>
<body>

<div>
  <p>Paragraph 1 in the div.</p>
  <p>Paragraph 2 in the div.</p>
</div>

<p>Paragraph 3. Not in a div.</p>
<p>Paragraph 4. Not in a div.</p>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

The block of code above would be displayed as;

Image description

OVERFLOW

The CSS overflow property allows is what controls what happens to content that is normally too huge to fit on the page/screen. So what the overflow property basically does is, it adds a scroll bar to the content or reveals just a part of the content. This will be explained better below;

The overflow property has these values;
📌auto: It adds scrollbars only when necessary.

<!DOCTYPE html>
<html>
<head>
<style>
div {
  background-color: #eee;
  width: 200px;
  height: 50px;
  border: 1px dotted black;
  overflow: auto;
}
</style>
</head>
<body>

<h2>CSS Overflow</h2>
<p>The auto value is similar to scroll, only it add scrollbars when necessary:</p>

<div>You can use the overflow property when you want to have better control of the layout. The overflow property specifies what happens if content overflows an element's box.</div>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

The code above would be displayed as;

Image description
📌visible: Default option;

<!DOCTYPE html>
<html>
<head>
<style>
div {
  background-color: #eee;
  width: 200px;
  height: 50px;
  border: 1px dotted black;
  overflow: visible;
}
</style>
</head>
<body>

<h2>CSS Overflow</h2>
<p>By default, the overflow is visible, meaning that it is not clipped and it renders outside the element's box:</p>

<div>You can use the overflow property when you want to have better control of the layout. The overflow property specifies what happens if content overflows an element's box.</div>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

The block of code will be displayed as;

Image description

📌hidden: The overflow is clipped and some contents are clipped off.

<!DOCTYPE html>
<html>
<head>
<style>
div {
  background-color: #eee;
  width: 200px;
  height: 50px;
  border: 1px dotted black;
  overflow: hidden;
}
</style>
</head>
<body>

<h2>CSS Overflow</h2>
<p>With the hidden value, the overflow is clipped, and the rest of the content is hidden:</p>
<p>Try to remove the overflow property to understand how it works.</p>

<div>You can use the overflow property when you want to have better control of the layout. The overflow property specifies what happens if content overflows an element's box.</div>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

The code above would be displayed as;

Image description

📌scroll: A scrollbar is added to see the rest of the content on the page.

<!DOCTYPE html>
<html>
<head>
<style>
div {
  background-color: #eee;
  width: 200px;
  height: 100px;
  border: 1px dotted black;
  overflow: scroll;
}
</style>
</head>
<body>

<h2>CSS Overflow</h2>
<p>Setting the overflow value to scroll, the overflow is clipped and a scrollbar is added to scroll inside the box. Note that this will add a scrollbar both horizontally and vertically (even if you do not need it):</p>

<div>You can use the overflow property when you want to have better control of the layout. The overflow property specifies what happens if content overflows an element's box.</div>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Image description

OVERFLOW-X AND OVERFLOW-Y

These properties specify whether to change the overflow of content vertically or horizontally.

📌overflow-x : Controls what happens with the left/right edges of the content.

📌overflow-y: Controls what happens with the top/bottom edges of the content.

<!DOCTYPE html>
<html>
<head>
<style>
div {
  background-color: #eee;
  width: 200px;
  height: 50px;
  border: 1px dotted black;
  overflow-x: hidden;
  overflow-y: scroll;
}
</style>
</head>
<body>

<h2>CSS Overflow</h2>
<p>You can also change the overflow of content horizontally or vertically.</p>
<p>overflow-x specifies what to do with the left/right edges of the content.<br>
overflow-y specifies what to do with the top/bottom edges of the content.</p>

<div>You can use the overflow property when you want to have better control of the layout. The overflow property specifies what happens if content overflows an element's box.</div>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

The code above would be displayed as;

Image description

CONCLUSION

That is all for today. I hope you enjoyed reading my post!

Top comments (2)

Collapse
 
anwar_sadat profile image
Anwar Sadat Ayub

Bookmarked this for reference. Great work!

Collapse
 
maame-codes profile image
Maame Afia Fordjour

Thanks Anwar!