写在最前:移动端中导航的网花样结构无处无在,宽高怎么设置相适应?米素怎么居中对齐?差别场景怎么选择代码更高效?巧妙使用margin、padding等基础属性,小小技巧可以解决许多烦恼!
首页导航结构(无间距)
<div class="a286-3b37-c724-79e7 g-grid"> <div class="3b37-c724-79e7-e6bb g-grid-item"> <div class="c724-79e7-e6bb-1bac g-grid-imgWrap"> <img class="5fee-b011-ed7d-800b item-img" src="img/g1.png" /> </div> <p class="b011-ed7d-800b-d16e 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="ed7d-800b-d16e-22f7 g-grid"> <div class="800b-d16e-22f7-14a1 g-grid-item"> <img class="d16e-22f7-14a1-98b0 item-img" src="img/g1.png" /> <p class="22f7-14a1-98b0-9792 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米代金券、领会最新阿里云产物的种种优惠流动点击进入
微商必备的两款全自动引流软件!导师;qwx5510或者lwy720927 引流首先是为了提高成交率,正所谓得流量者,得天下!现在不管是微商还是电商都都离不开精准客源,但是如何获取精准客源成为了大家的难...
做为国内物流的关键一极,顺通部的领导干部近些年数次交替。现如今,各种各样数据信息显示信息,中通快递一直稳居第一。尤其是上星期,中通速递与阿里巴巴新星协同项目投资,中通速递完成了一轮股权融资,临时无...
织梦DedeCMS系统栏目列表页中,对于推荐的文章中,织梦dedecms系统会默认对其标题进行加粗,有些用户觉得没有必要。 去掉加粗方法 打开include目录下的arc....
常州这个老牌发达城市啊,有一批特殊的人,他们就是“常州高端模特”。模特们每日光鲜艳丽,但其背后的辛苦只有他们自己知道。每天都会有很多的演出来预约他们进行表演与展示和服务。当然收入也是不菲的,预约的价...
世界十大黑客的莫里斯 1、罗伯特·塔潘-莫里斯(Robert Tappan Morris) 莫里斯的父亲是前美国国家安全局的一名科学家,名叫罗伯特·莫里斯(Robert Morris)。他是莫里斯蠕虫...
初读《西游》时正当年少,觉得没有《水浒》好看,觉得不过是师徒四人经历了九九八十一难最后成佛成圣的故事罢了,哪像劫生辰纲那般诡计,哪像武松为兄报仇那般快意,哪像张清的石头籽那般神奇,就连大闹天宫,都不如...