CSS CSS中的“*”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8715860/
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
What does "*" mean in CSS?
提问by H Bellamy
I have been looking at the CSS files for many websites like Facebook and Youtube.
我一直在查看 Facebook 和 Youtube 等许多网站的 CSS 文件。
In almost all of them I see this code:
在几乎所有的人中,我都看到了这段代码:
* {
margin: 0;
padding: 0;
}
It is odd, as removing that block in chrome web developer tools doesn't affect the layout of the page.
这很奇怪,因为在 chrome web 开发人员工具中删除该块不会影响页面的布局。
What does this code mean, and when is it used and why?
这段代码是什么意思,什么时候使用,为什么?
回答by vcsjones
This is a common technique called a CSS reset. Different browsers use different default margins, causing sites to look different by margins. The *
means "all elements" (a universal selector), so we are setting all elements to have zero margins, and zero padding, thus making them look the same in all browsers.
这是一种称为 CSS 重置的常用技术。不同的浏览器使用不同的默认边距,导致站点的边距看起来不同。的*
意思是“所有元素”(一个普遍的选择),所以我们设置所有的元素具有零页边距,和零填充,从而使它们看起来一样在所有浏览器。
回答by Curt
*
is a wildcard
*
是通配符
It means apply those styles to all elements.
这意味着将这些样式应用于所有元素。
In this instance its setting the margin
and padding
on all elements to 0
. This is common with Reset CSS files in order to default all native browser margin/padding on different elements to a common value.
在这种情况下,它将所有元素上的margin
和设置padding
为0
。这对于重置 CSS 文件很常见,以便将不同元素上的所有本机浏览器边距/填充默认为一个公共值。
回答by Hauleth
Asterisk (*
) is a wildcard and means all elements.
星号 ( *
) 是通配符,表示所有元素。
* {
margin: 0;
}
sets the margin of all elements to 0.
将所有元素的边距设置为 0。
回答by Oomta
It resets the margin and padding of all HTML elements on the page to 0.
它将页面上所有 HTML 元素的边距和内边距重置为 0。
Some browsers may already do this by default, but it's always useful to try to reset everything manually, just in case.
某些浏览器可能已经默认执行此操作,但尝试手动重置所有内容总是有用的,以防万一。
In fact, many websites carry a reset.css (or similar) which when opened, you'll notice many rules to reset everything across every browser.
事实上,许多网站都带有一个 reset.css(或类似的),当打开它时,您会注意到许多规则可以在每个浏览器上重置所有内容。
回答by home
It's a wildcard and sets margin
and padding
to 0
for all HTML elements.
这是一个通配符,并集margin
和padding
对0
所有HTML元素。
Try a more interesting one like:
尝试一个更有趣的,比如:
* {
font-size: 20pt;
}
回答by Darwin Tech
This is a common part of a general css reset. This basically sets all margin and padding of all (*) elements to 0. You are then free to add your own margin and padding values to each element as per your requirements.
这是一般 css 重置的常见部分。这基本上将所有 (*) 元素的所有边距和填充设置为 0。然后您可以根据您的要求自由地为每个元素添加自己的边距和填充值。
回答by chubbsondubs
In CSS there are some default styles applied to every web page in addition to your styles. These default styles define certain padding
and margin
values for elements like <h1>, <li>, <p>, <table>
, etc. The annoying thing is that often you need to override these styles to get your page to look correctly, but not all browser manufacturers agree on the defaults. Often most developers find it easiest to reset all padding
and margins
to zero
so everything behaves as expected. *
is the wildcard
selector and will match all element types. Essentially that style says to reset all padding/margins to zero for all elements hence removing any default stylings.
在 CSS 中,除了您的样式之外,还有一些默认样式应用于每个网页。这些默认样式为 等元素定义了某些padding
和margin
值<h1>, <li>, <p>, <table>
。令人讨厌的是,您通常需要覆盖这些样式才能使您的页面看起来正确,但并非所有浏览器制造商都同意默认值。通常大多数开发者发现它最简单的重置所有padding
,并margins
以zero
如预期的那么一切的行为。 *
是wildcard
选择器,将匹配所有元素类型。本质上,该样式表示将所有元素的所有填充/边距重置为零,从而删除任何默认样式。
回答by BrettAdamsGA
*
is a wild card, it selects all elements
margin: 0;
and padding: 0;
set the margin and padding to 0 for the elements selected, which in this case would be all elements.
*
是一个通配符,它选择所有元素margin: 0;
并将padding: 0;
所选元素
的边距和填充设置为 0,在这种情况下将是所有元素。
This is very handy for web development, I use it in every site I build.
这对于 Web 开发非常方便,我在我构建的每个站点中都使用它。