HTML和CSS属性使用各类案例
2022-05-22 本文已影响0人
coderhzc
1. 当鼠标经过当前框的时候,显示出来一个播放按钮
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box {
position: relative;
width: 222px;
height: 220px;
margin: 100px auto;
}
.mask {
position: absolute;
display: none;
top: 0;
left: 0;
width: 222px;
height: 220px;
background: rgba(0,0,0,.3) url(images/arr.png) no-repeat center center;
}
.box a:hover .mask {
display: block;
width: 222px;
height: 220px;
background-color: rgba(0,0,0,.4);
}
</style>
</head>
<body>
<div class="box">
<a href="#">
<div class="mask"></div>
<img src="images/3.jpg" alt="">
</a>
</div>
</body>
</html>'
实际截图
image.png2. 去除出图片底测空白缝隙
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div {
border: 2px solid red;
}
div img {
/* 因为默认的是基线对齐,所有底册有空白缝隙 */
/* 去掉基线空白缝隙 */
vertical-align: bottom;
}
</style>
</head>
<body>
<div>
<img src="images/3.jpg" alt="">
</div>
</body>
</html>
实际截图
image.png3.white-space省略号效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div {
width: 150px;
height: 25px;
border: 1px solid red;
/*
想实现省略号的效果,这三行代码是必须要的
1.先强制一行内显示文本
white-space:nowrap
2.超出本分隐藏
overflow:hidden
3.文字用省略号代表超出的部分 /clip:不显示省略号,而是简单的裁切
text-overflow:ellipsis /clip
*/
white-space: nowrap;
overflow:hidden;
text-overflow: ellipsis;
}
</style>
</head>
<body>
<div>你在干什么,我是胡振楚,你在上面地方啊??</div>
</body>
</html>
实际截图
image.png4. 精灵图的制作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>精灵图的制作</title>
<style>
.icon1 {
width: 108px;
height: 110px;
/* 精灵图的显示制作 */
background: url(images/abcd.jpg) no-repeat 0 -7px;
}
.icon2 {
margin-top: 100px;
width: 94px;
height: 112px;
background: url(images/abcd.jpg) no-repeat -495px -275px;
}
</style>
</head>
<body>
<div class="icon1"></div>
<div class="icon2"></div>
</body>
</html>
实际截图
image.png5.margin负值突显出某个盒子效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div {
float: left;
width: 180px;
height: 320px;
margin-left: -1px;
border: 1px solid #ccc;
}
div:hover {
/* 定位的盒子是最高层的 */
border: 1px solid red;
/* 我们只要保证当前的盒子是定位,就会压住 标准流和浮动盒子 */
position: relative;
}
</style>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>
实际截图
image.png6.三角之美
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div {
width: 0;
height: 0;
border-top: 100px solid #ccc;
border-left: 100px solid blue;
border-right: 100px solid yellow;
border-bottom: 100px solid pink;
margin: 100px auto;
}
/*
1.我们用css边框可以模拟三角效果
2.宽度高度为0
3.我们4个边框都必须要写,只保留需要的边框的颜色,其余的不能省略,都可以改为transparent透明就可以了
4.为了战鼓兼容性,加上font-size:0;line-height:0
*/
p {
width: 0;
height: 0;
border-style: solid;
border-width: 100px;
border-color: transparent transparent transparent red;
margin: 100px auto;
font-size:0;
line-height:0
}
</style>
</head>
<body>
<div></div>
<p></p>
</body>
</html>
实际截图
image.png7.浮动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
height: 200px;
background-color: red;
}
strong {
float: left;
background-color: skyblue;
}
a {
/* float: right; */
float: left;
background-color: #fff;
}
</style>
</head>
<body>
<div class='box'>
<span>span元素</span>
<strong>strong元素</strong>
<a href="javascript:;">a元素</a>
</div>
</body>
</html>
实际截图
image.png8.背景滚动还是固定
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
body {
background-image: url(images/sms.jpg);
background-repeat: no-repeat;
background-position: center top;
/* 这个是将背景图片固定到页面上,文字可以实现滚动 */
/* background-attachment: fixed; */
/*默认是滚动的 */
background-attachment: fixed;
}
</style>
</head>
<body>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
<p>我是你胡爷爷,你看到了招呼还不打吗??</p>
</body>
</html>
实际截图
image.png9.背景半透明
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.alpha {
width: 300px;
height: 300px;
background-color: #000;
/*
背景半透明:
它对应着四个参数,red green blue 和 透明度(取值是0到1之间的一个数)
总结:这种写法的话,最主要的是它里面的内容文字这些的是不受影响的
*/
/* background:rgba(red, green, blue, alpha) */
background:rgba(0,0,0,.1) /*这个里面的文本是不会受到影响的*/
}
</style>
</head>
<body>
<div class="alpha">
啊哈哈哈哈哈哈哈啊哈哈
</div>
</body>
</html>
实际截图
image.png10.表格合并的问题
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
table,
td,
th {
border: 1px solid deeppink;
/*
这个属性的话,就是让我们的表格,单元格,th 合并相邻的边框
简单来说的意思就是说把边框的线条边的比以前要细很多
*/
border-collapse: collapse;
}
</style>
</head>
<body>
<!--表头单元格的设置,只需要把td换位th就可以了 -->
<table width="500" height="300" align="center" cellspacing='0' cellpadding='10'>
<!-- caption表格标题的定义,
通常这个标题会被居中显示与表格之上
caption标签必须紧随table标签之后
这个标签只存在表格里面才有意义
-->
<caption>个人信息表</caption>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
<tr>
<td style="color: red; background-color: pink;">张三</td>
<td>26</td>
<td>男</td>
</tr>
<tr>
<td>小王</td>
<td>28</td>
<td>男</td>
</tr>
<tr>
<td>小李</td>
<td>29</td>
<td>男</td>
</tr>
</table>
</body>
</html>
实际截图
image.png11.input动感表单效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
background-color: #999;
}
main {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 400px;
height: 400px;
border: 4px solid #eee;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.filed {
position: relative;
overflow: hidden;
margin-bottom: 20px;
}
.filed::before {
content: "";
position: absolute;
left: 0;
bottom: 0;
height: 4px;
width: 100%;
/* 线性渐变 */
background: linear-gradient(to right, white, #1abc9c, #f1c40f, skyblue, yellowgreen);
transform: translateX(-100%);
transition: 2s;
}
.filed:hover::before {
transform: translateX(100%);
}
.filed input {
border: none;
outline: none;
background: #eee;
padding: 6px;
}
</style>
</head>
<body>
<main>
<div class="filed"><input type="text" placeholder="请输入内容"></div>
<div class="filed"><input type="text" placeholder="请输入密码"></div>
</main>
</body>
</html>
实际截图
image.png12.鼠标经过,Tab栏切换:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
main {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
ul {
display: flex;
list-style: none;
}
ul li strong {
padding: 5px 30px;
background: skyblue;
text-transform: uppercase;
color: wheat;
}
ul li {
margin-right: 10px;
cursor: pointer;
}
div {
display: flex;
flex-direction: column;
background: #939229;
padding: 10px;
text-align: center;
line-height: 40px;
transform: scale(0);
transform: .6s;
}
div a {
color: wheat;
text-decoration: none;
padding: 5px 0;
}
ul li:hover strong+div {
transform: scale(1);
}
</style>
</head>
<body>
<main>
<ul>
<li>
<strong>video</strong>
<div>
<a href="">HDCMS</a>
<a href="">怕什么</a>
</div>
</li>
<li>
<strong>Live</strong>
<div>
<a href="">LINUX</a>
<a href="">CSS3</a>
</div>
</li>
</ul>
</main>
</body>
</html>
实际截图
image.png13.缩放图片案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
body {
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background: #2c3e50;
}
main {
display: flex;
justify-content: center;
align-items: center;
}
div {
width: 200px;
height: 200px;
margin: 20px;
overflow: hidden;
transition: all 2s;
}
div img {
width: 100%;
height: 100%;
border: 1px solid #95a5a6;
}
/* 这个是让div盒子变大 */
/*
main:hover div {
transform: scale(.6);
滤镜效果
filter: blur(5px);
}
main:hover div:hover {
transform: scale(1.6);
} */
/* 这个是让img变大 */
div>img {
filter: blur(5px);
height: 100%;
transition: 1s;
}
div>img:hover {
filter: none;
transition: scale(1.3)
}
</style>
</head>
<body>
<main>
<div><img src="../../../../img/0.JPG" alt=""></div>
<div><img src="../../../../img/9.JPG" alt=""></div>
<div><img src="../../../../img/16.JPG" alt=""></div>
</main>
</body>
</html>
实际截图
image.png14.安卓机器人
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
body {
background-color: #ccc;
}
.content {
width: 500px;
height: 500px;
border: 10px solid #fff;
margin: 100px auto;
background-color: skyblue;
position: relative;
border-right: 10px dashed #fff;
border-top: 10px dashed deeppink;
border-left: 10px dashed rgb(20, 255, 184);
border-bottom: 10px dashed rgb(153, 216, 35);
box-shadow: 12px 12px 20px rgb(80, 83, 80);
}
.an_header {
width: 250px;
height: 125px;
margin: 10px auto;
border-radius: 125px 125px 0 0;
background-color: green;
}
/* an_header里面的伪元素 */
.an_header::before {
content: '';
position: absolute;
width: 30px;
height: 30px;
background-color: rgb(196, 157, 157);
border-radius: 50%;
left: 180px;
top: 60px;
border: 2px dashed rgb(180, 138, 138);
border: 2px dashed rgb(0, 0, 0);
}
.an_header::after {
content: '';
position: absolute;
width: 30px;
height: 30px;
background-color: rgb(196, 157, 157);
border-radius: 50%;
left: 280px;
top: 60px;
border: 2px dashed rgb(0, 0, 0);
}
.an_body {
width: 250px;
height: 250px;
background-color: green;
margin: 10px auto;
position: relative;
border-radius: 5px;
}
.an_body::before {
content: '';
width: 30px;
height: 200px;
background-color: green;
border-radius: 10px;
position: absolute;
left: -45px;
top: 20px;
}
.an_body::after {
content: '';
width: 30px;
height: 200px;
background-color: green;
border-radius: 10px;
position: absolute;
left: 260px;
top: 20px;
}
.an_footer::before {
content: "";
width: 30px;
height: 100px;
background-color: green;
position: absolute;
border-radius: 0 0 10px 10px;
left: 160px;
top: 395px;
}
.an_footer::after {
content: "";
width: 30px;
height: 100px;
background-color: green;
position: absolute;
border-radius: 0 0 10px 10px;
left: 315px;
top: 395px;
}
.an_header>.div1 {
width: 50px;
height: 20px;
background-color: pink;
position: absolute;
left: 220px;
top: 100px;
border-radius: 0 0 22px 22px;
border: 2px solid red;
}
.div2,
.div3,
.div4,
.div5 {
width: 20px;
height: 20px;
position: absolute;
left: 110px;
top: 40px;
background-color: red;
border-radius: 50%;
border: 1px dashed #fff;
}
.div3 {
left: 110px;
top: 80px;
background-color: yellow;
border: 1px dashed #000;
}
.div4 {
left: 110px;
top: 120px;
background-color: deeppink;
border: 1px dashed rgb(166, 233, 11);
}
.div5 {
left: 110px;
top: 160px;
background-color: blue;
border: 1px dashed #fff;
}
.div6,
.div7 {
width: 50px;
height: 30px;
position: absolute;
left: 20px;
top: 160px;
background-color: bisque;
border-radius: 1px 1px 15px 15px;
font-size: 14px;
text-align: center;
line-height: 30px;
border: 1px dashed #000;
}
.div7 {
left: 180px;
top: 160px;
}
</style>
</head>
<body>
<div class="content">
<div class="an_header">
<div class="div1"></div>
</div>
<div class="an_body">
<div class="div2"></div>
<div class="div3"></div>
<div class="div4"></div>
<div class="div5"></div>
<div class="div6">RMB</div>
<div class="div7">RMB</div>
</div>
<div class="an_footer"></div>
</div>
</body>
</html>
实际截图
image.png15. video播放前视频的前置图片
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>视频封面</title>
</head>
<body>
<video width="400" height="400"
poster="https://img0.baidu.com/it/u=512340543,3139277133&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=281"
controls>
<source src="https://www.w3school.com.cn/i/movie.mp4" type="video/mp4">
<source src="https://www.w3school.com.cn/i/movie.mp4" type="video/ogg">
</video>
</body>
</html>
实际截图
image.png16. 两栏布局的实现
一般两栏布局指的是左边一栏宽度固定,右边一栏宽度自适应,两栏布局的具体实现:
利用浮动,将左边元素宽度设置为200px,并且设置向左浮动。将右边元素的margin-left设置为200px,宽度设置为auto(默认为auto,撑满整个父元素)。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>两栏布局的实现</title>
<style>
.other {
height: 100px;
}
.left {
height: 100%;
float: left;
width: 200px;
background: tomato;
}
.right {
height: 100%;
margin-left: 200px;
width: auto;
background: gold;
}
</style>
</head>
<body>
<div class="other">
<div class="left">1</div>
<div class="right">2</div>
</div>
</body>
</html>