简体   繁体   English

使背景伸展到内容的最大高度

[英]Getting background to stretch to full height of contents

My codepen http://codepen.io/leongaban/pen/sBvfL 我的Codepen http://codepen.io/leongaban/pen/sBvfL

So having a strange issue, I'm trying to get the background of the #portfolio div to stretch to contain the thumbnails below which are in an ul list inside of the portfolio div. 因此,有一个奇怪的问题,我试图获取#portfolio div的背景以拉伸以包含下面的缩略图,这些缩略图在投资组合div的ul列表中。

However 100% or auto doesn't affect the height. 但是100%或自动不会影响高度。 I have to set a static height, like 1000px. 我必须设置一个静态高度,例如1000px。 To get the background to cover the thumbs. 使背景遮住拇指。 However I'm trying to not set a static height since the thumbnails will get longer. 但是,我尝试不设置静态高度,因为缩略图会变长。

Perhaps I've been coding this too long, how would you code this? 也许我已经编码太久了,您将如何编码?


HTML HTML

<div id="portfolio">

  <div class="portfolio-nav">
       <h1>Portfolio</h1>
  </div>

        <div id="showcase-holder">

            <div id="showcase-div">

            <ul id="portfolio-thumbs">

                <li>
                    <a href="/portfolio/chipestimate">
                        <img class="role-thumb" src="http://leongaban.com/images/thumb_chipestimate.jpg" alt="ChipEstimate"/>
                        </a><p>ChipEstimate</p>

                </li>

                <li>
                    <a href="/portfolio/shabang" title="Shabang">
                        <img class="role-thumb" src="http://leongaban.com/images/thumb_shabang.jpg" alt="Shabang"/>
                        </a><p>Shabang</p>

                </li>
        </ul>
    </div>
 </div>

CSS CSS

body {
    background: brown;
}

#portfolio {
    position: relative;
    width: 100%;
    height: 50%;
    background: #fff;
    border-top: 2px solid #ccc;
    z-index: 1;
}

#portfolio ul { list-style: none; }

.portfolio-nav { margin: 0 0 20px 0; }

.portfolio-nav h1 {
    padding: 30px 0 10px 0;
    text-align: center;
    font-size: 2.5em;
    font-weight: 700;
    color: #d74927;
    text-shadow: 1px 1px #ccc;
}

#showcase-holder {
    position: relative;
    width: 80%;
    height: 100%;
    margin: 0 auto;
    border-bottom: 2px solid #ccc;
}

#portfolio-thumbs {
    position: relative;
    float: left;
    list-style-type: none;
    margin: 0 0 40px 0;
    padding: 0 0 0 5%;
    width: 100%;
    height: 100%;
}

#portfolio-thumbs li {
    position: relative;
    float: left;
    width: 20%;
    margin: 1%;
    text-align: center;
    padding: 5px 5px 15px 5px;
    background-size: 100% auto;
    overflow: hidden;
    background: white;
    -webkit-transition: background .3s;
    -moz-transition: background .3s;
    -ms-transition: background .3s;
    transition: background .3s;
}

#portfolio-thumbs li:hover {
    color: #fff;
    background: #d74927;
    -webkit-transition: background .5s;
    -moz-transition: background .5s;
    -ms-transition: background .5s;
    transition: background .5s;
}

#portfolio-thumbs li a {
    color: #333;
    text-decoration: none;
}

#portfolio-thumbs li p {
    padding: 10px 0 0 0;
}

#portfolio-thumbs li img.role-thumb {
    width: 95%;
    min-width: 170px;
    padding-top: 5px;
}

Parents will normally expand to the height of their children, though won't in case the children are relative. 父母通常会扩大到孩子的高度,但是如果孩子是亲戚则不会。

  • You can remove positions and floats to accomplish expanding. 您可以删除头寸和浮动来完成扩展。
  • In order to expand a parentdiv based on positioned children try overflow: auto; 为了扩展基于已定位子项的parentdiv,请尝试溢出:auto; on #portfolio. 在#作品集上。 This will make #portfolio expand to the height of its children. 这将使#portfolio扩展到其子代的高度。 As seen on this fork of your example. 如您的示例的分支所示。

overflow: auto; will actually let your browser decide, which normally renders this to overflow: hidden; 实际上将让您的浏览器做出决定,通常这会导致overflow: hidden; . Though I tend to use overflow: auto; 虽然我倾向于使用overflow: auto; to prevent issues with scrollbars as the page possibly expands later on. 以防止滚动条出现问题,因为以后页面可能会扩展。

You have added float to your li elements, which means that the parent will not expand to contain these elements. 您已经在li元素中添加了float ,这意味着父元素将不会扩展为包含这些元素。

You can work around this by adding a clearing div . 您可以通过添加清除div来解决此问题。

<div style="clear:both;"></div>

after the showcase-holder div. showcase-holder之后

Adding overflow: hidden to both #showcase-div and #portfolio-thumbs should do it for you. 添加overflow: hidden #showcase-div#portfolio-thumbs都应该为您做到这一点。

http://jsfiddle.net/5SFFP/ http://jsfiddle.net/5SFFP/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM