CSS Button to top-right corner of image (Gallery)

和自甴很熟 提交于 2021-01-29 09:04:18

问题


I am trying to position a close button (x) to the top-right corner of an image.

That's just before the footer of my page:

<div class="modal" id="modal">
    <span class="close" id="close">✖</span>
    <img class="modal-content" id="modal-content">
</div>

How I show the modal:

function showModal(name) {
    modal.style.display = "block";
    modalImg.src = document.getElementById(name).src;
}

And here is my styling:

/* Background when image is shown */
#modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(119, 119, 119); /* Fallback color */
    background-color: rgba(119, 119, 119, 0.9); /* Black w/ opacity */
}

/* Image */
#modal-content {
    /* Horizontally center image */
    margin: auto;
    /* Sizing */
    display: block;
    max-width: 90%;
    max-height: calc(100vh * 0.5);
    width: auto;
    /* Zoom (image gets bigger) on open */
    animation-name: zoom;
    animation-duration: 0.6s;
    /* Style */
    border: 10px solid #ffffff;
    box-shadow: rgba(0, 0, 0, 0.54) 0px 0px 7px 4px;
    /* Vertically center image */
    position: relative;
    top: 40%;
    transform: translateY(-40%);
}

@keyframes zoom {
    from {
        transform: scale(0)
    }
    to {
        transform: scale(1)
    }
}

/* Close Button */
#close {
    /* Position */
    position: absolute;
    top: 10px;
    right: 10px;
    /* Style */
    color: #ffffff;
    font-size: 20px;
    font-weight: bold;
    transition: 0.3s;
    background-color: #000000;
    border: 3px solid #ffffff;
    box-shadow: rgba(0, 0, 0, 0.5) 0px 0px 25px 3px;
    /* Makes the Button round */
    border-radius: 50%;
    padding-left: 7px;
    padding-right: 7px;
    padding-bottom: 3px;
}

/* Change cursor when pointing on button */
#close:hover,
#close:focus {
    text-decoration: none;
    cursor: pointer;
}

/* 100% image width on smaller screens */
@media only screen and (max-width: 700px) {
    .modal-content {
        width: 100%;
    }
}

The problem is that

position: absolute;
top: 10px;
right: 10px;

positions the close button in the top-right corner of the page, not at the top right corner of the image.

How can I fix that?


回答1:


The close element it's positioning relative to the closest positioned ancestor, in your case the modal div instead of the modal-content (as you wanted).

I recomend you to wrap either the modal-content and close "button" in a (relative) div, like this:

<div class="modal" id="modal">
    <div id='modal-content' class='modal-content">
       <span class="close" id="close">✖</span>
       <img> //make img height/width to 100%
    </div>
</div>

This should position the close button in the top-right position of the absolute div and the image inside this div.

If the close button doesn't appear, make sure it has a higher z-index than the image.

I made a codepen: https://codepen.io/jpaulet/pen/RwGxpxy



来源:https://stackoverflow.com/questions/65478447/css-button-to-top-right-corner-of-image-gallery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!