1. css文字超出宽度显示成…
.txt{
overflow:hidden;
white-space:nowrap;
text-overflow:ellipsis;
}
/* 如果没有文字超出隐藏的效果,可以加上min-width:0*/
2. 弹性布局自适应垂直居中
.div{
display:flex;
flex-direction:column; // 意义为:弹性盒子中的内容排列方式为纵列排列。
justify-content:center; // 元素开始的对齐方式 从中间开始对齐。 flex-start | flex-end | center | space-between | space-around;
}
附W3C display弹性布局链接:http://www.runoob.com/w3cnote/flex-grammar.html
3.css渐变色边框
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<style>
.demo1 {
width: 200px;
height: 100px;
border: 10px solid;
border-image: -webkit-linear-gradient(left, red, blue) 200 30;
border-image: -o-linear-gradient(right, red, blue) 200 30;
border-image: -moz-linear-gradient(right, red, blue) 200 30;
border-image: linear-gradient( to right,red, blue) 200 30;
}
</style>
<body>
<div class="demo1"></div>
</body>
</html>
效果图:
4. css四角边框样式
.box{
width: 300px;
height: 300px;
margin: 300px auto;
border: 2px solid #eeeeee;
/* 以下为css样式 */
background:
linear-gradient(red, red ) left top,
linear-gradient(blue, blue ) left top,
/* 以上为左上角 */
linear-gradient(red, red ) left bottom,
linear-gradient(yellow, yellow ) left bottom,
/* 以上为左下角 */
linear-gradient(red, red ) right top,
linear-gradient(black, black ) right top,
/* 以上为右下角 */
linear-gradient(red, red ) right bottom,
linear-gradient(green, green ) right bottom;
/* 以上为右下角 */
background-repeat: no-repeat;
background-size: 4px 50px, 50px 4px;
}
效果图:
5. 文字的横向距离
p{
letter-spacing: 0.15em;
}
6. css3媒体查询
引用链接
7. div的高度自适应的与宽度一样
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>宽高相等demo</title>
<style>
* {
margin: 0%;
padding: 0%;
}
.box {
width: 50%;
height: 200px;
margin: 200px auto;
background: red;
}
.content1 {
width: 200px;
background: greenyellow;
overflow: hidden;
}
.content1::after {
content: '';
display: block;
padding-top: 100%;
background: blue;
overflow: hidden;
}
</style>
</head>
<body>
<div class="box">
<div class="content1">
</div>
</div>
</body>
</script>
</html>
效果:
此种方法不受父级元素的影响,但是缺点在于,宽度是由div .content设置的,高度是div .content的伪类提供的,因此当div .content中 或 div .content::after 中有内容后,div .content 或 div .content::after 会被撑开;
效果图:
因此要用此类方法,想要在 div .content中添加内容时,需要在给 div .content中加入一个子元素,使用position定位的方法。代码如下:
<!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>宽高相等demo</title>
<style>
* {
margin: 0%;
padding: 0%;
}
.box {
width: 50%;
height: 200px;
margin: 200px auto;
background: red;
}
.content1 {
width: 200px;
background: greenyellow;
overflow: hidden;
position: relative;
}
.content1::after {
content: '';
display: block;
padding-top: 100%;
background: blue;
overflow: hidden;
}
.content1-son{
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: 10;
background: darkblue;
color: #fff;
}
</style>
</head>
<body>
<div class="box">
<div class="content1">
<div class="content1-son">
这是content1-son 的内容
</div>
</div>
</div>
</body>
</html>
效果图 :
2.第二种方法:结合父级元素的宽度。代码如下:
<!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>宽高相等demo</title>
<style>
* {
margin: 0%;
padding: 0%;
}
.box {
width: 50%;
height: 200px;
margin: 200px auto;
background: red;
}
.content2{
width: 20%;
height: 0;
padding-bottom: 20%;
background: darkcyan;
}
</style>
</head>
<body>
<div class="box">
<div class="content2">
wqewqeqw
</div>
</div>
</body>
</html>
效果图:
如图可以看出,给div .content 设置width: 20%时,height: 0px; padding-bottom: 20%;只要padding- bottom与width的值相等,div .content便会坐到宽高相等。且,与父级元素高度无关,只有父级元素的宽度有关。
8. img 图片在div中自适应的水平垂直居中
.imgbox{
width: 100%;
height: 140px;
overflow: hidden;
position: relative;
}
.imgbox img{
width: auto !important;
max-width: 100%;
max-height: 140px;
position: absolute;
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
主要用到。position定位和margin外边距, img 要添加width: auto;使其宽度自适应。但是也要设置max-width和max-height,防止其超出父级盒子。此方法具有很好的适应性。
9.一个鼠标悬停时的边框样式
html部分
<ul>
<li>
<a class="facebook" href="#">
<span></span>
<span></span>
<span></span>
<span></span>
<i class="fa fa-facebook" aria-hidden="true"></i>
</a>
</li>
</ul>
css部分
body {
margin: 0;
padding: 0;
background: #ebebeb;
}
ul {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
margin: 0;
padding: 0;
display: flex;
}
ul li {
list-style: none;
}
ul li a {
display: block;
position: relative;
width: 100px;
height: 100px;
line-height: 100px;
font-size: 40px;
text-align: center;
text-decoration: none;
color: #404040;
margin: 0 30px;
transition: .5s;
padding-top: 50px;
}
ul li a span {
position: absolute;
transition: transform .5s;
}
ul li a span:nth-child(1),
ul li a span:nth-child(3) {
width: 100%;
height: 3px;
background: #404040;
}
ul li a span:nth-child(1) {
top: 0;
left: 0;
transform-origin: right;
}
ul li a:hover span:nth-child(1) {
transform: scaleX(0);
transform-origin: left;
transition: transform .5s;
}
ul li a span:nth-child(3) {
bottom: 0;
left: 0;
transform-origin: left;
}
ul li a:hover span:nth-child(3) {
transform: scaleX(0);
transform-origin: right;
transition: transform .5s;
}
ul li a span:nth-child(2),
ul li a span:nth-child(4) {
width: 3px;
height: 100%;
background: #404040;
}
ul li a span:nth-child(2) {
top: 0;
left: 0;
transform: scale(0);
transform-origin: bottom;
}
ul li a:hover span:nth-child(2) {
transform: scale(1);
transform-origin: top;
transition: transform .5s;
}
ul li a span:nth-child(4) {
top: 0;
right: 0;
transform: scale(0);
transform-origin: top;
}
ul li a:hover span:nth-child(4) {
transform: scale(1);
transform-origin: bottom;
transition: transform .5s;
}
.facebook:hover {
color: #3b5998;
}
.facebook:hover span {
background: #3b5998;
}
效果图: 正常情况下
鼠标悬停时: