Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

HTML & CSS Can't center a grid item with a max-width

Hello there!

I want to center a grid item that has a max-width but the item is not centered. Here is a code example:

HTML:
<!DOCTYPE html>
<html>
    <head>
        <style>
            .container {
                display: grid;
                grid-template-columns: 1fr;
                height: 300px;
                background-color: antiquewhite;
            }

            .item {
                max-width: 1400px;
                height: 50px;
                border: 4px solid black;
                margin: 0 auto;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="item">
            </div>
        </div>
    </body>
</html>

How can I center a grid item that has a max-width?

Thanks a lot in advance!
 
max-width is the maximum width the item will show. If you want a width of 1400 for the item that will have to be set with width or min-width. Or am I misunderstanding what you are trying to do?
 
max-width is the maximum width the item will show. If you want a width of 1400 for the item that will have to be set with width or min-width. Or am I misunderstanding what you are trying to do?
Hello Menator! Thanks for your answer ;)

I want the grid item to be flexible and to not go beyond 1400px. That is why I want it to have a maximum width of 1400px.

Here is a codepen example of what I want to achieve: https://codepen.io/hebrerillo/pen/rNvNPRo
 
But I am still wondering why grid items with a maximum width cannot be centered...
Your div with a max-width did center! If it's not doing that for you my guess is you have other items and styles that you are not giving us.

Post the entire code you are using that won't center the div element.
 
Your div with a max-width did center! If it's not doing that for you my guess is you have other items and styles that you are not giving us.

Post the entire code you are using that won't center the div element.
Hello OldMan!

Here is the entire code:

HTML:
<!DOCTYPE html>
<html>
    <head>
        <style>
            .container {
                display: grid;
                height: 300px;
                background-color: antiquewhite;
                align-items: end;
            }

            .item {
                max-width: 1400px;
                height: 50px;
                background-color: lightsalmon;
                /*margin: 0 auto;*/
                /*justify-self: center;*/
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="item">
            </div>
        </div>
    </body>
</html>
 
What you're using and what you gave us are two different things.

align-items is used with flex not grid. And align-items: end; It's flex-end
But the most important thing is the commenting out
margin: 0 auto;
which centers things.

Also put the border back so you can see the div, without a width it can't be seen Or add a width.
 
Back
Top Bottom