简体   繁体   English

overflow:hidden和display:none有什么区别

[英]What is the difference between overflow:hidden and display:none

overflow:hidden和display:none有什么区别?

Example: 例:

.oh
{
  height: 50px;
  width: 200px;
  overflow: hidden;
}

If text in the block with this class is bigger (longer) than what this little box can display, the excess will be just hidden. 如果具有此类的块中的文本比此小框可以显示的更大(更长),则多余的文本将被隐藏。 You will see the start of the text only. 您将只看到文本的开头。

display: none; will just hide the block. 只会隐藏块。

Note you have also visibility: hidden; 请注意,您还有visibility: hidden; which hides the content of the block, but the block will be still in the layout, moving things around. 它会隐藏块的内容,但块仍然会在布局中移动。

display: none removes the element from the page, and the flow of the page acts as if it's not there at all. display: none从页面中删除元素,页面流程就像它根本不存在一样。

overflow: hidden : overflow: hidden

The CSS overflow: hidden property can be used to reveal more or less of an element based on the width of the browser window. CSS overflow: hidden属性可用于根据浏览器窗口的宽度显示或多或少的元素。

Overflow:hidden just says if text flows outside of this element the scrollbars don't show. 溢出:隐藏只是说如果文本在此元素之外流动,则滚动条不显示。 display:none says the element is not shown. display:none表示元素未显示。

Simple example of overflow: hidden http://www.w3schools.com/Css/tryit.asp?filename=trycss_pos_overflow_hidden 溢出的简单示例:隐藏http://www.w3schools.com/Css/tryit.asp?filename=trycss_pos_overflow_hidden

If you edit the CCS on that page, you can see the difference between the overflow attributes (visible | hidden | scroll | auto ) - and if you add display: none to the css, you will see the whole content block is disappears. 如果您在该页面上编辑CCS,您可以看到溢出属性(可见|隐藏|滚动|自动)之间的区别 - 如果您向css添加display:none,您将看到整个内容块消失。

Basically it's a way of controlling layout and element "flow" - if you are allowing user input (from a CMS field say), to render in a fixed sized block, you can adjust the overflow attribute to stop the box increasing in size and therefore breaking the layout of the page. 基本上它是一种控制布局和元素“流”的方法 - 如果你允许用户输入(从CMS字段说),要在固定大小的块中渲染,你可以调整overflow属性来阻止盒子的大小增加,因此打破页面的布局。 (conversely, display: none prevents the element from displaying and therfore the entire page re-adjusts) (相反,显示:无阻止元素显示,因此整个页面重新调整)

By default, HTML elements are as tall as required to contain their content. 默认情况下,HTML元素与包含其内容所需的一样高。

If you give an HTML element a fixed height, it may not be big enough to contain its content. 如果为HTML元素指定固定高度,则可能不足以包含其内容。 So, for example, if you had a paragraph with a fixed height and a blue background: 因此,例如,如果您有一个具有固定高度和蓝色背景的段落:

<p>This is an example paragraph. It has some text in it to try and give it a reasonable height. In a separate style sheet, we’re going to give it a blue background and a fixed height. If we add overflow: hidden, we won’t see any text that extends beyond the fixed height of the paragraph. Until then, the text will “overflow” the paragraph, extending beyond the blue background.</p>

p {
    background-color: #ccf;
    height: 20px;
}

The text within the paragraph would extend beyond the bottom edge of the paragraph. 该段内的案文将延伸到该段的下边缘之外。

The overflow property allows you to change this default behaviour. overflow属性允许您更改此默认行为。 So, if you added overflow: hidden : 所以,如果你添加了overflow: hidden

p {
    background-color: #ccf;
    height: 20px;
    overflow: hidden;
}

Then you wouldn't see any of the text beyond the bottom edge of the paragraph. 然后,您将看不到段落底部边缘之外的任何文本。 It would be clipped to the fixed height of the paragraph. 它将被剪裁到段落的固定高度。

display: none would simply make the entire paragraph (visually) disappear, blue background and all, as if it didn't appear in the HTML at all. display: none只会使整个段落(视觉上)消失,蓝色背景和所有,就好像它根本没有出现在HTML中一样。

Let's say you have a div that measures 100 x 100px 比方说,你有一个div测量100×100像素

You then put a whole bunch of text into it, such as it overflows the div. 然后你把一大堆文本放进去,比如溢出div。 If you use overflow: hidden; 如果你使用overflow: hidden; then the text that fits into the 100x100 will not be displayed, and will not affect layout. 那么适合100x100的文本将不会显示,也不会影响布局。

display: none is completely different. display: none完全不同。 It renders the rest of the page as if if the div was still visible. 它呈现页面的其余部分, 好像div仍然可见。 Even if there is overflow, that will be taken into account. 即使有溢出,也会考虑到这一点。 It simply leaves space for the div , but does not render it. 它只是为div留出空间,但不渲染它。 If both are set: display: none; overflow: hidden; 如果两者都设置: display: none; overflow: hidden; display: none; overflow: hidden; then it will not be displayed, the text will not overflow, and the page will be rendered as if the invisible div were still there. 然后它将不会显示,文本不会溢出,页面将呈现为好像隐形div仍在那里。

In order to make the div not affect the rendering at all, then both display: none; overflow: hidden; 为了使div完全不影响渲染,那么display: none; overflow: hidden; display: none; overflow: hidden; should be set, and also, do something such as set height: 0; 应该设置,并且还要做一些诸如set height: 0; . Or with the width , or both, then the page will be rendered as if the div did not exist at all. 或者使用width或两者,然后页面将呈现为好像div根本不存在。

display:none means that the the tag in question will not appear on the page at all (although you can still interact with it through the dom). display:none表示相关标签根本不会出现在页面上(尽管您仍然可以通过dom与它进行交互)。 There will be no space allocated for it between the other tags. 其他标签之间不会为其分配空间。 Overflow hidden means that the tag is rendered with a certain height and any text etc which would cause the tag to expand to render it will not display. 溢出隐藏意味着标记以某个高度和任何文本等呈现,这将导致标记扩展以呈现它将不会显示。 I think what you mean to ask is visibility:hidden. 我想你要问的是可见性:隐藏。 This means that unlike display none, the tag is not visible, but space is allocated for it on the page. 这意味着与display none不同,标签不可见,但在页面上为其分配了空间。 so for example 所以例如

<span>test</span> | <span>Appropriate style in this tag</span> | <span>test</span>

display:none would be: display:none将是:

test | 测试| | | test 测试

visibility:hidden would be: 可见性:隐藏的将是:

test | 测试| | | test 测试

In visibility:hidden the tag is rendered, it just isn't seen on the page. 在可见性中:隐藏标记已呈现,它只是在页面上看不到。

overflow: hidden - hides the overflow of the content, in contrast with overflow: auto who shows scrollbars on a fixed sized div where it's inner content is larger than it's size overflow:hidden - 隐藏内容的溢出,与overflow:auto相比,在固定大小的div上显示滚动条,其内部内容大于其大小

display: none - hides an element and it completely doesn't participant in content layout display:none - 隐藏一个元素,它完全不参与内容布局

PS there is no difference between the two, they are completely unrelated PS两者没有区别,它们完全不相关

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM