前言
收录一下常用的css的代码片段,方便日常的工作。喜欢可收藏,在下会持续更新以及优化
在下从事前端开发不到六年,技术虽不牛,但是喜欢探讨,如有问题,也可交流探讨!
目录
代码
超出省略
- 单行
.ellipsis{
overflow:hidden;
white-space:nowrap;
text-overflow:ellipsis;
}
- 多行
- 弹性布局
display: -webkit-box; /*自适应布局——-webkit-box布局*/
overflow: hidden; /*溢出隐藏*/
-webkit-box-orient: vertical; /*在父元素设置子元素的排列式——垂直*/
-webkit-line-clamp: 3; /*显示文本的行数——2行,可根据需要修改*/
text-overflow: ellipsis;
- 伪元素定位遮挡
.box3 {
height: 100px;
width: 200px;
background-color: blueviolet; /* 背景色需要与元素背景色一置*/
overflow: hidden;
line-height: 20px;
position: relative;
}
.box3::after {
content: "...";
width: 1em;
background-color: blueviolet;
position: absolute;
right: 0;
bottom: 0;
}
元素文本不可选中
/* 元素文本不可选中 */
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
居中布局
flex
居中
.center{
display: flex;
align-items: center;
justify-content: center;
}
- 定位居中
transform
+ 定位
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 50%;
height: 100px;
margin
+ 定位
width: 50%;
margin: auto;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100px;
grid
居中
.grid-container {
display: grid;
place-items: center; /* 水平和垂直居中 */
height: 100vh;
}
- 文本以及内联元素居中
.center-text {
height: 100px; /* 或其他具体值 */
line-height: 100px; /* 与容器高度相同 */
text-align: center; /* 水平居中文本 */
}
毛玻璃效果
具体颜色可根据自身需要进行调整。
.glassBox {
background: rgba(99, 99, 99, 0.2) !important; /* 半透明背景 */
border-radius: 16px; /* 圆角 */
backdrop-filter: blur(5px); /* 模糊效果 */
border: 1px solid rgba(255, 255, 255, 0.5); /* 边框 */
}