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

Asked

Viewed 287 times

1

I’m a beginner in HTML and CSS and I’m doing a free theme work for my course, but I ended up getting stuck at one point.
I have the following code in an excerpt 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 Section, the height is automatic (height: auto;), as well as for the li. What I want is that the height of these containers is equal to the height of the image, since the h2 is above it. However, as the screen is resized, a white stripe is added to the container (I believe it is the height+padding of the h2).

I wonder how I do to ignore the height of the 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 the full screen:
Note que mesmo na resolução máxima há uma linha branca abaixo do h2

As the screen resizes and gets smaller:
Uma faixa branca é adicionada abaixo da imagem

1 answer

1


This is happening because you’re wearing position:relative; in your title h2.

this can be solved by moving the attribute position:relative; for your li and giving a position:absolute; to your h2 instead of relative in order to achieve the desired 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’s the code with all the bugs fixed and improved:

You can also see an online example here: http://jsfiddle.net/xv2h4utc/

<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%;
}

A class #first:after it’s not doing anything here, but if for some reason you need to use clear:both; creates a class especially for this property and uses it as follows:

<div class="clear"></div>
.clear {clear:both;}
  • You really solved the problem. As for the organization of the markings you have passed, it also solved another problem that arose after implementing the changes. Thank you very much!

  • No @Frost :) probably the other problem must have been because of this: <section id=#first> in your HTML code that I modified and fixed for <section id="first"> in my answer. There were other mistakes too, but this was the most relevant. This type of markup errors can cause the code to crash or some CSS style to not be implemented correctly. Happy Coding =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.