当我给一个图像一个百分比的宽度或高度,它将增长/收缩保持其纵横比,但如果我想要同样的效果与另一个元素,它是否可能在所有的宽度和高度使用百分比一起?
当前回答
<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 =视口高度
其他回答
你可以使用纯CSS来做到这一点;不需要JavaScript。这利用了填充顶部百分比相对于包含块的宽度的事实(有点违反直觉)。这里有一个例子:
.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>
这就是我的解
<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%;
}
http://jsfiddle.net/aG4Fs/3/
对于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
你也可以设置max-height, max-width,和/或min-height, min-width,如果你不想让它变得非常大或小,因为它现在是基于浏览器的宽度,而不是容器,并且会无限地增长/收缩。
注意,如果您将字体大小设置为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 =视口高度
(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');
});
工作示例:http://jsfiddle.net/QtftX/1/
推荐文章
- 相应地改变div大小保持长宽比
- 如何从web应用程序访问手机摄像头?
- 我如何从一个元标签与JavaScript的信息?
- 如何在iPhone/iOS上删除电话号码的蓝色样式?
- JavaScript数据网格数百万行
- 如何改变一个画布元素中的元素的不透明度(alpha,透明度)?
- 我如何编码/解码HTML实体在Ruby?
- ' coll -xs-* '在引导4中不工作
- 什么是类="mb-0"在引导4?
- 当flexbox项目以列模式换行时,容器的宽度不会增长
- CSS单位- vh/vw和%之间的区别是什么?
- 没有声明HTML文档的字符编码
- :在Sass中的伪元素选择器之后和之前
- 使用HTML形式的PUT方法
- <div>元素中的中心元素