目次
「display: flex」属性から始めます
ホームページ ウェブフロントエンド htmlチュートリアル 柔軟なレイアウトを使用して、よく使用される 10 個の UI コンポーネントを作成_html/css_WEB-ITnose

柔軟なレイアウトを使用して、よく使用される 10 個の UI コンポーネントを作成_html/css_WEB-ITnose

Jun 21, 2016 am 08:48 AM

「display: flex」属性から始めます

Flexbox CSS の基本属性は display: flex です。この属性を要素に追加するだけで、フレキシブル レイアウトが使用できます。それはとても簡単です!

この属性はどのような役割を果たしているのかと疑問に思われるかもしれません。デフォルトでは、コンテナ内の子要素が主軸 (デフォルトでは水平) に沿って配置されるように指定されます。

次に、2 つの例を使用して、この属性が UI コンポーネントでどのように使用されるかを紹介します。

### ステップ カウンターのデモ

最初に紹介する例は、ステップ入力ボックス コンポーネントです。 JavaScript コードをバインドした後、+ / - 記号をクリックすると、入力ボックスの値がそれに応じて増減します。

訳: ここでのカウンターの実装では、JavaScript の代わりに CSS カウンターを使用することもできます。具体的な例については、CSS カウンターインクリメントを使用して作成されたゲームを参照してください。

前のアイデアによれば、このインライン スタイルを実装するには、display: inline-block または float: left を使用することを選択できます。ただし、これを行うには、これらの各要素に特定のセレクターを追加する必要もあります。フレックス ボックスを使用して、display: flex 属性をコンテナに追加するだけで同じ効果を実現できます。

この属性を追加すると、要素に対して「おい、フレックスボックス ルールに従って子要素を配置する責任を負ってください!」と言うのと同じになります。最も基本的な形式では、要素は主軸 (デフォルトでは X 軸) に沿って左から右に配置されます。

CSS コード

```CSS

.stepperInput {

/**

* 表示を flex に設定すると、このコンテナは flexbox を使用して子を配置します

*。デフォルトでは、

* 項目は上揃えで水平方向に並べられます。

* これは、子

* に display: inline-block を設定するのと同様の効果があります。

*/

表示: flex;

}

.stepperInput__input {

ボーダー左: 0;

ボーダー右: 0;

width: 60px;

text-align: center;

}

.button {

カーソル: ポインター;

パディング: 5px 15px;

色: #FFFFFF;

背景色: #4EBBE4;

フォントサイズ: 12px ;

border: 1px ソリッド #16A2D7;

border-radius: 4px;

}

.button–addOnLeft {

ボーダー右上半径: 0;

ボーダー右下半径: 0;

}

.button–addOnRight {

ボーダー左上半径: 0;

ボーダー左下半径: 0;

}

.input {

border: 1px Solid #D7DBDD;

padding: 0 10px;

border-radius: 0;

box-shadow: none;

}

```

HTML コード````HTML

<a name="tabs"></a>### 标签页 [Demo](http://www.flexboxpatterns.com/tabs)![image](http://www.flexboxpatterns.com/images/thumbs/tabs.png)同理,使用 `display: flex` 也可以实现这种标签页效果。CSS 代码```CSS.tabs {  /**   * Setting display to flex makes this container lay   * out its children using flexbox, the exact same   * as in the above "Stepper input" example.   */  display: flex;  border-bottom: 1px solid #D7DBDD;}.tab {  cursor: pointer;  padding: 5px 30px;  color: #16A2D7;  font-size: 12px;  border-bottom: 2px solid transparent;}.tab.is-tab-selected {  border-bottom-color: #4EBBE4;}
ログイン後にコピー

