写在最前:移动端中导航的网花样结构无处无在,宽高怎么设置相适应?米素怎么居中对齐?差别场景怎么选择代码最高效?巧妙使用margin、padding等基础属性,小小技巧可以解决许多烦恼!
首页导航结构(无间距)
<div class="g-grid">
<div class="g-grid-item">
<div class="g-grid-imgWrap">
<img class="item-img" src="img/g1.png" />
</div>
<p class="g-grid-label">汽车票船票</p>
</div>
<!-- 以下九个子米素结构相同省略 -->
</div>
.g-grid {
text-align: center;
overflow: hidden;
background: #fff;
}
.g-grid-item {
position: relative;
float: left;
width: 20%;
padding: 10px 0;
text-align: center;
}
.g-grid-imgWrap {
display: inline-block;
width: 30%;
height: 0;
padding-bottom: 30%;
}
.g-grid-imgWrap img {
width: 100%;
}
.g-grid-label {
font-size: 12px;
color: #333;
}
①、父米素g-grid通过overflow: hidden确立BFC,使得整体高度从1酿成自适应。通常可使用clearfix来消灭浮动的副作用
.clearfix:after{
display: block;
clear: both;
content: "";
visibility: hidden;
height: 0;
}
.clearfix{
zoom:1;
}
②、子米素g-grid-item通过float: left属性浮动起来,这也是该方式最主要的属性。
③、移动端对照常见的一个需求是高度凭据宽度举行自适应。这个时刻可以使用到padding-bottom。当width和padding-bottom相等时就实现了宽高相等(注重要将height置为0),闻一知十,种种比例下也可以设置。
width: 30%; height: 0; padding-bottom: 30%;
引申:vh和vw是css引入视口的观点来取代显示器的物理尺寸,它们作为单米的时刻也可实现该效果,虽然现在兼容性逐步变好,然则Android4.4之前不支持是硬伤。
vw:1vw即是视口宽度的1%。
vh:1vh即是视口高度的1%。
height:1vw; width:1vw;
首页导航结构(有间距)
页面结构与Float结构相同
.g-grid {
margin-right: -2%;
padding: 10px 10px 0;
font-size: 0;
background: #fff;
}
.g-grid-item {
position: relative;
display: inline-block;
width: 31.33%;
padding-bottom: 31.33%;
margin-right: 2%;
margin-bottom: 10px;
}
.g-grid-imgWrap {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 20px;
}
.g-grid-imgWrap img {
width: 100%;
height: 100%;
}
.g-grid-label {
position: absolute;
bottom: 0;
width: 100%;
height: 20px;
line-height: 20px;
font-size: 12px;
color: #333;
text-align: center;
}
①、g-grid-item设置display:inline-block结构经常会使得米素米素间莫名其妙泛起清闲。可以在写代码时使得米素和米素牢牢相连,但不太利便我们编写代码,IDE花样化之后也会自动离开。此处建议设置父米素g-grid的font-size属性为0就可以去掉清闲。
②、g-grid-item这些子米素之间需要距离时用到margin-right(或者margin-left),经常要处置最后一行设置为margin-right(或者margin-left)为0。有以下解决方式:
楼上的float结构也可以使用该方式去设置间隙
③、宽高若干不仅仅可以通过设置值来决议,该例子内里使用以下代码实现了width:100%,高度为父级高度减去20px,凭据场景差别来决议写法。
position: absolute; top: 0; left: 0; right: 0; bottom: 20px;
④、float结构和display:inline-block结构的水平居中通常使用text-align: center;,子米素在父米素里水平居中要求子米素display不为block
网格结构(无间距)
<div class="g-grid">
<div class="g-grid-item">
<img class="item-img" src="img/g1.png" />
<p class="g-grid-label">汽车票船票</p>
</div>
<!-- 以下八个子米素结构相同省略 -->
</div>
.g-grid {
display: grid;
grid-template-columns: repeat(3, 33.33%);
grid-template-rows: repeat(3, 100px);
background: #fff;
}
.g-grid-item {
display: inline-grid;
border-right: 1px solid #eee;
border-top: 1px solid #eee;
align-content: center
justify-items: center;
}
.g-grid-item:nth-child(3n) {
border-right: none;
}
.g-grid-item img {
height: 30px;
width: 30px;
}
.g-grid-label {
font-size: 12px;
color: #333;
}
①、grid结构通过grid-template-columns和grid-template-rows来设置几列几行
②、g-grid-item通过设置align-content: center;来使得子米素都处于垂直居中,justify-items: center来使得子米素都处于水平居中
九宫格结构(有清闲)
页面结构与Grid结构相同请输入代码
.g-grid {
display: flex;
flex-wrap: wrap;
}
.g-grid-item {
flex: 0 1 31.33%;
margin: 0px 1% 10px;
padding: 1.2rem;
box-sizing: border-box;
text-align: center;
background: #eee;
}
.g-grid-item img {
height: 30px;
width: 30px;
}
.g-grid-label {
font-size: 12px;
color: #333;
}
①、Flex结构通过flex-wrap: wrap;来举行换行,但当需要米素与米素之间存在间距时,不能使用justify-content: space-between;,削减一个米素会酿成下图:
以是该例子通过margin来设置间距。
②、.g-grid-item设置flex: 0 1 31.33%;意思是米素的原本巨细为父米素的31.33%,空间不足时该米素将缩小,存在剩余空间也不放大。
当该值设为flex: 1 1 31.33%;时,削减一个米素会酿成下图:
尊重原创,如需转载请注明出处!
原文来自:https://segmentfault.com/a/1190000020047828
1.阿里云: 本站现在使用的是阿里云主机,平安/可靠/稳固。点击领取2000米代金券、领会最新阿里云产物的种种优惠流动点击进入
2.腾讯云: 提供云服务器、云数据库、云存储、视频与CDN、域名等服务。腾讯云各种产物的最新流动,优惠券领取点击进入
3.广告同盟: 整理了现在主流的广告同盟平台,若是你有流量,可以作为参考选择适合你的平台点击进入
链接: http://www.fly63.com/article/detial/4655