当我给一张图片设置百分比宽度或高度时,它会保持纵横比而增加或缩小大小。但是如果我希望另一个元素也具有相同的效果,是否有可能使用百分比将宽度和高度绑定在一起呢?
当我给一张图片设置百分比宽度或高度时,它会保持纵横比而增加或缩小大小。但是如果我希望另一个元素也具有相同的效果,是否有可能使用百分比将宽度和高度绑定在一起呢?
padding-top
百分比是相对于包含块的 宽度 的。以下是一个例子:
.wrapper {
width: 50%;
/* whatever width you want */
display: inline-block;
position: relative;
}
.wrapper:after {
padding-top: 56.25%;
/* 16:9 ratio */
display: block;
content: '';
}
.main {
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
/* fill parent */
background-color: deepskyblue;
/* let's see it! */
color: white;
}
<div class="wrapper">
<div class="main">
This is your div with the specified aspect ratio.
</div>
</div>
基于 Chris 的想法,另一个选项是使用伪元素,这样就不需要使用绝对定位的内部元素了。
<style>
.square {
/* width within the parent.
can be any percentage. */
width: 100%;
}
.square:before {
content: "";
float: left;
/* essentially the aspect ratio. 100% means the
div will remain 100% as tall as it is wide, or
square in other words. */
padding-bottom: 100%;
}
/* this is a clearfix. you can use whatever
clearfix you usually use, add
overflow:hidden to the parent element,
or simply float the parent container. */
.square:after {
content: "";
display: table;
clear: both;
}
</style>
<div class="square">
<h1>Square</h1>
<p>This div will maintain its aspect ratio.</p>
</div>
我已经在这里制作了一个演示:http://codepen.io/tcmulder/pen/iqnDr
编辑:
现在,借鉴Isaac的想法,在现代浏览器中使用vw单位来强制宽高比更加容易(尽管我不建议也使用vh,因为这样宽高比将基于窗口高度而改变)。
所以,这简化了事情:
<style>
.square {
/* width within the parent (could use vw instead of course) */
width: 50%;
/* set aspect ratio */
height: 50vw;
}
</style>
<div class="square">
<h1>Square</h1>
<p>This div will maintain its aspect ratio.</p>
</div>
我在这里提供了一个修改过的演示: https://codepen.io/tcmulder/pen/MdojRG?editors=1100
此外,如果您不想让它变得非常大或非常小,您还可以设置最大高度、最大宽度和/或最小高度、最小宽度,因为现在它是基于浏览器的宽度而不是容器的宽度,并且会无限制地增长 / 缩小。
请注意,如果您将字体大小设置为vw测量单位并将所有内容设置为em测量单位,则还可以缩放元素内部的内容。这是一个演示:https://codepen.io/tcmulder/pen/VBJqLV?editors=1100
<style>
#aspectRatio
{
position:fixed;
left:0px;
top:0px;
width:60vw;
height:40vw;
border:1px solid;
font-size:10vw;
}
</style>
<body>
<div id="aspectRatio">Aspect Ratio?</div>
</body>
需要注意的关键是 vw
表示视口宽度,vh
表示视口高度
<div class="main" style="width: 100%;">
<div class="container">
<div class="sizing"></div>
<div class="content"></div>
</div>
</div>
.main {
width: 100%;
}
.container {
width: 30%;
float: right;
position: relative;
}
.sizing {
width: 100%;
padding-bottom: 50%;
visibility: hidden;
}
.content {
width: 100%;
height: 100%;
background-color: red;
position: absolute;
margin-top: -50%;
}
(function( $ ) {
$.fn.keepRatio = function(which) {
var $this = $(this);
var w = $this.width();
var h = $this.height();
var ratio = w/h;
$(window).resize(function() {
switch(which) {
case 'width':
var nh = $this.width() / ratio;
$this.css('height', nh + 'px');
break;
case 'height':
var nw = $this.height() * ratio;
$this.css('width', nw + 'px');
break;
}
});
}
})( jQuery );
$(document).ready(function(){
$('#foo').keepRatio('width');
});
Working example: http://jsfiddle.net/QtftX/1/
<div class="main">
垂直居中于 body 中? - Krunal Mevada