【发布时间】:2022-02-11 17:12:21
【问题描述】:
html/CSS 新手,学习 flex 框,我觉得我已经正确地关注了 W3schools 网站 example,我还从 here 获取了一些代码以使其具有响应性,但我无法获取图像视口缩小时换行。
这是我的代码开始: CSS:
* {font-family: arial, sans-serif; box-sizing: border-box;}
html body {margin: 0;}
.flex-container {
display: flex;
margin: 0 .5%;
}
.flex-container > div {
margin: 1%;
}
.whatwecando {padding-top: 125px;
padding-bottom: 15px;
}
.nav {position:flex; top:0; left:0;
background-color: black;
font-size: 20px;
float: left;
border-radius: 0px;
border: none;
width: 100%;
overflow: hidden;
margin: 0;
list-style-type: none;
padding: 25px;
display: flex;
list-style: none;
flex-direction: row;
/* vertical alignement */
align-items: center;
/* how you want horizontal distribution */
/* space-evenly | space-around | space-between */
justify-content: space-evenly;
}
.item {
color: white;
}
.item:first-child {
height: 50px;
line-height: 50px;
}
@media screen and (max-width:500px){
.column {
width: 100%;
}
}
ul {list-style: none}
HTML:
<div class="flex-container">
<div><img src="Images/Printing/Banner.jpg" style="max-width:100%; height: auto;" width="100%" alt="Banners" /></div>
<div><img src="Images/Printing/Banner.jpg" style="max-width:100%; height: auto;" width="100%" alt="Posters" /></div>
<div><img src="Images/Printing/Banner.jpg" style="max-width:100%; height: auto;" width="100%" alt="Flock again" /></div>
当我在容器类中添加flex-wrap: wrap;(这在逻辑上是解决方案)时,所有图像都会垂直排列,因此我将其删除。
我正在尝试创建像网格一样的图像,我没有正确理解 flexbox 吗?
【问题讨论】:
-
可能是因为您的图像的 原始尺寸 - 实际上我看不到任何问题... 当我添加 flex-wrap: wrap; (这在逻辑上是解决方案)在容器类中,所有图像都垂直排列 =>增加浏览器窗口的宽度,您可以看到正确的包装
-
你说得对。我添加了 flex-wrap: wrap;并减少了图像的 % 宽度,但它们仍然垂直对齐。我必须为每列创建一个新容器吗?我很困惑
-
检查一下并尝试更改宽度:jsfiddle.net/zg4vd3st 看看当宽度增加时它们如何在下面一层一层包裹
-
这太奇怪了......它有效。我尝试将我的代码复制到其中并且它可以正常工作。 Dreamweaver 不是一个好用的 IDE 吗?当我通过dreamweaver在chrome上运行代码时,即使在1080p显示视图端口上,它也会显示它们全部垂直对齐:S