Skip to content

Instantly share code, notes, and snippets.

@asbarron
Created July 11, 2012 20:45
Show Gist options
  • Save asbarron/3093263 to your computer and use it in GitHub Desktop.
Save asbarron/3093263 to your computer and use it in GitHub Desktop.
Floating divs to auto shrink to text size
a:link { color:white }
img {
height:600px;
width:300px;
position: relative;
}
ul {
list-style-type: none;
padding:0;
}
#container {
top:30px;
padding:5px 0px 5px 0px;
position: absolute;
width:300px;
}
.item {
float:left;
background: rgba(0, 0, 0, 0.5);
height:25px;
margin: 5px;
padding:10px;
clear:both;
}
<img src="http://subtlepatterns.com/patterns/tileable_wood_texture.png" >
<div id="container">
<ul>
<li><a href="#"><div class="item">My</div><a></li>
<li><a href="#"><div class="item">supar long link hax</div><a></li>
<li><a href="#"><div class="item">brings all the</div><a></li>
<li><a href="#"><div class="item">divs inside the container.</div><a></li>
</ul>
</div>
http://jsfiddle.net/mztriz/xbvmf/
@asbarron
Copy link
Author

A code snippit to show that divs can be auto resized by text length.
Works by floating the div to the left and then clearing it after.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment