浮動元素不換行
做豆瓣的時候模仿豆瓣的正在熱映的列表做的一個樣式,效果及代碼如下
.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)