【发布时间】:2014-11-01 18:37:29
【问题描述】:
我想要一个页面尽可能大地显示图像,但整个图像都在视图中,不改变图像的比例,并且在页面上居中(即全高,两边有边框,或全宽顶部和底部有边框)。有没有办法纯粹在 CSS 中做到这一点?我尝试了各种宽度/高度/最小宽度/最小高度属性的组合,但无法按我的意愿显示。
我还希望能够覆盖由带有背景图像的 div 组成的链接,该背景图像在悬停时会发生变化,其代码如下。我希望它位于图像的右上角:
HTML:
<div class="imagecontainer">
<img src='gallery/images/<?php echo $image;?>' max-height="100%" width="100%"/>
<div class="backbuttoncontainer">
<a class="gallerybackbutton" href="gallery/index.htm"></a>
</div>
</div>
CSS:
.imagecontainer{
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
.backbuttoncontainer{
text-align:right;
margin-top: 10px;
margin-right: 10px;
height: 38px;
width: 100%;
display: block;
}
.gallerybackbutton{
background: url('icons/back_to_gallery.png') bottom;
display: inline-block;
height: 28px;
}
.gallerybackbutton:hover{
background-position: 0 0;
}
【问题讨论】:
-
您检查过
margin: auto;中的.backbuttoncontainer吗? -
我刚刚添加了,但是按钮不显示。
-
我不认为他们在问同样的事情。我希望它根据照片和浏览器的尺寸垂直或水平居中 - 即在不裁剪图像或不必滚动的情况下使图像尽可能大,并将其居中在未填充的轴上。