Tho*_*man 414 html javascript css jquery responsive-design
是否可以设置与宽度相同的高度(比例1:1)?
例
+----------+
| body |
| 1:3 |
| |
| +------+ |
| | div | |
| | 1:1 | |
| +------+ |
| |
| |
| |
| |
| |
+----------+
Run Code Online (Sandbox Code Playgroud)
CSS
div {
width: 80%;
height: same-as-width
}
Run Code Online (Sandbox Code Playgroud)
Nat*_*yan 668
[更新:虽然我独立发现了这个技巧,但我已经知道Thierry Koblentz打败了我.你可以在A List Apart上找到他2009年的文章.信用到期的信用.]
我知道这是一个老问题,但我遇到了我类似的问题也只能用CSS解决.这是我的博客文章,讨论了解决方案.帖子中包含一个实例.代码转发如下.
HTML:
<div id="container">
<div id="dummy"></div>
<div id="element">
some text
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
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! */
}
Run Code Online (Sandbox Code Playgroud)
#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! */
}Run Code Online (Sandbox Code Playgroud)
<div id="container">
<div id="dummy"></div>
<div id="element">
some text
</div>
</div>Run Code Online (Sandbox Code Playgroud)
Kri*_*jan 389
有一种方法使用CSS!
如果根据父容器设置宽度,可以将高度设置为0,并将padding-bottom设置为将根据当前宽度计算的百分比:
.some_element {
position: relative;
width: 20%;
height: 0;
padding-bottom: 20%;
}
Run Code Online (Sandbox Code Playgroud)
这适用于所有主流浏览器.
Sat*_*ran 93
没有任何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>
Run Code Online (Sandbox Code Playgroud)
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%;
}
Run Code Online (Sandbox Code Playgroud)
Hus*_*ein 70
使用jQuery,您可以通过执行此操作来实现
var cw = $('.child').width();
$('.child').css({'height':cw+'px'});
Run Code Online (Sandbox Code Playgroud)
web*_*iki 69
简单和neet:vw根据视口宽度使用单位作为响应高度/宽度.
vw:视口宽度的1/100.(来源MDN)
HTML:
<div></div>
Run Code Online (Sandbox Code Playgroud)
1:1宽高比的CSS:
div{
width:80vw;
height:80vw; /* same as width */
}
Run Code Online (Sandbox Code Playgroud)
表根据所需的纵横比和元素宽度计算高度.
aspect ratio | multiply width by
-----------------------------------
1:1 | 1
1:3 | 3
4:3 | 0.75
16:9 | 0.5625
Run Code Online (Sandbox Code Playgroud)
这种技术允许您:
position:absolute;这些单元由IE9 +支持,请参阅canIuse以获取更多信息
Nin*_*non 59
HTML
<div id="container">
<div id="element">
some text
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS
#container {
width: 50%; /* desired width */
}
#element {
height: 0;
padding-bottom: 100%;
}
Run Code Online (Sandbox Code Playgroud)
扩展填充顶部/底部技术,可以使用伪元素来设置元素的高度.使用浮点和负边距从流和视图中删除伪元素.
这允许您将内容放在框内,而无需使用额外的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;
}Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)