搜尋
首頁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="/static/imghwm/default1.png" data-src="profile.jpg" class="lazy"   style="max-width:90%" 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
將框陰影添加到WordPress塊和元素將框陰影添加到WordPress塊和元素Mar 09, 2025 pm 12:53 PM

CSS盒子陰影和輪廓屬性獲得了主題。讓我們查看一些在真實主題中起作用的示例,以及我們必須將這些樣式應用於WordPress塊和元素的選項。

使用GraphQL緩存使用GraphQL緩存Mar 19, 2025 am 09:36 AM

如果您最近開始使用GraphQL或審查了其優點和缺點,那麼您毫無疑問聽到了諸如“ GraphQl不支持緩存”或

使您的第一個自定義苗條過渡使您的第一個自定義苗條過渡Mar 15, 2025 am 11:08 AM

Svelte Transition API提供了一種使組件輸入或離開文檔(包括自定義Svelte Transitions)時動畫組件的方法。

優雅且酷的自定義CSS捲軸:展示櫃優雅且酷的自定義CSS捲軸:展示櫃Mar 10, 2025 am 11:37 AM

在本文中,我們將深入研究滾動條。我知道,這聽起來並不魅力,但請相信我,一個精心設計的頁面是齊頭並進的

展示,不要說展示,不要說Mar 16, 2025 am 11:49 AM

您花多少時間為網站設計內容演示文稿?當您撰寫新的博客文章或創建新頁面時,您是在考慮

使用Redwood.js和Fauna構建以太坊應用使用Redwood.js和Fauna構建以太坊應用Mar 28, 2025 am 09:18 AM

隨著最近比特幣價格超過20k美元的攀升,最近打破了3萬美元,我認為值得深入研究創建以太坊

NPM命令是什麼?NPM命令是什麼?Mar 15, 2025 am 11:36 AM

NPM命令為您運行各種任務,無論是一次性或連續運行的過程,例如啟動服務器或編譯代碼。

讓我們使用(x,x,x,x)來談論特殊性讓我們使用(x,x,x,x)來談論特殊性Mar 24, 2025 am 10:37 AM

前幾天我只是和埃里克·邁耶(Eric Meyer)聊天,我想起了我成長時代的埃里克·邁耶(Eric Meyer)的故事。我寫了一篇有關CSS特異性的博客文章,以及

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SublimeText3 Mac版

SublimeText3 Mac版

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

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。