HTML code```HTML

タブ 1

タブ 2

タブ 3

タブ 4

_译注:tab 的切换效果可以通过纯 CSS 实现,可以参考 [CSS trick for tabs without javascript demo](http://poppinlp.com/demos/tabs-without-javascript.html)_<a name="site-header"></a>### 站点头部 [Demo](http://www.flexboxpatterns.com/site-header)![image](http://www.flexboxpatterns.com/images/thumbs/siteHeader.png)你可以把这种组件作为网站或者 webapp 的顶部。要构建这种组件,典型方法是把 logo 和导航按钮包裹在一个容器,设置按钮放在另一个容器中。然后,使用 `float` 把两个容器方别推向两边。紧接着,你要对整个顶部元素清除浮动。对于一个简单布局而言,这也太复杂了。如果使用弹性盒子,你依然需要容器元素,但是你不需要为容器添加特定的样式了,因为 flexbox 已经让父元素来负责布局了。除开 `display: flex` 属性外,我们还需要另外两个属性来调整布局:* `justify-content: space-between` 让容器左右分开成为了可能。这个属性把 logo 和导航按钮推向了左边,把设置推向了右边。* `align-items: center` 讲主轴上的元素垂直居中对齐。这个属性很重要,因为 logo 和导航按钮高度不同,如果不设置该属性,它们默认会对齐上边沿。```CSS.siteHeader {  /**   * Lay out the children of this container with   * flexbox, which is horizontal by default.   */  display: flex;  /**   * Make the container put as much space as possible   * between its children, with the children at either   * end laying flush against the container's edges.   */  justify-content: space-between;  padding: 10px;  background-color: #56727C;}.siteHeader__section {  /**   * Lay out the children of this container with   * flexbox.   */  display: flex;  /**   * Align the children in the center, along   * the main axis. By default the children will   * align along their top edges.   */  align-items: center;}.siteHeader__item {  padding: 5px 15px;  font-size: 12px;}.siteHeader__item + .siteHeader__item {  margin-left: 5px;}.siteHeader__item.is-site-header-item-selected {  color: #FFFFFF;  background-color: #415F69;  border-radius: 4px;}.siteHeaderLogo {  font-size: 20px;  line-height: 0;  color: white;}.siteHeaderButton {  cursor: pointer;  color: #D9E9EF;}
ログイン後にコピー

<div class="siteHeader">  <!-- This section gets pushed to the left side-->  <div class="siteHeader__section">    <div class="siteHeader__item siteHeaderLogo">      <div class="fa fa-inbox"></div>    </div>    <div class="siteHeader__item siteHeaderButton is-site-header-item-selected">Inbox</div>    <div class="siteHeader__item siteHeaderButton">Sent</div>    <div class="siteHeader__item siteHeaderButton">Trash</div>  </div>  <!-- This section gets pushed to the right side-->  <div class="siteHeader__section">    <div class="siteHeader__item siteHeaderButton">Settings</div>  </div></div>
ログイン後にコピー

### フォームの下部にデモ

進捗状況のサークルまたは検証フィードバックを配置する必要がある場合がありますフォームの下部にあります。上の例と同様に、フレックスボックスを使用して、このコンテンツをフォーム送信ボタンから分離します。

CSS コード

```CSS

.formFooter {

/**

* このコンテナの子を

* フレックスボックスでレイアウトします。デフォルトでは水平になります。

*/

表示: flex;

/**

* 主軸 (この場合は水平)

* に沿って、子を中央に配置します。

*/

align-items: center;

/* *

* コンテナの子の間にはできるだけ多くのスペースを置きます

* のいずれかの端に子を置き、

* コンテナの端と面一になるように配置します。

*/

justify-content: space-between;

border-top: 1px ソリッド #D7DBDD;

padding: 10px ;

}

.formFooter__section {

/**

* このコンテナはアイテムを水平に並べます。

*/

display: flex;

/**

* 縦方向中央に揃えます。

*/

align-items: center;

}

.formFooter__item + .formFooter__item {

margin-left: 5px;

}

.formFooterFeedback {

color: #86969C;

font-size: 12px; >アニメーション: formFooterSpinner 1s 無限ステップ(8, end);

}

@keyframes formFooterSpinner {

0% { 変換: 回転(0度) }

100% { transform: rotate(360deg); }

}

.button–clear {

color: #16A2D7;

background-color: #FFFFFF;

border: 1px solid #FFFFFF;

}

```

HTML 代码```HTML

Saving...

Reset

Save

<a name="sidebar"></a>### 侧边栏 [Demo](http://www.flexboxpatterns.com/side-bar)![image](http://www.flexboxpatterns.com/images/thumbs/sideBar.png)这也是一个相当经典的组件。你甚至不需要 flexbox 来构造它,div 就足够了…… 不过,如果设计师让你把其中一个按钮单独拿到下面,你可以用 `position: absolute` 来处理,当然也可以用 flexbox。除了 `display: flex` 和 `justify-content: space-between`,我们还需要另一个属性:* `flex-direction: column` 会将主轴 (main axis) 从水平方向改为垂直方向。正如你所猜测的那样,现在子元素将会垂直排列了。CSS 代码```CSS.sideBar {  /**   * This container orders items according to flexbox   * rules. By default, this would arrange its children   * horizontally. However, the next property...   */  display: flex;  /**   * ...sets the main axis to be vertical instead of   * horizontal, so now the children are laid out   * vertically, from top to bottom.   */  flex-direction: column;  /**   * It will also put as much space as possible   * between its children, with the children at either end   * laying flush against the container's edges.   */  justify-content: space-between;  height: 300px;  width: 150px;  border-right: 1px solid #D7DBDD;  background-color: #FCFDFD;  padding: 10px;}.sideBar__item {  cursor: pointer;  padding: 5px 10px;  color: #16A2D7;  font-size: 12px;}.sideBar__item.is-side-bar-item-selected {  background-color: #EEF3F5;  border-radius: 4px;}
ログイン後にコピー

HTML 代码```HTML

Inbox

Contacts

Account

Legal

<a name="centered-propmt"></a>### 居中提示 [Demo](http://www.flexboxpatterns.com/centered-prompt)![image](http://www.flexboxpatterns.com/images/thumbs/centeredPrompt.png)不使用 flexbox 的居中布局通常需要一些 hack 技巧,比如使用绝对布局然后通过 2D transform 平移。使用 flexbox,你只需要在容器元素设置 4 个属性:* `display: flex`* `flex-direction: column`* `align-items: center` 设置或检索弹性盒子元素在侧轴(纵轴)方向上的对齐方式。在这里和设置 `text-align: center` 的效果有点类似。* `justify-content: center` 子元素向主轴中心紧挨靠拢。CSS 代码```CSS.centeredPrompt {  /**   * Lay out the children of this container with   * flexbox.   */  display: flex;  /**   * Rotate the main axis so that the children   * are laid out vertically, the same as in the   * above "Side bar" example.   */  flex-direction: column;  /**   * Align the children in the center, along   * the main axis. Because the direction is   * "column" this has a similar effect as setting   * text-align: center.   */  align-items: center;  /**   * Instead of pushing the children apart, as in   * previous examples, we will group them together   * in the center of their container.   */  justify-content: center;  min-height: 300px;  padding: 10px;}.centeredPrompt__item + .centeredPrompt__item {  margin-top: 5px;}.centeredPromptIcon {  font-size: 60px;  line-height: 0;}.centeredPromptLabel {  color: #86969C;  font-size: 30px;  font-weight: 700;  text-align: center;}.centeredPromptDetails {  color: #86969C;  font-size: 16px;  margin-bottom: 10px;  text-align: center;}.icon {  color: #BCD2DA;}
ログイン後にコピー

HTML 代码```HTML

Welcome to the app!

Thanks for signing up. Let’s take a look around.

Begin tour

<a name="centered-icon"></a>### 居中图标 [Demo](http://www.flexboxpatterns.com/centered-icon)![image](http://www.flexboxpatterns.com/images/thumbs/centeredIcon.png)使用同样的方法,我们还可以居中更多东西,比如图标。CSS 代码```CSS.centeredIcon {  /**   * Lay out the children of this container with   * flexbox.   */  display: flex;  /**   * As in the above "Centered prmopt" example,   * we'll rotate the main axis so that the children   * are ordered vertically.   */  flex-direction: column;  /**   * And just like in that example, align the children   * in the center, along the main axis.   */  align-items: center;  /**   * Same thing here as before: group the children   * together in the center of their container.   */  justify-content: center;  border: 1px solid #D7DBDD;  font-size: 40px;  width: 90px;  height: 90px;  border-radius: 100%;  box-shadow:    0 2px 1px rgba(0, 0, 0, 0.05),    0 2px 3px rgba(0, 0, 0, 0.05),    0 4px 8px rgba(0, 0, 0, 0.05);}
ログイン後にコピー

HTML 代码```HTML

<a name="feature-list"></a>### 功能列表 [Demo](http://www.flexboxpatterns.com/feature-list)![image](http://www.flexboxpatterns.com/images/thumbs/featureList.png)在这个列表中,其中一行是反向排列的,实现方法很简单,只需要添加一个 flexbox 属性:`flex-direction: row-reverse` 把容器中的子元素按照 HTML 中的顺序反向排列。CSS 代码```CSS.featureListItem {  /**   * Lay out the children of this container with   * flexbox, which is horizontal by default.   */  display: flex;  /**   * Align the children in the center, along   * the main axis, which is horizontal in this case.   */  align-items: center;  max-width: 400px;  padding: 10px;}.featureListItem + .featureListItem {  border-top: 1px solid #D7DBDD;}.featureListItem--reverse {  /**   * We can specify the flex-direction so that   * the children elements are displayed in the   * reverse order of how they appear in the   * markup.   */  flex-direction: row-reverse;}.featureListItem__icon,.featureListItem__description {  padding: 5px 15px;}.featureListItem__icon {  font-size: 50px;  line-height: 0;}.featureListItem__description {  color: #86969C;  font-size: 12px;}
ログイン後にコピー

HTML 代码```HTML

The calendar feature makes scheduling a snap.

Our dashboard gives you a bird’s-eye-view-at-a-glance-on-the-go.

You can get notified by email or SMS.

<a name="card"></a>### 卡片 [Demo](http://www.flexboxpatterns.com/card)![image](http://www.flexboxpatterns.com/images/thumbs/card.png)接下来复习一下,这个卡片的组件用到了此前提到的属性。* display: flex* flex-direction: column* align-items: center* justify-content: center这里没什么新的知识点,但是为我们接下来要介绍的卡片组做了铺垫。CSS 代码```CSS.card {  /**   * Lay out the children of this container with   * flexbox, which is horizontal by default.   */  display: flex;  /**   * Rotate the main axis so that the children   * are laid out vertically.   */  flex-direction: column;  border: 1px solid #CAD0D2;  border-radius: 4px;  overflow: hidden;}.card__description {  /**   * Lay out the children of this container with   * flexbox.   */  display: flex;  /**   * We're going to lay out this element's children   * just like in the "Centered prompt" example.   * First we'll rotate the main axis so that the   * children are laid out vertically.   */  flex-direction: column;  /**   * Just like in the "Centered prompt" example,   * we'll align the children in the center, along   * the main axis.   */  align-items: center;  /**   * And just like in the "Centered prompt", we'll   * group the children in the center of their   * container.   */  justify-content: center;  padding: 15px;}.card__descriptionIcon {  font-size: 32px;  margin-bottom: 10px;}.card__descriptionText {  color: #57727C;  font-size: 12px;  text-align: center;}.card__price {  text-align: center;  color: #57727C;  font-size: 12px;  font-weight: 700;  padding: 5px 15px;  border-top: 1px solid #D7DBDD;  background-color: #EEF3F5;}.card--fixedWidth {  max-width: 120px;}
ログイン後にコピー

HTML 代码```HTML

Science potion

Costs $5

<a name="card-group"></a>### 卡片组 [Demo](http://www.flexboxpatterns.com/card-group)![image](http://www.flexboxpatterns.com/images/thumbs/cardGroup.png)现在我们来学习最后一个 “卡片组” 组件。我们需要考虑两个麻烦的地方:1. 我们想要每个卡片的宽度相等,但是里面包含的内容多少可能各不相同。2. 我们想要所有的卡片注释高度相等,同样,每张卡片的注释长度也不相等。不适用 flexbox,你可能会想到使用 `table` 元素来实现这些需求,或者对所有元素使用绝对布局,并组合使用百分比、像素和 calc() 来实现。这样也太复杂了。使用弹性盒子,整个问题的解决方案就变得优雅多了。这里我们引入一个新的属性,但是和之前我们提到的属性不同,这个属性应用在 **子元素** 而不是容器。我们为每张卡片设置 `flex: 1 1 0`,使得每张卡片的宽度相等。这个属性是以下三个属性的简写方法:* `flex-grow: 1` 设置或检索弹性盒的扩展比率。默认值为 0,我们设置为 1,此时卡片会尽可能扩展以填充容器。* `flex-shrink: 1` 设置或检索弹性盒的收缩比率。默认值为 1。我们设置为 1,此时卡片会尽可能收缩以适应容器。* `flex-basis: 0` 设置或检索弹性盒伸缩基准值。设置为 0 的时候,其大小仅由容器的大小所决定。这些属性满足了第一点要求。对于我们的第二个要求,我们可以对 flex 属性稍作调整,改成 `flex: 1 1 auto`。* `flex-basis: auto` 导致卡片注释的高度自适应,意思就是最长的注释框拥有最大的高度。由于注释同时拥有 `flex-grow: 1` 属性,因此较短注释的卡片会自动伸长来适配容器高度。CSS 代码```CSS.cardGroup {  /**   * Lay out the children of this container with   * flexbox, which is horizontal by default.   */  display: flex;  border: 1px solid #CAD0D2;  border-radius: 4px;  overflow: hidden;}.cardGroup__card {  /**   * The flex property is a short-hand for setting   * the flex-shrink, flex-grow, and flex-basis   * properties. These properties control how the   * element resizes to fill its container.   *   * We'll also set flex-grow to 1 so that it   * will expand to fill its container. (The   * default value is 0.)   *   * We'll set flex-shrink to 1 so that the element   * will shrink as its container gets smaller.   * (The default value is 1.)   *   * Last, we set flex-basis to 0 so that its   * size is solely determined by the size of   * the container. (The default value   * is auto, which would cause the content's   * size to also be a factor in this calculation.)   *   * The net result of these properties is that the   * element is a fluid size, and will expand and   * shrink with its container and siblings, but   * they will all have the same size, even if they   * have different amounts of content.   */  flex: 1 1 0;  border: none;  border-radius: 0;}.cardGroup__card + .cardGroup__card {  border-left: 1px solid #D7DBDD;}.cardGroup__cardDescription {  /**   * We're doing almost the exact same thing here as   * we did above. The difference is that its   * flex-basis is auto, so now the size of its content   * will affect how large it is.   */  flex: 1 1 auto;}
ログイン後にコピー

HTML 代码```HTML

Trial

Free!

Basic tier(most popular)

$10.00

Advanced tier(only for enterprise-level professionals)

$6,000.00

```

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

&lt; Progress&gt;の目的は何ですか 要素? &lt; Progress&gt;の目的は何ですか 要素? Mar 21, 2025 pm 12:34 PM

この記事では、HTML&lt; Progress&gt;について説明します。要素、その目的、スタイリング、および&lt; meter&gt;との違い要素。主な焦点は、&lt; Progress&gt;を使用することです。タスクの完了と&lt; Meter&gt; statiの場合

&lt; datalist&gt;の目的は何ですか 要素? &lt; datalist&gt;の目的は何ですか 要素? Mar 21, 2025 pm 12:33 PM

この記事では、HTML&lt; Datalist&GT;について説明します。オートコンプリートの提案を提供し、ユーザーエクスペリエンスの改善、エラーの削減によりフォームを強化する要素。

HTML5のクロスブラウザー互換性のベストプラクティスは何ですか? HTML5のクロスブラウザー互換性のベストプラクティスは何ですか? Mar 17, 2025 pm 12:20 PM

記事では、HTML5クロスブラウザーの互換性を確保するためのベストプラクティスについて説明し、機能検出、プログレッシブエンハンスメント、およびテスト方法に焦点を当てています。

&lt; meter&gt;の目的は何ですか 要素? &lt; meter&gt;の目的は何ですか 要素? Mar 21, 2025 pm 12:35 PM

この記事では、html&lt; meter&gt;について説明します。要素は、範囲内でスカラーまたは分数値を表示するために使用され、Web開発におけるその一般的なアプリケーション。それは差別化&lt; Meter&gt; &lt; Progress&gt;およびex

HTML5フォーム検証属性を使用してユーザー入力を検証するにはどうすればよいですか? HTML5フォーム検証属性を使用してユーザー入力を検証するにはどうすればよいですか? Mar 17, 2025 pm 12:27 PM

この記事では、ブラウザのユーザー入力を直接検証するために、必要、パターン、MIN、MAX、および長さの制限などのHTML5フォーム検証属性を使用して説明します。

HTML5&lt; time&gt;を使用するにはどうすればよいですか 日付と時刻を意味的に表す要素? HTML5&lt; time&gt;を使用するにはどうすればよいですか 日付と時刻を意味的に表す要素? Mar 12, 2025 pm 04:05 PM

この記事では、html5&lt; time&gt;について説明します。セマンティックデート/時刻表現の要素。 人間の読み取り可能なテキストとともに、マシンの読みやすさ(ISO 8601形式)のDateTime属性の重要性を強調し、Accessibilitを増やします

ビューポートメタタグとは何ですか?レスポンシブデザインにとってなぜそれが重要なのですか? ビューポートメタタグとは何ですか?レスポンシブデザインにとってなぜそれが重要なのですか? Mar 20, 2025 pm 05:56 PM

この記事では、モバイルデバイスのレスポンシブWebデザインに不可欠なViewportメタタグについて説明します。適切な使用により、最適なコンテンツのスケーリングとユーザーの相互作用が保証され、誤用が設計とアクセシビリティの問題につながる可能性があることを説明しています。

&lt; iframe&gt;の目的は何ですか タグ?使用する際のセキュリティ上の考慮事項は何ですか? &lt; iframe&gt;の目的は何ですか タグ?使用する際のセキュリティ上の考慮事項は何ですか? Mar 20, 2025 pm 06:05 PM

この記事では、&lt; iframe&gt;外部コンテンツをWebページ、その一般的な用途、セキュリティリスク、およびオブジェクトタグやAPIなどの代替案に埋め込む際のタグの目的。

See all articles