【问题标题】:How to get divs to take up full width of parent?如何让 div 占据父级的全宽?
【发布时间】:2019-01-29 08:07:16
【问题描述】:

我一直试图让 div(名称和猜测)占据父容器的整个宽度。无论我尝试过什么,它们都只能达到容器的 98%。如果我将每一个设置为 50%,它们就会崩溃,然后一个容器就在另一个容器之上。他们目前设置为 49%。有什么想法吗?

.guess-section {
  border: 1px solid red;
  width: 50%;
  background-color: #F7F7F7;
}

.guess-section article {
  background-color: #FFF;
  border: 2px solid #E0E0E0;
  margin: 2%;
  /*padding: 15px;*/
}

.r-low,
.r-high {
  font-weight: 700;
  text-decoration: underline;
}

.user-input {
  display: inline-block;
  border: 1px solid red;
  height: 100px;
  width: 49%;
}

.user-input input {
  display: inline-block;
  width: 100%;
}
<article class="set-challengers">
  <p>The Current Range is <span class="r-low">1</span> to <span class="r-high">100</span></p>
  <section class="challenger2">
    <h3>Challenger 2</h3>
    <div class="user-input">
      <label for="name">
  				<span>Name</span></br>
  				<input type="text" id="name" name="user_name">
				</label>
    </div>
    <div class="user-input">
      <label for="name">
  				<span>Guess</span></br>
  				<input type="text" id="name" name="user_name">
				</label>
    </div>
  </section>
  <section class="challenger-buttons">
    <button class="submit">SUBMIT GUESS</button>
    <button class="reset">RESET GAME</button>
    <button class="clear">CLEAR GAME</button>
  </section>
</article>

【问题讨论】:

标签: html css


【解决方案1】:

由于box model 的设计方式,大多数CSS 中包含的一个有用的“重置”是* { box-sizing: border-box; }。使用普通盒子模型,您的填充和边框被添加到元素的宽度。因此,“100% 宽”元素实际上是 100% + 边框 + 填充。 border-box 对此进行了更改,使宽度 + 边框 + 内边距为 100%。

另外:如果您想使用inline-block 创建两个彼此相邻的“正好 50% 宽”的元素,那么您需要使用负边距或技巧来删除标记中元素之间的空白。这是 HTML 中的一个小麻烦,您可以在下面的 sn-p 中看到我通过执行 &lt;/div 修复它,然后在下一行添加最后一个右括号 &gt;&lt;div class="user-input"&gt; inline-block 元素之间放置了少量空间当您的标记中它们之间有空格时。

* {
    box-sizing: border-box;
}

.guess-section {
    border: 1px solid red;
    width: 50%;
    background-color: #F7F7F7;
}

.guess-section article {
    background-color: #FFF;
    border: 2px solid #E0E0E0;
    margin: 2%;
    /*padding: 15px;*/
}

.r-low, .r-high {
    font-weight: 700;
    text-decoration: underline;
}

.user-input {
    display: inline-block;
    border: 1px solid red;
    height: 100px;
    width: 50%;
}

.user-input input {
    display: inline-block;
    width: 100%;
}
<article class="set-challengers">
        <p>The Current Range is <span class="r-low">1</span> to <span class="r-high">100</span></p>
        <section class="challenger2">
        <h3>Challenger 2</h3>
            <div class="user-input">
                <label for="name">
                <span>Name</span></br>
                <input type="text" id="name" name="user_name">
                </label>
            </div
            ><div class="user-input">
                <label for="name">
                <span>Guess</span></br>
                <input type="text" id="name" name="user_name">
                </label>
            </div>
        </section>
        <section class="challenger-buttons">
            <button class="submit">SUBMIT GUESS</button>
            <button class="reset">RESET GAME</button>
            <button class="clear">CLEAR GAME</button>
        </section>
</article>

【讨论】:

  • 我认为这两个.user-input divs 需要是50%,OP 希望它们并排。未堆叠。
  • 是的,还添加了删除空格以获得完美 50/50 元素的编辑。
  • 不错。不知道HTML 中的whitespace 技巧。 +1
【解决方案2】:

box-sizing:border-box 给父母和它的孩子。

【讨论】:

    【解决方案3】:

    试试

        .challenger2 {
          postion: relative;
          overflow: hidden;
        }
    
        .user-input {
          float: left;
          border: 1px solid red;
          height: 100px;
          width: 50%;
          box-sizing: border-box;
        }
    
        .user-input input {
          display: inline-block;
          width: 100%;
          box-sizing: border-box;
        }
    

    【讨论】:

      猜你喜欢
      • 2014-10-26
      • 2011-11-02
      • 2018-06-17
      • 2012-06-28
      • 1970-01-01
      • 2018-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多