CSS 垂直对齐 div,同时保持纵横比

标签 css center aspect-ratio

我在 jsfiddle.net/5tzk3/10 使用了 JsFiddle .我将其更改为将 div 显示为方形对话框(水平和垂直居中)。结果在jsfiddle.net/5tzk3/548 .

如您所见,水平居中很容易,但我无法将其垂直居中。谁知道如何使用纯 CSS 做到这一点?

编辑代码如下:

<div class="blind">
    <div class="wrapper">
        <div class="main">
            I'm your div with an aspect-ratio of 1:1!
        </div>
    </div>
</div>

html, body, .blind {
    height: 100%;
    width: 100%;    
}

.blind {
    left: 0;
    position: fixed;
    text-align: center;
    top: 0;
}

.wrapper {
    display: inline-block;
    margin: auto;
    position: relative;
    width: 50%;
}

.wrapper:after {
    content: '';
    display: block;
    padding-top: 100%;
}

.main {
    background-color: rgb(0, 162, 232);
    bottom: 0;
    left: 0;
    margin: auto;
    position: absolute;
    right: 0;
    top: 0; 
}

最佳答案

使用display:table作为父div,display:table-cell; vertical-align: middle 用于要垂直居中的内容 div。

关于CSS 垂直对齐 div,同时保持纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20315493/

相关文章:

ImageMagick:如何将图像最小化到一定的纵横比?

css - 将 float div 垂直对齐到未知大小的 div

iphone - 如何在 safari、chrome 或 firefox 等桌面浏览器上测试适用于 iPhone 的条件 CSS?

html - 如何在div的底部制作一个三 Angular 形

javascript - 响应式顶部导航——使用 div 重新定位链接?

html - CSS - 中心和圆形 Img Inside Div

html - 如何以HTML5为中心?

jquery - 如何使用CSS在中心并排对齐5个div?

android - 设备纵横比(媒体查询)不适用于 Droid 2

iOS:我从网络上得到很多 `UIImage` ,我怎么知道他们的 `aspect ratio` ?