简体   繁体   English

如何使DIV仅占用其包含的元素的宽度?

[英]How do I make my DIV only take up the width of the elements it contains?

I have a little form that is inside a larger div: 我在较大的div内有一个小表格:

<div id="confirmEmailContainer">
  <form class="edit_user" id="edit_user_2" action="/users/2" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="&#x2713;" /><input type="hidden" name="_method" value="patch" /><input type="hidden" name="authenticity_token" value="hWjOQEpKyMKVrk9+vWEeenl3D49rVe4hXFKgF4lza09xXRo0HENKMqGRpq9xUflWqbGJUdoxrmQrsksQWV5s7g==" />

    <div class="field">
      <label for="user_email">Email</label> <span class="required">*</span> <br>
      <div>
        <input size="30" style="display: inline;" type="text" value="myemail@msn.com" name="user[email]" id="user_email" /> 
        <span id="emailConfirmedStatus">
          <div class="unconfirmed" style="display: block"><img style="vertical-align:middle" src="/assets/x-mark-621493006f6052df98aa84352a33f21bccabfc670129ca9572009545745040d9.png" alt="X mark" width="16" height="16" /> Unconfirmed</div>
          <div class="confirmed" style="display: none"><img style="vertical-align:middle" src="/assets/checkmark-b87ae8ffea696b9c9587f26fdb656af7a21da4f7f73da01fc4bef9b35a1b9465.svg" alt="Checkmark" width="16" height="16" /> Confirmed</div>
        </span>
      </div>
    </div>

    <div class="actions buttonContainer">
      <input type="submit" name="commit" value="Send Confirmation Email" id="submit" class="button btn" data-disable-with="Send Confirmation Email" />
    </div>
  </form>
</div>

I do not specify CSS width properties anywhere, including the form or its container: 我没有在任何地方指定CSS width属性,包括表单或其容器:

#confirmEmailContainer {
    background-color: #fff;
    text-align: left;
}

form {
    margin: 4% 15%;
    font-family: Manuelle;
    font-size: 14px;
}

but yet the container is almost taking up the entire width of the content area -- https://jsfiddle.net/70pgd6jn/ . 但是容器几乎占据了内容区域的整个宽度-https://jsfiddle.net/70pgd6jn/ How do I make the form only occupy the width of its elements ? 如何使表格仅占据其元素的宽度?

您应该将float: left#confirmEmailContaineroverflow: hidden#content

You might be looking for the display: inline-block property, which will permit your container element to only occupy the space it requires. 您可能正在寻找display: inline-block属性,该属性将允许您的容器元素仅占用其所需的空间。

#confirmEmailContainer {
    background-color: #fff;
    text-align: left;
    display: inline-block;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM