Unordered and Ordered Lists with Auto-Increment Using Pseudoclasses
Unordered and Ordered Lists with Auto-Increment Using Pseudoclasses
July 25, 2024
Tooltips with Positioning (Top, Bottom, Left, Right)
Tooltips with Positioning (Top, Bottom, Left, Right)
July 26, 2024

July 25, 2024

Center elements with flexbox

Flexbox (Flexible Box) is a powerful CSS module that makes layout design a breeze. When it comes to centering elements, flexbox is the simplest and most versatile solution. Set the container property to display: flex and justify-content: center and align-items: center thanks to that you can center any child elements both horizontally and vertically.

HTML

<div class="sf-wrapper">

    <div>Centered element</div>

</div>

CSS

/* ---------------------------------------------------------- */
/*                 Snippflow Centering elements               */
/* ---------------------------------------------------------- */

.sf-wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-height: 100vh;
    padding: 40px;
    box-sizing: border-box;
}
/* Example div */
.sf-wrapper > div {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 250px;
    height: 250px;
    background-color: #46A787;
    color: #fff;
    border-radius: 8px;
    box-shadow: 6px 10px 20px 0px rgba(0,0,0,.1);
}

Result

See the Pen Center elements with flexbox by Snippflow (@snippflow) on CodePen.

Leave a Reply

Your email address will not be published. Required fields are marked *

Level Up Your Coding Skills

Get the best CSS, HTML, and JS snippets delivered to your inbox monthly. 🚀
No spam—just coding gold! 💻✨"

We don’t spam! Read our privacy policy for more info.

cookie
This website uses cookies to improve your experience. By using this website you agree to our Data Protection Policy.
Read more