How to ignore the height of an element inside a box with automatic height in CSS?

1

I'm a beginner in HTML and CSS and I'm doing a freelance work for my course, but I ended up packing at one point. I have the following code in a snippet of my HTML:

<section id=#first>
    <h1> Recomendações da semana </h1>
    <ul>
    <a href="http://myanimelist.net/anime/10793/Guilty_Crown" target="_blank">
        <li><img src="img/gc.png"/><h2>Guilty Crown</h2></li>
    </a>
    <a href="http://myanimelist.net/anime/11757/Sword_Art_Online" target="_blank">
        <li><img src="img/sao.png"/><h2>Item 1</h2></li>
    </a>
    <a href="http://myanimelist.net/anime/12189/Hyouka" target="_blank">
        <li><img src="img/hyouka.png"/><h2>Hyouka</h2></li>
    </a>
    <a href="http://myanimelist.net/anime/11887/Kokoro_Connect" target="_blank">
        <li><img src="img/kkr_con.png"/><h2>Kokoro Connect</h2></li>
    </a>
    </ul>
</section>

For the section, the height is automatic ( height: auto; ), as well as li . What I want is that the height of these containers is equal to the height of the image, since h2 is above it. However, as the screen is resized, a white stripe is being added to the container (I believe it is the height + padding of h2 ).

I would like to know how to ignore the height of h2 , leaving only the height of the image in the container.

Follow the code used in CSS:

#first{
    width: 90%;
    max-width: 1440px;
    height: auto;
    margin: 0 auto;
    margin-bottom: 25px;
    background-color: white;
    overflow: hidden;
}
#first:after{
    clear: both;
    content: "";
    display: block;
}
section h2{
    width: 100%;
    height: 35px;
    margin: 0;
    background-color: rgba(0, 0, 0, 0.6);
    color: white;
    font-family: calibri;
    font-size: 18px;
    font-weight: 100;
    text-align: center;
    padding-top: 12px;
    position: relative;
    bottom: 47px;
}
section ul{
    margin: 0;
    height: auto;
    float: left;
    width: 100%;
    padding: 0;
    list-style: none;
}
section ul a li{
    float: left;
    width: 25%;
    height: auto;
    max-height: 306px;
    margin: 0;
    overflow: hidden;
    cursor:pointer;
}
section ul a li img{
    width: 100%;
    height: auto;
}  

The final result with full screen:

Asthescreenisresizedandgetssmaller:

    
asked by anonymous 25.05.2015 / 01:41

1 answer

1

This is happening because you are using position:relative; in your title h2 .

This can be solved by moving the position:relative; attribute to your li and giving position:absolute; to your h2 instead of relative to achieve the pretentious style:

li {position:relative;}
h2 {position:absolute; bottom:0;}

However, I noticed several markup errors in your HTML code, and the structure of your CSS can also be improved, here is the code with all errors corrected and improved:

You can also see an example online here: link

<section id="first">
    <h1> Recomendações da semana </h1>
        <ul>
            <li class="reco-li">
                <a href="http://myanimelist.net/anime/10793/Guilty_Crown" target="_blank">
                <img src="img/gc.png"/><h2>Guilty Crown</h2>
                </a>
            </li>
            <li class="reco-li">
                <a href="http://myanimelist.net/anime/11757/Sword_Art_Online" target="_blank">
                <img src="img/sao.png"/><h2>Item 1</h2>
                </a>
            </li>
            <li class="reco-li">
                <a href="http://myanimelist.net/anime/12189/Hyouka" target="_blank">
                <img src="img/hyouka.png"/><h2>Hyouka</h2>
                </a>
            </li>
            <li class="reco-li">
                <a href="http://myanimelist.net/anime/11887/Kokoro_Connect" target="_blank">
                <img src="img/kkr_con.png"/><h2>Kokoro Connect</h2>
                </a>
            </li>
        </ul>
</section>
.reco-li {
    position: relative;
    float: left;
    width: 25%;
    max-height: 306px;
    margin: 0;
    cursor: pointer;
}
#first{
    width: 90%;
    max-width: 1440px;
    margin: 0 auto;
    margin-bottom: 25px;
    background-color: white;
    overflow: hidden;
}
.reco-li h2{
    width: 100%;
    margin: 0;
    background-color: rgba(0, 0, 0, 0.6);
    color: white;
    font-family: calibri;
    font-size: 18px;
    font-weight: 100;
    text-align: center;
    padding-top: 12px;
    position: absolute;
    bottom: 0;
}
#first ul{
    margin: 0;
    width: 100%;
    padding: 0;
    list-style: none;
    display: table-cell; /* isto é uma correcção para poder usar a border abaixo correctamente */
    border: 5px solid #000;
}

.reco-li img{
    width: 100%;
}

The class #first:after is not doing anything here, but if for some reason you need to use clear:both; create a class specially for this property and use it as follows:

<div class="clear"></div>
.clear {clear:both;}
    
25.05.2015 / 09:18