[英]HTML/CSS - How do I make divs to size according to their contents?
I have 2 divs side by side. 我有2个div并排。
In the first div, I have 2 labels side by side, and one input text below them. 在第一个div中,我并排有2个标签,并且在它们下方有一个输入文本。 One of the labels is an error information.
标签之一是错误信息。 Sometimes it will be displayed, sometimes not.
有时会显示,有时则不会。 When it's not display, I'd like the div to resize to be smaller, so the second div can be closer to it.
当它不显示时,我希望将div的大小调整为较小,以便第二个div可以更接近它。
The second thing is the same thing, except it has one label div, therefore it doesn't requires a resize. 第二件事是同一件事,除了它有一个标签div,因此不需要调整大小。
Is there a way to achieve what I want? 有没有办法实现我想要的? There is an awesomely drawn example of what I want to acahieve below:
下面是我想知道的一个很棒的示例:
This is the code. 这是代码。
<div id="main-div">
<div id="address-number-div">
<label>Number</label>
<label class="error" id="number-error">Empty Field</label>
<input id="number-input" onfocus="onfocus('number-error')"/>
</div>
<div id="address-complement-div">
<label>Complement</label>
<input id="complement-input" />
</div>
and CSS: 和CSS:
div {border: 1px solid #000000; padding: 5px;}
.error{color:#FF0000; margin-left:5px;}
#main-div div {display:inline-block;}
#main-div input {display:block;}
#number-input {
width: 16%;
}
Set them to display: inline-block;
将它们设置为
display: inline-block;
to let them shrink to fit the the content. 让它们缩小以适合内容。
Don't give width
<persentage>
type value, give <length>
: 不要给出
width
<persentage>
类型的值,而给出<length>
:
#number-input { width: 24px; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.