将div居中放置在另一个div中,并防止其离开

时间:2020-03-23 21:09:14

标签: html css twitter-bootstrap bootstrap-4

我开发了一个div,我打算在该div内放置一个图像,然后放置两个div。

在缩小屏幕时,是否有办法使两个div居中,并防止它们超出主div?

有人可以帮我吗?

DEMO - 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居中?

Image

如何防止他们离开主要部门?

Images

1 个答案:

答案 0 :(得分:1)

我不是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;
}