【问题标题】:Keep aspect ratio of fluid size containers保持流体大小容器的纵横比
【发布时间】:2019-09-16 21:17:06
【问题描述】:

我一直在尝试构建一个类似于表格/网格的结构,该结构在大小上应该是流动的,同时保持元素的纵横比。

我想出了一些接近我想要构建的东西,只是缺少“保持纵横比”部分。我知道我不应该同时设置宽度和高度,但是删除任何声明都会完全破坏布局。

标记:

<div class="testWrap2">

    <h2 class="dMarginBottom">Resposive Solution?</h2>


    <div class="teaserGrid">
        <div class="gridRow">
            <div class="gridElement small cimage"><img src="http://placekitten.com/g/336/200"></img><div>2a</div></div>
            <div class="gridElement small cimage"><img src="http://placekitten.com/g/336/200"></img><div>2b</div></div>
            <div class="gridElement small cimage"><img src="http://placekitten.com/g/336/200"></img><div>2c</div></div>
        </div>      
        <div class="gridRow">
            <div class="gridElement small image"><img src="http://placekitten.com/g/336/234"></img></div>
            <div class="gridElement large cimage"><img src="http://placekitten.com/g/672/200"></img><div>2b</div></div>

        </div>                  
    </div>



</div>

CSS

body, html {
    height:100%;
    width:100%;
}

.testWrap2{
    margin: 30px auto;
    max-width: 1010px;
    height:37.1%; /*this is so arbitrary and wrong  but it somehow works*/
}

.teaserGrid{
    height:100%;
    max-height:469px;
}


.gridRow{
    margin-left:-1px;
    height:100%;
    max-height:234px;
    clear:both;
}


.gridElement{
    float:left;
    padding-left:1px;
    padding-bottom:1px;
    -webkit-box-sizing: border-box; 
    -moz-box-sizing: border-box;   
    box-sizing: border-box;      
    height:100%;
    max-height:234px;
    }

.gridElement div{
    background-color:green;
    width:100%;
    height:14.5%;
}

.gridElement img{
    width:100%;
}


.gridElement.image img{
    height:100%;
}

.gridElement.cimage img{
    display:block;
    height:85.5%
}


.gridElement.small{
    width:33.33%;
}

.gridElement.large{
    width:66.66%;
}

更新说明

在调整大小时我想保持两个比率:

  • (高度 A):(宽度 A+WidthB)[绿线] 和
  • (Heigt B) : (Heigt C) [蓝线]

【问题讨论】:

  • @web-tiki 老实说,我不明白为什么会这样。你能详细说明一下吗?这在数学上是可能的。
  • 对不起,我误会了,我会删除我的评论。我做了这个应该是你需要的新小提琴。您必须对绿色 div 的内容使用绝对定位,以便它们保持纵横比。 jsfiddle.net/webtiki/9XJJQ/7

标签: css fluid-layout aspect-ratio


【解决方案1】:

https://css-tricks.com/fun-viewport-units/

.full-width {
  width: 100vw;
  height: calc(100vw * (9/16));
}

【讨论】:

    猜你喜欢
    • 2021-06-21
    • 2012-10-06
    • 2015-09-26
    • 1970-01-01
    • 2020-06-14
    • 2013-06-26
    • 2012-04-15
    • 1970-01-01
    相关资源
    最近更新 更多