【问题标题】:How to make a div the width of the container?如何使 div 成为容器的宽度?
【发布时间】:2019-08-21 07:31:27
【问题描述】:

我一直在寻找解决方案,但似乎找不到相关的解决方案。我基本上希望底部带有文本(黄色圆圈)的 div 是容器的全宽(红色供参考)

另外,底部的 div 似乎不在页面的中心,为什么会这样?

HTML

<body>



    <header>
        <div class="container">
            <nav>
                <ul>
                    <li id="main_link"><a href="#">Website</a></li>
                    <li><a href="#">Home</a></li>
                    <li><a href="#">About</a></li>
                    <li><a href="#">Gallery</a></li>
                    <li><a href="#">News</a></li>
                    <li><a href="#">Contact</a></li>
                </ul>
            </nav>
        </div>
    </header>


    <div class="showcase">
        <div class="container">

            <div class="showcase_overtext">
                <h1>A beautiful website</h1>
                <p>I did ma best</p>

            <div class="button">
                    <button>READ MORE</button>
                </div>
            </div>

            <div class="floating_section">
                <h1>FEATURED</h1>
            </div>

        </div>
    </div>

CSS:

/*Universal*/

body {
    margin:0;
    padding:0;
}

.container {
    text-align:center;
    margin:auto;
    width:80%;
    background:red;
}

/*Header*/

header {
    background-color:rgba(0,0,0,0.3);
    font-family:'Montserrat',sans-serif;
    padding:10px;
}

header ul {
    margin:0;
}

header ul li {
    display:inline;
    padding:0 10px 0 15px;
}

header ul li a {
    color:#353535;
    font-weight:bold;
    text-decoration: none;
}

#main_link {
    padding-right:50px;
}

/*Showcase*/

.showcase {
    height:500px;
    background-image:url('Showcase.jpg');
    background-size:cover;
    position:relative;
}

.showcase_overtext {
    color:#353535;
    font-family:'Montserrat',sans-serif;
    font-size:25px;
    font-weight:bold;
    position:absolute;
    top:300px;
    right:50px;
}

.showcase_overtext h1, p {
    margin:5px;
}

.button {
    text-align:center;
}

.floating_section {
    position:absolute;
    bottom:10px;
    display:inline-block;
}

谢谢

【问题讨论】:

  • link 会帮助你

标签: html css layout css-position


【解决方案1】:

您真的不想像以前那样使用绝对元素。绝对元素从页面流中删除,因此具有自己的大小。移除绝对定位让网站更接近您的目标:

body {
  margin: 0;
  padding: 0;
}

.container {
  text-align: center;
  margin: auto;
  width: 80%;
  background: red;
}


/*Header*/

header {
  background-color: rgba(0, 0, 0, 0.3);
  font-family: 'Montserrat', sans-serif;
  padding: 10px;
}

header ul {
  margin: 0;
}

header ul li {
  display: inline;
  padding: 0 10px 0 15px;
}

header ul li a {
  color: #353535;
  font-weight: bold;
  text-decoration: none;
}

#main_link {
  padding-right: 50px;
}


/*Showcase*/

.showcase {
  height: 500px;
  background-image: url('Showcase.jpg');
  background-size: cover;
  position: relative;
}

.showcase_overtext {
  color: #353535;
  font-family: 'Montserrat', sans-serif;
  font-size: 25px;
  font-weight: bold;
  margin-top: 300px;
  right: 50px;
}

.showcase_overtext h1,
p {
  margin: 5px;
}

.button {
  text-align: center;
}

.floating_section {
  display: inline-block;
}
<header>
  <div class="container">
    <nav>
      <ul>
        <li id="main_link"><a href="#">Website</a></li>
        <li><a href="#">Home</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Gallery</a></li>
        <li><a href="#">News</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
    </nav>
  </div>
</header>


<div class="showcase">
  <div class="container">

    <div class="showcase_overtext">
      <h1>A beautiful website</h1>
      <p>I did ma best</p>

      <div class="button">
        <button>READ MORE</button>
      </div>
    </div>

    <div class="floating_section">
      <h1>FEATURED</h1>
    </div>

  </div>
</div>

【讨论】:

    【解决方案2】:

    通过将left: 0right: 0 添加到floating_section绝对定位您的中心 - 请参阅下面的演示:

    body {
      margin: 0;
      padding: 0;
    }
    
    .container {
      text-align: center;
      margin: auto;
      width: 80%;
      background: red;
    }
    
    header {
      background-color: rgba(0, 0, 0, 0.3);
      font-family: 'Montserrat', sans-serif;
      padding: 10px;
    }
    
    header ul {
      margin: 0;
    }
    
    header ul li {
      display: inline;
      padding: 0 10px 0 15px;
    }
    
    header ul li a {
      color: #353535;
      font-weight: bold;
      text-decoration: none;
    }
    
    #main_link {
      padding-right: 50px;
    }
    
    .showcase {
      height: 500px;
      background-image: url('https://via.placeholder.com/500');
      background-size: cover;
      position: relative;
    }
    
    .showcase_overtext {
      color: #353535;
      font-family: 'Montserrat', sans-serif;
      font-size: 25px;
      font-weight: bold;
      position: absolute;
      top: 300px;
      right: 50px;
    }
    
    .showcase_overtext h1,
    p {
      margin: 5px;
    }
    
    .button {
      text-align: center;
    }
    
    .floating_section {
      position: absolute;
      bottom: 10px;
      display: inline-block;
      left: 0; /* added */
      right: 0; /* added */
    }
    <header>
      <div class="container">
        <nav>
          <ul>
            <li id="main_link"><a href="#">Website</a></li>
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Gallery</a></li>
            <li><a href="#">News</a></li>
            <li><a href="#">Contact</a></li>
          </ul>
        </nav>
      </div>
    </header>
    <div class="showcase">
      <div class="container">
        <div class="showcase_overtext">
          <h1>A beautiful website</h1>
          <p>I did ma best</p>
          <div class="button">
            <button>READ MORE</button>
          </div>
        </div>
        <div class="floating_section">
          <h1>FEATURED</h1>
        </div>
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 2018-01-04
      • 1970-01-01
      • 1970-01-01
      • 2015-05-09
      • 2011-09-06
      • 1970-01-01
      • 1970-01-01
      • 2011-06-06
      • 1970-01-01
      相关资源
      最近更新 更多