What is CSS Overflow and how to use it?

CSS Overflow The overflow property specifies whether to clip the content or to add scrollbars when the content of an element is too big to fit in the specified area.

The overflow property has the following values:. visible – Default. The overflow is not clipped. The content renders outside the element’s box; hidden – The overflow is clipped, and the rest of the content will be invisible

div {
  width: 200px;
  height: 50px;
  background-color: #eee;
  overflow: visible;
}

The overflow property has the following values:

  • visible – Default. The overflow is not clipped. The content renders outside the element’s box
  • hidden – The overflow is clipped, and the rest of the content will be invisible
  • scroll – The overflow is clipped, and a scrollbar is added to see the rest of the content
  • auto – Similar to scroll, but it adds scrollbars only when necessary

CSS3 Media Queries

Example of CSS Overflow

<!DOCTYPE html>
<html>
<head>
<style>
div.ex1 {
  background-color: lightblue;
  width: 110px;
  height: 110px;
  overflow: scroll;
}

div.ex2 {
  background-color: lightblue;
  width: 110px;
  height: 110px;
  overflow: hidden;
}

div.ex3 {
  background-color: lightblue;
  width: 110px;
  height: 110px;
  overflow: auto;
}

div.ex4 {
  background-color: lightblue;
  width: 110px;
  height: 110px;
  overflow: visible;
}
</style>
</head>
<body>

<h1>The overflow Property</h1>

<p>The overflow property specifies whether to clip content or to add scrollbars when an element's content is too big to fit in a specified area.</p>

<h2>overflow: scroll:</h2>
<div class="ex1">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</div>

<h2>overflow: hidden:</h2>
<div class="ex2">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</div>

<h2>overflow: auto:</h2>
<div class="ex3">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</div>

<h2>overflow: visible (default):</h2>
<div class="ex4">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</div>

</body>
</html>

Syntax of CSS overflow-wrap

overflow-wrap: anywhere|initial|break-word|normal|inherit;
Value Description
normal Words will flow out of their desired borderline. A single word will not break here. Break will occur only when the word finishes and new word starts. Normal is the default value of CSS overflow wrap property.
break-word In some cases, you may require that under any circumstances words within a sentence should not flow out of the border area. In those cases, you should use the CSS overflow wrap property as “break-word”. To contain the whole sentence inside its border, “break-word” property breaks long words into pieces to fit inside the border.
anywhere When “anywhere” property is used, words are broken at any random portions so that long words will reside inside within their designated area. It prevents overflow using random break down of words in a sentence.
initial Initial value makes the overflow wrap property which is a default one.
inherit Child element automatically inherits the features from their parent element.
Share.

Terry White is a professional technical writer, WordPress developer, Web Designer, Software Engineer, and Blogger. He strives for pixel-perfect design, clean robust code, and a user-friendly interface. If you have a project in mind and like his work, feel free to contact him

Comments are closed.