How to make a <div> with display: inline-block center on the browser horizontally?

Earlier, I needed a <div> that 'hugged' the elements inside it instead of expanding to the width of the browser. I was advised to set the <div>'s display property to inline-block, and that did the trick.

But after doing that, it no longer would center on the browser. Is there a way I can have a <div> hug its child elements and still be center-able inside the browser?

<html>
    <head>
        <style>

            html {
                min-width: 300px;
                height: 100%;
            }

            #testDiv {
                margin: 0 auto;
                display: inline-block; // If you took this out, it will center again
                width: 100px;
                height: 100px;
                background: red;
                border: 1px solid black;
            }

        </style>
    </head>
    <body>
        <div id="testDiv"></div>
    </body>
</html>

Open in new window


Thanks.
elepilAsked:
Who is Participating?
 
RobOwner (Aidellio)Commented:
Wrap your div in <center> tags :)
0
 
elepilAuthor Commented:
Rob, thanks! You know, I've never heard of <center> before! Is that something new in HTML5?
0
 
RobOwner (Aidellio)Commented:
No, it's been around since the beginning of time I believe, just under utilised :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.