开发者

Make lis take the width of their contents, center them, but keep them on separate lines

开发者 https://www.devze.com 2023-04-06 02:16 出处:网络
Is there anyway to make the width of a<li> element relative to the text within it, while still having each <开发者_开发技巧;li> centred and one to a line. I want the styling to work just li

Is there anyway to make the width of a<li> element relative to the text within it, while still having each <开发者_开发技巧;li> centred and one to a line. I want the styling to work just like the image (in the link below)

Make lis take the width of their contents, center them, but keep them on separate lines

*please note the Items are centred within a surrounding box.

*also note the width of the button is relative to the size of the text.


You can do it fairly easily if you add a span to the lis.

<ul>
    <li><span>Longer Item</span></li>
    <li><span>Item 1000000000000</span></li>
    <li><span>Short</span></li>
</ul>

CSS

ul{
    text-align:center;
    width:400px;
    border:1px solid black;
}

li{
    display:block;
}

li span{
    padding:1em;
    background:red;
    color:white;
    margin:1em auto;
    display:inline-block;
}

Example: http://jsfiddle.net/jasongennaro/tDBbA/2/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号