首頁 web前端 css教學 了解 CSS 中的 Flex 屬性

了解 CSS 中的 Flex 屬性

Sep 12, 2024 am 10:16 AM

Einführung:

Eine Website zu entwerfen ist wie Kunst zu schaffen. So wie die Anordnung eines Gemäldes eine Geschichte erzählt, sagt das Layout einer Website mehr über ihre Botschaft aus. Flexbox ist ein leistungsstarkes Tool, um dies zu erreichen.

Vor Flexbox mussten Entwickler Techniken wie schwebende Elemente, Rand-Hacks und Tabellenlayouts verwenden, um responsive Designs zu erstellen. Diese Methoden funktionierten, waren jedoch nicht skalierbar und erforderten zusätzliche Medienabfragen zur Anpassung an unterschiedliche Bildschirmgrößen.

Flexbox hat dies geändert, indem es eine einfache und effiziente Möglichkeit bietet, Elemente innerhalb eines Containers auszurichten, zu verteilen und ihre Größe anzupassen.

Was ist Flexbox?

Flexbox ist ein Layoutmodell, mit dem Sie auf einfache Weise komplexe Layouts erstellen können. Damit können Sie Elemente innerhalb eines Containers horizontal oder vertikal ausrichten. Flexbox ist eindimensional, das heißt, es steuert das Layout jeweils entlang einer einzelnen Achse (entweder horizontal oder vertikal).

  1. Horizontale Ausrichtung: Sie können Elemente problemlos nebeneinander in Reihen ausrichten.
  2. Vertikale Ausrichtung: Elemente in Spalten ausrichten.

In diesem Blog befassen wir uns mit den wichtigsten Eigenschaften von Flexbox und wie sie die Layoutverwaltung vereinfachen.

Flex-Container-Eigenschaften:

Bevor wir uns mit den Flex-Eigenschaften befassen, ist es wichtig, die beiden Achsen von Flexbox zu verstehen:

  1. Hauptachse
  2. Kreuzachse

Das Verständnis dieser Achsen ist wichtig, da einige Eigenschaften Elemente entlang der Hauptachse ausrichten, während andere sie entlang der Querachse ausrichten. Wenn Sie dies wissen, können Sie besser verstehen, wie die Eigenschaften funktionieren.

Understanding Flex Properties in CSS

Flex-Richtung: Reihe | Spalte

Bei Flexbox geht es darum, Elemente in Zeilen oder Spalten auszurichten. Standardmäßig ist es auf row.

eingestellt
  • Zeile: Richtet Elemente horizontal aus (Standard).
  • Spalte: Richtet Elemente vertikal aus.
<div style="display: flex; flex-direction: row;">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
</div>
登入後複製

Flex-Wrap:nowrap | wickeln | Wrap-Reverse

Dadurch können flexible Artikel in die nächste Zeile eingewickelt werden, anstatt sie zu verkleinern. Der Standardwert ist nowrap.

  • nowrap: Alle Flex-Elemente stehen in einer Zeile (Standard).
  • Umbruch: Flex-Elemente werden in die nächste Zeile umgebrochen, wenn sie nicht passen.
  • Wrap-Reverse: Flex-Artikel werden in die umgekehrte Richtung gewickelt.
<div style="display: flex; flex-wrap: wrap;">
  <div class="box">Item 1</div>
  <div class="box">Item 2</div>
  <div class="box">Item 3</div>
</div>
登入後複製

justify-content:flex-start | Flex-Ende | Mitte | Leerzeichen zwischen | Raum-um | raumgleichmäßig

Wird verwendet, um Elemente entlang der Hauptachse auszurichten. Für Flex-Richtung: Zeile ist die x-Achse die Hauptachse und die y-Achse die Querachse.

  • Flex-Start: Richtet Elemente am Anfang des Containers aus.
  • flex-end: Richtet Elemente am Ende aus.
  • center: Zentriert Elemente.
  • space-between: Verteilt Elemente mit Leerzeichen dazwischen.
  • space-around: Verteilt Elemente mit Platz um sie herum.
  • space-evenly: Verteilt den Platz gleichmäßig zwischen den Elementen.
<div style="display: flex; justify-content: space-between;">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
</div>
登入後複製

align-items: flex-start | Flex-Ende | Mitte | dehnen | Grundlinie

