发表于: 2018-11-20 21:23:01

2 471


今天完成的事情:今天完成了任务十一 ,在vscode中下载两个插件就可以使用sass了:

使用选择器继承来精简CSS;

使用sass的时候,最后一个减少重复的主要特性就是选择器继承。基于Nicole Sullivan面向对象的css的理念,选择器继承是说一个选择器可以继承为另一个选择器定义的所有样式。这个通过@extend语法实现,如下代码:

//通过选择器继承继承样式

.error {

  border: 1px solid red;

  background-color: #fdd;

}

.seriousError {

  @extend .error;

  border-width: 3px;

}

在上边的代码中,.seriousError将会继承样式表中任何位置处为.error定义的所有样式。以class="seriousError" 修饰的html元素最终的展示效果就好像是class="seriousError error"。相关元素不仅会拥有一个3px宽的边框,而且这个边框将变成红色的,这个元素同时还会有一个浅红色的背景,因为这些都是在.error里边定义的样式。

.seriousError不仅会继承.error自身的所有样式,任何跟.error有关的组合选择器样式也会被.seriousError以组合选择器的形式继承,如下代码:

//.seriousError从.error继承样式

.error a{  //应用到.seriousError a

  color: red;

  font-weight: 100;

}

h1.error { //应用到hl.seriousError

  font-size: 1.2rem;

}

何时使用继承;

混合器主要用于展示性样式的重用,而类名用于语义化样式的重用。因为继承是基于类的(有时是基于其他类型的选择器),所以继承应该是建立在语义化的关系上。当一个元素拥有的类(比如说.seriousError)表明它属于另一个类(比如说.error),这时使用继承再合适不过了。

这有点抽象,所以我们从几个方面来阐释一下。想象一下你正在编写一个页面,给html元素添加类名,你发现你的某个类(比如说.seriousError)另一个类(比如说.error)的细化。你会怎么做?

你可以为这两个类分别写相同的样式,但是如果有大量的重复怎么办?使用sass时,我们提倡的就是不要做重复的工作。

你可以使用一个选择器组(比如说.error.seriousError)给这两个选择器写相同的样式。如果.error的所有样式都在同一个地方,这种做法很好,但是如果是分散在样式表的不同地方呢?再这样做就困难多了。

你可以使用一个混合器为这两个类提供相同的样式,但当.error的样式修饰遍布样式表中各处时,这种做法面临着跟使用选择器组一样的问题。这两个类也不是恰好有相同的 样式。你应该更清晰地表达这种关系。

综上所述你应该使用@extend。让.seriousError从.error继承样式,使两者之间的关系非常清晰。更重要的是无论你在样式表的哪里使用.error.seriousError都会继承其中的样式。

任务四改过之后:

$blue:#5fc0cd;
$grey : #eff0f4;
$whtie: #fff;

@mixin top-word {
position : absolute;
top      : 0;
margin   : 3rem;
font-size: 2.3rem;
}
@mixin content {
height          : 10rem;
position        : relative;
top             : 9rem;
background-color: $whtie;
}
@mixin content-left {
padding       : 0 4rem;
vertical-align: middle;
border        : none;
}
@mixin input {
width      : 15rem;
padding    : 0 0 0 1rem;
height     : 5rem;
border     : none;
background : $whtie;
outline    : none;
font-size  : 2.3rem;
margin     : 2.5rem 0;
border-left: 0.2rem solid $grey;
}

html{
font-size: 62.5% ;
}
body,div,p{
margin: 0;
}
body{
background-color: $grey;
min-width       : 320px;
}
header{
height          : 9rem;
width           : 100%;
background-color: $blue;
position        : fixed;
top             : 0;
text-align      : center;
}
div{
color: $whtie;
}
.header-top{
@include top-word;
right: 0;
}
.header-middle{
position : absolute;
left     : 0;
top      : 3rem;
right    : 0;
font-size: 2.3rem;
}
.header-bottom{
@include top-word;
left: 0;
}
.content-top{
@include content;
width     : 100%;
margin-top: 1.6rem;
img{
@include content-left;
}
input{
@include input;
}
}
.content-bottom{
@include content;
margin-top: 1.4rem;
img{
@include content-left;
}
input{
@include input;
}
}
.footer{
position        : absolute;
top             : 32rem;
width           : 100%;
padding         : 3.5rem 0 ;
margin          : 7rem 0 1.8rem;
background-color: $blue;
text-align      : center;
outline         : none;
border          : none;
color           : $whtie;
font-size       : 2.3rem;
}
.harf{
position        : absolute;
top             : 50.8rem;
right           : 0;
background-color: $grey;
margin          : 0 3rem;
color           : $blue;
font-size       : 2.2rem;
text-decoration : none;

明天计划的事情:完成任务十二
遇到的问题:没问题 就是用起来慢一点
收获:学会使用sass


返回列表 返回列表
评论

    分享到