html - 为不同宽高比的图像在图像的右上角放置一个按钮

标签 html css position css-position

我试图将按钮定位在图像的右上角。

这是我尝试实现的一小部分代码的 fiddle :https://jsfiddle.net/sc5vfu9e/3/

.modalDialog {
	position: fixed;
	font-family: "Proxima-Nova", sans-serif;
	top: 0;
	right: 0;
	bottom: 0;
	left: 0;
	background: rgba(0,0,0,0.8);
	z-index: 10;
  text-align: center;
}

.modalDialog > div {
	position: relative;
	width: 100%;
	height: 100%;
}

.modalDialog img {
	max-width: 95%;
	max-height: 95%;
	position: relative;
    top: 50%;
    -webkit-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
}
.closeModal {
	position: absolute;
	right: 0;
	top: 50%;
	display: block;
	background: #606060;
	color: white;
	font-size: 0.5em;
	line-height: 25px;
	text-align: center;
	border-radius: 50%;
	text-decoration: none;
	width: 24px;
	-webkit-border-radius: 50%;
	-moz-border-radius: 50%;
	box-shadow: 1px 1px 3px #000;
	-webkit-box-shadow: 1px 1px 3px #000;
	-moz-box-shadow: 1px 1px 3px #000;	
}

.closeModal:hover,
.closeModal:active,
.closeModal:focus {
	background: #333;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="modalDialog">
						<div>
							<img src="https://s-media-cache-ak0.pinimg.com/736x/f2/c3/9c/f2c39c1651d11f47c5a5f35f205663fa.jpg" alt="Image" id="">
							<a href="#" title="close" class="closeModal">x</a>
						</div>
					</div>

我将加载不同宽高比的图像。如何在每种情况下都在图像右上角设置关闭按钮?

PS:我是编程新手,所以请多多包涵。

最佳答案

好的,试试这个 JSfiddle

已更新 JSfiddle

.modalDialog {
  position: fixed;
  font-family: "Proxima-Nova", sans-serif;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: rgba(0, 0, 0, 0.8);
  z-index: 10;
  text-align: center;
}

.modalDialog > div {
  position: relative;
  width: 95%;
  height: 95%;
  margin: auto;
}

.modalDialog > div > div {
  width: auto;
  height: 95%;
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}

img {
  max-width: 100%;
  height: auto;
}

@media only screen and (min-width: 800px) {
  img {
    max-height: 95%;
    width: auto;
  }
}

.closeModal {
  position: absolute;
  right: -12px;
  /* adjust these accordingly*/
  top: -12px;
  /*adjust these accordingly */
  display: block;
  background: #606060;
  color: white;
  font-size: 0.5em;
  line-height: 25px;
  text-align: center;
  border-radius: 50%;
  text-decoration: none;
  width: 24px;
  -webkit-border-radius: 50%;
  -moz-border-radius: 50%;
  box-shadow: 1px 1px 3px #000;
  -webkit-box-shadow: 1px 1px 3px #000;
  -moz-box-shadow: 1px 1px 3px #000;
}

.closeModal:hover,
.closeModal:active,
.closeModal:focus {
  background: #333;
}
<div class="modalDialog">
  <div>
    <div>
      <img src="https://s-media-cache-ak0.pinimg.com/736x/f2/c3/9c/f2c39c1651d11f47c5a5f35f205663fa.jpg" alt="Image" id="">
      <a href="#" title="close" class="closeModal">x</a>
    </div>
  </div>
</div>

关于html - 为不同宽高比的图像在图像的右上角放置一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41607605/

相关文章:

html - 为什么我的网站上显示水平滚动条?

html - 鼠标悬停按钮/图像未与链接对齐

user-interface - 如何获得 Electron 中的操作系统窗口位置?

javascript - Cytoscape.js 孤儿节点位置

html - 列与 css 网格并排

ios - 从网页 iOS 中的文本字段获取字符串

css - 如何在没有 href ="#"的情况下将焦点放在 anchor 标记上

html - 一个元素的正常流动

position - 如何使内联ckeditor工具栏固定在顶部而不是 float

html - 如何有条件地加载 CSS