CSS实用技巧

25次阅读

共计 1213 个字符,预计需要花费 4 分钟才能阅读完成。

浮动元素不换行
做豆瓣的时候模仿豆瓣的正在热映的列表做的一个样式,效果及代码如下
.item-lists {
overflow-x: auto;
white-space: nowrap;
font-size: 0;
padding: 15px 0 30px 0; /*no*/
/* 去掉滚动条 */
&::-webkit-scrollbar {
display: none;
}
.item {
display: inline-block;
width: 100px; /*no*/
margin-left: 40px;
vertical-align: top;
&:nth-of-type(1) {
margin-left: 0;
}
}
最主要的就是要在在父元素设置 white-space: nowrap; 来保证子元素强制不换行
设置子元素与父元素等大小
之前我们写这样的了能就是为子元素设置 width:100%;height:100%;,其实也可以使用如下的写法
.parent{
position:relative;
width:200px;
height:200px;
.child{
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: red;
}
}
/* 也就是说子元素不设置高度和宽度,通过 absolute 的属性将子元素‘撑开’到父元素的大小 */
/* 如果我们设置了如 left:20px; right:20px; 那么就相当于设置父元素 padding:0 20px; 子元素设置 width:100%;*/
元素等大小左右浮动

之前如果我们想要实现一个这样的效果,可能会使用浮动,现在可以考虑使用如下的写法
ul{
width: 100%;
padding:0 18px;/*no*/
box-sizing: border-box;
margin-top: 30px;/*no*/
>li{
width: 50%;
padding: 3px;/*no*/
float: left;
box-sizing: border-box;
text-align: center;
font-size: 15px;/*no*/
background-color: #ffffff;
a{
display: block;
padding: 12px 0;/*no*/
width: 100%;
height: 100%;
background-color: #f6f6f6;
color: #333;
}
}
}
元素向上 / 向左拉伸
CSS 默认情况下盒子的长度变化是由上向下的方向进行变化,宽度变化是由左向右,有时候需要实现盒子由底层弹出,或者盒子由右侧向左弹出的效果,可以使用如下代码,由右向左同理
.test {
position: absolute;
bottom: 0;
width: 100px;
height: 10px;
background-color: red;
}
测试效果
$(“.test”).animate({“height”: “100px”}, 2000)

正文完
 0