search
HomeWeb Front-endCSS TutorialShare CSS overflow overflow example tutorial

Clip the left/right edge of the content in the div element - if it overflows the content area of ​​the element:

div
{
overflow-x:hidden;
}

Browser support

All major browsers support the overflow-x attribute.

Note: The overflow-x attribute does not work correctly in IE8 and earlier browsers.

Definition and Usage

The overflow-x attribute specifies whether to clip the left/right edges of the content - if it overflows the element's content area.

overflow-X | overflow-y

The properties of overflow-x and overflow-y were originally properties independently developed by IE browser. It was later adopted and standardized by CSS3. overflow-x is mainly used to define the shearing of horizontal content overflow, and overflow-y is mainly used to define the shearing of vertical content overflow

[Note] If the overflow-x and overflow-y values The same is equivalent to overflow. If the overflow-x and overflow-y values ​​are different, and one of the values ​​is explicitly set to visible or is not set, the default is visible, and the other value is a non-visible value. The visible value will be reset to auto

Value: visible | hidden | scroll | auto | inherit | no-display | no-content

Initial value: visible

Applies to: block-level elements, replacement elements, table cells

Inheritance: None

Attribute

visible

The content of the element is also visible outside the element box

[Note 1] The containing block of the element in IE6-browser will be extended so that its excess content can be wrapped

.box{
    height: 200px;
    width: 200px;
    background-color: lightgreen;
}.in{
    width: 300px;
    height: 100px;
    background-color: lightblue;
}
<p>
    </p><p></p>

The picture on the left is IE6-browser, the picture on the right is other browsers

## [Note 2] There is a bug in the buttons of IE7-browser (including

The picture on the left shows the default situation, and the picture on the right shows the situation after setting overflow

##auto

If the content is clipped, the browser will display scroll bars In order to view the rest of the content

[Note] For general browsers, and

//IE7-浏览器 
html{overflow-y: scroll;}//其他浏览器
html{overflow: auto;}//去除页面默认滚动条
html{overflow: hidden;}

scroll

The content of the element will be in the element box Clipping at the border, but the browser will display scroll bars to view the rest of the content

[Note] Firefox and IE8+ browsers have a missing

padding-bottom

phenomenon when using overflow:scroll or auto.

.box{
    width: 100px;
    height: 100px;
    padding: 50px;
    background-color: pink;
    overflow:scroll;
}.in{
    width: 100px;
    height: 200px;
    background-color: lightgreen;
}

<p>
    </p><p></p>

The picture on the left shows the situation of the chrome browser, and the picture on the right shows the situation of the firefox browser

hidden

  元素的内容会在元素框的边界处剪裁,并且超出剪裁区域的内容不可见

no-display

  当内容溢出容器时不显示元素,类似于元素添加了display:none属性一样

no-content

  当内容溢出窗口时不显示内容,类似于元素添加了visibility: hidden属性一样

  [注意]no-display和no-content这两个属性目前没有浏览器支持

 

失效

  绝对定位元素不总是被父级overflow属性剪裁,尤其是当overflow在绝对定位元素及其包含块之间的时候

    [注意]由于固定定位是相对于视窗定位的,所以固定定位元素无法被其所有的父级元素overflow属性剪裁 

【解决办法】

【1】overflow元素自身为包含块

  给父级设置position:absolute或fixed或relative

【2】overflow元素的子元素为包含块

  在绝对定位元素和overflow元素之间增加一个元素并设置position:absolute或fixed或relative


<p>
    </p><p>
        </p><p>绝对定位元素</p>    
        

应用

  当overflow设置为auto或scroll或hidden时可以触发BFC,使得overflow可以实现一些相关应用。关于BFC的详细信息移步至此

【1】清除浮动影响

  [注意]IE6-浏览器使用overflow这种方式并不能清除浮动,常用的消除浮动的方法是


.clear{
    *zoom: 1;
}.clear:after{
    content: '';
    display: block;
    clear: both;
}

【2】避免margin穿透

  [注意]使用overflow属性只是避免margin穿透的很多方法中的一个,其他的方法还有BFC化、设置padding、设置border等 

 

【3】两栏自适应布局

  [注意]使用overflow属性的场景限制比较明显,常用的两栏自适应布局的方法:

.cell{
    display: table-cell; width: 2000px;
    *display: inline-block; *width:auto;
}

【4】选项卡

  overflow选项卡主要用于单页应用


<p>
    </p>
            
  • 1
  •         
  • 2
  •         
  • 3
  •         
  • 4
  •     
        


body{
    margin: 0;
    text-align: center;
}ul{
    margin: 0;
    padding: 0;
    list-style: none;
}a{
    text-decoration: none;
    color: inherit;
}.show{
    width: 100px;
    height: 100px;
    overflow: hidden;
    border: 1px solid black;
    line-height: 100px;
    font-size: 40px;
}    .show-in{
    width: 100px;
    height: 100px;
}#one{
    background-color: lightgreen;
}#two{
    background-color: lightyellow;
}#three{
    background-color: lightblue;
}#four{
    background-color: pink;
}.con{
    margin: 10px 0 0 10px;
    width: 100px;
}.con-in{
    display:inline-block;
    width: 16px;
    line-height: 16px;
    border: 1px solid black;
    background-color: gray;
}

The above is the detailed content of Share CSS overflow overflow example tutorial. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Iterating a React Design with Styled ComponentsIterating a React Design with Styled ComponentsApr 21, 2025 am 11:29 AM

In a perfect world, our projects would have unlimited resources and time. Our teams would begin coding with well thought out and highly refined UX designs.

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Apr 21, 2025 am 11:26 AM

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons

SVG Properties in CSS GuideSVG Properties in CSS GuideApr 21, 2025 am 11:21 AM

SVG has its own set of elements, attributes and properties to the extent that inline SVG code can get long and complex. By leveraging CSS and some of the forthcoming features of the SVG 2 specification, we can reduce that code for cleaner markup.

A Few Functional Uses for Intersection Observer to Know When an Element is in ViewA Few Functional Uses for Intersection Observer to Know When an Element is in ViewApr 21, 2025 am 11:19 AM

You might not know this, but JavaScript has stealthily accumulated quite a number of observers in recent times, and Intersection Observer is a part of that

Revisting prefers-reduced-motionRevisting prefers-reduced-motionApr 21, 2025 am 11:18 AM

We may not need to throw out all CSS animations. Remember, it’s prefers-reduced-motion, not prefers-no-motion.

How to Get a Progressive Web App into the Google Play StoreHow to Get a Progressive Web App into the Google Play StoreApr 21, 2025 am 11:10 AM

PWA (Progressive Web Apps) have been with us for some time now. Yet, each time I try explaining it to clients, the same question pops up: "Will my users be

The Simplest Ways to Handle HTML IncludesThe Simplest Ways to Handle HTML IncludesApr 21, 2025 am 11:09 AM

It's extremely surprising to me that HTML has never had any way to include other HTML files within it. Nor does there seem to be anything on the horizon that

Change Color of SVG on HoverChange Color of SVG on HoverApr 21, 2025 am 11:04 AM

There are a lot of different ways to use SVG. Depending on which way, the tactic for recoloring that SVG in different states or conditions — :hover,

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools