简体   繁体   English

如何将图像放在 flexbox 中的同一行中

[英]how to place images in the same line inside a flexbox

so for responsiveness, I have created a flexbox and placed some images inside but for some reason, I am not able to place all the images in the same line.所以为了响应,我创建了一个 flexbox 并在其中放置了一些图像,但由于某种原因,我无法将所有图像放在同一行中。 written below is my CSS code.下面写的是我的 CSS 代码。 the container-project is the class for the div in which I am placing all the images and the project-img class is the class for every image placed inside the div. container-project 是我放置所有图像的 div 的 class,而 project-img class 是放置在 div 中的每个图像的 class。

    .container-projects{
      display: flex;
      flex-wrap: wrap;
      justify-content: space-around;
    }
    .project-img{
      width: 25%;
      height: auto;
      border-radius: 30px;
      padding: 20px;
    }

so for responsiveness, I have created a flexbox and placed some images inside but for some reason, I am not able to place all the images in the same line.所以为了响应,我创建了一个 flexbox 并在里面放置了一些图像,但由于某种原因,我无法将所有图像放在同一行。 written below is my CSS code.下面写的是我的 CSS 代码。 the container-project is the class for the div in which I am placing all the images and the project-img class is the class for every image placed inside the div.容器项目是用于放置所有图像的 div 的 class 和项目 img class 是放置在 div 内的每个图像的 class。

    .container-projects{
      display: flex;
      flex-wrap: wrap;
      justify-content: space-around;
    }
    .project-img{
      width: 25%;
      height: auto;
      border-radius: 30px;
      padding: 20px;
    }

I see two issues.我看到两个问题。

One is that you've set flex-wrap to wrap which means that flex items will wrap onto multiple lines, from top to bottom.一种是您将flex-wrap设置为wrap ,这意味着 flex 项目将从上到下换行到多行。 Try setting it to nowrap instead.尝试将其设置为nowrap

The second depends on how many images you're trying to put in the line.第二个取决于您要放入行中的图像数量。 If it's 4 then you're fine, otherwise you want to change the width: 25%;如果它是 4 那么你很好,否则你想改变width: 25%; to a different value as at the moment it will be dividing the width of the containing area in to 4. You might want to look into the flex-basis property instead.到一个不同的值,因为它会将包含区域的宽度分成 4。您可能想要查看flex-basis属性。 It defines the default size of an element before the remaining space is distributed.它定义了分配剩余空间之前元素的默认大小。

Add align-items: center;添加align-items: center;

   .container-projects{
      display: flex;
      flex-wrap: wrap;
      justify-content: space-around;
      align-items: center;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM