CSS小案例
运动的汽车
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
img {
animation: move 5s infinite;
}
@keyframes move {
0% {
transform: translate3d(0,0,0);
}
50% {
transform: translate3d(800px,0,0);
}
51% {
transform: translate3d(800px,0,0) rotateY(180deg);
}
100% {
transform: translate3d(0,0,0) rotateY(180deg);
}
}
</style>
</head>
<body>
<img src="car.jpg" alt="" width="100" />
</body>
</html>
无缝滚动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
* {
padding: 0;
margin: 0;
}
ul {
list-style: none;
}
img {
display: block;
}
section {
width: 378px;
height: 86px;
margin: 100px auto;
overflow: hidden;
}
section ul {
width: 1000%;
animation: moving 5s linear infinite;
}
ul li {
float: left;
}
@keyframes moving {
from {
transform: translateX(0);
}
to {
transform: translateX(-882px);
}
}
section:hover ul {
animation-play-state: paused;
}
</style>
</head>
<body>
<section>
<ul>
<li><img src="nav1.jpg" alt=""></li>
<li><img src="nav2.jpg" alt=""></li>
<li><img src="nav3.jpg" alt=""></li>
<li><img src="nav4.jpg" alt=""></li>
<li><img src="nav5.jpg" alt=""></li>
<li><img src="nav6.jpg" alt=""></li>
<li><img src="nav7.jpg" alt=""></li>
<li><img src="nav1.jpg" alt=""></li>
<li><img src="nav2.jpg" alt=""></li>
<li><img src="nav3.jpg" alt=""></li>
</ul>
</section>
</body>
</html>
传统三等分
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
section {
width: 80%;
height: 200px;
margin: 100px auto;
border: 1px solid #ccc;
}
section div{
width: 33.33%;
height: 100%;
float: left;
}
div:nth-child(1) {
background-color: pink;
}
div:nth-child(2) {
background-color: red;
}
div:nth-child(3) {
background-color: blue;
}
</style>
</head>
<body>
<section>
<div></div>
<div></div>
<div></div>
</section>
</body>
</html>
伸缩布局实现三等分
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
section {
width: 80%;
height: 200px;
margin: 100px auto;
border: 1px solid #ccc;
display: flex;
}
section div{
height: 100%;
flex: 1;
}
div:nth-child(1) {
background-color: pink;
flex: 2;
order: 10;
}
div:nth-child(2) {
background-color: red;
order: -2;
}
div:nth-child(3) {
background-color: blue;
order: -1;
}
</style>
</head>
<body>
<section>
<div></div>
<div></div>
<div></div>
</section>
</body>
</html>
伸缩布局实现固定高度
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
section {
width: 80%;
height: 200px;
margin: 100px auto;
border: 1px solid #ccc;
display: flex;
min-width: 500px;
}
section div{
height: 100%;
}
div:nth-child(1) {
background-color: pink;
width: 200px;
}
div:nth-child(2) {
background-color: red;
width: 100px;
}
div:nth-child(3) {
background-color: blue;
flex: 1;
}
div:nth-child(4) {
background-color: green;
flex: 1;
}
</style>
</head>
<body>
<section>
<div>111</div>
<div>222</div>
<div>3333</div>
<div>4444</div>
</section>
</body>
</html>
伸缩布局实现排列方式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
section {
width: 80%;
height: 200px;
margin: 100px auto;
border: 1px solid #ccc;
display: flex;
min-width: 500px;
flex-direction: row-reverse;
flex-direction: column-reverse;
}
section div{
}
div:nth-child(1) {
background-color: pink;
width: 200px;
}
div:nth-child(2) {
background-color: red;
width: 100px;
margin: 0 6px;
}
div:nth-child(3) {
background-color: blue;
flex: 1;
}
div:nth-child(4) {
background-color: green;
flex: 1;
}
</style>
</head>
<body>
<section>
<div>111</div>
<div>222</div>
<div>3333</div>
<div>4444</div>
</section>
</body>
</html>
justify-content属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
section {
width: 80%;
height: 200px;
margin: 100px auto;
border: 1px solid #ccc;
display: flex;
min-width: 500px;
justify-content: flex-start;
justify-content: flex-end;
justify-content: center;
justify-content: space-between;
justify-content: space-around;
}
section div{
width: 200px;
}
div:nth-child(1) {
background-color: pink;
}
div:nth-child(2) {
background-color: red;
margin: 0 6px;
}
div:nth-child(3) {
background-color: blue;
}
</style>
</head>
<body>
<section>
<div>111</div>
<div>222</div>
<div>3333</div>
</section>
</body>
</html>
align-items属性详解
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
section {
width: 80%;
height: 600px;
margin: 100px auto;
border: 1px solid #ccc;
display: flex;
min-width: 500px;
align-items: stretch;
align-items: center;
align-items: flex-start;
align-items: flex-end;
}
section div{
width: 200px;
height: 200px;
}
div:nth-child(1) {
background-color: pink;
}
div:nth-child(2) {
background-color: red;
margin: 0 6px;
}
div:nth-child(3) {
background-color: blue;
}
</style>
</head>
<body>
<section>
<div>111</div>
<div>222</div>
<div>3333</div>
</section>
</body>
</html>
flex-wrap属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
section {
width: 80%;
height: 600px;
margin: 100px auto;
border: 1px solid #ccc;
display: flex;
min-width: 500px;
flex-wrap: nowrap;
flex-wrap: wrap-reverse;
flex-wrap: wrap;
align-content: space-between;
}
section div{
width: 500px;
height: 200px;
}
div:nth-child(1) {
background-color: pink;
}
div:nth-child(2) {
background-color: red;
margin: 0 6px;
}
div:nth-child(3) {
background-color: blue;
}
</style>
</head>
<body>
<section>
<div>111</div>
<div>222</div>
<div>3333</div>
</section>
</body>
</html>