高度等于动态宽度(CSS流体布局)

时间:2011-03-26 21:35:43

标签: javascript jquery html css responsive-design

是否可以设置与宽度相同的高度(比率1:1)?

示例

+----------+
| body     |
| 1:3      |
|          |
| +------+ |
| | div  | |
| | 1:1  | |
| +------+ |
|          |
|          |
|          |
|          |
|          |
+----------+

CSS

div {
  width: 80%;
  height: same-as-width
}

9 个答案:

答案 0 :(得分:655)

[更新:虽然我独立发现了这个技巧,但我已经知道Thierry Koblentz打败了我。你可以在A List Apart上找到他的2009 article。信用到期的信用。]

我知道这是一个老问题,但我遇到了类似的问题,我只用CSS解决了。这是我的blog post讨论解决方案。该帖子中包含live example。代码将在下面重新发布。

HTML:

<div id="container">
    <div id="dummy"></div>
    <div id="element">
        some text
    </div>
</div>

CSS:

#container {
    display: inline-block;
    position: relative;
    width: 50%;
}
#dummy {
    margin-top: 75%; /* 4:3 aspect ratio */
}
#element {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: silver /* show me! */
}

#container {
  display: inline-block;
  position: relative;
  width: 50%;
}

#dummy {
  margin-top: 75%;
  /* 4:3 aspect ratio */
}

#element {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: silver/* show me! */
}
<div id="container">
  <div id="dummy"></div>
  <div id="element">
    some text
  </div>
</div>

答案 1 :(得分:376)

有一种使用CSS的方法!

如果根据父容器设置宽度,可以将高度设置为0,并将padding-bottom设置为将根据当前宽度计算的百分比:

.some_element {
    position: relative;
    width: 20%;
    height: 0;
    padding-bottom: 20%;
}

这适用于所有主流浏览器。

JSFiddle:https://jsfiddle.net/ayb9nzj3/

答案 2 :(得分:87)

没有任何Javascript可以:)

本文完美地描述了它 - http://www.mademyday.de/css-height-equals-width-with-pure-css.html

HTML:

<div class='box'>
    <div class='content'>Aspect ratio of 1:1</div>
</div> 

CSS:

.box {
    position: relative;
    width:    50%; /* desired width */
}

.box:before {
    content:     "";
    display:     block;
    padding-top: 100%; /* initial ratio of 1:1*/
}

.content {
    position: absolute;
    top:      0;
    left:     0;
    bottom:   0;
    right:    0;
}

/* Other ratios - just apply the desired class to the "box" element */
.ratio2_1:before{
    padding-top: 50%;
}
.ratio1_2:before{
    padding-top: 200%;
}
.ratio4_3:before{
    padding-top: 75%;
}
.ratio16_9:before{
    padding-top: 56.25%;
}

答案 3 :(得分:65)

使用jQuery,你可以通过

实现这一点
var cw = $('.child').width();
$('.child').css({'height':cw+'px'});

检查http://jsfiddle.net/n6DAu/1/

处的工作示例

答案 4 :(得分:64)

简单明了:根据视口宽度使用vw单位作为响应高度/宽度。

  

vw:视口宽度的1/100。 (Source MDN

<强> DEMO

HTML:

<div></div>

CSS 的宽高比为1:1:

div{
    width:80vw;
    height:80vw; /* same as width */
}

根据所需的宽高比和元素宽度计算高度的表。

   aspect ratio  |  multiply width by
    -----------------------------------
         1:1      |         1
         1:3      |         3
         4:3      |        0.75
        16:9      |       0.5625

此技术允许您:

  • 在不使用position:absolute;
  • 的情况下在元素中插入任何内容
  • 没有必要的HTML标记(只有一个元素)
  • 使用vh单位
  • 根据视口的高度调整元素纵横比
  • 您可以根据视口的高度和宽度制作始终适合视口的响应方形或其他宽高比(请参阅此答案:Responsive square according to width and height of viewport或此demo

IE9 +支持这些单位,请参阅canIuse for more info

答案 5 :(得分:55)

非常简单的方法jsfiddle

<强> HTML

<div id="container">
    <div id="element">
        some text
    </div>
</div>

<强> CSS

#container {
    width: 50%; /* desired width */
}

#element {
    height: 0;
    padding-bottom: 100%;
}

答案 6 :(得分:8)

扩展填充顶部/底部技术,可以使用伪元素来设置元素的高度。使用浮点和负边距从流和视图中删除伪元素。

这允许您在不使用额外的div和/或CSS定位的情况下将内容放在框中。

.fixed-ar::before {
  content: "";
  float: left;
  width: 1px;
  margin-left: -1px;
}
.fixed-ar::after {
  content: "";
  display: table;
  clear: both;
}


/* proportions */

.fixed-ar-1-1::before {
  padding-top: 100%;
}
.fixed-ar-4-3::before {
  padding-top: 75%;
}
.fixed-ar-16-9::before {
  padding-top: 56.25%;
}


/* demo */

.fixed-ar {
  margin: 1em 0;
  max-width: 400px;
  background: #EEE url(https://lorempixel.com/800/450/food/5/) center no-repeat;
  background-size: contain;
}
<div class="fixed-ar fixed-ar-1-1">1:1 Aspect Ratio</div>
<div class="fixed-ar fixed-ar-4-3">4:3 Aspect Ratio</div>
<div class="fixed-ar fixed-ar-16-9">16:9 Aspect Ratio</div>

答案 7 :(得分:4)

这真的是对Nathan答案的评论,但我还是不允许这样做...... 我想保持宽高比,即使有太多的东西可以放在盒子里。他的例子扩大了高度,改变了纵横比。我发现添加了

overflow: hidden;
overflow-x: auto;
overflow-y: auto;

给.element提供了帮助。见http://jsfiddle.net/B8FU8/3111/

答案 8 :(得分:-2)

宽度:80vmin; 身高:80vmin;

CSS 做最小视图,高度或宽度的80%

http://caniuse.com/#feat=viewport-units