CSS:“显示:自动;”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6556994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 00:55:35  来源:igfitidea点击:

CSS: "display: auto;"?

css

提问by jameshfisher

The autooption that several CSS attributes give is really useful. However, there doesn't seem to be one for the displayattribute. I would expect this to set the displayattribute to the browser default depending on the tag; e.g., a <div>would reset to display: block;, but a <span>would reset to display: inline;.

auto几个 CSS 属性提供的选项非常有用。但是,该display属性似乎没有。我希望这会display根据标签将属性设置为浏览器默认值;例如, a<div>会重置为display: block;,但 a<span>会重置为display: inline;

  1. Is there a display: autoequivalent I've missed?
  2. If not, why not?
  3. What's the most elegant workaround? (I'm working programmatically, specifically with jQuery.)
  1. 有没有display: auto我错过的等价物?
  2. 如果没有,为什么不呢?
  3. 最优雅的解决方法是什么?(我正在以编程方式工作,特别是使用 jQuery。)

采纳答案by thirtydot

You should use:

你应该使用:

$('element').css('display','');

That will set displayto whatever is the default for elementaccording to the current CSS cascade.

根据当前的 CSS 级联,这将设置display为默认值element

For example:

例如:

<span></span>

$('span').css('display','none');
$('span').css('display','');

will result in a spanwith display: inline.

将导致spandisplay: inline.

But:

但:

span { display: block }

<span></span>

$('span').css('display','none');
$('span').css('display','');

will result in a spanwith display: block.

将导致spandisplay: block.

This is not a problem: in fact, it's almost always the desired result.

这不是问题:事实上,它几乎总是想要的结果。

回答by Miles Libbey

In CSS3, there is an initial value. Perhaps try display: initial;

在 CSS3 中,有一个初始值。也许试试display: initial;

回答by Ryan

There is no display: autoproperty in CSS, the valid values are here

display: autoCSS 中没有属性,有效值在这里

The default value is display: inline. See the display reference from MDN

默认值为display: inline请参阅来自 MDN 的显示参考

Why is there no auto value? Simply because the CSS definition for the rule. You should also see the CSS rules around specificity as outlined in this article

为什么没有自动值?仅仅是因为规则的 CSS 定义。您还应该看到本文中概述的关于特异性的 CSS 规则

I hope this helps.

我希望这有帮助。

回答by Kon

There's a inheritoption, which:

有一个继承选项,其中:

Specifies that the value of the display property should be inherited from the parent element

指定应该从父元素继承显示属性的值

Other than that, just don't set display at all and it'll default to whatever it defaults to. Also, if you programmatically set it to nothing, I believe it just follows the default behavior then:

除此之外,根本不要设置显示,它会默认为它默认的任何内容。此外,如果您以编程方式将其设置为空,我相信它只是遵循默认行为:

document.getElementById('myElement').style.display = '';

回答by Charming Prince

depending on the parent element and the tag element in question the browser can quickly adjust to suite it. Most effectively it's always good to use DTD standards in your HTML opening tag to declare other available properties for the display selector, otherwise it uses only "block" & "inline". consequently if DTD is declared, using a display: inline-block;property would be best in this case.

根据父元素和有问题的标签元素,浏览器可以快速调整以适应它。最有效的做法是在 HTML 开始标记中使用 DTD 标准来声明显示选择器的其他可用属性,否则它只使用“块”和“内联”。因此,如果声明了 DTD,display: inline-block;在这种情况下最好使用属性。