html& CSS响应式网格问题,使用浏览器检查工具时的比例完美,窗口不扩展
我正在尝试创建一个具有三个相等大小的正方形的设计。
我想要的是,在分辨率高于991px宽度的屏幕上,旁边有3个正方形。
如果用户屏幕宽度低于991px,高于767px,我希望他或她看到每行两个正方形:
“希望他或她看到每行的一张图像。
到目前为止,我已经取得了成就。 由于我的结果是:
现在我的问题开始的地方。 我已经使用Firefox检查工具对此进行了测试。缩放屏幕分辨率并调整我的HTML& CSS代码。 而且看起来很完美。
但是,当我试图在窗户浏览屏幕上检查结果时,突然间缩放了。 而且我几乎彼此之间几乎有两个屏幕。但是右边的一个空字段。 如您在下面的屏幕上所见:
我的代码: html
<section class="home-grid">
<div class="grid-house" data-img="images/bg.jpg">
<div class="house-grid-content">
<h5>Name #1</h5>
</div>
</div>
<div class="grid-house" data-img="images/bg.jpg">
<div class="house-grid-content">
<h5>Name #2</h5>
</div>
</div>
<div class="grid-house" data-img="images/bg.jpg">
<div class="house-grid-content">
<h5>Name #3</h5>
</div>
</div>
</section>
css
.home-grid {
background-color: #0f0f0f;
display: inline-block;
padding: 0px;
}
.grid-house {
display: block;
width: 33.02vw;
height: 33.02vw;
}
.grid-house .house-grid-content {
display: flex;
justify-content: center;
align-content: center;
flex-direction: column;
height: 100%;
padding-left: 20%;
padding-right: 20%;
position: relative;
z-index: 999;
}
@media only screen and (max-width: 991px) {
.grid-house {
width: 50vw;
height: 50vw;
}
}
@media only screen and (max-width: 767px) {
.grid-house {
width: 100vw;
height: 100vw;
}
}
编辑: 我的头部设置带有视口元标记。
tacoshy的解决方案,非常感谢
I am trying to create a design with three equal sized squares next to one another.
What I want is that at screens with a resolution above 991px width to have 3 squares next to another.
If the users screen width is below 991px and above 767px I want him or her to see two squares per row :
And when the users screen resolution is below 767px I want him or her to see one image per row.
That I have so far accomplished.
Since my result is :
Now where my issue starts.
I have tested this by using the FireFox inspection tool. Scaling the screen resolution and adjusting my HTML & CSS code.
And it looks perfect.
However when I try to check my result in a windowed browserscreen all of a sudden the scaling is off.
And I have almost immediately two screens next to one another. But an empty field on the right.
As you can see on the following screen :
My code :
HTML
<section class="home-grid">
<div class="grid-house" data-img="images/bg.jpg">
<div class="house-grid-content">
<h5>Name #1</h5>
</div>
</div>
<div class="grid-house" data-img="images/bg.jpg">
<div class="house-grid-content">
<h5>Name #2</h5>
</div>
</div>
<div class="grid-house" data-img="images/bg.jpg">
<div class="house-grid-content">
<h5>Name #3</h5>
</div>
</div>
</section>
CSS
.home-grid {
background-color: #0f0f0f;
display: inline-block;
padding: 0px;
}
.grid-house {
display: block;
width: 33.02vw;
height: 33.02vw;
}
.grid-house .house-grid-content {
display: flex;
justify-content: center;
align-content: center;
flex-direction: column;
height: 100%;
padding-left: 20%;
padding-right: 20%;
position: relative;
z-index: 999;
}
@media only screen and (max-width: 991px) {
.grid-house {
width: 50vw;
height: 50vw;
}
}
@media only screen and (max-width: 767px) {
.grid-house {
width: 100vw;
height: 100vw;
}
}
Edit:
I have the head setup with the viewport meta-tag.
Solution by tacoshy, many thanks for that
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我感到困惑的是,您会谈论abotu
网格
,但从未真正使用网格,而是inline-block
。使用实际的CSS-GRID
或flexbox
将更容易IMHO。对于bot,
css-grid
和flexbox
我删除了.grid-house
的CSS。尤其是将硬编码为正方形的高度和宽度设置为无效。使用faction-ratio
-property或此方法。在此Methode中使用CSS-GRID
在容器中使用
display-grid
,并应用3列与`grid-template-columns:repot:repot(3,1fr);等于每个列的空间(作为1个分数)。在Medi查询中,我以相同的方式将列设置为2或1列。使用Flexbox:
只需使用
显示:Flex;
在容器上,然后将其与flex-wrap:wrap
组合。然后将.grid-house
的宽度设置为calc(100%/3)
(对于像素精度)或50%
或<代码> 100%带有更有用的功能,然后vw/vh
将容器宽度而不是屏幕宽度。使用vw
是导致令人讨厌的oevrflow并发行的原因。What confuses me is, that you talk abotu
Grid
but never actually use a grid butinline-block
. Using an actualCSS-Grid
orFlexbox
would be easier IMHO.For bot,
CSS-Grid
andFlexbox
I removed your CSS for.grid-house
. Espacially setting the height and width hardcoded to be a square is ineffective. It is easier to useaspect-ratio
-property or this methode.Using CSS-Grid
In this methode I use
display-grid
for the container and apply 3 columns with `grid-template-columns: repeat(3, 1fr); which equaly distribute the space (as 1 fraction) to every column. In the medi query I set the columns the same way either to 2 or 1 column.Using Flexbox:
Just use
display: flex;
on the container and combine it withflex-wrap: wrap
. Then set the width of the.grid-house
to eithercalc(100% / 3)
(for pixel accuracy) or50%
or100%
with is more usefull thenvw/vh
as it takes the container width instead of the screen width. Usingvw
is what causing the nasty oevrflow and issue you having.