Hen*_*son 108 html css aspect-ratio responsive-design
我有一个网站,它有一个固定的长宽比近似16:9
景观,像一个视频.
我想让它居中并扩展以填充可用的宽度和可用的高度,但不要在任何一侧变大.
例如:
我一直在研究两种方法:
div
,但我无法让它在主要浏览器中以相同的方式运行.我知道你可以很容易地用JS做到这一点,但我想要一个纯CSS解决方案.
有任何想法吗?
Dan*_*eld 256
使用新的CSS视口单位 vw
和vh
(视口宽度/视口高度)
垂直和水平调整大小,您将看到元素将始终填充最大视口大小,而不会破坏比例和没有滚动条!
div
{
width: 100vw;
height: 56.25vw; /* height:width ratio = 9/16 = .5625 */
background: pink;
max-height: 100vh;
max-width: 177.78vh; /* 16/9 = 1.778 */
margin: auto;
position: absolute;
top:0;bottom:0; /* vertical center */
left:0;right:0; /* horizontal center */
}
Run Code Online (Sandbox Code Playgroud)
* {
margin: 0;
padding: 0;
}
div {
width: 100vw;
height: 56.25vw;
/* 100/56.25 = 1.778 */
background: pink;
max-height: 100vh;
max-width: 177.78vh;
/* 16/9 = 1.778 */
margin: auto;
position: absolute;
top: 0;
bottom: 0;
/* vertical center */
left: 0;
right: 0;
/* horizontal center */
}
Run Code Online (Sandbox Code Playgroud)
<div></div>
Run Code Online (Sandbox Code Playgroud)
如果要使用视口最大宽度和高度的90%:FIDDLE
* {
margin: 0;
padding: 0;
}
div {
width: 90vw;
/* 90% of viewport vidth */
height: 50.625vw;
/* ratio = 9/16 * 90 = 50.625 */
background: pink;
max-height: 90vh;
max-width: 160vh;
/* 16/9 * 90 = 160 */
margin: auto;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
Run Code Online (Sandbox Code Playgroud)
<div></div>
Run Code Online (Sandbox Code Playgroud)
此外,浏览器支持也很不错:IE9 +,FF,Chrome,Safari- caniuse
Chr*_*ois 17
只需Danield
在LESS mixin中重新制定答案,以便进一步使用:
// Mixin for ratio dimensions
.viewportRatio(@x, @y) {
width: 100vw;
height: @y * 100vw / @x;
max-width: @x / @y * 100vh;
max-height: 100vh;
}
div {
// Force a ratio of 5:1 for all <div>
.viewportRatio(5, 1);
background-color: blue;
margin: 0;
position: absolute;
top: 0; right: 0; bottom: 0; left: 0;
}
Run Code Online (Sandbox Code Playgroud)
将CSS媒体查询 @media
与CSS视口单元 一起使用vw
,vh
以适应视口的宽高比.这样做的好处是可以更新其他属性font-size
.
这个小提琴演示了div的固定宽高比,以及与其比例完全匹配的文本.
初始大小基于全宽:
div {
width: 100vw;
height: calc(100vw * 9 / 16);
font-size: 10vw;
/* align center */
margin: auto;
position: absolute;
top: 0px; right: 0px; bottom: 0px; left: 0px;
/* visual indicators */
background:
url(data:image/gif;base64,R0lGODlhAgAUAIABAB1ziv///yH5BAEAAAEALAAAAAACABQAAAIHhI+pa+EPCwA7) repeat-y center top,
url(data:image/gif;base64,R0lGODlhFAACAIABAIodZP///yH5BAEAAAEALAAAAAAUAAIAAAIIhI8Zu+nIVgEAOw==) repeat-x left center,
silver;
}
Run Code Online (Sandbox Code Playgroud)
然后,当视口宽度大于所需的宽高比时,切换到高度作为基本度量:
/* 100 * 16/9 = 177.778 */
@media (min-width: 177.778vh) {
div {
height: 100vh;
width: calc(100vh * 16 / 9);
font-size: calc(10vh * 16 / 9);
}
}
Run Code Online (Sandbox Code Playgroud)
这是在静脉非常相似max-width
和max-height
办法由@Danield,只是更加灵活.
我最初的问题是如何既有一个固定方面的元素,又要在指定的容器中准确地适应它,这使得它有点繁琐.如果您只是想要一个单独的元素来保持其宽高比,那么它就容易多了.
我遇到的最好方法是给元素零高度,然后使用百分比填充 - 底部给它高度.填充百分比始终与元素的宽度成比例,而不是其高度,即使其顶部或底部填充也是如此.
因此,利用它可以为元素提供百分比宽度以容纳在容器内,然后填充以指定宽高比,或者换句话说,指定宽度和高度之间的关系.
.object {
width: 80%; /* whatever width here, can be fixed no of pixels etc. */
height: 0px;
padding-bottom: 56.25%;
}
.object .content {
position: absolute;
top: 0px;
left: 0px;
height: 100%;
width: 100%;
box-sizing: border-box;
-moz-box-sizing: border-box;
padding: 40px;
}
Run Code Online (Sandbox Code Playgroud)
因此,在上面的示例中,对象占据容器宽度的80%,然后其高度为该值的56.25%.如果它的宽度是160px那么底部填充,因此高度将是90px - 16:9的宽高比.
这里的一个小问题,对你来说可能不是问题,就是你的新对象里面没有自然空间.如果您需要放置一些文本,并且文本需要获取自己的填充值,则需要在其中添加容器并在其中指定属性.
某些旧浏览器也不支持vw和vh单元,因此我可能无法接受我的问题的答案,你可能不得不使用更低级的东西.
现在有规定来解决这个新的CSS属性:object-fit
。
https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
该功能现在得到广泛支持(http://caniuse.com/#feat=object-fit)。
我知道你问过你想要一个CSS
特定的解决方案.要保持纵横比,您需要将高度除以所需的纵横比.16:9 = 1.777777777778.
要获得容器的正确高度,您需要将当前宽度除以1.777777777778.由于你不能width
用正好CSS
或百分比检查容器的容量,CSS
如果没有JavaScript
(据我所知),这是不可能的.
我编写了一个可以保持所需宽高比的工作脚本.
HTML
<div id="aspectRatio"></div>
Run Code Online (Sandbox Code Playgroud)
CSS
body { width: 100%; height: 100%; padding: 0; margin: 0; }
#aspectRatio { background: #ff6a00; }
Run Code Online (Sandbox Code Playgroud)
JavaScript的
window.onload = function () {
//Let's create a function that will scale an element with the desired ratio
//Specify the element id, desired width, and height
function keepAspectRatio(id, width, height) {
var aspectRatioDiv = document.getElementById(id);
aspectRatioDiv.style.width = window.innerWidth;
aspectRatioDiv.style.height = (window.innerWidth / (width / height)) + "px";
}
//run the function when the window loads
keepAspectRatio("aspectRatio", 16, 9);
//run the function every time the window is resized
window.onresize = function (event) {
keepAspectRatio("aspectRatio", 16, 9);
}
}
Run Code Online (Sandbox Code Playgroud)
function
如果您想使用不同比例显示其他内容,可以再次使用
keepAspectRatio(id, width, height);
Run Code Online (Sandbox Code Playgroud)