如何使div成为容器的宽度?

时间:2019-03-30 15:33:42

标签: html css layout css-position

我一直在寻找解决方案,但似乎找不到相关的解决方案。我基本上想让底部带有黄色圆圈的文本的div为容器的整个宽度(红色为参考)

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

enter image description here

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

谢谢

2 个答案:

答案 0 :(得分:1)

通过在其中添加floating_sectionleft: 0

使您完全定位 right: 0 {请参见下面的演示

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>

答案 1 :(得分: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>