简体   繁体   English

CSS 按钮到图像右上角(图库)

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

I am trying to position a close button (x) to the top-right corner of an image.我正在尝试 position 关闭按钮 (x) 到图像的右上角。

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?我该如何解决?

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).它相对于最近定位的祖先定位的close元素,在您的情况下是modal div 而不是modal-content (如您所愿)。

I recomend you to wrap either the modal-content and close "button" in a (relative) div, like this:我建议您将modal-contentclose “按钮”包装在(相对)div中,如下所示:

<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.这应该 position 绝对 div 的右上角 position 中的关闭按钮和此 div 内的图像。

If the close button doesn't appear, make sure it has a higher z-index than the image.如果关闭按钮没有出现,请确保它的z-index比图像高。

I made a codepen: https://codepen.io/jpaulet/pen/RwGxpxy我做了一个codepen: https://codepen.io/jpaulet/pen/RwGxpxy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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