如何让没有内容的div有宽度?
我正在尝试向 div
添加宽度,但我似乎遇到了问题,因为它没有内容。
这是我迄今为止拥有的 CSS 和 HTML,但它不起作用:
CSS
body{
margin:0 auto;
width:1000px
}
ul{
width:800px;
}
ul li{
clear:both;
}
.test1{
width:200px;
float:left;
}
HTML
<body>
<div id="test">
<ul>
<li>
<div class="test1">width1</div>
<div class="test1">width2</div>
<div class="test1">width3</div>
</li>
<li>
<div class="test1"></div>
<div class="test1">width2</div>
<div class="test1">width3</div>
</li>
<li>
<div class="test1"></div>
<div class="test1">width2</div>
<div class="test1">width3</div>
</li>
</ul>
</div>
I am trying to add a width to a div
, but I seem to be running into a problem because it has no content.
Here is the CSS and HTML I have so far, but it is not working:
CSS
body{
margin:0 auto;
width:1000px
}
ul{
width:800px;
}
ul li{
clear:both;
}
.test1{
width:200px;
float:left;
}
HTML
<body>
<div id="test">
<ul>
<li>
<div class="test1">width1</div>
<div class="test1">width2</div>
<div class="test1">width3</div>
</li>
<li>
<div class="test1"></div>
<div class="test1">width2</div>
<div class="test1">width3</div>
</li>
<li>
<div class="test1"></div>
<div class="test1">width2</div>
<div class="test1">width3</div>
</li>
</ul>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
div 通常至少需要一个不间断空格 ( ) 才能具有宽度。
a div usually needs at least a non-breaking space ( ) in order to have a width.
使用
使宽度在空
padding
、height
或div
时生效编辑:
非零
min-height
也很好用Either use
for width to take effect with empty
padding
,height
ordiv
EDIT:
Non zero
min-height
also works great使用
min-height: 1px;
所有东西的 min-height 至少为 1px,这样 nbsp 或 padding 就不会占用额外的空间,或者被迫先知道高度。Use
min-height: 1px;
Everything has at least min-height of 1px so no extra space is taken up with nbsp or padding, or being forced to know the height first.使用 CSS 向您的 div 添加零宽度空间。 div 的内容不会占用空间,但会强制 div 显示
Use CSS to add a zero-width-space to your div. The content of the div will take no room but will force the div to display
它有宽度,但没有内容或高度。向类 test1 添加高度属性。
It has width but no content or height. Add a height attribute to the class test1.
有不同的方法可以使
float: left
或float: right
的空 DIV 可见。这里介绍了我所知道的:
width
(或min-width
)和height
(或min-height
) )padding-top
padding-bottom
border-top
border-bottom
::before
或::after
与:{内容:“\200B”;}
{内容:“。”;可见性:隐藏;}
text-decoration: underline;
结合使用)There are different methods to make the empty DIV with
float: left
orfloat: right
visible.Here presents the ones I know:
width
(ormin-width
) withheight
(ormin-height
)padding-top
padding-bottom
border-top
border-bottom
::before
or::after
with:{content: "\200B";}
{content: "."; visibility: hidden;}
text-decoration: underline;
)虽然来不及回答,但还是这样。
使用 CSS 时,要设置 div 样式(无内容),必须将 min-height 属性设置为“n”px 以使 div 可见(适用于 webkit 和 chrome,但不确定此技巧是否适用于 IE6 和下)
代码:
Too late to answer, but nevertheless.
While using CSS, to style the div (content-less), the min-height property must be set to "n"px to make the div visible (works with webkits and chrome, while not sure if this trick will work on IE6 and lower)
Code:
尝试将
display:block;
添加到您的 test1Try to add
display:block;
to your test1我有同样的问题。
我希望通过按下按钮来显示图标,但不需要任何移动和滑动环境,就像灯泡一样:打开和关闭,出现和消失,所以我需要制作一个具有固定大小的空 div。
对我有用
I had the same issue.
I wanted icons to appear by pressing the button but without any movement and sliding the enviroment, just like bulb: on and off, appeared and dissapeared, so I needed to make an empty div with fixed sizes.
did the trick for me
通过为元素定义最小宽度/高度,您可以在没有内容的情况下渲染它:
By defining min width/hright for your element you can render it without content:
也许是最优雅的:
display: inline-block;
(信用:@Luccas 建议在最佳答案下发表评论)
Perhaps the most elegant:
display: inline-block;
(credit: @Luccas suggestion in a comment under the top answer)
如果将
display:
设置为inline-block
、block
、flex
、. ..,在没有内容的元素上,然后要让
min-width
在没有内容的标签上生效,只需要为顶部或机器人应用填充。要使
min-height
在没有内容的标签上生效,您只需为左或应用内边距。这个示例展示了它;在这里,我只坐了 padding-left 以使
min-width
在空的 span 标签上生效If you set
display:
toinline-block
,block
,flex
, ..., on the element with no content, thenFor
min-width
to take effect on a tag with no content, you only need to apply padding for either top or bot.For
min-height
to take effect on a tag with no content, you only need to apply padding for left or right.This example showcases it; here I only sat the padding-left for the
min-width
to take effect on an empty span tag您可以使用
position:absolute
直接指定宽度和高度,而不需要任何内容。如果您正在考虑使用单个 div,那么可以轻松做到这一点。
You can use
position:absolute
to assign width and height directly, without any content.that can easily do it if you are considering a single div.
同样的问题,我的初始 css 是
width: 5px
,将其更新为min-width: 5px
使无内容的 div 出现!不需要额外的
height/min-height/padding/display
属性Same problem, my initial css is
width: 5px
, updating it tomin-width: 5px
made the contentless div appear!No need for extra
height/min-height/padding/display
properties