根据高度设置宽度
Set width according to height
我看到了高度取决于宽度的解决方案:css height same as width. Or here: 。
但是我的问题是相反的。
我的元素:
<body>
<div id="square"></div>
</body>
它的样式:
body, html {
margin: 0;
height: 100%;
min-height: 300px;
position: relative;
}
div#square { /* My square. */
height: 75%; /* It's height depends on ancestor's height. */
width: 75vh; /* If supported, preliminarily sets it's width. */
position: absolute; /* Centers it. */
left: 50%; top: 50%; transform: translate(-50%, -50%);
background-color: darkorange; /* Makes it visible. */
}
保持正方形的脚本:
window.onload = window.onresize = function (event) {
var mySquare = document.getElementById("square");
mySquare.style.width = mySquare.offsetHeight + 'px';
};
完整代码在这里:http://jsfiddle.net/je1h/hxkgfr9g/
问题是在纯 CSS 中做同样的事情。没有脚本。
我知道有两种技术可以根据元素的高度来保持元素的纵横比:
当高度相对于视口时:
您可以使用 vh 单位:
div {
width: 75vh;
height: 75vh;
background:darkorange;
}
<div></div>
对于基于父元素高度的高度 :
您可以使用具有所需宽高比的虚拟图像。具有 1:1 宽高比的示例,您可以使用 1*1 透明 .png 图像或@vlgalik 评论的 1*1 base64 编码的 gif:
html,body{
height:100%;
margin:0;
padding:0;
}
#wrap{
height:75%;
}
#el{
display:inline-block;
height:100%;
background:darkorange;
}
#el img{
display:block;
height:100%;
width:auto;
}
<div id="wrap">
<div id="el">
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7">
</div>
</div>
请注意,最后一个演示不会在 window 调整大小时更新。但是纵横比在页面加载时保持
更新:
如 中所述,在 #el
上设置 display:inline-flex:
似乎解决了 window 调整大小问题的更新。
编辑:错过了您想根据高度设置宽度,这恰恰相反:(
要支持所有比率,您可以使用 padding-bottom
。 padding-bottom 中的百分比总是相对于元素的宽度:
/* the wrapper has a width */
.wrapper {
position: relative;
width: 25%;
margin-bottom: 10px;
}
/* these elements set the height (using padding-bottom) */
.square {
padding-bottom: 100%;
position: relative;
}
.widescreen {
padding-bottom: 56.25%; /* 9/16 = 0.5625 */
}
/*
* This is the content.
* Needs position:absolute to not add to the width of the parent
*/
.content {
/* fill parent */
position: absolute;
top: 0; bottom: 0;
left: 0; right: 0;
/* visual */
padding: 10px;
background: orange;
color: white;
}
<div class="wrapper">
<div class="square">
<div class="content">square</div>
</div>
</div>
<div class="wrapper">
<div class="widescreen">
<div class="content">16:9 ratio</div>
</div>
</div>
唯一的缺点是,您需要一堆包装器元素。
我看到了高度取决于宽度的解决方案:css height same as width. Or here: 。 但是我的问题是相反的。
我的元素:
<body>
<div id="square"></div>
</body>
它的样式:
body, html {
margin: 0;
height: 100%;
min-height: 300px;
position: relative;
}
div#square { /* My square. */
height: 75%; /* It's height depends on ancestor's height. */
width: 75vh; /* If supported, preliminarily sets it's width. */
position: absolute; /* Centers it. */
left: 50%; top: 50%; transform: translate(-50%, -50%);
background-color: darkorange; /* Makes it visible. */
}
保持正方形的脚本:
window.onload = window.onresize = function (event) {
var mySquare = document.getElementById("square");
mySquare.style.width = mySquare.offsetHeight + 'px';
};
完整代码在这里:http://jsfiddle.net/je1h/hxkgfr9g/
问题是在纯 CSS 中做同样的事情。没有脚本。
我知道有两种技术可以根据元素的高度来保持元素的纵横比:
当高度相对于视口时:
您可以使用 vh 单位:
div {
width: 75vh;
height: 75vh;
background:darkorange;
}
<div></div>
对于基于父元素高度的高度 :
您可以使用具有所需宽高比的虚拟图像。具有 1:1 宽高比的示例,您可以使用 1*1 透明 .png 图像或@vlgalik 评论的 1*1 base64 编码的 gif:
html,body{
height:100%;
margin:0;
padding:0;
}
#wrap{
height:75%;
}
#el{
display:inline-block;
height:100%;
background:darkorange;
}
#el img{
display:block;
height:100%;
width:auto;
}
<div id="wrap">
<div id="el">
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7">
</div>
</div>
请注意,最后一个演示不会在 window 调整大小时更新。但是纵横比在页面加载时保持
更新:
如 #el
上设置 display:inline-flex:
似乎解决了 window 调整大小问题的更新。
编辑:错过了您想根据高度设置宽度,这恰恰相反:(
要支持所有比率,您可以使用 padding-bottom
。 padding-bottom 中的百分比总是相对于元素的宽度:
/* the wrapper has a width */
.wrapper {
position: relative;
width: 25%;
margin-bottom: 10px;
}
/* these elements set the height (using padding-bottom) */
.square {
padding-bottom: 100%;
position: relative;
}
.widescreen {
padding-bottom: 56.25%; /* 9/16 = 0.5625 */
}
/*
* This is the content.
* Needs position:absolute to not add to the width of the parent
*/
.content {
/* fill parent */
position: absolute;
top: 0; bottom: 0;
left: 0; right: 0;
/* visual */
padding: 10px;
background: orange;
color: white;
}
<div class="wrapper">
<div class="square">
<div class="content">square</div>
</div>
</div>
<div class="wrapper">
<div class="widescreen">
<div class="content">16:9 ratio</div>
</div>
</div>
唯一的缺点是,您需要一堆包装器元素。