简体   繁体   English

CSS-使所有内容适合div,中心背景图像

[英]css - fit all content inside a div, center background image

屏幕截图说明了问题

As shown in the screenshot, there are contents inside a div element which has a background image. 如屏幕截图所示, div元素中包含内容,该内容具有背景图像。 This div container is set to have height: 40% of its parent, I believe it is <ion-content> in this case. div容器设置为具有height: 40%其父容器的height: 40% ,在这种情况下,我相信它是<ion-content> Now, the image and the text underneath are cropped. 现在,下面的图像和文本被裁剪。 I want to make them fit inside the div container. 我想让它们适合div容器。 The background image is also not centered in the div . 背景图像也不位于div中心。

  1. How can I style it such that all the contents can be displayed? 如何设置样式以便可以显示所有内容?
  2. How can I center my background image both vertically and horizontally? 如何将背景图像垂直和水平居中?

It somehow works if I make the browser viewport narrower as shown below. 如果使浏览器视口变窄,它会以某种方式起作用,如下所示。 However, I would like to have everything displayed and the background image centered both vertically and horizontally regardless the width of the viewport. 但是,我想显示所有内容并且无论视口的宽度如何, 背景图像的垂直和水平居中对齐。

屏幕截图显示了预期的结果


HTML 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 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);
}

Edit 编辑

Added a CodePen link to illustrate my problem. 添加了CodePen链接来说明我的问题。

The problem is the background-size: cover css rule. 问题是background-size: cover css规则。 You can test the difference between cover and contain here: http://www.w3schools.com/cssref/playit.asp?filename=playcss_background-size&preval=contain 您可以在此处测试掩护和包含掩护之间的区别: http : //www.w3schools.com/cssref/playit.asp?filename= playcss_background-size& preval= contain

It's fully compatibility with latest browsers: http://caniuse.com/background-img-opts 与最新的浏览器完全兼容: http : //caniuse.com/background-img-opts

Try this: 尝试这个:

.user-profile .user-profile-background {
    position: absolute;
    z-index: 1;
    width: 100%;
    height: 100%;
    background-image: url('../img/basketball.jpg');
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
}

You can set an explicit height for the background image for example height = 350px and set center center as the position property where you have specified the background image. 您可以为background image设置一个明确的height ,例如height = 350px ,并将center center设置为您指定背景图像的位置属性。 This way the height will remain fixed but as the view port is reduced, the background image will start cropping from left and right sides ie it would remain in the center. 这样,高度将保持固定,但是随着视口的减小,背景图像将从左右两侧开始进行裁剪,即它将保留在中央。 Then on smaller view port, you can reduce the height to say 250px . 然后在较小的视口上,您可以将height降低到250px

HTML HTML

<div class="root">
  <div class="subroot">
    <div class="container">
      <div class="square bg"></div>
    </div>
    <div class="passage">
      <p>Hello world!</p>
    </div>
  </div>
</div>

CSS CSS

.root {
  width: 70%;
  margin: auto;
  border: 2px solid yellow;
  background-image: url('http://baconmockup.com/600/400');
  background-size: cover;
  background-position: center;
}

.subroot {
  background-color: rgba(0, 0, 0, 0.7);
}

.container {
  border: 2px solid black;
  width: 100px;
  margin: auto;
}

.square {
  border: 2px dotted red;
  padding-bottom: 100%;
}

.bg {
  background-image: url('http://placehold.it/200x300');
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  border-radius: 50%;
}

.passage {
  border: 2px dotted green;
  width: 80%;
  margin: auto;
  text-align: center;
  white-space: nowrap;
  overflow: scroll;
  color: white;
}

Problem solved. 问题解决了。

问题解决了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM