div中内容上下居中小结
微wx笑
2020-08-06【网页网站】
143
5
0关键字:
CSS
虽然div中内容上下居中的问题已经是一个比较古老的话题,但是最近发现还是有很多前端开发者在询问如何实现。其实网络上已经有很多资料和案例了,我这里再总结一下几个比较常见的处理方式。
目录
虽然div中内容上下居中的问题已经是一个比较古老的话题,但是最近发现还是有很多前端开发者在询问如何实现。其实网络上已经有很多资料和案例了,我这里再总结一下几个比较常见的处理方式。
情形一:div限高,内容长度限一行
1 2 3 4 5 6 7 8 9 10 11 12 | < style > .v-align { margin: 0 auto; width: 200px; height: 80px; text-align: center; line-height: 80px; border: 1px solid #ddd; } </ style > <!-- html --> < div class = "v-align" >我的内容只能有一行。</ div > |
情形二:div限高,内容不限
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | .v-mult { margin: 0 auto; width: 200px; height: 100px; border: 1px solid #ddd; overflow: hidden; } .v-mult .empty, .v-mult .text { display: inline-block; *display: inline; *zoom: 1; vertical-align: middle; } .v-mult .empty { height: 100%; } <!-- html --> < div class = "v-mult" > < span class = "empty" ></ span > < span class = "text" >我的内容不限,多高都行 换行照常</ span > </ div > |
情形三:div高度不定,内容高度一定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | .v-auto { position: relative; margin: 0 auto; width: 200px; border: 1px solid #ddd; } .v-auto .text { position: absolute; top: 50%; margin-top: -50px; height: 100px; border: 1px dashed #ddd; } <!-- html --> < div class = "v-auto" > < div class = "text" > 我的高度是固定的,只有100px高,但是我的父及高度不定,我怎么垂直居中呢? </ div > </ div > |
情形四:div高度不定,内容高度不定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | .v-auto-out { position: relative; margin: 0 auto; width: 200px; border: 1px solid #ddd; } .v-auto-out .auto-in { position: absolute; top: 50%; border: 1px dashed #ddd; /* 这里有兼容性问题 */ -webkit-transform: translateY(-50%); -ms-transform: translateY(-50%); -o-transform: translateY(-50%); transform: translateY(-50%); } <!-- html --> < div class = "v-auto-out" > < div class = "auto-in" >我的高度不定,我的父及高度也不定,这下要上下居中,该如何是好?我们一起来瞧瞧吧。</ div > </ div > |
来源网址:http://www.divcss5.com/rumen/r50304.shtml
应用实例:图片放大查看上一张下一张按钮
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | < div id = "div_mark" style = "display:none; position: absolute; width:100%; height:1000px; top:0; left:0; background:rgba(0,0,0,0.7); z-index: 99999;" > < div style = "width:1200px;height:848px; margin:auto auto;" > < div style = "float:right; width:48px; height:48px; padding-right: 20px;" >< img id = "img_close" src = "/resource/close.png" style = "cursor:pointer; border: 1px dashed #888;" ></ div > < div style = "width:100%; clear:both;" > < div style = "width:60px;height:800px;float:left;" > < img id = "img_prev" src = "/resource/prev.png" style = "cursor:pointer; position: relative; top: 49%; border: 1px dashed #888;" > </ div > < div style = "width:1074px;height:100%;float:left;" > < img id = "img_ctnr" src = "" style = "width:100%;" > </ div > < div style = "width:60px;height:800px;float:right; text-align:right;" > < img id = "img_next" src = "/resource/next.png" style = "cursor:pointer; position: relative; top: 49%; border: 1px dashed #888;" > </ div > </ div > </ div > </ div > |
这里考虑到按钮的高度使用:position: relative; top: 49%;
完整代码请移步:图片放大查看上一张下一张功能实现方法
本文为转载文章,版权归原作者所有,不代表本站立场和观点。