mirror of
https://github.com/phuoc-ng/csslayout.git
synced 2025-08-08 23:26:32 +02:00
103 lines
1.7 KiB
Plaintext
103 lines
1.7 KiB
Plaintext
---
|
|
category: Display
|
|
created: '2019-11-15'
|
|
description: Center an element with CSS flexbox
|
|
keywords: css centering, css flexbox
|
|
thumbnail: /assets/css-layout/thumbnails/centering.png
|
|
title: Centering
|
|
---
|
|
|
|
## HTML
|
|
|
|
```html index.html
|
|
<div class="centering">...</div>
|
|
```
|
|
|
|
## CSS
|
|
|
|
```css styles.css
|
|
.centering {
|
|
align-items: center;
|
|
display: flex;
|
|
justify-content: center;
|
|
}
|
|
```
|
|
|
|
<Playground>
|
|
```css placeholders.css hidden
|
|
.circle {
|
|
background: #d1d5db;
|
|
border-radius: 9999px;
|
|
height: var(--circle-size);
|
|
width: var(--circle-size);
|
|
}
|
|
.circle--sm {
|
|
--circle-size: 0.5rem;
|
|
}
|
|
.circle--md {
|
|
--circle-size: 1.5rem;
|
|
}
|
|
.circle--lg {
|
|
--circle-size: 4rem;
|
|
}
|
|
.lines {
|
|
padding: 0.25rem 0;
|
|
width: 100%;
|
|
align-items: center;
|
|
display: flex;
|
|
justify-content: center;
|
|
flex-direction: column;
|
|
}
|
|
.line {
|
|
background: #d1d5db;
|
|
height: 1px;
|
|
margin-bottom: 0.25rem;
|
|
}
|
|
.line.line--20 {
|
|
width: 20%;
|
|
}
|
|
.line.line--40 {
|
|
width: 40%;
|
|
}
|
|
.line.line--60 {
|
|
width: 60%;
|
|
}
|
|
.line.line--80 {
|
|
width: 80%;
|
|
}
|
|
.line.line--100 {
|
|
width: 100%;
|
|
}
|
|
```
|
|
|
|
```css styles.css hidden
|
|
body {
|
|
align-items: center;
|
|
display: flex;
|
|
justify-content: center;
|
|
}
|
|
.centering {
|
|
align-items: center;
|
|
display: flex;
|
|
justify-content: center;
|
|
|
|
/* Demo */
|
|
flex-direction: column;
|
|
width: 16rem;
|
|
}
|
|
```
|
|
|
|
```html index.html hidden
|
|
<div class="centering">
|
|
<div class="circle circle--md"></div>
|
|
<div class="lines">
|
|
<div class="line line--80"></div>
|
|
<div class="line line--40"></div>
|
|
<div class="line line--100"></div>
|
|
<div class="line line--60"></div>
|
|
<div class="line line--20"></div>
|
|
</div>
|
|
</div>
|
|
```
|
|
</Playground>
|