【发布时间】:2013-12-31 01:41:40
【问题描述】:
已经玩了一段时间,试图让它工作,所以我设置了一个 CodePen,希望你们能提供帮助:
我需要用百分比而不是静态像素宽度来指定 .container 的 height 和 width,以适应我的设计并保持流畅和响应。
在笔中删除.container 上设置的像素尺寸,你会看到会发生什么。
有没有办法做到这一点?
【问题讨论】:
标签: css image percentage centering
已经玩了一段时间,试图让它工作,所以我设置了一个 CodePen,希望你们能提供帮助:
我需要用百分比而不是静态像素宽度来指定 .container 的 height 和 width,以适应我的设计并保持流畅和响应。
在笔中删除.container 上设置的像素尺寸,你会看到会发生什么。
有没有办法做到这一点?
【问题讨论】:
标签: css image percentage centering
对于现代浏览器,您可以使用绝对定位和transform 来定位图像
.container
width: 50%
height: 50%
background-color: #ebebeb
position: relative
img
position: absolute
left: 50%
top: 50%
transform: translate(-50%, -50%)
【讨论】:
transform 的标准语法
【讨论】: