简体   繁体   中英

Text appear on hover image does not fit the image

I've been working on a website and I'm trying to show some text when my mouse is hovering an image. This is a pretty common thing on website today I believe. However for some reasons when I hover my image the text goes "out of bonds" of the image. (I will attach a screenshot you'll understand) 我的文字超出图像边界的图像 As you can see not only it goes below the image at the bottom, but also it goes through on the right of the image. I am not sure how to fix this? Is this because of the font size? I don't think so

div className='image_section'>
            <h1 className='section_title-top'>SERVICES </h1>
            <div className='img-container'>
                <div class="image">
                    <img class="image__img" src={bdh1Img} alt="Bricks" />
                    <div class="image__overlay image__overlay--primary">
                        <div class="image__title">This is my title and I want three same image</div>
                        <p class="image__description">
                            this is some random text
                            Lorem ipsum dolor sit amet, consectetur 
                            adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, 
                            adipiscing nec, ultricies sed, dolor. Cras elementum ultrices diam. Maecenas 
                            ligula massa, varius a, semper congue, euismod non, mi. Proin porttitor, 
                            orci nec nonummy molestie, enim est eleifend mi, non fermentum diam nisl 
                            sit amet erat. Duis semper. Duis arcu massa, scelerisque vitae, consequat in, pretium a, enim. Pellentesque congue. Ut in risus volutpat libero pharetra tempor. Cras vestibulum bibendum augue. Praesent egestas leo in pede. Praesent blandit odio eu enim. Pellentesque sed dui ut augue blandit sodales. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aliquam nibh. Mauris ac mauris sed pede pellentesque fermentum. Maecenas adipiscing ante non diam sodales hendrerit.
                            Ut velit mauris, egestas sed, 
                            gravida nec, ornare ut, mi. Aenean 
                            ut orci vel massa suscipit pulvinar. Nulla sollicitudin. Fusce varius, ligula non tempus aliquam, 
                            nunc turpis ullamcorper nibh, in tempus sapien eros vitae ligula. Pellentesque
                        </p>
                    </div>
                </div>
            </div>
        </div>
.image_section {
    width: 100%;
    background: #eee;
    display: flex;
    flex-direction: column;
    padding: 2rem 0;
}
.section_title-top{
    text-align: center;
    color: #000;
    padding: 2.5rem 0;
}

.img-container {
    max-width: 1140px;
    margin: auto;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
}
.image {
    position: relative;
}

.image p, h2{
    font-size: 14px;
}

.image__img {
    display: block;
    max-width: 100%;
    height: auto;
}

.image__overlay {
    position: absolute;
    top: 0;
    left: 0;
    background: rgba(0, 0, 0, 0.75);
    color: #ffffff;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.25s;
}

.image__overlay > * {
    transform: translateY(20px);
    transition: transform 0.25s;
}

.image__overlay:hover {
    opacity: 1;
}

.image__overlay:hover > * {
    transform: translateY(0);
}


.image__title {
    font-size: 1.5em;
    text-align: center;
    font-weight: bold;
}

.image__description {
    text-align: center;
    font-size: 1.15em;
    margin-top: 0.35em;
}

@media screen and (max-width: 980px) {
    .img-container {
        max-width: 90%;
        margin: auto;
        grid-template-columns: 1fr;
    }
}

Thanks for any help

I think this is too much text to be on an image you can use ellipsis property like this

for example:

 .image__description { text-align: center; font-size: 1.15em; margin-top: 0.35em; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 3; /* this set the number of rows to show*/ -webkit-box-orient: vertical; }

then if want the user to display the full text u can make a modal to show the full text or something

I saw that in the hover div (class name: image__overlay image__overlay--primary) you use position absulate so that it renders aspect of the main body. Add a width, it will be solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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