图片右上角的CSS按钮(图库) [英] CSS Button to top-right corner of image (Gallery)
问题描述
我正在尝试将关闭按钮(x)定位到图像的右上角.
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>
我如何显示模式:
function showModal(name) {
modal.style.display = "block";
modalImg.src = document.getElementById(name).src;
}
这是我的风格:
/* 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%;
}
}
问题是
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.
我该如何解决?
推荐答案
相对于最接近的祖先定位的 close
元素,在您的情况下为 modal
div modal-content
(根据需要).
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).
我建议您包装 modal-content
和 close
"button"在(相对)div中,如下所示:
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>
这应该将关闭按钮放置在绝对div的右上角位置,并将图像放置在该div内.
This should position the close button in the top-right position of the absolute div and the image inside this div.
如果没有显示关闭按钮,请确保其 z-index
高于图片.
If the close button doesn't appear, make sure it has a higher z-index
than the image.
我制作了一个Codepen: https://codepen.io/jpaulet/pen/RwGxpxy
I made a codepen: https://codepen.io/jpaulet/pen/RwGxpxy
这篇关于图片右上角的CSS按钮(图库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!