惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
GbyAI
GbyAI
博客园 - Franky
宝玉的分享
宝玉的分享
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
人人都是产品经理
人人都是产品经理
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
腾讯CDC
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
I
InfoQ
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
有赞技术团队
有赞技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
F
Full Disclosure
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Vercel News
Vercel News
博客园 - 【当耐特】
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Project Zero
Project Zero
量子位
M
MIT News - Artificial intelligence
Stack Overflow Blog
Stack Overflow Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
美团技术团队
Attack and Defense Labs
Attack and Defense Labs
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Blog of Author Tim Ferriss
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Troy Hunt's Blog
Google Online Security Blog
Google Online Security Blog
罗磊的独立博客
P
Proofpoint News Feed
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
S
SegmentFault 最新的问题
L
LINUX DO - 最新话题
Simon Willison's Weblog
Simon Willison's Weblog
爱范儿
爱范儿
博客园 - 聂微东
A
About on SuperTechFans
PCI Perspectives
PCI Perspectives
D
Docker

Ahmad Shadeed

Better fluid sizing with round() Use Cases for Field Sizing The Basics of Anchor Positioning Item Flow CSS Relative Colors Balancing Text In CSS Should masonry be part of CSS grid? CSS display contents CSS Grid Areas CSS Cap Unit An Interactive Guide to CSS Container Queries CSS :has() Interactive Guide CSS Nesting UX in DevTools CSS Nesting Future CSS: State Container Queries Rebuilding a comment component with modern CSS Conditional CSS with :has and :nth-last-child CSS Text balancing with text-wrap:balance CSS Masking Do we need CSS flex-wrap detection? My CSS Wishlist Conditional CSS CSS Style Queries Inside the mind of a frontend developer: Article layout Inside the mind of a frontend developer: Hero section CSS container queries are finally here The CSS behind Figma First Look At The CSS object-view-box Property Learn CSS Subgrid CSS :has Parent Selector Aligning Content In Different Wrappers Flexbox Dynamic Line Separator Hello, CSS Cascade Layers Building UI Components With SVG and CSS A Deep CSS Dive Into Radial And Conic Gradients Defensive CSS Building Real-life Components: Facebook Messenger Conditional Border Radius In CSS CSS Container Query Units Less Absolute Positioning With Modern CSS Aligning a Button Label Vertically Comparing Design Mockups To Code Result Using HSL Colors In CSS Custom Scrollbars In CSS Let CSS Container Queries For Designers The State of CSS Cross-Browser Development Overflow Issues In CSS Inspect Element As A Way To Increase Your Curiosity Handling Text Over Images in CSS Digging Into CSS Logical Properties Clipping Scrollable Areas On The inline-start Side Understanding Clip Path in CSS The Art of Building Real-life Components Handling Short And Long Content In CSS CSS Scroll Snap A Deep Dive Into CSS Grid minmax() CSS Variables 101 Finding The Root Cause of a CSS Bug Learn CSS centering How to detect browser support for Flexbox Gap CSS Mistakes While On Autopilot Digging Into the Flex Property Understanding CSS Multiple Backgrounds Aligning Logo Images in CSS Grid for layout, Flexbox for components Colors in CSS Thinking About The In-between Design Cases min(), max(), and clamp() CSS Functions Image Techniques On The Web Everything About Auto in CSS Let Learn CSS Positioning Intrinsic Sizing In CSS CSS Grid Template Areas In Action Hiding Elements On The Web Creating a Variable Color Font From Scratch Building a Football Ticket With CSS and SVG Blending Modes in CSS CSS Variables With Inline Styles Implementing Dark Mode For My Website Rebuilding Apple Music Header in HTML & CSS Accessible Checkbox Layout Flickering On Browser Resize Enhancing The Clickable Area Size Custom Underlines with SVG Part 3: The Process of Implementing A UI Design From Scratch Part 2: The Process of Implementing A UI Design From Scratch Building An Old Nav Design CSS Flexbox: 5 Real World Use Cases I Used CSS Inline Flex For The First Time The Process of Implementing A UI Design From Scratch Common CSS Issues For Front-End Projects Handling Long and Unexpected Content in CSS How to Build Web Form Layouts With CSS Grid Grid Layout Ah-ha Moment Enhancing Our Components with CSS :empty Building Resizeable Components with Relative CSS Units CSS Writing Mode The Journey of Learning Front End Web Development on a Daily Basis
Learn Box Alignment
Ahmad Shadeed · 2020-03-04 · via Ahmad Shadeed

Introduction

We have a table and four plates. Throughout this article, we will learn how to align the plates differently for our guests by using Flexbox and Grid.

Make sure you’re not hungry when reading this. ;)

Plates Size

To start, I added a width and height for the plates.

<div class="c-table">
  <div class="c-plate salmon"></div>
  <div class="c-plate chicken"></div>
  <div class="c-plate steak"></div>
  <div class="c-plate kebab"></div>
</div>
.c-plate {
  width: 96px;
  height: 96px;
}

Since the plates are block elements, each one of them is in its own line.

Flexbox

To start, we should add display: flex to the .table element.

.c-table {
  display: flex;
}

Notice that the plates are next to each other. Since flexbox is a single layout direction, the flex items are either aligned in rows or columns. The default is rows.

Flexbox Axes

In flexbox, we have the two axes: main axis and cross axis. See the following figure. By default, the location of each axis is as the figure.

.c-table {
  display: flex;
  /* Default flex-direction */
  flex-direction: row;
}

When the flex direction is flex-direction: column, the plates will be displayed vertically.

