gpt4 book ai didi

html - 无滚动条的响应式 3*3 方形 div 网格

转载 作者:可可西里 更新时间:2023-11-01 13:06:50 27 4
gpt4 key购买 nike

我想要一个没有滚动条的响应式 9x9 div 网格。 div 网格应根据可见的浏览器窗口调整大小。我合并了"How to maintain the aspect ratio of a div using only CSS""Grid of responsive squares" :

html

<div class="stretchy-wrapper">
<div class="inner">
<div class="square">
<div class="content">
<div class="table">
<div class="table-cell numbers">
98%
</div>
</div>
</div>
</div>
<div class="square">
<div class="content">
<div class="table">
<div class="table-cell numbers">
3.9/5
</div>
</div>
</div>
</div>
<div class="square">
<div class="content">
<div class="table">
<div class="table-cell numbers">
3.9/5
</div>
</div>
</div>
</div>
<div class="square">
<div class="content">
<div class="table">
<div class="table-cell numbers">
3.9/5
</div>
</div>
</div>
</div>
<div class="square">
<div class="content">
<div class="table">
<div class="table-cell numbers">
3.9/5
</div>
</div>
</div>
</div>
<div class="square">
<div class="content">
<div class="table">
<div class="table-cell numbers">
3.9/5
</div>
</div>
</div>
</div>
<div class="square">
<div class="content">
<div class="table">
<div class="table-cell numbers">
3.9/5
</div>
</div>
</div>
</div>
<div class="square">
<div class="content">
<div class="table">
<div class="table-cell numbers">
3.9/5
</div>
</div>
</div>
</div>
<div class="square">
<div class="content">
<div class="table">
<div class="table-cell numbers">
3.9/5
</div>
</div>
</div>
</div>
</div>
</div>

CSS

@import url(http://fonts.googleapis.com/css?family=Lato:400,900);  /* <-- Just for the demo, Yes I like pretty fonts... */

.square {
float:left;
position: relative;
width: 30%;
padding-bottom : 30%; /* = width for a 1:1 aspect ratio */
margin:1.66%;
background-color:#1E1E1E;
overflow:hidden;
}

.content {
position:absolute;
height:90%; /* = 100% - 2*5% padding */
width:90%; /* = 100% - 2*5% padding */
padding: 5%;

}
.table{
display:table;
width:100%;
height:100%;
}

body {
font-size:20px;
font-family: 'Lato',verdana, sans-serif;
color: #fff;
text-align:center;
background:#ECECEC;
}

.numbers{
font-weight:900;
font-size:100px;
}

div.stretchy-wrapper {
width: 100%;
padding-bottom: 56.25%; /* 16:9 */
position: relative;

background: lightgrey;
}

div.stretchy-wrapper > .inner {
position: absolute;
top: 0; bottom: 0; left: 0; right: 0;

color: white;
font-size: 24px;
text-align: center;
}

然而,div 仍然会扩展到最大宽度,这会导致滚动条:Fiddle .

div 仅扩展到最大可见宽度或高度的 css 技巧是什么?所以我看不到滚动条?

最佳答案

用 Flexbox 和 vmin 解决了

CSS

    .container {
margin: 0 auto;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
align-content: space-around;

height: 93vmin;
width: 93vmin;
background: lightgrey;
}
.cell {
height: 30vmin;
width: 30vmin;
background-color:#1E1E1E;
}

html

<div class="container">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>

<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>

<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</div>

Full Screen Fiddle

Fiddle

Recommended read about flexbox

关于html - 无滚动条的响应式 3*3 方形 div 网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31908526/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com