Wird verwendet, um Elemente entlang der Querachse auszurichten. Für die Biegerichtung der Spalte ist die y-Achse die Hauptachse und die x-Achse die Querachse.

  • Flex-Start: Richtet Elemente am Anfang der Querachse aus.
  • flex-end: Richtet Elemente am Ende aus.
  • Mitte: Zentriert Elemente auf der Querachse.
  • Strecken: Dehnt Gegenstände, um den Behälter zu füllen.
  • Grundlinie: Richtet Elemente basierend auf ihrer Textgrundlinie aus.
<div style="display: flex; align-items: center; height: 200px;">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
</div>
登入後複製

Flex-Elementeigenschaften

align-self: flex-start | Flex-Ende | Mitte | dehnen | Grundlinie
Mit der Eigenschaft „align-self“ können Sie ein einzelnes untergeordnetes Element entlang der Querachse ausrichten.

<div style="display: flex; height: 200px;">
  <div style="align-self: flex-start;">Item 1</div>
  <div style="align-self: center;">Item 2</div>
  <div style="align-self: flex-end;">Item 3</div>
</div>
登入後複製

Wachsen und Schrumpfen
3 Eigenschaften, die Sie kennen sollten: Flex-Grow, Flex-Shrink und Flex-Basis.

flex-grow:

This property defines how much a flex item will grow relative to the other items inside a flex container when there is extra space available. By default, flex-grow is set to 0, meaning items won't grow beyond their natural size. Setting flex-grow: 1 allows the item to expand and occupy the remaining available space within the container.

If multiple items have flex-grow:1 applied, they will divide the extra space proportionally, based on the grow values set for each item.

Imagine a dashboard layout where you have a sidebar and a main content area. You want the sidebar to stay fixed in size, but the main content area should expand and take up the remaining space.

<div style="display: flex;">
  <div style="flex-grow: 0; width: 200px;">Sidebar</div> <!-- Fixed width sidebar -->
  <div style="flex-grow: 1;">Main Content Area</div> <!-- Expanding content area -->
</div>
登入後複製

flex-shrink:

When the container size reduces, items inside will also shrink proportionally.

For example, consider a profile card with a rounded image and a name. As the container shrinks, the image may distort, turning from a circle to an oval. To prevent this, you can set flex-shrink: 0, ensuring the image retains its original size while the rest of the content adapts.

<div style="display: flex;">
  <img src="profile.jpg" style="flex-shrink: 0; width: 50px; height: 50px; border-radius: 50%;" alt="Profile Picture"> <!-- Image won't shrink -->
  <div style="flex-shrink: 1;">User Name</div> <!-- Name can shrink -->
</div>
登入後複製

While you might think of using min-width to achieve the same effect, flex-shrink is a more straightforward and flexible approach within the Flexbox algorithm.

  • flex-grow controls how extra space is distributed among items.
  • flex-shrink controls how space is reduced when the container size decreases.

flex-basis: Setting the Initial Size

The flex-basis property defines the initial size of a flex item. If the flex-direction is set to row, flex-basis controls the width of the items. If the flex-direction is column, it controls the height.

flex-basis is similar to the width or height properties, but with one key difference: it only sets the initial size, while allowing the item to grow or shrink depending on the available space and the flex-grow and flex-shrink values.

  • If flex-basis is set to auto or content, the size of the item is based on its content.
  • If you want to define a fixed starting size but still allow the item to grow or shrink, use flex-basis.
.child {
  flex-basis: 25%;  /* Starts at 25% width, but can grow to fill space */
  flex-grow: 1;     /* Will grow to take up more space if available */
}
登入後複製

In this example, the child element initially takes up 25% of the container’s width, but it can grow beyond that if there’s more space available.

Setting a fixed size:

If you want the item to have a fixed size (not grow or shrink), you can use the flex shorthand, like this:

.child {
  flex: 0 0 100px; /* No growth, no shrinking, stays fixed at 100px */
}
登入後複製

This shorthand breaks down as:

  • 0 (flex-grow): The item will not grow.
  • 0 (flex-shrink): The item will not shrink.
  • 100px (flex-basis): The item has a fixed size of 100px.

Using width instead of flex-basis inside a flex layout can lead to issues sometimes.because the item defined with width and won't adjust if the container grows or shrinks, making the layout less responsive. So use it appropriately.

align-content:

We've already learned about flex-wrap. Flex-wrap allows flex items to wrap to the next lines instead of shrinking, right?

Each of these flex lines acts like a separate "mini flex container". We also know that the align-items property is used to align items on the cross axis. Here, this align-items property will work inside this flex line only, because as I mentioned, each line itself is a mini flex container. We also have an outer flex container, right? If we need to align these lines with respect to the outer container, we need one more property that aligns these flex lines on the cross axis. That property is align-content.

