How to Know When You Have an Overflow
The overflow
property controls what happens to content that breaks outside of its premises: imagine a div
in which you've explicitly set up to exist 200px wide, merely contains an image that is 300px broad. That paradigm will stick out of the div and be visible
past default. Whereas if you set the overflow
value to hidden
, the image will cutting off at 200px.
div { overflow: visible | subconscious | scroll | auto | inherit }
Values
-
visible
: content is not clipped when it proceeds outside its box. This is the default value of the property -
hidden
: overflowing content will exist hidden. -
coil
: like to hidden except users will exist able to curlicue through the subconscious content. -
clip
: content is clipped when it proceeds exterior its box. This tin be used withoverflow-prune-margin
to set the clipped area. -
automobile
: if the content gain exterior its box then that content will be hidden whilst a scroll bar should be visible for users to read the residue of the content. -
initial
: uses the default value which isvisible
-
inherit
: sets the overflow to the value of its parent element.
Think that text will naturally wrap at the end of an chemical element (unless white-space is inverse) so text volition rarely be the crusade of overflow. Unless a height is set, text will but push an element taller also. Overflow comes into play more commonly when explicit widths and heights are set and it would exist undesirable for any content to spill out, or when scrolling is explicitly being avoided.
visible
If you don't fix the overflow property at all, the default is visible. So in full general, there is no reason to explicitly gear up this property to visible unless you are overriding it from existence set elsewhere.
The of import thing to recall here is that even though the content is visible outside of the box, that content does not bear on the menses of the page. For example:
Generally, you shouldn't be setting static heights on boxes with spider web text in them anyhow, so it shouldn't come up upwards.
hidden
The reverse of the default visible is hidden. This literally hides whatsoever content that extends beyond the box.
This is particularly useful in use with dynamic content and the possibility of an overflow causing serious layout problems. Still, conduct in heed that content that is subconscious in this way is utterly inaccessible (short of viewing the source). So for example a user has their default font size set larger than you would look, you may be pushing text outside of a box and hiding it completely from their view.
ringlet
Setting the overflow value of a box to scroll will hide the content from rendering outside the box, just volition offer scrollbars to scroll the interior of the box to view the content.
Of note with this value is that you get BOTH horizontal and vertical scrollbars no matter what, fifty-fifty if the content requires just i or the other.
iOS' momentum scrolling can exist enabled for this value with -webkit-overflow-scrolling
.
Note: In Os X Lion, when scrollbars are ready to only show when being used, scroll
behaves more like car
, in that just needed scrollbars will bear witness upwards.
auto
Auto overflow is very similar to the scroll value, only information technology solves the problem of getting scrollbars when you lot don't need them. The scrollbars volition but show upwards if in that location is content that really breaks out of the chemical element.
overflow-x and overflow-y
It's besides possible to manipulate the overflow of content horizontally or vertically with the overflow-10
and overflow-y
backdrop. For example in the demo below the horizontal overflow tin be scrolled through whilst the text that extends beyond the height of the box is hidden:
.box { overflow-y: hidden; overflow-x: whorl; }
Immigration floats
One of the more pop uses of setting overflow, strangely enough, is float clearing. Setting overflow doesn't articulate the float at the element, it self-clears. This means that the element with overflow (any value except visible
) will extend as large every bit it needs to encompass child elements inside that are floated (instead of collapsing), bold that the tiptop isn't declared. Like this:
A better float clearing technique is the clearfix, as it doesn't require you to change the overflow property in a fashion y'all don't demand.
Generating cake formatting context
It'southward interesting to annotation that overflow
will too create a new cake formatting context which is useful if we desire to align a cake chemical element next to a floated i. In the instance below we testify how a number of paragraphs will interact with a floated image past default and then we utilise overflow: hidden
to marshal the text within its own box:
This comes from a neat post by Nicole Sullivan which went on to inspire the media object.
Tin can scrollbars be styled with CSS?
You used to exist able to way scrollbars in IE (v5.5?) but no more than. You tin can fashion them now again in WebKit browsers. If you need cross-browser custom scrollbars, await to JavaScript.
If an element needs to have scrollbars appended to honour the overflow value, Firefox puts them outside the element, keeping the visible width/meridian equally alleged. IE puts the scrollbars inside, keeping the overall width/height as declared.
Demo
Demos for this article taken from this sample folio.
Browser Support
Desktop
Chrome | Firefox | IE | Edge | Safari |
---|---|---|---|---|
90 | 81 | xi | ninety | TP |
Mobile / Tablet
Android Chrome | Android Firefox | Android | iOS Safari |
---|---|---|---|
98 | 96 | 98 | 15.4 |
More information
- Understanding the Humble Clearfix
- Overflow: a secret do good
- Overflow on MDN
- Overflow on W3C
- Finding/fixing unintended torso overflow
float
melendezasher1949.blogspot.com
Source: https://css-tricks.com/almanac/properties/o/overflow/
0 Response to "How to Know When You Have an Overflow"
Post a Comment