我正在维护一个包含将近 120.000 页的网站。我正在为主页设计一个新布局,因此我需要让每个类都独一无二,这样网站的其余部分才不会受到影响。
如果我想做一个a
标记唯一,我如何正确地做到这一点?是外汇<h1 class="foo-a">Hover on me</h1>
,并在 CSS 中:foo-a {etc}
?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS Smooth Animation of Element's Text Color</title>
<style type="text/css">
a {
margin: 20px;
-webkit-transition: color 0.5s; /* For Safari 3.0 to 6.0 */
transition: color 0.5s; /* For modern browsers */
}
a:hover {
color: #ff0000;
}
</style>
</head>
<body>
<h1><a href="#">Hover on me</a></h1>
</body>
</html>
最佳答案
作为使任何样式声明不影响其他页面的更实用和可行的方法,只需给主页的 body 元素一个 id
。或 class
其他页面上没有任何元素,例如 <body id="this-is-the-only-true-main-page">
或 <body class="this-is-the-only-true-main-page">
.
现在要重新设计您的主页样式,只需在您使用的任何选择器前加上该类/id。所以不要做
.my-class { color: red; }
你会
.this-is-the-only-true-main-page .my-class { color: red; }
这个上下文选择器将确保它只影响带有 .my-class
的元素具有 .this-is-the-only-true-main-page
的祖先元素.
关于html - 让每一节课都独一无二,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56680336/