Margin: auto; not centering my div


Hi, i am just started learning how to make a simple website and for sake of learning creating a fake business website. I am trying to get my divs to all be centered by using margin: auto, but haven’t been able to get it to work and the divs stick to the left side.

<!DOCTYPE html>
<html lang="en">
    <title>Lil Mafe</title>
        font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif;

        background: lightcoral;
        color: white;
        border-radius: 5px;

        background: rgb(255, 236, 207);
        padding: 25px;
        display: flex;
        flex-direction: row;

        margin: 0px;
        padding: 0px;
        list-style-type: none;;

        display: inline-block;
        margin-right: 20px;

        background: rgb(255, 218, 118);
        margin: auto;
        display: inline-block;
        width: 150px;
        text-align: center;

        background:rgb(137, 225, 255);
        padding: 5px;
        border-radius: 5px;
      <h1>Mafe's Cafe</h1>
    <section>More Info</section>
    <footer>© 2018 Mafe Inc.</footer>


Hi there mattdus,

and a warm welcome to these forums.:winky:

CSS margin-auto only works on block elements.
You have set the divs to inline-block
Remove it and you’re set to go. :biggrin:



If you want those divs aligned horizontally and centred while using display:inline-block then you would need the text-align:center to be on a parent of those three items instead. The margin:auto does nothing for inline-block elements as @coothead mentions above (inline-blocks are essentially treated the same as text would be in a similar position).

Or use flexbox to center the items as you are already using some flex styles in other elements.

Also even when creating demos you should avoid styling things like divs globally and use a class instead of the element selector. For example if you took my advice in the first paragraph and wrapped a div around your three inline-block divs then everything would be broken because you already styled all divs to be inline-block. :slight_smile:


Thanks for the warm welcome! Also wow I wish I knew that, thanks you for the clarification!


Oh thank you! That makes more sense also what do you mean formatting with flex box?


You can centre things with flexbox. There are some examples here that you will find useful.


Thank you so much!