CSS中overflow属性怎么使用

css中overflow属性是经常可以用到的属性,接下来的这篇文章我们就来看看css中overflow属性的具体用法。

overflow

我们首先来看一下overflow属性的值有哪些?

overflow 属性规定当内容溢出元素框时发生的事情。

overflow有以下四个属性值

visible:初始值,内容不会被修剪,会呈现在元素框之外。

立即学习“前端免费学习笔记(深入)”;

scroll:内容会被修剪,但是浏览器会显示滚动条以便查看其余的内容。

hidden:内容会被修剪,并且其余内容是不可见的。

auto:如果内容被修剪,则浏览器会显示滚动条以便查看其余的内容。    

下面我们来详细说一说overflow属性的这四个值

我们来看具体的示例

代码如下

HTML代码

          CSS overflow            

The European languages are members of the same family. Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary. The languages only differ in their grammar, their pronunciation and their most common words. Everyone realizes why a new common language would be desirable: one could refuse to pay expensive translators.


The European languages are members of the same family. Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary. The languages only differ in their grammar, their pronunciation and their most common words. Everyone realizes why a new common language would be desirable: one could refuse to pay expensive translators.


The European languages are members of the same family. Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary. The languages only differ in their grammar, their pronunciation and their most common words. Everyone realizes why a new common language would be desirable: one could refuse to pay expensive translators.

CSS代码

/*hidden*/div.hid{  width: 200px;  height: 100px;  overflow: hidden;  background-color: #FF9999;}/*scroll*/div.scr{  width: 200px;  height: 100px;  overflow: scroll;  background-color: #99FF99;}/*visible*/div.vis{  width: 200px;  height: 100px;  overflow: visible;  background-color: #9999FF;}

在浏览器上显示如下结果

当overflow的属性值是hidden时,效果如下

overflow

在hidden的情况下,不会显示出剩下的部分,也不能滚动显示剩下的内容。

当overflow的属性值是scroll时,效果如下

overflow

在scroll的情况下,没有显示的内容可以滚动。默认的情况下,文字在横向上会折回,垂直方向显示滚动栏。

在css中设置white-space : nowrap(不自动改行的意思),也可以向横向滚动。

CSS代码

div.scr{  width: 200px;  height: 100px;  white-space:nowrap;  overflow: scroll;  background-color: #99FF99;}

效果如下

2345截图20181210135009.png

此外,也可以使用overflow-x和overflow-y属性对垂直和横向的滚动条的显示进行更为细致的设置。

当overflow的属性值是visible时,效果如下

2345截图20181210135431.png

visible的情况下,从盒子中溢出显示。紫色的部分是div盒子。默认的情况下,文字在div的横向width中被折回,并在纵向方向上显示。

这个也和scroll属性值一样,在css中设置white-space : nowrap,也可以横向滚动。

另外,如果不设置盒子的高度,就会自动改变盒子的高度。

div.vis{  width: 200px;  /* height: 100px;  */  overflow: visible;  background-color: #9999FF;}

效果如下

2345截图20181210140026.png

最后,我们来看看当overflow属性值是auto时的具体情况

HTML代码

    CSS overflow  

The European languages are members of the same family. Their separate existence is a myth. For science, music, sport, etc, Europe uses the same vocabulary. The languages only differ in their grammar, their pronunciation and their most common words. Everyone realizes why a new common language would be desirable: one could refuse to pay expensive translators.

CSS代码

div.aut{width: 200px;height: 100px;overflow: auto;background-color: red;}

在浏览器上运行的效果和scroll相似

2345截图20181210140828.png

以上就是CSS中overflow属性怎么使用的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1613864.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月24日 03:55:30
下一篇 2025年12月24日 03:55:41

相关推荐

  • css的overflow属性如何定义滚动条

    滚动条在网页中经常见到,却并没有受到足够的重视。只有当因为滚动条的问题需要处理兼容性时,才进行调试操作。本章就给大家带来css滚动条的常见内容。 一:条件      滚动条和overflow是紧密相关的。只有当父级的overflow的值是auto或scroll,并且元素的内容超出元素区域时,才有可能…

    2025年12月24日 好文分享
    000
  • css关于 overflow属性的一些小技巧, overflow属性介绍

    本章我们给大家带来overflow属性的一些小技巧,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。 一:overflow基本属性          overflow基本属性有:visibel , hidden, scroll,auto, inherit, overflow-x和ove…

    好文分享 2025年12月24日
    000
  • HTML布局技巧:如何使用overflow属性进行文本溢出控制

    HTML布局技巧:如何使用overflow属性进行文本溢出控制 在网页开发中,有时我们经常会遇到文本内容过长溢出的问题。为了控制这种溢出,使网页布局更加美观和规整,可以使用CSS的overflow属性来实现。本文将介绍overflow属性的使用方法,并提供具体的代码示例。 一、overflow属性的…

    2025年12月21日
    000
  • HTML布局技巧:如何使用overflow属性进行内容溢出控制

    HTML布局技巧:如何使用overflow属性进行内容溢出控制 在网页设计中,经常会遇到内容溢出的情况。如果在容器中的内容超过了容器的大小,会导致布局混乱,影响用户体验。为了解决这个问题,HTML中提供了overflow属性,可以通过设置不同的属性值来控制内容的溢出情况。本文将介绍如何使用overf…

    2025年12月21日
    000

发表回复

登录后才能评论
关注微信