当外部div固定宽度并水平滚动时,如何使内部div宽度适合文本?

时间:2012-09-24 07:19:28

标签: html css text width

HTML

<div class="outer">
    <ul>
        <li>
            <div>text</div>
        </li>
        <li>
            <div>longlonglonglonglonglonglonglonglonglonglonglonglong</div>
        </li>
    </ul>
</div>

的CSS

.outer {
    width: 300px;
    height: 200px;
    background-color: #ddd;
    overflow: scroll;
}

li {
    margin: 5px;
}

li>div {
    cursor: pointer;
    height: 30px;
    line-height: 30px;
    border: 1px solid #369;
    background-color: #fff;
}

JSFiddle

需要:

内部div宽度适合文本。而文字只是单行。而内部div不使用固定宽度。

像这样(这个演示LI的边距不起作用。) JSFiddle

3 个答案:

答案 0 :(得分:2)

float:left;
margin:5px;

in li&gt; div style

DEMO

DEMO 2

答案 1 :(得分:1)

<强> HTML

<div class="outer">
    <ul>
        <li>
            <div>text</div>
        </li>
        <li>
            <div>longlonglonglonglonglonglonglonglonglonglonglonglong</div>
        </li>
    </ul>
</div>

<强> CSS

.outer {
    width: 300px;
    height: 200px;
    background-color: #ddd;
    overflow: scroll;
}

li {
    margin: 5px;
}

li>div {
    cursor: pointer;
    height: 30px;
    width:400px;
    line-height: 30px;
    border: 1px solid #369;
    background-color: #fff;

}

选中Fiddle

如果您不想破解文本,则可以使用overflow-x:scroll属性

答案 2 :(得分:0)

你现在习惯了

word-break: break-all;

.....

li>div {
    word-break: break-all; // add this line
    height:30px; // remove this line
}
height

删除 li>div

live demo

演示中的HTML

<div class="outer">
    <ul>
        <li>
            <div>text</div>
        </li>
        <li>
            <div>longlonglonglonglonglonglonglonglonglonglonglonglong</div>
        </li>
    </ul>
</div>

演示中的CSS

.outer {
    width: 300px;
    height: 200px;
    background-color: #ddd;
    overflow: scroll;
}

li {
    margin: 5px;
}

li>div {
    cursor: pointer;
    line-height: 30px;
    border: 1px solid #369;
    background-color: #fff;
     word-break: break-all;
}