【问题标题】:Creating a responsive flex box with same size images创建具有相同大小图像的响应式弹性框
【发布时间】: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

标签: html css flexbox


【解决方案1】:

这是一个简单而漂亮的guide to flexbox content。我认为你有一些混合(css 和 html 中的类)。

尝试使图像具有相同大小的另一件事是:

.image-container{
    width:100px;
}
.image-container img {
    width:100%;
    height:100px;
    object-fit:cover;
} 

(图像高度必须固定才能正常工作)。

【讨论】:

  • 欢迎堆栈溢出。我建议对您的帖子进行编辑以改进代码示例的格式。我还可以建议您在答案中总结链接中的关键信息,以帮助提问者和其他访问者理解它(在链接中断的情况下特别有用)。这里有如何回答的指南:stackoverflow.com/help/how-to-answer
猜你喜欢
  • 2019-02-12
  • 1970-01-01
  • 2018-10-02
  • 1970-01-01
  • 1970-01-01
  • 2019-08-05
  • 2018-09-07
  • 2023-04-10
  • 1970-01-01
相关资源
最近更新 更多