The space between the items continues to be left...

Asked 2 years ago, Updated 2 years ago, 94 views

Prerequisites/What you want to achieve

Flex is used to align divs like menus, but they don't line up well

Problems/Error Messages you are experiencing

The error message is not specific.

Source Codes Affected

<div class="fluid-wrap">
  <div class="el"></div>
  <div class="el"></div>
</div>


body{
  margin:0;display:flex;justify-content:center;
}

.fluid-wrap{
  width —80%;
  background:brown;
  height:100vh;display:flex;

  flex-wrap:wrap;
}

div>div{
    flex: 100%;
}

.el{
  height —20px;
  background: #ffa;
}

Tried

I thought the align-items might be related, so I modified the align to start, but I couldn't see any special changes

Supplementary information (for example, FW/Tool Version)

pass —4.11.0
windows:10

css sass

2022-09-29 22:13

1 Answers

In this case, handling the margins between flex items is a problem, not the placement of flex items.In other words, you must consider the value of the align-content property instead of the align-items property.

According to the CSS Flexible Box Layout Module Level 1, the initial value of the align-content property is stretch.This value inserts the margins evenly so that the sum of each item on the cross axis is the dimensions of the flex container.

8 8.4. Packing Flex Lines:the align-content property [1]

[1]
  • Name:align-content
  • Value: flex-start | flex-end | center | space-between | space-around | stretch
  • Initial:stretch
  • Applies to:multi-line flex containers
  • Inherited:no
  • Percentages:n/a
  • Computed value:specified keyword
  • Canonical order:pergrammar
  • Animation type:discrete

Therefore, in order to do what the questioner wants to do, the CSS needs to be modified as follows:

body{
  margin:0;
  display:flex;
  justify-content:center;
}

.fluid-wrap{
  width —80%;
  background:brown;
  height —100vh;
  display:flex;
  flex-wrap:wrap;
  align-content:flex-start; /*addition */
}

div>div{
  flex: 100%;
}

.el{
  height —20px;
  background: #ffa;
}
<div class="fluid-wrap">
  <div class="el"></div>
  <div class="el"></div>
</div>


2022-09-29 22:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.