首页 文章

缩小适合具有最大宽度的div

提问于
浏览
1

我需要缩小父类的拟合以适合本身具有最大宽度设置的子div .

问题是,一旦孩子的内容溢出到多行,它的宽度被设置为它的最大宽度,即使它不需要那么宽 . 在这里,我希望灰色外框完全符合内容 .

enter image description here

这是代码:

<div class="tags">
    <ul>
        <li>One
        <li>Two
        <li>Three
        <li>Four
    </ul>
</div>

.tags {
    max-width: 190px;
    background: #AAA;
}

.tags ul {
    list-style-type: none;
    padding: 0;
    text-align: right;
}

.tags li {
    display: inline-block;
    margin-bottom: 5px;
    margin-right: 5px;
    padding: .2em .6em .3em;
    font-size: 20px;
    font-weight: bold;
    line-height: 1;
    color: #ffffff;
    text-align: center;
    border-radius: .25em;
    background-color: #579cc1;
}

我有一个小提琴:

http://jsfiddle.net/yw75kg15/

有人有解决方案吗?

1 回答

  • 0

    看起来您需要做的就是在li块中指定一个带有自动边距的宽度和右边的百分比边距 . 宽度应该解决问题 . 试着把它放在你的小提琴里 .

    .tags li {
        display: inline-block;
        width:30%;
        margin-left: auto;
        margin-right: 5%;
        margin-bottom: 5px;
        padding: .2em .6em .3em;
        font-size: 20px;
        font-weight: bold;
        line-height: 1;
        color: #ffffff;
        text-align: center;
        border-radius: .25em;
        background-color: #579cc1;
    }
    

相关问题