Build a Classic Layout FAST in CSS Grid

Mozilla Developer
14 Oct 201908:30

Summary

TLDRこのビデオスクリプトでは、CSS Gridの使い方を簡単に紹介しています。デフォルトのグリッドはブロックフローに似ており、安全に追加できます。グリッドギャップを追加して余白を一貫性のある方法で追加し、Firefoxのグリッドインスペクターを使って視覚化しました。グリッドテンプレート行と列を使って、ヘッダー、ナビゲーション、メインコンテンツ、フッターのサイズを設定し、レイアウトを調整。メディアクエリを使って、画面サイズが大きくなるとレイアウトを変更し、グリッドテンプレートエリアを使ってASCIIアートのようにレイアウトを定義。この方法で、さまざまな画面サイズやコンテキストで複数のレイアウトを設定できます。

Takeaways

  • 🌟 CSS Gridsを使用すると、コードを少なくともすることができるで、興味深いレイアウトを作成できます。
  • 🎨 display: gridをbodyに追加することで、グリッドの基本的な設定を始めることができます。
  • 🔧 grid gapを使用して、グリッド間隔をより一貫した方法で調整できます。
  • 🔎 Firefoxのnightly版でグリッドインスペクターを使用して、グリッドの可視化を確認できます。
  • 📏 grid template rowsを使用して、ページの流れに沿った行のサイズを指定できます。
  • 📐 min-height: 100vhを使用して、ビューポートの高さに合わせたレイアウトを作成できます。
  • 📱 メディアクエリを使用して、画面サイズに応じてレイアウトを調整できます。
  • 👉 grid template columnsを使用して、カラムの幅を設定し、サイドバーやメインスペースを定義できます。
  • 🔄 grid column spanを使用して、ヘッダーやナビゲーションを複数の列や行にまたがらせることができます。
  • 🏷️ grid areaを使用して、要素を特定のグリッド領域に割り当てることができます。
  • 🎨 ASCIIアートのようにgrid template areasを使用して、レイアウトを定義することができます。

Q & A

  • CSS Gridの基本的な機能は何ですか?

    -CSS Gridは、ウェブページのレイアウトを作成するための機能で、グリッドラインを自動的に生成し、グリッドギャップを追加することでブロック要素の間に一貫したスペースを提供します。

  • グリッドギャップを追加するとどうなりますか?

    -グリッドギャップを追加すると、ブロック要素は指定されたスペースだけ離れ、ページの見通しを向上させます。

  • Firefoxのnightly版でグリッドを確認するにはどうすればいいですか?

    -Firefoxのnightly版では、グリッドインスペクターを使用して、グリッドの視覚化を確認できます。

  • グリッドテンプレート行とグリッドテンプレート列はどのように使われますか?

    -グリッドテンプレート行はページを下方向に、グリッドテンプレート列はページを右方向に拡張します。これらを使用することで、レイアウトに必要なスペースを効果的に管理できます。

  • auto-sizeとfr単位の違いは何ですか?

    -auto-sizeは要素のコンテンツに基づいて自動的にサイズが決定され、fr単位は残りのスペースを的比例配分されます。

  • ビューポートの高さを指定するために100vhは使用されますが、これはどういう意味ですか?

    -100vhはビューポートの高さを意味し、要素の高さをビューポート全体に合わせるために使用されます。

  • メディアクエリを使用する目的は何ですか?

    -メディアクエリは、ページの幅が一定のサイズを超えた場合に、より複雑なレイアウトを適用するために使用されます。

  • グリッドエリアを使用する際に命名された「banner」、「nav」、「main」、「footer」はどのように機能しますか?

    -これらの名前はグリッドエリアを識別し、レイアウトの調整を簡素化するために使用されます。1つの場所でレイアウトを変更することで、それらのエリアに割り当てられた全ての要素に影響を与えることができます。

  • グリッドテンプレートエリアを使用してレイアウトを定義する際のASCIIアートは何を意味しますか?

    -ASCIIアートを使用することで、簡易な方法でグリッドレイアウトを視覚的に表現できます。これにより、モバイルレイアウトやさまざまな画面サイズに応じたレイアウトを簡単に調整できます。

  • グリッドを使用する際のフォールバックとは何を指しますか?

    -フォールバックは、グリッドが無効になった場合に、最初に定義されたモバイルレイアウトに自動的に切り替わることを意味します。

  • CSS Gridを使用する際の利点は何ですか?

    -CSS Gridを使用すると、固定単位と流体単位をより信頼性の高い方法で混在させてレイアウトを作成することができ、複雑なレイアウトの作成が簡単になります。

Outlines

00:00

🌟 CSS Gridsの基本的な使い方

