文章目录
1-1:弹性布局 --横向居中
display:block; 允许设定宽高,添加换行符
display:inline-block:允许设定宽高,且不添加换行符,因此该元素可以位于其他元素旁边
display:inline:设定宽高无效,且不添加换行符,因此该元素可以位于其他元素旁边
使文字等比例放大缩小全局设定:font-size:25px;有文字的元素中设定font-size:0.25rem
注:10px=1em
块级标签弹性布局: 父元素设定使子元素上下左右垂直:display: flex; justify-content: center; align-items: center;
1-2:文字居中
vertical-align
父元素:display: table; 子元素: display: table-cell; vertical-align: middle; text-align: center;
line-height
子元素设定固定高度:height: 100px;text-align: center; line-height: 100px;
1-3 文字超出父级标签
文字一行-超出为省略号
text-overflow: ellipsis;overflow: hidden; white-space:nowrap
文字多行-超出为省略号
overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
1-4 图片随div的大小变化,自动填充并居中显示
1-5 普通流布局
去除默认间隙:父元素: font-size:0; letter-spacing: -4px;
子元素: display: inline-block
1-6 清除浮动
为什么清除浮动
1.当父元素不设定高度时,高度由子元素的内容决定.但子元素为浮动时,父元素的高度为0
2.为了避免产生浮动,父组件的背景或边框不能正确显示,这个时候我们就需要clear:both清除浮动
不清除浮动
清除浮动
可使用overflow:hidden或者clear:both
overflow:hidden-----------在父组件中用
clear:both-----------创建同级子组件,样式中增加
1-7 :after :before 的使用
功能:before,after 在元素内容前后加入指定内容
利用after 画小三角形
.flex_container:after{
content: '';
display: inline-block; /*使设定的宽高有效*/
width: 0;
height: 0;
border: 16px solid transparent; /*transparent:透明*/
border-left: 16px solid #b31d1d;
/* border-top: 16px solid #54b31d;
border-right: 16px solid #b7ca0d;
border-bottom: 16px solid #1815b8; */
position: relative;
top: 2px;
left: 10px;
}
如果设定全部的border
1-8 块级元素上下左右居中
宽高已知
width: 100px;height:100px;position:absolute; left:0;right:0; top:0;bottom:0;margin:auto;
宽高未知
width:20%; height: 20%; position: absolute; top:50%; left:50%; transform: translate(-50%,-50%); background-color: orange;
1-9 动画
div波纹
.main{
width: 400px;
height: 400px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
background-color: darkturquoise;
padding: 10px;
border-radius:40%;
animation: wave 5s linear infinite;
}
@keyframes wave{
100%{
transform: translate(-50%,-50%) rotate(360deg);
}
}
1-10 border属性
原图:
border-image/border | demo | 样列 |
---|---|---|
round | border-image: url('./image/border.png') 30 30 round; | ![]() |
stretch | border-image: url('./image/border.png') 30 30 stretch; | ![]() |
double | border: 12px double #b1a4a4; | ![]() |
groove | border: 4px groove #5a9fe9; | ![]() |
ridge | border: 4px ridge #5a9fe9; | ![]() |
inset | border: 4px inset #5a9fe9; | ![]() |
outset | border: 4px outset #5a9fe9; | ![]() |
inherit | #inheritPartent{border:1px dotted #5a9fe9; } #inheritChildren{ border:inherit } | ![]() |
1-11 渐变色
background-image:linear-gradient(45deg,red,yellow)
1-12 横向书签
.tag-horizontal {
position: absolute;
background-color: #f2f5fa;
padding: 0 1.4rem;
margin-top: 0.3rem;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
height: 1.8rem;
min-width: 5rem;
text-align: center;
line-height: 1.8rem;
left: -1.5px;
&::after {
content: "";
position: absolute;
left: 100%;
top: 0;
border-color: #f2f5fa transparent #f2f5fa #f2f5fa;
border-width: 0.9rem 0.9rem 0.9rem 0;
border-style: solid;
}
}
1-13 圆点(1-12图片的圆点)
.tag-horizontal::before {
content: "";
position: absolute;
left: 0.4rem;
top: 0.45rem;
width: 0.7rem;
height: 0.7rem;
background-color: var(--bgColor);
border-radius: 50%;
-webkit-box-shadow: inset 2px 0 2px #3e6d81;
box-shadow: inset 2px 0 2px #3e6d81;
}
1-14 毛玻璃
backdrop-filter: blur(24px); background: hsla(0, 0%, 100%, 0.7);
hsla()
函数使用色相、饱和度、亮度、透明度来定义颜色。backdrop-filter
:透过该层的底部元素模糊化
- 完整代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>毛玻璃</title>
</head>
<style>
* {
padding: 0;
margin: 0;
}
html,
body {
height: 100%;
background: #f4f4f4;
}
.box {
height: 100%;
background: url('../img/bg.png') 100% 100% no-repeat;
background-position: center center;
}
.glass {
width: 50%;
height: 50%;
margin: 0 auto;
backdrop-filter: blur(24px);
background: hsla(0, 0%, 100%, 0.7);
box-shadow: 0 10px 28px rgb(25 78 183 / 6%);
padding: 32px;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
</style>
<body>
<div class="box">
<div class="glass"></div>
</div>
</body>
</html>
1-15 模糊遮罩
backdrop-filter:saturate(150%) contrast(50%) blur(8px);
-webkit-backdrop-filter:saturate(150%) contrast(50%) blur(8px);
background-color:rgba(0,0,0,.3);
- 防止透过遮罩层内容过暗,配合了
saturate(150%)
使用,意为使…饱和,类似ps饱和度效果,<100%变暗,>100%变亮
1-16 图片放大
//父盒子
overflow: hidden;
// 图片
transition: 1s;
// 图片悬浮
transform: scale(2);
<style>
* {
padding: 0;
margin: 0;
}
html,
body {
height: 100%;
}
.box {
width: 200px;
height: 200px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border: 5px solid orange;
overflow: hidden;
}
img {
width: 200px;
height: 200px;
background: url("../img/bg.png") no-repeat;
background-position: center center;
transition: 1s;
background-size: cover;
}
img:hover {
transform: scale(2);
}
</style>
<body>
<div class="box">
<img url="../img/bg.png" />
</div>
</body>
</html>
1-17 全屏图片
body {
margin: 0;
background: #e8e8e8 url("../img/bg.png") center bottom;
/* 设置背景图片会不会随着屏幕滚动 */
background-attachment: fixed;
/* 此时会保持图像的纵横比并将图像缩放成将完全覆盖背景定位区域的最小大小。 */
background-size: cover;
/* 去掉好像也没什么 */
display: flex;
/* 设置height:100vh,能够保证元素无论是否有没有内容,高度都等于屏幕高度。 */
min-height: 100vh;
flex-direction: column;
}
1-18 按钮悬浮特效
html:
<div class="primary-btn">
<a href="#" class="price-btn"> <span>Try it's Free</span> </a>
</div>
.primary-btn {
width: 300px;
height: 200px;
margin: 0 auto;
margin-top: 25%;
}
.price-btn {
color: #ffffff;
position: relative;
display: inline-block;
overflow: hidden;
padding: 15px 50px;
border-radius: 50px;
text-align: center;
text-transform: capitalize;
background: #ef9334;
display: block;
}
.price-btn span {
color: #ffffff;
position: relative;
z-index: 2;
}
.price-btn:hover {
color: #ffffff;
}
.price-btn:hover::before {
animation: criss-cross-left 0.8s both;
animation-direction: alternate;
}
.price-btn:hover::after {
animation: criss-cross-right 0.8s both;
animation-direction: alternate;
}
.price-btn::before,
.price-btn::after {
position: absolute;
top: 50%;
content: "";
width: 20px;
height: 20px;
background-color: #7215fa;
border-radius: 50%;
color: #ffffff;
}
.price-btn::before {
left: -20px;
transform: translate(-50%, -50%);
}
.price-btn::after {
right: -20px;
transform: translate(50%, -50%);
}
@keyframes criss-cross-left {
0% {
left: -20px;
}
50% {
left: 50%;
width: 20px;
height: 20px;
}
100% {
left: 50%;
width: 375px;
height: 375px;
}
}
@keyframes criss-cross-right {
0% {
right: -20px;
}
50% {
right: 50%;
width: 20px;
height: 20px;
}
100% {
right: 50%;
width: 375px;
height: 375px;
}
}
1-19 动画
html:
<div class="feature-image-area shake-y">
<img class="feature-img" src="../img/bg.png" />
<img class="feature-img_1 pulse" src="../img/bg.png" />
<div class="feature-shape left-right-rotate"></div>
</div>
css:
.feature-image-area {
width: 500px;
height: 500px;
margin: 0 auto;
margin-top: 25%;
position: relative;
}
.feature-img {
width: 500px;
height: 500px;
outline: 2px solid rgb(102, 54, 173);
outline-offset: 10px;
border-radius: 10px;
position: relative;
z-index: 2;
}
.feature-img_1 {
width: 250px;
height: 250px;
position: absolute;
border-radius: 10px;
bottom: -20%;
left: -20%;
z-index: 22;
}
/* 半圆 */
.feature-shape {
position: absolute;
bottom: -44%;
width: 230px;
height: 260px;
margin: 20px;
clip-path: circle(45% at 50% 1%);
background: #7215fa;
right: -29%;
}
.shake-y {
animation: shakeY 5s ease-in-out infinite;
}
@keyframes shakeY {
0% {
transform: translateY(10px);
}
50% {
transform: translateY(-10px);
}
100% {
transform: translateY(10px);
}
}
.pulse {
animation: pulse 3s ease-in-out infinite;
}
@keyframes pulse {
from {
transform: scale3d(1, 1, 1);
}
50% {
transform: scale3d(1.05, 1.05, 1.05);
}
to {
transform: scale3d(1, 1, 1);
}
}
.left-right-rotate {
animation: rotated-style3 2s infinite alternate;
}
@keyframes rotated-style3 {
0% {
transform: rotate(-10deg);
-webkit-transform: rotate(-10deg);
}
100% {
transform: rotate(10deg);
-webkit-transform: rotate(10deg);
}
}
1-20 卡片悬浮动画
可以如此布局:
<div class="card"></div>
.card {
width: 400px;
height: 400px;
margin: 0 auto;
margin-top: 25%;
background-color: #fff;
box-shadow: 0 10px 25px rgb(0 0 0 / 3%);
padding: 40px 0;
border-radius: 20px;
position: relative;
border: 1px solid #ef9334;
transition: 0.4s;
-webkit-transition: 0.4s;
}
.card:before {
position: absolute;
background: #fff;
z-index: 2;
transition: 0.6s;
-webkit-transition: 0.6s;
-moz-transition: 0.6s;
content: "";
height: 100%;
left: -20px;
top: -20px;
width: 100%;
}
.card:hover {
box-shadow: 0 10px 25px rgb(0 0 0 / 8%);
}
.card:hover::before {
width: 0px;
height: 0;
}
1-21 图标悬浮动画
代码:关键代码:
transition: 0.6s;
-webkit-transition: 0.6s;
-moz-transition: 0.6s;
.card:hover img {
transform: scale(-1) rotate(180deg);
}
具体代码
.card {
width: 100px;
height: 100px;
border-radius: 50%;
background-color: orange;
margin: 0 auto;
vertical-align: middle;
text-align: center;
line-height: 120px;
}
img {
width: 40px;
height: 40px;
position: relative;
z-index: 3;
transition: 0.6s;
-webkit-transition: 0.6s;
-moz-transition: 0.6s;
}
.card:hover img {
transform: scale(-1) rotate(180deg);
}
</style>
<body>
<div class="card">
<img src="../img/bg.png" />
</div>
</body>
1-22 图片悬浮动画
此为动画效果
代码:关键代码:
animation: vgJgjBFw 1s steps(30) forwards;
@keyframes vgJgjBFw {
0% {
background-position: 0 0;
}
to {
background-position: 0 100%;
}
}
具体代码:
.box {
width: 106px;
height: 103px;
position: absolute;
top: 50%;
left: 50%;
overflow: hidden;
}
.img {
width: 100%;
height: 100%;
background: url(../img/icon04_977c284_977c284.png) no-repeat;
}
.box:hover .img {
animation: vgJgjBFw 1s steps(30) forwards;
}
@keyframes vgJgjBFw {
0% {
background-position: 0 0;
}
to {
background-position: 0 100%;
}
}
<body>
<div class="box">
<div class="img"></div>
</div>
</body>
此图为原图:
<div class="card">
<img src="../img/bg.png" />
</div>
.card {
width: 100px;
height: 100px;
border-radius: 50%;
background-color: orange;
margin: 0 auto;
vertical-align: middle;
text-align: center;
line-height: 120px;
}
img {
width: 40px;
height: 40px;
position: relative;
z-index: 3;
transition: 0.6s;
-webkit-transition: 0.6s;
-moz-transition: 0.6s;
}
.card:hover img {
transform: scale(-1) rotate(180deg);
}
1-23 div 两侧圆形内部凹陷
- html
<div class='btn'></div>
- css
.btn {
width: 200px;
text-align: center;
height: 50px;
line-height: 50px;
background: #fff;
color: #fff;
position: relative;
border-radius:10px;
border:1px solid #f66;
}
.btn:after {
position: absolute;
content: '';
width: 10px;
height: 20px;
background: #fff;
top: 13px;
right: -1px;
border-radius: 10px 0 0 10px;
border:1px solid #f66;
border-right:none;
}
.btn:before {
position: absolute;
content: '';
width: 10px;
height: 20px;
background: #fff;
top: 13px;
left: -1px;
border-radius: 0 10px 10px 0 ;
border:1px solid #f66;
border-left:none;
}
code
1-6
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>清除浮动</title>
<style>
*{
margin:0;
padding:0;
}
.flex_container{
border:1px solid #0aabb1;
}
.clear{
clear: both;
}
.flex_item-left{
width:10rem;
height: 10rem;
border:1px solid #0aabb1;
float: left;
}
.flex_item-right{
width:10rem;
height: 10rem;
border:1px solid #0aabb1;
float: right;
}
</style>
</head>
<body>
<div class="flex_container">
<div class="flex_item-left"></div>
<div class="flex_item-right"></div>
<div class="clear"></div>
</div>
</body>
</html>
1-5
<style>
html,body{
margin:0;
padding:0;
height:100%;
}
.flex_container {
width: 50%;
height: 50%;
background-color: #bff7d4;
margin: 0 auto;
margin-top: 15%;
/*去除普通流布局子元素之间的div间隙*/
font-size:0;
letter-spacing: -4px;
}
.flex_item{
width: 15%;
height: 50%;
background-color: palevioletred;
margin: 0 auto;
/* 普通流布局 */
display: inline-block; /*子元素在一样*/
}
</style>
<div class="flex_container">
<div class="flex_item"></div>
<div class="flex_item"></div>
<div class="flex_item"></div>
<div class="flex_item"></div>
<div class="flex_item"></div>
</div>
1-4
<style>
html,body{
margin:0;
padding:0;
height:100%;
}
.flex_container {
width: 50%;
height: 50%;
background-color: #bff7d4;
margin: 0 auto;
margin-top:10%;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
.flex_item{
width: 20%;
height: 50%;
background-color: palevioletred;
margin: 0 auto;
margin-right: 1%;
text-align: center;
position: relative;
background-image: url('./油彩.jpeg'); /*图片背景颜色 ---当图片想要填充到一个div中时*/
background-position: 50% 50%; /*图片居中*/
background-size: 90% 90%; /* 图片大小*/
background-repeat: no-repeat;/*图片只有一张,不重复*/
}
</style>
<div class="flex_container">
<div class="flex_item"></div>
<div class="flex_item"></div>
<div class="flex_item"></div>
<div class="flex_item"></div>
<div class="flex_item"></div>
</div>
1-3-2
<style>
html,body{
margin:0;
padding:0;
height:100%;
}
.flex_container {
width: 50%;
height: 50%;
background-color: #bff7d4;
margin: 0 auto;
}
.flex_item{
width: 50%;
height: 40px;
background-color: palevioletred;
margin: 0 auto;
/* 文字多行-超出显示为省略号 */
overflow: hidden;
display: -webkit-box;
-webkit-line-clamp: 2; /*表示显示的行数,溢出加省略号*/
-webkit-box-orient: vertical;/*从上到下垂直排列子元素(设置伸缩盒子的子元素排列方式)*/
}
</style>
<div class="flex_container">
<div class="flex_item">文字居中文字居中文字居中文字居中文字居中文字居中文字居中文字居中文字居中文字居中文字居中文字居中文字居中文字居中文字居中文字居中文字居中文字居中</div>
</div>
1-3-1
<style>
html,body{
margin:0;
padding:0;
height:100%;
}
.flex_container {
width: 50%;
height: 50%;
background-color: #bff7d4;
margin: 0 auto;
}
.flex_item{
width: 50%;
height: 100px;
background-color: palevioletred;
margin: 0 auto;
/* 文字一行-超出显示为省略号 */
text-overflow: ellipsis;
overflow: hidden;
white-space:nowrap;/*文本不会换行,文本会在在同一行上继续,直到遇到 <br> 标签为止 */
}
</style>
<div class="flex_container">
<div class="flex_item">文字居中文字居中文字居中文字居中文字居中文字居中文字居中文字居中文字居中</div>
</div>
1-2-2
<style>
html,body{
margin:0;
padding:0;
height:100%;
}
.flex_container {
width: 50%;
height: 50%;
background-color: #bff7d4;
margin: 0 auto; /*div 左右居中*/
}
.flex_item{
width: 50%;
height: 100px;
background-color: palevioletred;
margin: 0 auto;
text-align: center;/*文字左右居中*/
line-height: 100px;/*文字垂直居中*/
}
</style>
<!-- 准备好一个容器 -->
<div class="flex_container">
<div class="flex_item">文字居中</div>
</div>
1-2-1
<style>
html,body{
margin:0;
padding:0;
height:100%;
}
.flex_container {
width: 50%;
height: 500px;
background-color: #bff7d4;
/* margin: 0 auto;
line-height: 500px; */
display: table;
}
.flex_item{
width: 50%;
height: 50%;
background-color: palevioletred;
display: table-cell;
vertical-align: middle;/*垂直居中*/
text-align: center;/*水平居中*/
}
</style>
<div class="flex_container">
<div class="flex_item">文字居中</div>
</div>
1-1
<style>
/* 宽度高度为整个屏幕 */
html,body{
margin:0;
padding:0;
/* 宽度不用设置,默认为100% */
height:100%;
font-size: 25px;
}
/* 使div上下左右居中 */
.flex_container {
width: 50%;
height: 50%;
background-color: #bff7d4;
margin: 0 auto;
margin-top: 5%;
display: flex; /*弹性布局*/
flex-direction:row;/*子元素横向排列*/
justify-content: center;/*相对父元素水平居中-包括行和块级元素*/
align-items: center; /*相对父元素垂直居中*/
}
.flex_item{
width: 20%;
height: 50%;
margin-right: 2%;
background-color: palevioletred;
text-align: center; /* 文字居中 */
font-size: 0.25rem;
/*
1.定义了项目的放大 比例,如果为0,即使有剩余空间也不会放大
2.如果取值为负数那么和0的取值效果相同
*/
flex-grow: 0;
/* font-size: 0.16em; */
}
</style>
<!-- 准备好一个容器 -->
<div class="flex_container">
<div class="flex_item">文字居中</div>
<div class="flex_item">文字居中</div>
<div class="flex_item">文字居中</div>
<div class="flex_item">文字居中</div>
<span>文字居中</span>
</div>