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

推荐订阅源

T
Tenable Blog
博客园_首页
Vercel News
Vercel News
WordPress大学
WordPress大学
美团技术团队
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Y
Y Combinator Blog
博客园 - 【当耐特】
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
The Cloudflare Blog
T
The Blog of Author Tim Ferriss
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
云风的 BLOG
云风的 BLOG
腾讯CDC
M
MIT News - Artificial intelligence
爱范儿
爱范儿
Recent Announcements
Recent Announcements
雷峰网
雷峰网
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
The Register - Security
The Register - Security
Jina AI
Jina AI
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
P
Privacy & Cybersecurity Law Blog
Recorded Future
Recorded Future
Help Net Security
Help Net Security
N
News and Events Feed by Topic
博客园 - Franky
P
Proofpoint News Feed
L
LINUX DO - 热门话题
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
D
Docker
Google DeepMind News
Google DeepMind News
有赞技术团队
有赞技术团队
IT之家
IT之家
Security Latest
Security Latest
L
LangChain Blog
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
J
Java Code Geeks

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 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 Learn Box Alignment 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 CSS centering
Ahmad Shadeed · 2020-09-14 · via Ahmad Shadeed

A guide to everything you need to know about centering in CSS

By Ahmad Shadeed - @shadeed9

Before starting, can you please choose your favorite plate from the below?
You will play with it throughout the guide.

Horizontal Centering

Inline Elements

Text Align

To center an inline element like a link or a span or an img, all you need is text-align: center.

<div class="desk">
  <span class="plate"></span>
</div>
.desk {
  text-align: center;
}

For multiple inline elements, the process is similar. It’s possible by using text-align: center.

<div class="desk">
  <span class="plate"></span>
  <span class="plate"></span>
</div>
.desk {
  text-align: center;
}

Flexbox

By changing the desk display type to flex, we can easily center the content of it.

.desk {
  display: flex;
  justify-content: center;
}

Even for multiple inline items, the centering works seamlessly.

.desk {
  display: flex;
  justify-content: center;
}

CSS Grid

With a grid container, the plate will be centered according to its grid area. Note that this won’t work with more than one plate unless they are wrapped in an element.

I wrote a guide about grid and flexbox alignment. Learn about box alignment.

.desk {
  display: grid;
  justify-content: center;
}

Block Elements

Auto Margin

A block element with a known width and height can be centered by using an auto value for the left and right margins.

.plate {
  width: 120px;
  height: 120px;
  margin-left: auto;
  margin-right: auto;
}

Auto Margin

With multiple block elements, they should be wrapped in an element to be centered.

.tray {
  display: flex;
  margin-left: auto;
  margin-right: auto;
}

Flexbox

You can use flexbox to center the plate.

.desk {
  display: flex;
  justify-content: center;
}

Also, you don’t need to wrap the plates in wrapper to center them. Flexbox can do that!

.desk {
  display: flex;
  justify-content: center;
}

CSS Positioning

By absolutely positioning the plate, we can easily center it horizontally with CSS transforms.

.plate {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}

I wrote a guide about CSS positioning in detail. Learn about CSS positioning.

In case the element width is known, you can use a negative margin instead of CSS transforms.

.plate {
  position: absolute;
  left: 50%;
  margin-left: -60px;
}

Vertical Centering

Inline Elements

Vertical Padding

One of the easiest ways to vertically center an element is using padding.

.desk {
  padding-top: 24px;
  padding-bottom: 24px;
}

Vertical Align

The vertical-align property can be used for one or multiple elements.

In this example, the fork and the knife should be centered vertically with the plate.

.desk {
  text-align: center;
}

.plate,
.fork,
.knife {
  vertical-align: middle;
}

Flexbox

To align the plate, fork, and knife, we can use flexbox for that.

.desk {
  display: flex;
  justify-content: center;
  align-items: center;
}

Block Elements

Absolute Positioning

By positioning an element absolutely, it’s possible to center it vertically with CSS transforms.

.plate {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

If the element height is known, you can use negative margin instead of positioning.

.plate {
  position: absolute;
  top: 50%;
  margin-top: -60px;
}

CSS Grid

With CSS grid, we can use align-items to center an item vertically to its grid area.

.desk {
  display: grid;
  align-items: center;
}

Horizontal & Vertical Centering

Inline Elements

Padding and Text Align

We can combine padding and text-align to center an element horizontally and vertically.

.plate {
  text-align: center;
  padding-top: 24px;
  padding-bottom: 24px;
}

Other Element Types

Absolute Positioning

By absolutely positioning the plate to the left and right sides.

.plate {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

Flexbox

By using setting justify-content and align-items to center, it’s easy and straightforward.

.plate {
  display: flex;
  justify-content: center;
  align-items: center;
}

CSS Grid

With the place-items property, it combines justify-items and align-items.

.desk {
  display: grid;
  place-items: center;
}

Other Articles That I Wrote

Resources

I’m writing an ebook

I’m excited to let you know that I’m writing an ebook about Debugging CSS. If you’re interested, head over to debuggingcss.com and subscribe for updates about the book.