gio*_*gos 5 html css html5 css3
我想在我的css文件中做下一个技巧,以便(高度=宽度)不设置像素。我想这样做,以便无论浏览器的分辨率如何,在这两个维度上都具有相同的值。
#test{
height: 100%;
width: (same as height);
}
Run Code Online (Sandbox Code Playgroud)
我更喜欢使用CSS而不是JavaScript。
先感谢您。
小智 56
这是我的方式
aspect-ratio: 1 / 1;
height: 100%;
Run Code Online (Sandbox Code Playgroud)
例子:
aspect-ratio: 1 / 1;
height: 100%;
Run Code Online (Sandbox Code Playgroud)
.my-container {
width: 100%;
height: 150px;
background: black;
padding: 20px
}
.my-element {
background: #fff;
aspect-ratio: 1 / 1;
height: 100%;
}
Run Code Online (Sandbox Code Playgroud)
目前唯一的 CSS 方法 (AFAIK) 是使用视口相关值 (vh / vw )
目前支持不是很好:http : //caniuse.com/viewport-units但这里有一个快速演示
CSS
.box {
background-color: #00f;
width: 50vw;
height:50vw;
}
Run Code Online (Sandbox Code Playgroud)
盒子是响应式的,但将始终保持方形。
纯%值将无法正常工作,height:100%
不等于width:100%
他们指的是不同的事情都是家长的相关尺寸。
另一种选择是使用img元素行为来制作纵横比
在这里,我使用svg图像并将其与数据 url内联以使其更简单
要描述所需的纵横比,您可以使用viewBox svg 属性viewBox='0 0 width-ratio height-ratio'
例子:
html,
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
"Ubuntu", "Cantarell", "Fira Sans",
"Droid Sans", "Helvetica Neue", sans-serif;
margin: 0;
padding: 0;
}
body {
margin: 1rem;
}
.row {
padding: 8px 0px;
}
.block {
display: inline-block;
vertical-align: top;
position: relative;
margin-left: 8px;
margin-right: 8px;
}
.block-content {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex;
justify-content: center;
align-items: center;
}
.ratio--width-to-height {
height: 100%;
width: auto;
}
.ratio--height-to-width {
height: auto;
width: 100%;
}
Run Code Online (Sandbox Code Playgroud)
<h1>
Block aspect ratio with svg image
</h1>
<h2>
width to height
</h2>
<div class="row">
<div class="block" style="background: lime; height: 120px;">
<img class="ratio--width-to-height"
src="data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1 1'></svg>">
<div class="block-content">1 : 1</div>
</div>
<div class="block" style="background: cyan; height: 120px;">
<img class="ratio--width-to-height"
src="data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 2 1'></svg>">
<div class="block-content">2 : 1</div>
</div>
<div class="block" style="background: orange; height: 120px;">
<img class="ratio--width-to-height"
src="data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1 1.25'></svg>">
<div class="block-content">1 : 1.25</div>
</div>
</div>
<h2>
height to width
</h2>
<div class="row">
<div class="block" style="background: lime; width: 120px;">
<img class="ratio--height-to-width"
src="data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1 1'></svg>">
<div class="block-content">1 : 1</div>
</div>
<div class="block" style="background: cyan; width: 120px;">
<img class="ratio--height-to-width"
src="data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 2 1'></svg>">
<div class="block-content">2 : 1</div>
</div>
<div class="block" style="background: orange; width: 120px;">
<img class="ratio--height-to-width"
src="data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1 1.25'></svg>">
<div class="block-content">1 : 1.25</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
codepen 上的相同示例https://codepen.io/forceuser/pen/MMWBBx
归档时间: |
|
查看次数: |
4400 次 |
最近记录: |