首页 > 解决方案 > 在另一个 div 中居中 div 并防止它出去

问题描述

我开发了一个 div,在那个 div 里面我打算放一个图像,然后是一组两个 div。

有没有办法将下面的两个 div 居中,并在屏幕缩小时防止它们超出主 div?

有人能帮我吗?

演示 - Stackblitz

代码

<div class="col-md-6 col-sm-12">
    <div class="drop">
        <div class="abc">
            <img src="https://material-components-web.appspot.com/images/photos/2x3/2.jpg">
            <div class="boxImage" style="display:flex">
                <div class="boxImage1">
                </div>
                <div>
                </div>
            </div>
        </div>
    </div>
</div>

如何使两个 div 居中?

图片

如何防止他们离开主 div?

图片

标签: htmlcsstwitter-bootstrapbootstrap-4

解决方案


我不是 100% 确定这会回答你的问题,但这是我的看法。我不得不对你的 css 进行一些更改,因为 boxImage 类有点把它扔掉了。我在这些 div 上设置了背景颜色,以便您可以看到它们在屏幕中的位置。

app.component.html

<div class="col-md-6 col-sm-12">
<div class="d-flex justify-content-center p-2"><img src="https://material-components-web.appspot.com/images/photos/2x3/2.jpg"></div>
<div class="d-flex justify-content-center mb-3">
  <div class="p-2 boxImage1">Flex item 1</div>
  <div class="p-2 boxImage">Flex item 2</div>
</div>

app.component.css

    .drop {
    height: 670px;
    border-radius: 8px;
    overflow: hidden;
    text-align: center;
    background: #eff0f2;
    position: relative;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    margin-left: 8px;
    margin-right: 8px;

}

.abc {
    height: 100%;
  background-image: ('https://material-components-web.appspot.com/images/photos/2x3/2.jpg');
  }



.abc .boxImage {
    position: absolute;
    right: 27px;
    bottom: 40px;
}

.abc .boxImage1 {
    position: absolute;
    right: 373px;
    bottom: 0px;
}

.boxImage {
    background-color: red;
    background-repeat: no-repeat;
    height: 50px;
    width: 60%;
    border-radius: 8px;
    opacity: 1;
    font-size: 18px;
}

.boxImage1 {
    background-color: yellow;
    background-repeat: no-repeat;
    height: 50px;
    border-radius: 8px;
    opacity: 1;
    font-size: 18px;
    z-index: 1001;
}

推荐阅读