CSS - UL父级小于子级

时间:2014-04-27 08:28:56

标签: html css

嘿伙计们我使用简单的ul li来设计3个简短的艺术品,事情看起来不错但问题是我注意到UL父母不是全尺寸而且li比它大(不好的是:P)

有人可以帮我解决吗?并使其与孩子的宽度和高度相同?

它看起来像: enter image description here

HTML:

<div id="article-container">

<ul>
      <li><a href="#"><img src="images/foto.jpg" width="109" height="109" alt="Popis"></a>
            <h2><a href="#">Název článku</a></h2>
            <p><span><strong>28. 2. 2014 8:29</strong> od Dwandy</span><br>
              Ahoj, jak se máš? Já úplně úžasně a úpe nejvíc. Závidíš, he? </p>
            <p class="more"><a href="#" class="links">Přečíst</a><br>
              <a href="#">4 reakce</a></p>
            <hr>
       </li>

  <li><a href="#"><img src="images/foto.jpg" alt="Popis"></a>
           <h2><a href="#">Název článku</a></h2>
           <p><span><strong>28. 2. 2014 8:29</strong> od Dwandy</span><br>
             Ahoj, jak se máš? Já úplně úžasně a úpe nejvíc. Závidíš, he? </p>
           <p class="more"><a href="#" class="links">Přečíst</a><br>
             <a href="#">4 reakce</a></p>
           <hr>
      </li>

      <li><a href="#"><img src="images/foto.jpg" alt="Popis"></a>
           <h2><a href="#">Název článku</a></h2>
           <p><span><strong>28. 2. 2014 8:29</strong> od Dwandy</span><br>
             Ahoj, jak se máš? Já úplně úžasně a úpe nejvíc. Závidíš, he? </p>
           <p class="more"><a href="#" class="links">Přečíst</a><br>
             <a href="#">4 reakce</a></p>
           <hr>
      </li>
 </ul>

CSS:

#article-container{
  width: 980px;
    margin: 0 auto;
    color:#4d4d4d;
    font-size:90%;
  height: 144px;
}

#article-container br, hr {
    display:none;
}
#article-container ul {
    width:960px;
  height: 144px
    overflow:hidden;
    margin:0;
    padding:15px 0;
}
#article-container li {
    width:310px;
    float:left;
    list-style:none;
    padding:0;
    margin:0px 0px 0px 8px;
}
#article-container img {
    float:left;
    margin: 0 10px 0 0
}
#article-container a img {
    border:2px solid #999e8a
}
#article-container a:hover img {
    border-color:#000
}
#article-container h2 {
    margin: 0;
    padding: 0 0 10px 0;
    font-size:110%;
    font-weight:normal;
}
#article-container h2 a {
    color:#4d4d4d
}
#article-container h2 a:hover {
    color:#840000
}
#article-container p {
    padding: 0 10px 0 0;
    margin:0
}
#article-container p span {
    display:block;
    padding: 0 0 5px 0
}
#article-container p.more {
    padding: 0px 0 0 0;
    overflow:hidden
}


#article-container p.more a {
    color:#2e79bc;
    line-height: 27px;
}
#article-container p.more a:hover {
    color:#840000
}
#article-container p.more a.links {
    float:right;
    padding: 5px 10px;
    background:#2e79bc;
    color:#fff;
    margin-right:15px;
    text-decoration:none;
    border-radius:3px;
    line-height: 18px;
}
#article-container p.more a.links:hover {
    background:#000
}

P.S。感谢您阅读这篇文章

4 个答案:

答案 0 :(得分:4)

不要浮动列表元素,但要使它们display: inline-block;,高度将固定。

答案 1 :(得分:2)

看起来li标签的结果很可能是float: left

尝试改为制作display: inline-block,这可能会使父容器以float的方式崩溃。

答案 2 :(得分:0)

您可以使用名为clearfix的css中的常见修复来解决此问题。 创建一个类似的类并将其放在包含浮动元素的父级上。

/**
 * For modern browsers
 * 1. The space content is one way to avoid an Opera bug when the
 *    contenteditable attribute is included anywhere else in the document.
 *    Otherwise it causes space to appear at the top and bottom of elements
 *    that are clearfixed.
 * 2. The use of `table` rather than `block` is only necessary if using
 *    `:before` to contain the top-margins of child elements.
 */
.cf:before,
.cf:after {
    content: " "; /* 1 */
    display: table; /* 2 */
}

.cf:after {
    clear: both;
}

/**
 * For IE 6/7 only
 * Include this rule to trigger hasLayout and contain floats.
 */
.cf {
    *zoom: 1;
}

Reference

答案 3 :(得分:0)

inline-block可行

但如果您想将它们保留为float:left,请在列表项<li style="clear:both"></li>

之后添加此列表元素

或者准确使用clearfix上的<ul>类并在css中添加它(如果你使用它,则无需添加上面显示的列表元素)。

.clearfix:after {
    visibility: hidden;
    display: block;
    content: "";
    clear: both;
    height: 0;
}
* html .clearfix { zoom: 1; } /* IE6 */
*:first-child+html .clearfix { zoom: 1; } /* IE7 */