CSS 万用选择器通常指的是通配符选择器(Universal Selector),其语法为 *
。这个选择器可以匹配页面上的所有元素,不考虑它们的类型、类名或 ID。
通配符选择器 *
是一个特殊的选择器,它可以应用于任何元素,包括 HTML 标签和它们的子元素。使用这个选择器时,它会选中页面上的所有元素。
实际上,CSS 中只有一个万用选择器,即 *
。
* { margin: 0; padding: 0; box-sizing: border-box; }
来移除所有元素的默认边距和填充,并设置盒模型。* { font-size: 16px; background-color: #f0f0f0; }
。/* 不推荐的做法 */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* 推荐的做法 */
body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
box-sizing: border-box;
}
通过上述信息,你应该对 CSS 万用选择器有了更深入的了解,并且知道如何在实际开发中合理使用它。
领取专属 10元无门槛券
手把手带您无忧上云