.container {
  display: flex;
  flex-wrap: wrap;
  align-content: center;
  height: 300px;
}

.item {
  width: 30%;
  height: 50px;
  background-color: #3498db;
  margin: 5px;
}
登入後複製

In this example, we have a flex container with multiple items that wrap onto multiple lines. The align-content: center; property centers the wrapped lines along the container's cross-axis.

The possible values for align-content include:

  • flex-start: Lines are aligned toward the start of the container.
  • flex-end: Lines are aligned toward the end of the container.
  • center: Lines are centered in the container.
  • space-between: Lines are evenly distributed; the first line is at the start of the container while the last one is at the end.
  • space-around: Lines are evenly distributed with equal space around each line.
  • stretch (default): Lines stretch to take up the remaining space.

Gaps

The gap property was not available in earlier versions of Flexbox. Previously, developers relied on margin properties to create space between flex items. The introduction of the gap property marked a significant improvement in Flexbox functionality.

It provides a straightforward method for creating space between flex items, simplifying the layout process.

.flex-container {
  display: flex;
  gap: 10px; /* Adds a 10px gap between all flex items */
}
登入後複製

Auto margins

margin: auto:

Last but not least, a commonly used spacing trick

The margin: auto property in Flexbox is a powerful tool for aligning flex items. It automatically uses the leftover space around the item, making it useful for centering or pushing items to opposite sides of a container.

For example, you can use margin-left: auto to push an item to the right side of a flex container:

.flex-container {
  display: flex;
}

.push-right {
  margin-left: auto;
}
登入後複製

This technique allows for quick and easy alignment without the need for additional positioning properties.

Conclusion

In this guide, we explored how Flexbox has simplified the task of aligning and distributing items within a webpage. Flexbox isn't just a layout tool—it's a critical skill for any web developer aiming to create responsive, well-structured designs. I hope this guide has helped you understand the power of Flexbox.

Try these demos and feel free to share your thoughts or questions. Thanks!

以上是了解 CSS 中的 Flex 屬性的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1655
14
CakePHP 教程
1414
52
Laravel 教程
1307
25
PHP教程
1255
29
C# 教程
1228
24
Google字體可變字體 Google字體可變字體 Apr 09, 2025 am 10:42 AM

我看到Google字體推出了新設計(Tweet)。與上一次大型重新設計相比,這感覺更加迭代。我幾乎無法分辨出區別

如何使用HTML,CSS和JavaScript創建動畫倒計時計時器 如何使用HTML,CSS和JavaScript創建動畫倒計時計時器 Apr 11, 2025 am 11:29 AM

您是否曾經在項目上需要一個倒計時計時器?對於這樣的東西,可以自然訪問插件,但實際上更多

HTML數據屬性指南 HTML數據屬性指南 Apr 11, 2025 am 11:50 AM

您想了解的有關HTML,CSS和JavaScript中數據屬性的所有信息。

如何通過CSS選擇第一個類名為item的子元素? 如何通過CSS選擇第一個類名為item的子元素? Apr 05, 2025 pm 11:24 PM

在元素個數不固定的情況下如何通過CSS選擇第一個指定類名的子元素在處理HTML結構時,常常會遇到元素個數不�...

為什麼Flex佈局中的紫色斜線區域會被誤認為是'溢出空間”? 為什麼Flex佈局中的紫色斜線區域會被誤認為是'溢出空間”? Apr 05, 2025 pm 05:51 PM

關於Flex佈局中紫色斜線區域的疑問在使用Flex佈局時,你可能會遇到一些令人困惑的現象,比如在開發者工具(d...

使Sass更快的概念證明 使Sass更快的概念證明 Apr 16, 2025 am 10:38 AM

在一個新項目開始時,Sass彙編發生在眼睛的眨眼中。感覺很棒,尤其是當它與browsersync配對時,它重新加載

我們如何創建一個在SVG中生成格子呢模式的靜態站點 我們如何創建一個在SVG中生成格子呢模式的靜態站點 Apr 09, 2025 am 11:29 AM

格子呢是一塊圖案布,通常與蘇格蘭有關,尤其是他們時尚的蘇格蘭語。在Tar​​tanify.com上,我們收集了5,000多個格子呢

在前端開發中,如何使用CSS和JavaScript實現類似Windows 10設置界面的探照燈效果? 在前端開發中,如何使用CSS和JavaScript實現類似Windows 10設置界面的探照燈效果? Apr 05, 2025 pm 10:21 PM

在前端開發中如何實現類似Windows...

See all articles