输入 Div Span 元素如何响应放置在其上方的浮动元素?
我们采用了 300px
宽的 div
,因为我们有一个 100px
宽的 label
标签并拥有它 float:left
之后我们立即采取了三种方案。我们放置了:
宽度:400px的
输入
一个
div
与width:400px
一个带有小文本和大文本的
span
text
您可以在此处查看它们的渲染方式:http://realution.in/htmlcss/practise/repost.html
任何人都可以解释为什么它们的渲染如此不同吗?
也是一个 block 或 inline 元素吗?
We took a 300px
wide div
, in that we had a 100px
wide label
tag and had it float:left
and immediately after that we took three scenarios. We placed:
an
input
withwidth:400px
a
div
withwidth:400px
a
span
with small text and large text
You can see how they are rendering here: http://realution.in/htmlcss/practise/repost.html
Can anyone explain why they are rendering so differently?
Also is <input>
a block or inline element?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它们的渲染方式如此不同,因为您设置了不同的 css 属性并在每个示例中使用不同的元素。另外,将
float
添加到标签但不添加到任何其他元素,并且不为任何其他元素提供 cssclear
属性会导致此渲染问题。对浮动的正确解释是在 A List Apart
内联:
内联元素仅占用所需的宽度,并且不会强制换行。
块:
块元素是占用整个可用宽度的元素,并且前后都有换行符它。
来自 w3schools
label
元素是一个内联元素。添加 float: left 会将其从文档流中删除并使其成为块元素。显式的display:block
是不必要的。span
元素上使用的大多数属性都是无用的,除非将其设置为display: block
、float: left
或display: inline -块
。现在设置的高度、宽度和边距将被忽略。如果你想调整span的大小只能使用:line-height
和padding
label
和input
都是内联元素。input
占用一个换行符,因为它永远无法与任何内容相邻。输入的大小大于其父级。display: block
对于标签来说并没有多大作用。因为它有浮动,而其他元素没有。在一个好的浏览器中,您不应该看到带有或不带有display:block
的标签之间有任何差异,除了最后一个示例,其中标签实际上位于左侧,而不是相对于前面的浮动>标签
float: left
标签位于正常文档流之外。 div 元素位于其之上,因为 div 位于正常文档流内。添加clear: left到div将恢复正常的文档流,将div放在一个新行上
width: 300px;
对内联元素实际上没有任何作用。如果你希望它有一个宽度,你应该浮动
它,给它一个display:block
或display:inline-block
overflow:hidden
添加到父 div 或在最后一个标签后添加clear:both
的空 div 都会修复该框以使其内部的所有元素周围。line-height
属性确定的两个元素They render so differently because you set different css properties and use different elements in every example. Also adding the
float
to label but not to any other elements, and not giving any other elements the cssclear
property creates this rendering issue.A proper explenation of floats is done on A List Apart
Inline:
An inline element only takes up as much width as necessary, and does not force line breaks.
Block:
A block element is an element that takes up the full width available, and has a line break before and after it.
from w3schools
The
label
element is a inline element. Adding the float: left removes it from the document flow and makes it a block element. The explicitdisplay:block
isnt necessary.Most properties you use on a
span
element are useless unless you set it todisplay: block
,float: left
ordisplay: inline-block
. Right now the set height, width and margin are ignored. If you want to adjust the size of the span you can only use:line-height
andpadding
label
andinput
are inline elements. Theinput
takes up a newline because it will never be able to fit next to anything. The size of the input is larger then its parent.display: block
doesnt really do much for a label in this case. Because it has a float and no other element does. In a good browser, you should not see any difference between the label with or withoutdisplay:block
except for the last example where the label will actually be on the left against instead of floating against the previouslabel
float: left
the label is outside of the normal document flow. The div element is ontop of it, because the div is inside the normal document flow.Adding a clear: left to the div would restore the normal document flow, putting the div on a new line
width: 300px;
on the span doesnt really do anything on inline elements. If you want it to have a width you should eitherfloat
it, give it adisplay: block
ordisplay: inline-block
overflow:hidden
to the parent div or a empty div withclear:both
after the last label will both fix the box to go around all elements inside it.line-height
property