この段落では、CSS Gridsの使い方について紹介されています。Gridsは複雑なレイアウトを作成するための仕様ですが、簡単に始められることがわかります。まず、body要素に`display: grid`を追加することで、既存のブロックフローに似たデフォルトグリッドが生成されます。次に、グリッドギャップを1emに設定し、ブロック要素同士の間にスペースを空けます。Firefoxのnightlyビルドでグリッドインスペクターを使用して、グリッドの可視化を行います。グリッドテンプレート行を定義し、ヘッダー、ナビゲーション、メインコンテンツ、フッターのサイズを調整します。特にメインコンテンツは、残りのスペースを必要に応じて自動的に調整します。ビューポートの高さを100vhに設定し、メインエリアがより多くのスペースを占有するようにします。

05:01

📱 モバイル向けのレイアウトとメディアクエリ

この段落では、モバイル向けのレイアウトを作成し、メディアクエリを使用して画面サイズが40emを超えた場合により複雑なレイアウトに変更する方法が説明されています。グリッドテンプレートカラムを使用して、サイドバーとメインスペースを定義し、サイドバーは12em、メインスペースは残りのスペースを占有するようにします。これにより、固定値と流体値をより信頼性の高い方法で混在させることができます。3つの行を定義し、ヘッダーが2つのカラムをまたぎ、ナビゲーションがサイドに沿って表示されるようにします。グリッドエリアを使用して、ヘッダー、ナビゲーション、メイン、フッターの配置を指定します。これにより、1つの場所でレイアウトを制御し、さまざまな画面サイズやコンテキストで複数のレイアウトを設定できます。また、ASCIIアートのようなグリッドテンプレートエリアを使用して、モバイルレイアウトを定義し、メディアクエリで構造を変更する方法も紹介されています。

Mindmap

Keywords

💡CSS Grids

CSS Gridsは、ウェブデザインにおいてレイアウトを作成するための機能です。この技術を使うことで、複雑なグリッドシステムを簡単に構築できます。ビデオでは、CSS Gridsを使ってどのように興味深いレイアウトを作成するかを紹介しています。

💡display grid

「display grid」は、CSSのプロパティの一つで、要素に対してグリッドシステムを適用することを意味します。ビデオでは、これを使用して、ウェブページの基本的なブロックフローに似たグリッドを安全に追加する方法が説明されています。

💡grid gap

「grid gap」は、グリッドシステム内の要素間のスペースを制御するためのCSSプロパティです。このプロパティを使用することで、グリッドの列と行の間のギャップ(スペース)を一貫して追加できます。

💡Firefox nightly

「Firefox nightly」は、Mozillaが開発中のFirefoxブラウザの開発版を指します。このバージョンは、最新の機能や修正が含まれているため、ウェブ開発者にとっては非常に有用です。ビデオでは、Firefox nightlyを使ってグリッドシステムを視覚的に確認する方法が紹介されています。

💡grid template rows

「grid template rows」は、CSS Gridsで使用されるプロパティで、グリッドの行の数とサイズを定義するために使用されます。これにより、グリッド内の要素がどのように配置されるかを制御できます。ビデオでは、グリッドの行を設定して、ヘッダー、ナビゲーション、メインコンテンツ、フッターのそれぞれの部分に適切なスペースを割り当てます。

💡viewport height

「viewport height」は、ウェブページを表示するブラウザのビューポートの高さを指します。CSS Gridsでは、要素のサイズを「100vh」という単位で指定することで、ビューポートの高さに合わせたサイズにすることができます。ビデオでは、この単位を使用して、メインコンテンツの高さをビューポートに合わせる方法が紹介されています。

💡media query

「media query」は、CSSで使用される機能で、特定の条件(例えば、画面サイズや解像度)を満たす場合にのみスタイルを適用することができます。これにより、ウェブページはさまざまなデバイスや画面サイズに適応して表示されます。ビデオでは、メディアクエリを使用して、画面サイズが40emを超えた場合により複雑なレイアウトを適用する方法が紹介されています。

💡grid template columns

「grid template columns」は、CSS Gridsで使用されるプロパティで、グリッドの列の数とサイズを定義するために使用されます。これにより、グリッド内の要素が横方向にどのように配置されるかを制御できます。ビデオでは、グリッドの列を設定して、サイドバーとメインスペースのそれぞれの部分に適切なスペースを割り当てます。

💡grid area

「grid area」は、CSS Gridsで使用される用語で、グリッド内の特定のセルまたはセルのグループを指します。グリッドエリアを使用することで、要素を特定のグリッドセルに配置することができます。ビデオでは、グリッドエリアを使用して、ヘッダーやナビゲーションバーが複数の列や行をまたぐように指定する方法が紹介されています。

💡ASCII art

