如何扩展ul和li的div容器?

时间:2014-07-17 14:35:46

标签: html css html-lists

我有一个div的内容(里面的列表),

<div id="container">
     <ul id="list">
       <li>
         <div class="title">
              something
         </div>
         <div class="text">
             something again
         </div>
       </li>
        <li>
         <div class="title">
              something
         </div>
         <div class="text">
             something again
         </div>
       </li>
    </ul>
</div>

我的列表中的项目显示为float:left,我希望div容器的高度扩展为其内容。

#container{ 
    position: relative; 
    width: 100%;    
    padding-bottom: 30px;       
}

#list{
    position: relative;
    padding-top: 80px;
    width: 95%;
    /*height: 100%;*/
    padding-bottom: 10px;
}

#list li{
    position: relative;
    float: left;    
    list-style: none outside none;
    width: 20%;
    height: 170px;  
    /*border:1px solid black;*/
}

2 个答案:

答案 0 :(得分:4)

您有几种选择:

    容器上的
  1. Clearfix

    #container:after {
        clear: both;
        content: "";
        display: table;
    }
    
  2. overflow的{​​{3}}:

    #container {
        overflow: hidden; /* or: auto | scroll */
    }
    
  3. 浮动容器:

    #container {
        float: left;
    }
    
  4. 使用inline-block代替float作为列表项:

    #container ul li {
        display: inline-block;
        float: none;
    }
    

答案 1 :(得分:1)

尝试这种方法......

li {
    display: inline-block;
    float: none; /* only if you have this set as you mentioned in your post */
}

#container {
  height: auto;
}