首页 > 解决方案 > 该框未居中,并且还与它一起移动背景图像

问题描述

我想在网页上将黑框居中,但是每当我尝试调整黑框#banner_image的填充时,就会开始上下移动,每当我尝试调整包含黑框的边距以使其居中时,bg 图像就会开始滚动。#banner_content

我无法理解这里发生了什么,有人可以解释一下吗?

    #banner_image{
    background: url(../Images/intro-bg_1.jpg) no-repeat center center;
    padding-top: 10%;
    padding-bottom: 25%;
    text-align: center;
    color: #f8f8f8;
    background-size: cover;
}

    #banner_content{
    background-color: rgba(0, 0, 0, .7);
    max-width: 660px;
    position: relative;
    padding-top: 6%;
    padding-bottom: 6%;
    margin-top: 6%;
    margin-bottom: 6%;
    margin-left: auto;
    margin-right: auto;
}
   <div id="banner_image">
            <div class="container">
                <div id="banner_content">
                    
                </div>
            </div>
        
        </div>

标签: htmlcssbootstrap-4

解决方案


<style>
    #banner_image{
    background: url(./bg.jpg) no-repeat center center;
    /* Adjust  the values as you need them */
    width: 100vw;
    height: 100vh;
    text-align: center;
    color: #f8f8f8;
    background-size: cover;
    }
    /* Use the container to center the black box */
    .container {
      position: relative;
      /* Adjust  values to what you need. Remember to always have a set width and height if you want your elements to not move around the page and cause cumulative layout shift problems */
      width: 100vw;
      height: 100vh;
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
    }
    #banner_content{
    background-color: rgba(0, 0, 0, .7);
    min-width: 400px;
    min-height: 400px;
    }


  </style>

<div id="banner_image">
    <div class="container">
        <div id="banner_content">
            
        </div>
    </div>

  </div>

结果应该是这样的


推荐阅读