Home > database >  How can i display correctly my image-gallery using h2
How can i display correctly my image-gallery using h2

Time:12-31

So im working on a gallery of image that can be scrolled vertically with centered and equally espace images, and when the mouse pass hover, the image scale up and a title show up. To make it work, i use an

h2

with a

background-image

But when i put it all together, the h2 just collapse next to each other.

This is the way i want it to look enter image description here

but this is what i get for now enter image description here

My code for now is this HTML :

<div id="gallery">
                            <h2 style="background-image: url(img/image-presentation.jpg);">
                                Concert</h2>
                            <h2 style="background-image: url(img/image-presentation.jpg);">
                                Concert</h2>
                            <h2 style="background-image: url(img/image-presentation.jpg);">
                                Concert</h2>
                            <h2 style="background-image: url(img/image-presentation.jpg);">
                                Concert</h2>
                            <h2 style="background-image: url(img/image-presentation.jpg);">
                                Concert</h2>
                            <h2 style="background-image: url(img/image-presentation.jpg);">
                                Concert</h2>
                        </div>

CSS:

#projet .content .window #gallery {
    text-align: center;
    height: 40vh;
    overflow-y: scroll;
    padding: 5vh;
    padding-top: 1vh;
    padding-bottom: 1vh;
    display: block;
}

#projet .content .window #gallery h2 {
    width: 25vh;
    height: 25vh;
    border-radius: 1vh;
    margin: 1.5vh;
    display: table-cell;
    vertical-align: middle;
    color: transparent;

    background-size: cover;
    background-repeat: no-repeat;

    transition: all 0.2s ease;
}

#projet .content .window #gallery h2:hover {    
    color: black;    
    backdrop-filter: blur(10px);
    transform: scale(1.2);
}

I dont know what to do or what i am doing wrong.

Can someone help me ?

I tried different display in css in the

#gallery

but i couldnt find a correct way to make it work

CodePudding user response:

You can use separate div for every image instead of using image as a background-imgage for h2 tag,like this:

<div id="gallery">
    <div >
        <img src="img/image-presentation.jpg" alt="image">
        <h2>Concert<h2>
    </div>
    <div >
        <img src="img/image-presentation.jpg" alt="image">
        <h2>Concert<h2>
    </div>
    <div >
        <img src="img/image-presentation.jpg" alt="image">
        <h2>Concert<h2>
    </div>
    <div >
        <img src="img/image-presentation.jpg" alt="image">
        <h2>Concert<h2>
    </div>
    <div >
        <img src="img/image-presentation.jpg" alt="image">
        <h2>Concert<h2>
    </div>
</div>

For CSS, you can use flex, like this:

#gallery{
    display:flex;
    width: 80%;
    flex-wrap: wrap;
 }
.image{width: 33%}

CodePudding user response:

Using the "background-image" of an <h2> tag is not the right way to do what you want. You should create a <div> where inside you put an <img> for the image and an <h2> for the text. Or you can use pseudo-elements on the image instead of the <h2> and the <div>. To put them however you want, you can use either display: grid; or display: flex;.

I tried to write on the spot as it should be one of the possibilities.

HTML

<div id="gallery">
    <div >
        <img src="img/image-presentation.jpg" alt="image">
        <h2>My Title<h2>
    </div>
    <div >
        <img src="img/image-presentation.jpg" alt="image">
        <h2>My Title<h2>
    </div>
    <div >
        <img src="img/image-presentation.jpg" alt="image">
        <h2>My Title<h2>
    </div>
    <div >
        <img src="img/image-presentation.jpg" alt="image">
        <h2>My Title<h2>
    </div>
    <div >
        <img src="img/image-presentation.jpg" alt="image">
        <h2>My Title<h2>
    </div>
</div>

CSS

#projet .content .window #gallery {
    display: grid;
    column-gap: 50px;
    row-gap: 50px;
    grid-template-columns: auto auto auto;
    padding: 10px;
}

.image-container {
    position: relative;
    width: 300px;
    height: 500px;
}

.image-container img {
    width: 100%;
    height: 100%;
    transition: all 1s;
    z-index: 10;
}

.image-container img:hover {
    transform: translateY(-30px);
}

.image-container h2 {
    position: absolute;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    opacity: 0;
}

.image-container img:hover h2 {
    opacity: 1;
}
  • Related