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

CSS How to Make a Website CSS: Cascading Style Sheets Take a Mobile-First Approach

Rifqi Fahmi
Rifqi Fahmi
23,164 Points

[ASK] manage the image position

what are html code for image to be in the center?? because "text-align: center" coded won't work.

Thanks :)

1 Answer

Chris Andruszko
Chris Andruszko
18,392 Points

I always use flexbox for centering things. I made a simple example here so you can see it working: https://jsfiddle.net/kfumn39t/

But I'll also put the code from the example here:

<div class='container'>
    <div class='centered_box'>
    </div>
</div>
.centered_box {
    width: 300px;
    height: 200px;
    background: blue;
    color: #fff;
}

.container{
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    align-items: center;
    width: 500px;
    height: 500px;
    border: 5px solid #000;
}

I highly suggest taking the course here on Treehouse called "CSS Layout Techniques."