09. Handling Element Overflow

The overflow property

You can tell CSS how to handle elements that are too large to be contained within its parent container.

We use the propertyoverflow, and the values can be set to hidden, visible, scroll, auto.

#sample {
  width: 18em;
  height: 7em;
  margin: 0 auto 5px;
  border: 2px black solid;
  border-radius: 5px;
  padding: 0.5em;
  overflow: scroll;   /* how container will handle overflow */
}

Scroll

This overflow is set to scroll, with the overflow text clipped.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ullamcorper ac magna eu sodales. Aliquam nec nibh ut odio interdum egestas quis eget mauris. Donec egestas libero quis velit facilisis sodales. Nullam quis magna vestibulum, accumsan nulla vitae, lobortis diam.

Visible

This overflow is set to visible, meaning the text just goes out of its containing box like it runs the place.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ullamcorper ac magna eu sodales. Aliquam nec nibh ut odio interdum egestas quis eget mauris. Donec egestas libero quis velit facilisis sodales.

Hidden

This overflow is set to hidden. Notice that the text just cuts off.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ullamcorper ac magna eu sodales. Aliquam nec nibh ut odio interdum egestas quis eget mauris. Donec egestas libero quis velit facilisis sodales. Nullam quis magna vestibulum, accumsan nulla vitae, lobortis diam.

Auto

This overflow is set to auto, which causes overflowing content to be clipped with a scroll-bar added.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ullamcorper ac magna eu sodales. Aliquam nec nibh ut odio interdum egestas quis eget mauris. Donec egestas libero quis velit facilisis sodales. Nullam quis magna vestibulum, accumsan nulla vitae, lobortis diam.

Specifying a dimension for overflow

If you'd like to handle just the horizontal or vertical overflow, we can use theoverflow-x andoverflow-y properties.

p {
  overflow-x: hidden;
}

Aching back from coding all day?

Acupressure Mat & Pillow

Aching back from coding all day? Try Back Problems

Relieve your stress, back, neck and sciatic pain through 1,782 acupuncture points for immediate neck pain relief. Made for lower, upper and mid chronic back pain treatment, and improves circulation, sleep, digestion and quality of life.

$$ Check price
144.87144.87Amazon 4.5 logo(1,890+ reviews)

More Back Problems resources

Build modern and responsive webpages

HTML & CSS

Build modern and responsive webpages Try Front-end

This book teaches HTML as if it's for anyone - hobbyists, students, and professionals - and it's full-color throughout. It utilizes information graphics and lifestyle photography to explain the topics in a simple way that is engaging. You can progress through the chapters from beginning to end or just dip into topics of particular interest at your leisure.

$ Check price
29.9929.99Amazon 4.5 logo(926+ reviews)

More Front-end resources

Ad