这是我参与「第四届青训营」笔记创作活动的的第2天
CSS的优点有那些?
CSS全称为Cascading Style Sheets(层叠样式表),中文翻译为“层叠样式表”,简称CSS样式表。它是一种用来表现HTML或 XML 等文件式样的计算机语言。
- 节省时间:可以编写一次CSS,然后在多个HTML页面中通过外部引入多次重复使用
- 页面加载速度更快:通过使用CSS,就不需要每次都编写HTML标记属性,只需要编写一个标记的CSS规则,并将其应用于该标记的所有实例,因此代码大大减少也就意味着下载时间短。
- 易于维护:如果要进行全局更改,则只需更改样式,所有网页中的所有元素都将会自动更新。
- 多设备兼容性:样式表允许针对多种不同类型的设备进行优化内容。
- 浏览器在样式表中从上到下读取样式定义。这也说明我们在样式表中定义的样式将覆盖样式表中先前定义的任何先前样式,不过我们可以在后面的元素中重新定义则不会被覆盖。
css选择器组合方式
| 语法 | 说明 |
|---|---|
| AB | 直接组合(满足A同时满足B) |
| A B | 后代组合(选中B,如果它是A的子孙) |
| A>B | 亲子组合(选中B,如果它是A的子孙) |
| A~B | 兄弟选择器(选中B,如果它在A后且与A同级) |
| A+B | 相邻选择器(选中B,如果它紧跟在A后面) |
如果要给多个标签添加样式,可以设置类属性或者用并集选择器 例:h1,h2,h3,h4{}
继承属性的完整列表
W3C指出可以继承的属性。
azimuthborder-collapseborder-spacingcaption-sidecolorcursordirectionelevationempty-cellsfont-familyfont-sizefont-stylefont-variantfont-weightfontletter-spacingline-heightlist-style-imagelist-style-positionlist-style-typelist-styleorphanspitch-rangepitchquotesrichnessspeak-headerspeak-numeralspeak-punctuationspeakspeech-ratestresstext-aligntext-indenttext-transformvisibilityvoice-familyvolumewhite-spacewidowsword-spacing
Flex布局
1. flex 是什么?
- flex 是 Flexible Box 的缩写,就是弹性盒子布局的意思
2. 为什么我们需要 flex?
- 解决元素居中问题
- 自动弹性伸缩,合适适配不同大小的屏幕,和移动端
3. 容器 container 的属性
外面的大容器的属性的设置
| 属性名 | 含义 |
|---|---|
| flex-direction | 主轴方向 |
| flex-wrap | 主轴一行满了换行 |
| flex-direction | 1和2的组合 |
| justify-content | 主轴元素对齐方式 |
| align-items | 交叉轴元素对齐方式//单行 |
| align-content | 交叉轴行对齐方式//多行 |
3.1 flex-direction 主轴方向
- row(默认值):主轴为水平方向,起点在左端。
- row-reverse:主轴为水平方向,起点在右端。
- column:主轴为垂直方向,起点在上沿。
- column-reverse:主轴为垂直方向,起点在下沿。
代码:
<style>
.container{
display: flex;
height: 600px;
background-color: rgb(219, 219, 219);
}
.container{
flex-direction: row;
/* flex-direction: row-reverse; */
/* flex-direction: column; */
/* flex-direction: column-reverse; */
}
.item{
padding: 100px;
}
</style>
<body>
<div class="container">
<div class="item" style="background-color: antiquewhite;">1</div>
<div class="item" style="background-color: aqua;">2</div>
<div class="item" style="background-color: aquamarine;">3</div>
</div>
</body>
3.2 flex-wrap 主轴一行满了换行
- nowrap (默认值) 不换行压缩宽度
- wrap 换行
- wrap-reverses 反向换行
代码:
<style>
.container{
display: flex;
height: 600px;
background-color: rgb(219, 219, 219);
}
.container{
/* flex-wrap: nowrap; */
flex-wrap: wrap;
/* flex-wrap: wrap-reverse; */
}
.item{
padding: 100px;
}
</style>
<body>
<div class="container">
<div class="item" style="background-color: antiquewhite;">1</div>
<div class="item" style="background-color: aqua;">2</div>
<div class="item" style="background-color: aquamarine;">3</div>
<div class="item" style="background-color: #aaff00;">4</div>
<div class="item" style="background-color: #ffff00;">5</div>
</div>
</body>