我看到很多人给HTML的标签添加id或class,以便通过CSS进行访问。我为什么要这样做,当我可以直接访问它本身作为标签时呢?
为什么要这样做?
例1:
为什么要这样做?
例1:
.body-wrapper {
margin: 0px 3px;
}
改为这个?
示例2:
body {
margin: 0px 3px;
}
如果你希望只有尽可能少的样式表(比如说,只有 一个 ),但仍然想要自定义特定页面的显示,那么这很有用。
这能够帮助你实现以下目的:
<html>
标签上)关于此更多信息请访问 CSS Tricks。
按下 Ctrl+U。
按下 Ctrl+F,输入 <body
。
惊喜不断!StackOverflow 使用了 <body>
类!
注意:SO 不会直接使用 CSS 样式,而是将其作为 JavaScript 的句柄。
请考虑以下两种情况:
You have anchor elements throughout your page. Each of these anchor elements takes the user to the top of the page, like so:
<a href="#top-of-page">Go to top</a>
Well, the top of the page could be this: <body id="top-of-page">
What if you want certain elements to have similar styling, such as a border.
.frame-this { border: 2px dashed black; }
If you want the body
element to have this border, it would take this class:
<body class="frame-this"> ... </body>
这些场景有些牵强,但它们说明了 body
元素可能需要一个 id
或一个 class
的两个原因。
此外,请参阅 CSS-Tricks 上的这篇文章:为什么在 HTML 元素上使用类或 ID?
.body-wrapper
类元素的主体... - user1189882classes
/id
可以实现的CSS技巧吗? - LastSecondsToLive