「ASCII art」とは、ASCIIコードを使用してシンプルな絵画やデザインを作成する芸術的な表現方法です。CSS Gridsでは、「grid template areas」を使用して、ASCIIアートのような記述でレイアウトを定義することができます。ビデオでは、この方法を使って、モバイルレイアウトを定義する方法が紹介されています。

💡sticky footer

「sticky footer」は、ウェブページのフッターが常に画面の最下部に固定されるようにするCSS技術です。これにより、ページのコンテンツが短い場合でもフッターが画面の最下部に表示され、余白が生じることを防ぎます。ビデオでは、sticky footerの効果を実現する方法が示されています。

Highlights

展示快速开始使用CSS Grid的方法

CSS Grid规范可能令人生畏,但可以用很少的代码做出有趣的布局

通过在body上添加display grid,开始使用Grid

默认网格与默认块流非常相似,可以安全添加

Grid可以更一致地添加间隔(gutters)

通过设置grid gap为1em,使块元素之间产生间隔

使用Firefox nightly的Grid inspector来可视化网格

自动生成的隐式网格,在我们尚未定义它时自动创建

添加grid template rows来定义行大小

通过min height设置为100vh,使主区域占据更多空间

在媒体查询中为超过40em宽度的页面创建更复杂的布局

使用grid template columns设置侧边栏和主空间的列布局

Grid允许固定单位和流式单位的混合使用,提高了布局的可靠性

定义三行布局,并通过auto和one fraction设置它们的大小

通过grid area将header和nav跨越多个列和行

使用grid template areas通过ASCII艺术定义布局

通过媒体查询改变不同区域的结构,适应不同屏幕尺寸

如果关闭Grid,将自动获得初始的移动布局

Transcripts

play00:00

(upbeat electronic music)

play00:04

- I want to show you a quick way to

play00:05

get started with CSS Grids.

play00:08

I know the spec can be daunting,

play00:10

there's a lot that it can do,

play00:12

but we can start to do some fairly interesting layouts

play00:16

with just very little code.

play00:18

So I'm going to start here by just adding

play00:20

display grid to the body.

play00:23

And you can see that nothing really changes here,

play00:26

the default grid is very much like the default block flow.

play00:31

We can put it in there pretty safely

play00:34

without expecting much to happen.

play00:36

Now one of the cool things about Grid is that we can add

play00:39

gutters in a more consistent way.

play00:42

So I'm going to do that.

play00:43

I'm going to add it's called grid gap

play00:47

of 1em.

play00:49

And all of my block elements will jump apart

play00:52

by that much, so,

play00:54

the grid gap goes between each of the grid areas.

play00:58

I'm going to inspect this

play01:01

in Firefox nightly here.

play01:08

I've opened the grid inspector here

play01:11

in Firefox.

play01:12

And you can see here the body

play01:16

now has this little tag next to it that says grid

play01:19

and I'm going to click on that.

play01:21

And that's going to show us the grid visually here

play01:25

in the main window.

play01:26

And you can see it's added lines around each area.

play01:29

It's added this shading over the gutters.

play01:32

And we have these line numbers.

play01:34

So the first line is one

play01:36

and then the second line is two,

play01:37

three, four, five, so we've

play01:39

automatically got five grid row lines

play01:44

down the page.

play01:46

And just one and two across.

play01:50

There are also negative one index going backwards, so,

play01:54

that's just happening automatically.

play01:56

I'm going to turn that off quick.

play01:59

So what we have there is the implicit grid

play02:01

that's generated automatically

play02:04

when we create a grid

play02:06

and we haven't defined it yet.

play02:08

What I'm going to do now is add some grid template rows.

play02:13

So rows go down the page.

play02:15

And I'm just going to give some sizing to these things.

play02:19

I'm going to say that the first one is auto-sized

play02:23

that's the header.

play02:24

Then the nav is also going to be auto-size.

play02:28

And then the main content is going to be one fraction

play02:32

which means it can take up as much space as it needs to

play02:35

in the layout.

play02:37

However much space is remaining.

play02:39

And then the footer, again, I'm going to set to auto.

play02:42

Now that didn't change anything yet

play02:43

because there's no extra space there

play02:46

for the main area to take up.

play02:48

So I'm just going to add some extra space

play02:50

with a min height

play02:53

of 100vh, that's viewport height.

play02:57

So it's going to be at least the height of the viewport.

play03:01

And you can see that because of the rows,

play03:02

if I didn't have grid template rows there,

play03:05

they would all expand in a similar way.

play03:09

But by adding the grid template rows,

play03:11

I've made it so that now the main area takes up more space

play03:16

and the others stay the same size.

play03:19

So this might work for us as a mobile layout.

play03:24

And we might not even worry about the grid fallback

play03:27

for mobile because it's fairly similar.

play03:31

There's not really much happening here yet.

play03:34

I'm going to try to create then inside of this media query

