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 CSS Foundations Advanced Selectors Pseudo-Elements: ::before and ::after

mrx3
mrx3
8,742 Points

How would I center the phone image and the phone number on my web-page?

In the video Guil adds a image of a phone to the phone number. I'm wondering is there a way to center the image and the number in on a web page? I tried adding margin: 0 auto and text-align: center; to the CSS but, it didn't work. Any advice would be much appreciated, and thanks in advance.

2 Answers

add the following css to the element:

width: 30%;
display: block;
margin: 0 auto;
Edward Bryan Kene Morales
Edward Bryan Kene Morales
7,374 Points

Hi there!

Hmmm, as you can see, the phone number is wrapped in a <p> tag. You cannot see it, but actually it is already set to occupy the full width of the browser since <p> has a display: block property by default.

To center this on a page, I would suggest that you wrap the paragraph in a <div>. Then set a width value to the div and position that on the page. The div acts as a "wrapper" and will move the paragraph nested in it when it is moved/centered on the page. The code could look like this:

HTML:

<div class="wrapper">
   <p class="phone">Phone Number</p>
</div>

CSS:

.wrapper {
            width: 500px;
            margin: 0 auto;
        }

        .phone {
            text-align: center;
        }

        .phone::before {
            content: "\2706";
        }

I added a text-align property to the paragraph because it's just two words and that won't be enough to span the whole 500px width of the <div>. Hope this helps.