About this snippet

Learn how to easily center elements horizontally and vertically using CSS Flexbox. A common layout requirement simplified, perfect for aligning content perfectly on your webpage.

Size
606 bytes
Uses
HTML, CSS
Loads
Nothing external
Views
441
<div class="flex-container">
  <div class="flex-item">Centered Content</div>
</div>
* {
  font-family: system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  box-sizing: border-box;
}
.flex-container {
  display: flex;
  justify-content: center; /* Center horizontally */
  align-items: center;    /* Center vertically */
  height: 200px;
  background-color: #f0f0f0;
  border: 1px solid #ccc;
}
.flex-item {
  padding: 20px 25px;
  background-color: #3498db;
  color: white;
  text-align: center;
  font-weight: 500;
  border-radius: 5px;
}
Esc
↑↓ moveEnter select

Start from a template

Open an .html file, or HTML, CSS, JS, Markdown, Less or TypeScript files. You can also drop them here. Styles and scripts go into their panes.

Share

Source

Keyboard shortcuts

Save