play03:38

so for any page over 40em's wide

play03:43

we're going to add a little bit more complex layout.

play03:45

Again, we're going to use grid template columns.

play03:48

But we're going to set up a sidebar that's 12ems

play03:52

and then a main space that's one fraction,

play03:54

that takes up all of the remaining space available

play03:57

in the window.

play03:58

And you can see that everything sort

play04:00

of falls into place there automatically.

play04:04

The first column stays 12em's

play04:07

and the second column takes up whatever space is left.

play04:11

And that's pretty cool,

play04:12

because that means that we can start doing layouts

play04:14

that really mix.

play04:15

Fixed units with fluid units

play04:18

in a more reliable way

play04:21

which is a really nice thing about grid.

play04:23

And then I'm going to say we actually want three rows.

play04:25

So right now it's giving us two by default.

play04:28

We want three rows.

play04:29

The first one's going to be auto-sized,

play04:31

the second one's going to be as much space as we need,

play04:34

and then the footer will go along the bottom.

play04:36

So, if I define those three rows auto, one fraction, auto.

play04:40

Right now, we're not seeing that last row,

play04:44

it's collapsing because there's nothing in it.

play04:48

And what I'm going to do is tell the header

play04:53

to span two columns.

play05:01

I added grid column span two to the header.

play05:05

And that made it take up the entire top space

play05:08

instead of making the footer go the whole way across.

play05:11

I'm going to make the nav go the whole way down that side.

play05:15

So, I'm going to add nav grid row

play05:20

span two

play05:23

and it will go down that whole side

play05:26

and then main and footer are both being pushed into place.

play05:29

I don't have to be more explicit here if I don't want to be.

play05:32

I could if it's useful for some reason.

play05:35

But you can see here now we have a mobile view

play05:38

on small screens

play05:39

and when I make the screen bigger it jumps

play05:42

to this layout.

play05:44

I can also change the height of the window.

play05:47

And you can see again that main and nav are stretching

play05:51

as much as they need to

play05:53

to take up the space while the header

play05:55

and footer always remain the same size.

play05:57

And it's a sort of sticky footer where

play05:59

if the window gets big, the footer is still there.

play06:02

But if the window is smaller

play06:04

we're able to scroll.

play06:06

Once we have these named areas,

play06:08

the only thing that we have to do then

play06:10

to make our header and navigation

play06:14

span the multiple columns and rows,

play06:17

is assign them a grid area.

play06:19

So, we say header, grid area banner.

play06:23

Nav grid area nav.

play06:24

Main grid area main, etc.

play06:27

And by giving them these names

play06:29

we can just change in one place

play06:33

what the layout that we want is going to look like

play06:37

and then that will change for anything assigned

play06:41

to that area.

play06:42

You can see there I changed banner to just span one.

play06:45

I could make nav come up here into the header.

play06:48

And it will take over that space.

play06:51

And that means that the parent element,

play06:53

in this case the body,

play06:55

it sort of controls the layout from one place.

play06:58

So we can set up multiple layouts at different screen sizes

play07:02

or in different contexts,

play07:03

and everything else will fall into place.

play07:06

Another thing that we can do with grid

play07:07

that is really helpful for getting started

play07:10

is use this grid template areas.

play07:13

It's a way of using sort of ASCII art

play07:16

to define a layout.

play07:18

So I have to find our mobile layout here

play07:20

by just giving names to

play07:23

the four different areas we have in mobile.

play07:25

And let's just see them there.

play07:26

We have a banner, nav, main, and footer.

play07:29

And then I can add a media query.

play07:33

All I have to do with a media query is change

play07:36

the structure of those areas.

play07:39

So now I've made banner go across two columns

play07:43

and then nav in a single column

play07:46

and then main side by side.

play07:48

And then nav and footer are also side by side.

play07:51

So you can see here, the banners going to go across the top.

play07:53

The nav is going to come down the side.

play07:55

And then main and footer are going to fill in the gaps.

play07:58

And when we extend our window

play08:01

that's what we see happen.

play08:03

And I think that's just a great way

play08:05

to get started with Grid.

play08:06

That's really sort of the,

play08:08

the terms and pieces of code that you need to get started.

play08:11

And you can see that some of the fallbacks

play08:14

are going to happen in some ways automatically.

play08:16

If you turn off Grid,

play08:17

you'll automatically get the mobile layout

play08:20

that you started with.

play08:21

And you can build on top of that.

play08:23

Have fun and get started with Grid.

play08:26

(upbeat electronic music)

Rate This

5.0 / 5 (0 votes)

相关标签
CSSグリッドレイアウト响应式デザイングリッドテンプレートグリッドエリアウェブデザイン初心者向けテクスチャートモバイル最適化フロントエンド
您是否需要英文摘要?