CSS Flexbox Alignment Issue: Items Not Centering Horizontally

I’m currently working on a front-end development project and I’m facing an issue with aligning items using CSS Flexbox. I want to horizontally center a set of items within a container using Flexbox, but they don’t seem to be aligning properly. Here’s the relevant HTML and CSS:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
    <div class="container">
        <div class="item">Item 1</div>
        <div class="item">Item 2</div>
        <div class="item">Item 3</div>
    </div>
</body>
</html>
/* styles.css */

.container {
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px solid #ddd;
    height: 200px;
}

.item {
    padding: 10px 20px;
    background-color: #f0f0f0;
    margin: 5px;
}

Despite setting justify-content: center; and align-items: center; on the .container, the items are not aligning at the center of the container horizontally.

Am I missing something in my CSS code, or is there another property I should be using to achieve this alignment? Any help or guidance on how to properly center these items within the container using Flexbox would be greatly appreciated. Thank you in advance for your assistance!

1 Like