【发布时间】:2016-03-07 00:29:23
【问题描述】:
如屏幕截图所示,div 元素中有内容,该元素具有背景图像。这个div 容器被设置为其父级的height: 40%,我相信在这种情况下它是<ion-content>。现在,图像和下面的文本被裁剪。我想让它们适合div 容器。背景图片也不在div居中。
- 如何设置样式以显示所有内容?
- 如何将背景图像垂直和水平居中?
如果我使浏览器视口变窄,如下所示,它会以某种方式起作用。但是,无论视口的宽度如何,我都希望显示所有内容并且背景图像垂直和水平居中。
HTML
<div style="height: 40%; overflow: hidden;">
<div class="user-profile">
<div class="user-profile-background"></div>
<div class="user-profile-content">
<div class="row" style="height: 100%;">
<div class="col col-center">
<div class="row">
<div class="col col-33 col-offset-33">
<div style="height: 0; border-radius: 50%; background-image: url('img/ionic.png'); background-repeat: no-repeat; background-size: cover; background-position: center center; padding-top: 100%;">
</div>
</div>
</div>
<div class="row">
<div class="col" style="text-align: center; color: rgba(200, 200, 200, 1);">
<h3 style="color: white;">Seraph Cheng</h3>
<p><strong>Male, 28</strong></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
CSS
.user-profile {
position: relative;
width: 100%;
height: 100%;
}
.user-profile .user-profile-background {
position: absolute;
z-index: 1;
width: 100%;
height: 100%;
background-image: url('../img/basketball.jpg');
background-size: cover;
}
.user-profile .user-profile-content {
position: relative;
z-index: 2;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
}
编辑
添加了CodePen link 来说明我的问题。
【问题讨论】:
标签: javascript html css ionic-framework