.c-table {
  display: flex;
  flex-direction: column;
}

The guests requested bigger plates, so we need to increase their width. I added the following CSS.

.c-plate {
  width: 50%;
  height: 96px;
}

The expected behaviour is that each plate have a width of 50% of the table, correct? Why it’s not working?

The reason is because flexbox doesn’t move the plates into a new line when there is no space available in the row.
To fix that, we should useflex-wrap: wrap, it will help in wrapping the plates correctly in case the space is not enough.

.c-table {
  display: flex;
  flex-wrap: wrap;
}

You might be wondering, why there is a space between each row of plates? Well, that’s a good question.

By default, flex items stretch in the cross axis because the default value for the table is align-items: stretch. In our case, the plates has a fixed height. Let’s remove it and see how the result will look.

.c-plate {
  width: 50%;
  height: initial;
}

Flexbox - Align Content

As you see, the plates are not centered correctly. To align them, we need to control the flex items in the cross axis. For that, align-content property is perfect.

Note that this property won’t work with single line flex items. It needs flex-wrap: wrap to work properly.

To center the plates on the cross axis, I added align-content: space-evenly and the result looks like the following figure.

.c-table {
  display: flex;
  flex-wrap: wrap;
  align-content: space-evenly;
}

.c-plate {
  width: 50%;
  height: 90px;
}

normal, flex-start, flex-end, center, stretch, space-around, space-between, space-evenly, baseline, first-baseline, last-baseline

Flexbox - Justify Content

The justify-content works on the main axis of the flex container.

.c-table {
  display: flex;
  flex-wrap: wrap;
}

.c-plate {
  width: 90px;
  height: 90px;
}

To have an equal spacing between the plates across the main axis, I added justify-content: space-between. See the result in the following figure.

.c-table {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

.c-plate {
  width: 90px;
  height: 90px;
}

normal, flex-start, flex-end, center, stretch, space-around, space-between, space-evenly, baseline, first-baseline, last-baseline

Flexbox - Align Items/Self

Align Items

By default, align-items works on the cross axis. I added align-items: center to center the plates vertically.

.c-table {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
}

.c-plate {
  width: 90px;
  height: 90px;
}

auto, normal, start, end, center, stretch, baseline, first baseline, last baseline

Align Self

Let’s suppose that we have some custom alignment requests from our guests. The first and last plates needs to be aligned differntly. For that purpose, we can use align-self on those flex items.

.salmon {
  align-self: flex-start;
}
.kebab {
  align-self: flex-end;
}

auto, normal, start, end, center, stretch, baseline, first baseline, last baseline

CSS Grid

For CSS grid, we have two axes: Inline and block. Those don’t change based on a specific direction. For this section, the plates are desserts.

I saved the best for last, so let’s enjoy some desserts with CSS grid.

Grid Alignment Container

Grid child items can either be aligned to their grid container (the table), or the grid area they are in.

Grid Container

The parent element of grid child items (The table).

The Grid Area

A part of the grid that the plate is placed it. The next figure has four grid areas. I highlighted one of them.

CSS Grid - The default

For CSS grid, we have two axes: Inline and block. Those don’t change based on a specific direction

<div class="table">
  <div class="c-plate konafa"></div>
  <div class="c-plate icecream"></div>
  <div class="c-plate donut"></div>
  <div class="c-plate cake"></div>
</div>
.table {
  display: grid;
}

Specify columns and rows

We will have two columns and rows. See the next figure.

.table {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
}

Grid - Justify Items

All the plates should be centred horizontally to their grid area, the first row of plates is close to the top side and the second row is close to the bottom side.

By using justify-items, we can control how the plates will be aligned to its grid area.

.table {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  justify-items: center;
}

auto, normal, start, end, center, stretch, baseline, first baseline, last baseline

Grid - Align Items

Aligning the plates to the center is not enough. To make it better, the plates should be centered vertically and horizontally in its grid area.

Note: the container is the grid area

.table {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  justify-items: center;
  align-items: center;
}

auto, normal, start, end, center, stretch, baseline, first baseline, last baseline

Grid - Align Self

The guest with the ice cream wants to move the plate closer to him. To do so, we will use align-self property.

.icecream {
  align-self: start;
}

auto, normal, start, end, center, stretch, baseline, first baseline, last baseline

Grid - Justify Self

Even better, the guest with the ice cream wants the plate to be at the top right on the table. We can use the justify-self property.

.icecream {
  align-self: start;
  justify-self: end;
}

auto, normal, start, end, center, stretch, baseline, first baseline, last baseline

Grid - Justify Content

When we have a grid with fixed size columns, this will create an additional space in case the container is bigger than the columns. In that case, we can use justify-content to control the spacing between the columns.

.table {
  display: grid;
  grid-template-columns: 150px 150px;
}

Adding Justify Content

By adding justify-content: space-between, the columns are distributed and the space between them is equal.

.table {
  display: grid;
  grid-template-columns: 150px 150px;
  justify-content: space-between;
}

auto, normal, start, end, center, stretch, baseline, first baseline, last baseline

Grid - Align Content

When we have a grid with fixed size rows, this will create an additional space in case the container height is bigger than the rows. In that case, we can use align-content to control the spacing between the rows.

.table {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 150px 150px;
}

Using Align Content

By adding align-content: space-between, the rows are distributed and the space between them is equal.

.table {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 150px 150px;
  align-content: space-between;
}

auto, normal, start, end, center, stretch, baseline, first baseline, last baseline

Other Articles That I Wrote