
CSS
继HTML后的CSS记录
CSS
style基础
选择器
css文件基本上是通过元素{ 属性: 值;}的方式来定义的,也可以通过#id{ 属性: 值;}的方式来定义或者.class{ 属性: 值;} 的方式来定义
h1 { /*文字居中*/ text-align: center;}
body { /*背景色*/ background-color: blue;}
#menu { /*id为menu的元素的宽度*/ width: 300px; /*也可以:width: 80%;*/ /*下面两条等价于居中*/ margin-left: auto; margin-right: auto;}
.cls { /*class为cls的元素的宽度*/ width: 300px;}多个对象要使用相同的配置时,可以对象1, 对象2{ 属性: 值;},若要全选,可以*{ 样式 }
对某元素的class,可以使用元素.class{ 样式 }来定义
组合选择器
.cls p { /*class为cls里的所有p元素进行设置值*/ display: inline-block;}- 后代选择器(以空格分隔)
.class 元素{ 属性: 值;} - 子元素选择器(以>号分隔)
.class>元素{ 属性: 值;}class为cls里的一级子元素p元素进行设置值 - 相邻兄弟选择器(以+分隔)
.class+元素{ 属性: 值;}与class为cls同父的紧邻&下一个元素p元素进行设置值 - 普通兄弟选择器(以~分隔)
.class~元素{ 属性: 值;}与class为cls同父的所有之后的元素p元素进行设置值
属性选择器
用于根据元素的属性或属性值来选择HTML元素:[type] {属性: 值;}选择所有具有type属性的元素(注意到是属性)。用法如:
/*将所有具有title属性的设置为蓝色*/<style> [title] { color: blue; }</style><h1 title="Hello world">Hello world</h1>此外还有其他用法:
[attribute="value"]选择具有指定属性和值完全匹配的元素。[attribute~="value"]选择属性值中包含指定词(用空格分隔的词列表之一)的元素。[attribute*="value"]选择属性值中包含指定词的元素。[attribute|="value"]选择属性值以指定值开头并紧跟着连字符-的属性值的元素(常用于语言)。[attribute^="value"]选择属性值以指定值开头的元素。[attribute$="value"]选择属性值以指定值结尾的元素。
背景
背景参数分为:background-color``background-image``background-repeat``background-attachment``background-position ,分别控制颜色、图片、是否重复、是否固定、起始位置。依照此顺序也可以简写为background一个参数,例如:
body { background-image: url('https://example.com/images/myImg.jpg'); background-repeat: no-repeat; /*背景图只出现一次,否则会多张背景图重复直到填满*/ background-position: right top; /*背景图在浏览器窗口的右上角*/ background-attachment: fixed; /*背景图固定,不会随页面滚动*/}
/*上面的等价于*/body { background: url('https://example.com/images/myImg.jpg') no-repeat fixed right top;}示例: 这是一个可以重复、不随页面滚动(相对静止)、初始处于右上角的背景
渐变
在背景图片设置中可以使用渐变gradient,分为线性渐变linear-gradient和径向渐变radial-gradient(圆形扩散)
示例: 线性语法:background-image: linear-gradient(direction, color-stop1, color-stop2, ...);其中direction表示渐变方向,可选值有 to top、to right、to bottom right等,分别表示从下到上、从左到右、从左上到右下
<style> #gradient1 { background-image: linear-gradient(#92ff11, #48e8ef); }</style><div id="gradient1"></div>径向语法:background-image: radial-gradient(center, shape size, start-color,..., last-color);
<style> #gradient2 { background-image: radial-gradient(#92ff11, #48e8ef); }</style><div id="gradient2"></div>文本样式
color:文本颜色font-family:字体text-align:文本对齐方式,可选值有left、right、center、justify,分别表示左对齐、右对齐、居中对齐、两端对齐。font-size:字体大小font-weight:字体粗细,可选值有normal、bold、bolder、lighter,分别表示正常、加粗、更粗、更细。font-style:字体样式,可选值有normal、italic、oblique,分别表示正常、斜体、倾斜。text-decoration:文本修饰,可选值有none、underline、overline、line-through,分别表示无修饰、下划线、上划线、中划线。text-indent:文本缩进
透明度
参数opacity:透明度,取值范围0-1,0为完全透明,1为完全不透明
tips:通过调用:hover可以设置鼠标悬停的透明度改变功能
链接
对于<a>的类型/伪类(见下):
- a:link - 正常,未访问过的链接
- a:visited - 用户已访问过的链接
- a:hover - 当用户鼠标放在链接上时
- a:active - 链接被点击的那一刻
其中可选择的参数有color text-decoration background-color,分别代表文字颜色、是否有下划线、背景颜色。例:
/*所有超参数的文字颜色*/a { color: black;}
/*点击后的文字颜色*/a:visited { color: grey;}
/*鼠标放置时的颜色*/a:hover { color: brown;}示例:
盒子模型
CSS盒子模型本质上是一个盒子,封装周围的HTML元素,它包括:边距margin,边框border,填充padding,和实际内容content,下面示例:
.box { /*实际内容宽高为300像素*/ width: 300px; height: 300px; /*边距为50像素*/ margin: 50px; /*边框为10像素绿色部分*/ border: 10px solid green; /*填充为10像素与背景颜色相同*/ padding: 10px; background-color: yellow;}示例: 一个实际内容(蓝色)宽高为300像素、边距(透明,与页面距离)为50像素、边框(绿色)为10像素、填充(黄色)为10像素的盒子:
水平/垂直对齐
使用margin:auto来水平居中;注意:需要设置宽度才能生效,否则是占满整个页面宽
对于文本,使用text-align:center设置居中
示例:
同样可以设置float浮动、position:absolute相对位置来水平居中
对于垂直居中,直接使用padding: 上下 左右设置一个值代表上下间距即可:
.center2 { width: 300px; padding: 20px 0;}示例:
同样的适用position:absolute
边框
border-style属性用于指定要显示的边框类型,其中值可选:
- none:无边框
- dotted:点状边框
- dashed:虚线边框
- solid:实线边框
- double:双线边框
- groove:三维沟槽边框。效果取决于 border-color 的值。
- ridge:三维脊状边框。效果取决于 border-color 的值。
- inset:三维嵌入边框。效果取决于 border-color 的值。
- outset:三维突出边框。效果取决于 border-color 的值。
- hidden:隐藏边框
- 混合边框:混合边框,可以指定多个边框类型,如
border-style: dotted dashed solid double;即上点右虚下实左双线 等价于border-top-style:dotted;border-right-style:solid;border-bottom-style:dotted;border-left-style:solid;
同时可通过border-width来设置边框的宽度、border-color来设置边框颜色、border-radius来设置边框的圆角弧度
也可以通过类似混合边框中的border-top-style:dotted;来设置border-top-width border-top-color
示例:
无边框。
虚线边框。
虚线边框。
实线边框。
双边框。
凹槽边框。
垄状边框。
嵌入边框。
外凸边框。
隐藏边框。
混合边框
显示
display和visibility属性用于指定元素如何显示。
display属性:display:none:隐藏元素,元素将不会显示,但是元素本身还在,并且会占用空间。display:block:元素会以块级元素显示,会独占一行,并且会填充父元素。display:inline:元素会以行内元素显示,不会独占一行,并且会填充父元素。visibility属性:visibility:hidden:隐藏元素,元素将不会显示,但是元素本身还在,并且会占用空间。visibility:visible:显示元素,元素将显示,并且会占用空间。visibility:collapse:隐藏元素,元素将不会显示,但是元素本身还在,并且不会占用空间。
表单
众所周知html表单是由input作为基石的,css中使用input[type=类型](属性选择器)来匹配指定类型的输入元素
其中推荐设置边框border,填充padding,聚焦:focus,鼠标悬停:hover,必填:required(其余表单元素相同)
对于文本框,推荐设置resize: none取消右下角的拖动改变大小功能
按钮
直接上例子:
<style> .my-button { background-color: antiquewhite; position: relative; font-size: 14px; padding: 10px; width: 100px; text-align: center; /*删除多出来的伪元素*/ overflow: hidden; }
/*伪元素(一块略大于按钮的白布)*/ .my-button:after { content: ""; background: #dadada; display: block; position: absolute; /*略大确保完全覆盖整个按钮*/ padding-top: 120%; padding-left: 120%; margin-left: -10px; margin-top: -100%; /*初始透明度为0*/ opacity: 0; transition: all 0.4s }
/*按钮被按下后*/ .my-button:active:after { /*使伪元素变为按钮大小(让他有个过度动画)*/ padding: 0; margin: 0; /*透明度变成1,并让伪元素瞬间可见*/ opacity: 1; transition: 0s }
/*按钮内文字*/ .my-button span { display: inline-block; position: relative; transition: 0.4s; }
/*内文字的伪元素*/ .my-button span:after { content: '»'; position: absolute; opacity: 0; top: 0; right: -20px; transition: 0.2s; }
/*鼠标移动到上面的时候,内文字移动一下*/ .my-button:hover span { padding-right: 10px; }
/*鼠标移动到上面时,内文字的伪元素就会显示*/ .my-button:hover span:after { opacity: 1; right: 0; }</style><button class="my-button"><span>按钮</span></button>运行结果:
溢出元素框overflow
取值:
- visible 默认值。内容不会被修剪,会呈现在元素框之外。
- hidden 内容会被修剪,并且其余内容是不可见的。
- scroll 内容会被修剪,但是浏览器会显示滚动条以便查看其余的内容。
- auto 如果内容被修剪,则浏览器会显示滚动条以便查看其余的内容。
- inherit 规定应该从父元素继承 overflow 属性的值。
.overflow { /*定义一个可以滚动的元素*/ overflow: scroll; height: 100px;}示例: - auto
1
2
3
布局
定位position
position属性用于指定元素的定位类型,可选值有:
static默认,文档流形式absolute绝对定位,相对于最近的已定位祖先元素(非 static)进行定位,没有的话找浏览器窗口定位。元素完全脱离文档流,不占据原来的空间,其他元素会忽略它的存在relative相对定位,相对于其正常位置(static)进行定位。元素仍在文档流中,占据原来的空间,只是视觉上发生了偏移fixed固定定位,相对于浏览器窗口进行定位(滚动位置不变)inherit继承父元素
<style> .container { /*祖先元素非静态*/ position: relative; width: 300px; height: 300px; border: 1px solid black; }
.box { width: 100px; height: 100px; background-color: #f5f5f5; border: 1px solid #ccc; }</style><div class="container"> <div class="box" style="position: relative; top: 20px; left: 20px;">Relative</div> <div class="box" style="position: absolute; top: 60px; left: 60px;">Absolute</div></div>多列
column-count属性用于指定元素应该被分成多少列,默认为 1 列;column-gap属性用于指定列之间的间隔;column-rule 属性用于指定列的分隔线。
<style> .multi-col-container { column-count: 3; /* 定义三列 */ column-gap: 20px; /* 列间距 */ column-rule: 1px solid #ccc; /* 列分隔线 */ }</style><div class="multi-col-container"> <p> 这是暮晓晚枫的猴窝,现在在演示的是多列布局。这是暮晓晚枫的猴窝,现在在演示的是多列布局。这是暮晓晚枫的猴窝,现在在演示的是多列布局。这是暮晓晚枫的猴窝,现在在演示的是多列布局。这是暮晓晚枫的猴窝,现在在演示的是多列布局。这是暮晓晚枫的猴窝,现在在演示的是多列布局。这是暮晓晚枫的猴窝,现在在演示的是多列布局。 </p></div>这是暮晓晚枫的猴窝,现在在演示的是多列布局。这是暮晓晚枫的猴窝,现在在演示的是多列布局。这是暮晓晚枫的猴窝,现在在演示的是多列布局。这是暮晓晚枫的猴窝,现在在演示的是多列布局。这是暮晓晚枫的猴窝,现在在演示的是多列布局。这是暮晓晚枫的猴窝,现在在演示的是多列布局。这是暮晓晚枫的猴窝,现在在演示的是多列布局。
浮动float/clear
浮于元素上方
clear指定不允许元素周围有浮动元素。float指定一个盒子(元素)是否可以浮动。
可选的值有: left左边 right右边 none不浮动 inherit继承
示例:第一个“这”字将浮动于页面的左侧,不论页面大小
这是一些文本。 这是一些文本。这是一些文本。 这是一些文本。这是一些文本。这是一些文本。 这是一些文本。这是一些文本。这是一些文本。
弹性盒子
通过display: flex应用于父容器,使子元素能够根据容器的大小自动调整宽度或高度。
通常应用于需要控制单行/单列时
<style> /* 示例:等高卡片布局(交叉轴拉伸 + 动态换行) */ .card-container { display: flex; flex-wrap: wrap; /* 允许换行 */ gap: 1rem; padding: 2rem; }
.card { flex: 1; /* 动态填充剩余空间 */ min-width: 150px; /* 最小宽度约束 保证可读*/ background: #f5f5f5; padding: 1.5rem; border-radius: 8px; }
/* 示例:垂直居中 */ .centered-section { display: flex; min-height: 300px; /* 需要高度才能垂直居中 */ background: #e0e0e0; }
.centered-content { margin: auto; /* 水平和垂直居中 */ text-align: center; }</style><!-- 示例:卡片布局 --><div class="card-container"> <div class="card"> <h3>卡片1</h3> <p>动态宽度卡片,内容高度不同但容器等高</p> </div> <div class="card"> <h3>卡片2</h3> <p>通过 flex:1 实现等宽效果,自动换行响应屏幕尺寸</p> <p>额外的段落用于展示高度差异</p> </div> <div class="card"> <h3>卡片3</h3> <p>最小宽度约束保证可读性</p> </div></div><!-- 示例:垂直居中 --><section class="centered-section"> <div class="centered-content"> <h2>居中</h2> <p>无需计算 margin 居中</p> </div></section>卡片1
动态宽度卡片,内容高度不同但容器等高
卡片2
通过 flex:1 实现等宽效果,自动换行响应屏幕尺寸
额外的段落用于展示高度差异
卡片3
最小宽度约束保证可读性
居中
无需计算 margin 居中
更多应用可看最后应用示例里的导航栏
网格布局
定义一个容器,并设置display: grid,并通过grid-template-columns和grid-template-rows显式定义行与列
应用时,grid-column: start / end和grid-row: start / end用于指定元素在网格中的位置。其中start起始行列end结束行列(不包含)
通常用于需要同时控制行/列时
<style> .dashboard { display: grid; grid-template-columns: repeat(3, 1fr); /* 定义三列,每列宽度相等 */ grid-template-rows: repeat(2, 100px) 200px; /* 定义三行,前两行高度为100px,最后一行高度为200px */ gap: 10px; /* 网格间隔 */ width: 100%; max-width: 900px; background-color: #fff; padding: 10px; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); }
.item { background-color: #e0e0e0; display: flex; justify-content: center; align-items: center; font-size: 1.2em; color: #333; border-radius: 4px; }
/* 调整特定模块的大小和位置 */ .item-1 { grid-column: 1 / 3; /* 跨越第1列到第2列(不包含第3列) */ grid-row: 1 / 2; /* 占据第1行 */ background-color: #ffcccb; }
.item-2 { grid-column: 3 / 4; grid-row: 1 / 3; /* 跨越第1行到第2行 */ background-color: #d4edda; }
.item-3 { grid-column: 1 / 2; grid-row: 2 / 3; background-color: #d1ecf1; }
.item-4 { grid-column: 2 / 3; grid-row: 2 / 3; background-color: #fff3cd; }
.item-5 { grid-column: 3 / 4; grid-row: 3 / 4; background-color: #d4edda; }
.item-6 { grid-column: 1 / 4; /* 跨越所有列 */ grid-row: 3 / 4; background-color: #f8d7da; }</style><div class="dashboard"> <div class="item item-1">Item 1</div> <div class="item item-2">Item 2</div> <div class="item item-3">Item 3</div> <div class="item item-4">Item 4</div> <div class="item item-5">Item 5</div> <div class="item item-6">Item 6</div></div>动画
使用@keyframes定义动画,并在元素中应用动画
<style> .flash-div { width: 100px; height: 100px; background: red; /*使用动画,infinite循环播放*/ animation: flash 5s infinite; }
/*定义动画*/ @keyframes flash { 0% { background: red; } 50% { background: yellow; } 100% { background: red; } }</style><div class="flash-div"></div>示例: - 循环动画
过渡
transition属性用于指定元素过渡效果,其中包括了:
transition-property规定应用过渡的CSS属性的名称transition-duration定义过渡效果花费的时间。默认是 0。transition-timing-function规定过渡效果的时间曲线。默认是 “ease”。transition-delay规定过渡效果何时开始。默认是 0。
<style> .transition { width: 100px; height: 100px; background: red; /*定义一个延迟1s后使用1s线性变换改变宽度的过渡动画*/ transition: width 1s linear 1s; }
.transition:hover { width: 200px; }</style><div class="transition"></div>示例:一个延迟1s后使用1s线性变换改变宽度的过渡动画
多媒体查询
多媒体查询包括:
- viewport(视窗) 的宽度与高度
- 设备的宽度与高度、朝向 (横屏,竖屏) 。
- 分辨率
语法为:
@media not|only 媒体类型 and (限制条件) { CSS 代码}例如:
<style> .my-media { width: 100px; height: 100px; background-color: antiquewhite; }
/* 当屏幕宽度小于或等于1000px时应用 */ @media screen and (max-width: 1000px) { .my-media { width: 100px; height: 100px; background-color: black; } }</style><div class="my-media"></div>运行结果:(可改变浏览器宽度查看效果)
伪类
在前面的超链接的示例已经看到了部分伪类,:hover :active :focus :visited :link,这里还有其他的部分:
:first-child
p:first-child i { /*匹配所有作为元素的第一个子元素的 <p> 元素中的所有 <i> 元素*/ color: blue;}同样的还有:last-child最后一个子元素、 :nth-child第二个子元素、 :nth-last-child 倒数第二个子元素
更多的伪类请自行查看w3school的伪类↗
伪元素
after
::after元素在元素之后添加内容,例如:
p::after { content: " - 尾部"; color: red;}示例:
这是实际的内容
其他的伪元素也感觉没什么用,自行查看w3school的伪元素↗
实战使用
导航/侧栏
一个导航框很容易想起无序列表+链接的形式,但这需要css辅助完成
<style> ul { /*移除列表前小标志*/ list-style-type: none; /*取消列表内外边距*/ margin: 0; padding: 0; /*设置宽度为页面的25%*/ width: 25%; background-color: #f1f1f1; /*设置列表为固定定位*/ position: fixed; /*溢出元素框,如果溢出就显示滚动条*/ overflow: auto; }
li a { /*块级,每个元素单独一行*/ display: block; color: #000; padding: 8px 16px; /*移除文本的修饰(下划线)*/ text-decoration: none; }
li a:active { background-color: #4CAF50; color: white; /*移除文本的修饰(下划线)*/ text-decoration: none; }
li a:hover:not(:active) { /*在没有选中并鼠标停留阶段*/ background-color: #555; color: white; /*移除文本的修饰(下划线)*/ text-decoration: none; }</style><ul> <li><a>菜单主页</a></li> <li><a>项目</a></li></ul>对于横向的导航,可以这样写
<style> ul { width: 100%; list-style-type: none; margin: 0; padding: 0; background-color: #f1f1f1; /*隐藏滚动条*/ overflow: hidden; /* 弹性布局 */ display: flex; }
/*多设置一个li的布局*/ li { /*让元素浮于左侧*/ float: left; /*弹性布局平分*/ flex: 1; }
li a { color: #000; padding: 15px 0; display: block; text-decoration: none; /* 文本水平居中 */ text-align: center; }
/*下面相同*/ li a:active { background-color: #4CAF50; color: white; text-decoration: none; }
li a:hover:not(:active) { background-color: #555; color: white; text-decoration: none; }</style>
<ul> <li><a>菜单主页</a></li> <li><a>项目</a></li></ul>下拉菜单
实现一个下拉菜单:
- 定义一个按钮,这个按钮需要在鼠标移上去后显示下拉菜单,最好能更改颜色
- 定义一个下拉菜单(直接就能显示的内容)
<style> /* 下拉按钮样式 */ .dropbtn { background-color: #4CAF50; color: white; padding: 16px; font-size: 16px; /*移除边框*/ border: none; /*使用光标指针(把鼠标变成手形)*/ cursor: pointer; }
/* 容器 <div> - 定位下拉内容 */ .dropdown { /*相对位置*/ position: relative; }
/* 下拉内容 (默认隐藏) */ .dropdown-content { /*隐藏*/ display: none; /*绝对位置*/ position: absolute; background-color: #f9f9f9; min-width: 160px; /*给下拉内容加个背景阴影*/ box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2); /*如果是右对齐(现在是左对齐)*/ /*right:0;*/ }
/* 下拉菜单的链接(内容) */ .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; }
/* 鼠标移上去后修改下拉菜单链接颜色 */ .dropdown-content a:hover { background-color: #f1f1f1 }
/* 在鼠标移上去后显示下拉菜单 */ .dropdown:hover .dropdown-content { display: block; }
/* 当下拉内容显示后修改下拉按钮的背景颜色 */ .dropdown:hover .dropbtn { background-color: #3e8e41; }</style>
<div class="dropdown"> <button class="dropbtn">下拉菜单</button> <div class="dropdown-content"> <a>选项 1</a> </div></div>tips:下拉菜单里也可以放置图片等
提示框
<style>/*提示框元素*/.tooltip { position: relative; display: inline-block; border-bottom: 1px dotted black;}
/*提示框文字*/.tooltip .tooltiptext { visibility: hidden; width: 120px; background-color: black; color: #fff; text-align: center; /*框圆角*/ border-radius: 6px; padding: 5px 0; position: absolute; /*堆叠层级,表示在大多数其他元素之上*/ z-index: 1; /*相对正常位置的偏移量(向上五个像素)因为padding是5px*/ top: -5px; /*表示位于包含元素宽度110%的位置(即稍微超出右侧边界)使提示框显示在触发元素的右侧*/ left: 110%;
如果是左侧就right: 110% 如果是上面就buttom: 100% 但同时要设置居中:left: 50%; margin-left: -60px;(偏移宽度的一半) 如果是下面就top: 100% 但同时要设置居中:left: 50%; margin-left: -60px;(偏移宽度的一半)
/*淡入:从透明度0开始*/ opacity: 0; transition: opacity 1s;}/*三角形的样式,可记*/.tooltip .tooltiptext::after { /*使用after伪元素在元素之后添加内容,这里是三角形*/ content: "";/*不需要字*/ position: absolute; /*垂直居中*/ top: 50%; /*元素的右侧距离其包含块右侧的距离为包含块宽度的100%,在提示框的左侧*/ right: 100%; margin-top: -5px; border-width: 5px; border-style: solid; /*上右下左的顺序,利用CSS边框的特性,当相邻边框颜色不同时会产生斜角效果*/ border-color: transparent black transparent transparent;}.tooltip:hover .tooltiptext { visibility: visible; /*淡入:透明度变为1*/ opacity: 1;}</style>
<div class="tooltip">鼠标移动到这 <span class="tooltiptext">提示文本</span></div>