You can simply use the CSS display property with the value inline-block to make a <div> not larger than its contents (i.e. only expand to as wide as its contents).
Using inline-block property: Use display: inline-block property to set a div size according to its content.
When you set a particular height and width for a div, it does not matter if it is empty or overflowed, it will only take up the space as given in the code.
a div usually needs at least a non-breaking space ( ) in order to have a width.
Either use padding
, height
or  
for width to take effect with empty div
EDIT:
Non zero min-height
also works great
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.
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::before{
content: "\200B";
}
It has width but no content or height. Add a height attribute to the class test1.
There are different methods to make the empty DIV with float: left
or float: right
visible.
Here presents the ones I know:
width
(or min-width
) with height
(or min-height
)padding-top
padding-bottom
border-top
border-bottom
::before
or ::after
with:
{content: "\200B";}
{content: "."; visibility: hidden;}
inside DIV (this sometimes can bring unexpected effects eg. in combination with text-decoration: underline;
)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:
.shape-round{
width: 40px;
min-height: 40px;
background: #FF0000;
border-radius: 50%;
}
<div class="shape-round"></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With