代码之家  ›  专栏  ›  技术社区  ›  kevin

如何让div占据父级的全部宽度?

  •  0
  • kevin  · 技术社区  · 7 年前

    我一直试图让div(Name&Guess)占据父容器的整个宽度。不管我怎么试,它们都只能达到容器的98%。如果我把每一个都设置为50%,它们就会塌陷,然后一个容器在另一个容器上。他们目前设定为49%。有什么想法吗?

    enter image description here

    .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>
    3 回复  |  直到 7 年前
        1
  •  3
  •   Alejalapeno    7 年前

    包含在大多数CSS中的一个有用的“重置”是 * { box-sizing: border-box; } 因为 box model 是专门设计的。使用普通长方体模型,填充和边框将添加到元素的宽度中。所以“100%宽”元素实际上是100%+边框+填充。 border-box

    另外:如果要创建两个“正好50%宽”的元素,请使用 inline-block 然后需要使用负边距或技巧来删除标记中元素之间的空白。这是HTML中的一个小麻烦,您可以在下面的代码片段中看到,我通过 </div 最后一个结束括号在下一行 ><div class="user-input"> 一小部分空间被放置在 内联块

    * {
        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>
        2
  •  0
  •   Hamza Arab    7 年前

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

        3
  •  0
  •   DAbuzied    7 年前

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