其实,本身刚开始接触前端的时候,觉得界面就是和正常的流式布局同样,这里放一个div,那里放一个div,整个界面就搭建完成了。作过几回项目后,好像本身想的也没错,就按照前面的思路,能够解决大部分的界面搭建。可是有的时候,须要一些特殊的处理,这些特殊的处理,可能会打破你脑壳中的流式布局。
下面就介绍几种状况,仅供参考(持续更新)css
从后端拉取一张图片,而后显示在前端,图片宽高各为100px,或者宽高比为1:1
//html <img src=url /> //css img { width: 100px; height: 100px; }
为何移动端要用百分比? 请戳👉
经过margin和padding实现占位:margin/padding百分比都是以父元素的width为参照物的
假设图片的宽度为20%html
#father { width: 20%; } #father:after { content: '', display: block; margin-top: 100%; }
经过上面的方式,就能够实现将div撑开,实现宽度和高度一致。
若是这样怎么把图片给加进去呢?前端
经过绝对定位就能够了。
#father { width: 20%; position: relative; } #father::after { content: '', display: block; margin-top: 100%; } img { position: absolute; width: 100%; top: 0; }
经过定位方式和伪类,实现内容覆盖
如图所示,div下面有一个横线,当点击它的时候,让他从中间开始向两侧消失segmentfault
思考:如何实现这样一个场景,能够分为两步,先在底部加一个横线,而后再让横线动起来
.test { width: 180px; height: 25px; line-height: 25px; color: #7E7E99; } .select { position: relative; } .select:after { position: absolute; bottom: 0; left: 0px; width: 100%; height: 2px; content: ''; background: 'black'; }
线段宽度设置为0就能够消失了,那么怎么可能让它从中间消失呢?敲黑板:将线段的margin-left设置为50%,至关于将线段从左侧向右移动了一半后端
上面两个效果合起来就是从两侧向中间消失。布局
具体代码以下:动画
.test { width: 180px; height: 25px; line-height: 25px; color: #7E7E99; } .select { position: relative; } .select:after { position: absolute; bottom: 0; left: 0px; width: 100%; height: 2px; content: ''; background: 'black'; transition: all 1s ease-in-out; } .no-select { position: relative; } .no-select:after { position: absolute; bottom: 0; left: 50%; width: 0; height: 2px; content: ''; background: 'balck'; transition: all 1s ease-in-out; }
经过动画的两种形态(宽度和左边距)的变化组合,变为一种形态(消失)的变化。