📜  felx-wrap css (1)

📅  最后修改于: 2023-12-03 15:15:03.112000             🧑  作者: Mango

CSS Flex-wrap

CSS Flex-wrap is a property that determines whether items within a flex container should wrap or not.

Syntax
.flex-container {
  flex-wrap: nowrap|wrap|wrap-reverse;
}
Values
  • nowrap: all flex items will be on one line, which may cause overflow if the parent flex container is not wide enough.
  • wrap: flex items will wrap onto a new line if there is no space left on the current line.
  • wrap-reverse: flex items will wrap onto a new line in reverse order, starting from the end of the container.
Usage

Flex-wrap is often used in combination with the flex-direction property to control the direction of the flex container and ensure that the flex items wrap as desired.

.flex-container {
  display: flex;
  flex-direction: row; /* or column */
  flex-wrap: wrap;
}

In this example, the flex-container is set to display: flex to make it a flex container. The flex-direction property determines whether the flex items should be arranged horizontally (row) or vertically (column). Finally, the flex-wrap property ensures that flex items will wrap onto a new line if there is not enough space on the current line.

Example

Consider the following HTML:

<div class="flex-container">
  <div class="flex-item">Item 1</div>
  <div class="flex-item">Item 2</div>
  <div class="flex-item">Item 3</div>
  <div class="flex-item">Item 4</div>
  <div class="flex-item">Item 5</div>
</div>

And the following CSS:

.flex-container {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center;
}

.flex-item {
  background-color: #eee;
  margin: 10px;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}

This will result in a flex container with five items that wrap onto a new line when there is not enough space on the current line. The justify-content property centers the items within the flex container.

flex-wrap example

Conclusion

CSS Flex-wrap is a powerful tool for controlling how flex items should wrap within a flex container. It can be used in combination with other flex properties to create flexible, responsive layouts that adapt to different screen sizes and device orientations.