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


Brandon Stiles
Brandon Stiles
12,305 Points

How can I center text over an image (horizontally and vertically), and make it responsive?

I've been trying all day but can't get this to work:

I want to to overlay and totally center text over a rectangle shape (to make a 'Click Here to Open' button), then horizontally center the complete button on the page. I can't get it right. Here's my code:

  <div class="button">
               <img src="img/menubutton.png" alt="free-stuff-button">
                    <p>FREE CONSULTATION</p>
.button img{
    display: block;
    margin: auto;
    height: 10%;
    width: 50%;
    align-content: center;
    vertical-align: middle;

.button p{
    font-size: 1.8em;
    color: blue;
    text-align: center;

2 Answers

As simple as centering elements within others might seem there is actually a lot that can be said for it. If you look into flexbox as Callum suggested but are unable to find the right solution then I recommend you take a look at this article that goes over different methods to achieve center vertical alignment.