我正在学习关于 udemy 的课程,因为我们正在为一家餐厅构建网页。正如我所遵循的那样,有时我的 css 规则没有得到正确应用。我似乎无法覆盖用户代理样式表的某些属性。这门课程是在 2015 年制作的,所以我想知道作者是否使用了一些过时的 css 规则?例如,我无法在页面上的按钮上使用下划线和蓝色文本。这是我的 css:
* {
margin:0;
padding: 0;
box-sizing:border-box;
/*set margin,padding,and border for whole page*/
}
html {
/*set background color, also sent font properties for html tag*/
background-color:#fff;
color:#555;
font-family: 'Lato','Arial','sans-serif';
font-weight: 300;
font-size:20px;
text-rendering: optimizeLegibility;
}
/*----------------------------*/
/*REUSABLE COMPONENTS */
/*----------------------------*/
.row {
max-width: 1140px;
margin:0 auto;
}
h1 {
margin:0;
color: #fff;
font-size: 200%;
font-weight: 300;
text-transform: uppercase;
letter-spacing: 1px;
word-spacing: 4px;
}
/*inline-block element*/
.btn:link,
.btn:visited {
display:inline-block;
padding:10px 30px;
font-weight:300;
text-decoration:none;
border-radius: 200px;
}
.btn-full:link,
.btn-full:visited{
background-color: #e67e22;
border: 1px solid #e67e22;
color: #fff;
}
.btn-ghost:link,
.btn-ghost:visited{
border: 1px solid #e67e22;
color: #e67e22;
}
.btn:hover,
.btn:active {
background-color: #cf6d17;
}
.btn-full:hover,
.btn-full:active {
border: 1px solid #cf6d17;
}
.btn-ghost:hover,
.btn-ghost:active {
border: 1px solid #cf6d17;
color: #fff;
}
/*------------------------------------*/
/*HEADER*/
/*------------------------------------*/
header {
/*background image and header properties
Linear gradient changes color of image to make text stand out more
*/
background-image: linear-gradient(rgba(0,0,0,0.7),rgba(0,0,0,0.7)),url("img/hero.jpg");
/*Let background cover entire page*/
background-size: cover;
/*center background position*/
background-position: center;
/*have height of img cover 100% viewport*/
height: 100vh;
}
.hero-text-box {
position: absolute;
width: 1140px;
/*completely center hero text*/
top:50%;
left: 50%;
transform: translate(-50%,-50%);
}
.logo {
height: 100px;
width:auto;
float:left;
margin-top: 20px;
}
.main-nav {
float: right;
list-style: none;
margin-top: 55px;
}
.main-nav li {
display: inline-block;
margin-left: 40px;
}
.main-nav li a:link,
.main-nav li a:visited {
padding: 8px 0;
color: #fff;
text-decoration: none;
text-transform: uppercase;
font-size: 90%;
border-bottom: 2px solid transparent;
transition: border-bottom 0.2s;
}
.main-nav li a:hover,
.main-nav li a:active {
border-bottom: 2px solid #e67e22;
}
答案 0 :(得分:0)
您是否尝试过在 css 末尾使用 !important
像
.main-nav li a:link,
.main-nav li a:visited {
padding: 8px 0;
color: #fff !important;
text-decoration: none !important;
text-transform: uppercase;
font-size: 90%;
border-bottom: 2px solid transparent;
transition: border-bottom 0.2s;
}
答案 1 :(得分:0)
要了解 CSS 的“覆盖”,您需要了解 CSS specificity,它决定了选择器之间的优先级。
以下是优先级从低到高的选择器:
a {...}
或 div {...}
.myClass{...}
或 div.myClass{...}
#myId{...}
或 div#myId{...}
除此之外,您还有 !important
,它可以作为 CSS 属性值的后缀,例如 div{font-size: 16px !important;}
。即使在其他地方应用了更具体的选择器,它也会自动赋予此属性声明优先级。
请注意,过度使用 !important
会降低 CSS 文件的可读性。