日常开发中高频使用的CSS代码分享给大家,其中可能有很多大家经常使用的,有一些是经常忘记的但是常用的。欢迎大家提出更优的解决方法!!
1、背景渐变
/*线性渐变*/
background-image: linear-gradient(#0066FF, #9966FF);
/*径向渐变*/
background-image: radial-gradient(red, yellow, green);
2、长英文字母或长数字自动换行
word-wrap: break-word;
white-space: normal;
word-break: break-all;
3、文本单行与多行超出使用省略号
/*如果实现单行文本的溢出显示省略号*/
overflow: hidden;
text-overflow:ellipsis;
white-space: nowrap;
/*多行文本溢出显示省略号*/
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
4、水平垂直居中
/*flex*/
div {
display: flex;
jjustify-content: center;
align-items: center;
}
/*position */
.box {
background-color: #FF8C00;
width: 300px;
height: 300px;
position: relative;
}
.content {
background-color: #F00;
width: 100px;
height: 100px;
position: absolute;
left: 50%;
top: 50%;
margin: -50px 0 0 -50px;
}
5、设置input 的placeholder的字体样式
input::-webkit-input-placeholder { /* Chrome/Opera/Safari */
color: red;
}
input::-moz-placeholder { /* Firefox 19+ */
color: red;
}
input:-ms-input-placeholder { /* IE 10+ */
color: red;
}
input:-moz-placeholder { /* Firefox 18- */
color: red;
}
6、取消input的边框
border: none;
outline: none;
7、图片不变形
- object-fit: cover;
.img {
object-fit: cover;/*表示图片将在保持自身比例不变的前提下填充整个无形框*/
}
如图:

- object-fit: contain;
.img1 {
object-fit: contain; /*表示图片将不形变地最大限度地完整显示在无形框内,且会居中显示*/
}
如下图:

8、阴影
/*内阴影*/
box-shadow: inset 2px 0 4px #000;
/*外阴影*/
box-shadow:0px 0px 10px #000;
9、CSS3对话气泡
.div{
width: 200px;
height: 100px;
border: 2px solid #ff0;
border-radius: 7px; /*圆角弧度为7px*/
position: relative;
background-color: #ff0;
}
.div::before{
content: '';
width: 0;
height: 0;
border: 20px solid;
position: absolute;
bottom: -40px;
left: 140px;
border-color: #ff0 transparent transparent;
}
<div class="div">
这是一个demo
</div>
如下图:

10、css多边形
- 三角形
.shape {
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 100px solid red;
}
如下图:

- 椭圆
.shape {
width: 200px;
height: 100px;
background: red;
-moz-border-radius: 100px / 50px;
-webkit-border-radius: 100px / 50px;
border-radius: 100px / 50px;
}

- 平行四边形
.shape {
width: 150px;
height: 100px;
margin-left:20px;
-webkit-transform: skew(20deg);
-moz-transform: skew(20deg);
-o-transform: skew(20deg);
background: red;
}

- 梯形
.shape {
border-bottom: 100px solid red;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
height: 0;
width: 100px;
}

- 心形
.shape {
position: relative;
width: 100px;
height: 90px;
}
.shape:before,
.shape:after {
position: absolute;
content: "";
left: 50px;
top: 0;
width: 50px;
height: 80px;
background: red;
-moz-border-radius: 50px 50px 0 0;
border-radius: 50px 50px 0 0;
-webkit-transform: rotate(-45deg);
-moz-transform: rotate(-45deg);
-ms-transform: rotate(-45deg);
-o-transform: rotate(-45deg);
transform: rotate(-45deg);
-webkit-transform-origin: 0 100%;
-moz-transform-origin: 0 100%;
-ms-transform-origin: 0 100%;
-o-transform-origin: 0 100%;
transform-origin: 0 100%;
}
.shape:after {
left: 0;
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
-ms-transform: rotate(45deg);
-o-transform: rotate(45deg);
transform: rotate(45deg);
-webkit-transform-origin: 100% 100%;
-moz-transform-origin: 100% 100%;
-ms-transform-origin: 100% 100%;
-o-transform-origin: 100% 100%;
transform-origin :100% 100%;
}

- 提示对话框
.shape {
width: 120px;
height: 80px;
background: red;
position: relative;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
}
.shape:before {
content:"";
position: absolute;
right: 100%;
top: 26px;
width: 0;
height: 0;
border-top: 13px solid transparent;
border-right: 26px solid red;
border-bottom: 13px solid transparent;
}

11、两个子元素 一个有内容自动撑开,另一个为空如何跟随高度
.div{
display: flex;
align-items: stretch;
}
12、flex 子元素设置固定宽度失效
flex-grow:0;
flex-shrink:0;
13、隐藏滚动条或更改滚动条样式
/*css主要部分的样式*//*定义滚动条宽高及背景,宽高分别对应横竖滚动条的尺寸*/
::-webkit-scrollbar {
width: 10px; /*对垂直流动条有效*/
height: 10px; /*对水平流动条有效*/
}
/*定义滚动条的轨道颜色、内阴影及圆角*/
::-webkit-scrollbar-track{
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3);
background-color: rosybrown;
border-radius: 3px;
}
/*定义滑块颜色、内阴影及圆角*/
::-webkit-scrollbar-thumb{
border-radius: 7px;
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3);
background-color: #E8E8E8;
}
/*定义两端按钮的样式*/
::-webkit-scrollbar-button {
background-color:cyan;
}
/*定义右下角汇合处的样式*/
::-webkit-scrollbar-corner {
background:khaki;
}
喜欢文章的小伙伴们欢迎关注我的公众号前端小喵,定期推送精选好文~
网友评论