Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


How to center div with unknown content (width)

Hey guys, I need your help...

How can I center this div? It will have unknown number of "icon divs" inside so I can't set fixed width and set margin auto trick...

            <div class="icons">
                <div class="icon">
                    <img src="images/icon_brands.svg" alt="Brands">
                <div class="icon">
                    <img src="images/icon_websites.svg" alt="Websites">
                <div class="icon">
                    <img src="images/icon_socialmedia.svg" alt="Social Media">
                    <h3>Social Media</h3>
                <div class="icon">
                    <img src="images/icon_printmaterial.svg" alt="Print Material">
                    <h3>Print Material</h3>
.icons {
    background: yellow; /* Random color for testing */

.icon {
    display: inline-block;
    width: 150px;
    margin: 0 25px;

.icons img {
    max-height: 50px;

.icon h3 {
    font-weight: 300;
    text-align: center;
    text-transform: uppercase;
    margin: 0;
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px solid #ccc;
    font-size: 16px;

Thank you

1 Answer

Erik McClintock
Erik McClintock
45,783 Points


Here is a great resource on this situation:


Chris' site is an excellent place to find all sorts of fixes and tricks like this; you should peruse it a bit!

Happy coding!


This helped :) Thanks!