宽高都为百分比的情况下有什么办法好垂直水平居中?transform会有模糊,不推荐

现在 有一个DIV有宽高的元素,想要在浏览器的中间,该如何处理?不使用transform

红糖糍粑
浏览 569回答 7
7回答

撒科打诨

div{    position:fixed;    top:0;    left:0;    right:0;    bottom:0;    margin:auto;}

PIPIONE

#father{    display: flex;    align-items: center;    justify-content: center;}或者:#father {    display: -webkit-box;    -webkit-box-align: center;    -webkit-box-pack : center;}

智慧大石

高宽百分比的话 left=(100 - 宽的百分比)/2+'%'top同理啊

慕虎7371278

div{        position:fixed;        left: 50%;        top: 50%;        margin-left: -20%;        margin-top: -150px;        width: 40%;        height: 300px;        background: yellowgreen;    }不定高度用JS或者CSS3.

陪伴而非守候

display:table-cell;text-align:center;vertical-align:middle;

手掌心

<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <title>Title</title>&nbsp; &nbsp; <style>&nbsp; &nbsp; &nbsp; &nbsp; * {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; margin: 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; padding: 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; .out {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width: 200px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height: 200px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; background: green;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; position: relative;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; .in {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width: 40%;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height: 40%;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; background: red;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; position: absolute;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*(100%-40%)/2 */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; top: 30%;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; left: 30%;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </style></head><body>&nbsp; &nbsp; <div class="out">&nbsp; &nbsp; &nbsp; &nbsp; <div class="in"></div>&nbsp; &nbsp; </div></body></html>

慕尼黑8549860

所有的水平垂直居中方案都在这了点进去选一个吧
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript