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 does one center an element (such as a button) on vertically on a webpage using CSS? I know about text-align.

How does one center an element (such as a button) on vertically on a webpage using CSS? I know about text-align. for centering text. I need to know how one does it for a button, list, or an image.

6 Answers

Anthony Lucio
Anthony Lucio
20,431 Points

.parent { position: relative; } .child { position: absolute; top: 50%; height: 100px; margin-top: -50px; /* account for padding and border if not using box-sizing: border-box; */ }

Here's a link to some examples:

https://css-tricks.com/centering-css-complete-guide/

Jonathan Etienne
Jonathan Etienne
24,572 Points

Hi,

You might want to look into CSS3's Flexbox Properties (justify-content is what you will need):

https://scotch.io/tutorials/a-visual-guide-to-css3-flexbox-properties

All the best,

Jonathan Etienne

h2 {
justify-content: center; }

I tried the following on my CSS page. It did not work. Any tips???

Jonathan Etienne
Jonathan Etienne
24,572 Points

Hi Donald,

You're not using flexbox properly. As the guide mentions:

https://scotch.io/tutorials/a-visual-guide-to-css3-flexbox-properties

1) To use flexbox layout just set the display property on the parent HTML element:

display: -webkit-flex; /* Safari */
display: flex;  

2) You apply the properties to center horizontally

  -webkit-justify-content: center; /* Safari */
  justify-content:   center;

3) You apply the property to center vertically

  -webkit-align-items: center; /* Safari */
  align-items:   center;

Hence,

 h2{
   display: -webkit-flex; /* Safari */
   display: flex;
   -webkit-justify-content: center; /* Safari */
    justify-content:   center;
    -webkit-align-items: center; /* Safari */
    align-items: center;
    }

Step 1 is absolute necessity as it defines the parent, and makes properties applicable.

Still no resolution trying justify_content:

Jonathan Etienne
Jonathan Etienne
24,572 Points

try

h2{ 
display: -webkit-flex; /* Safari */ 
display: flex; -webkit-justify-content: center; /* Safari */ 
justify-content: center; 
-webkit-align-items: center; /* Safari */ 
align-items: center; 
}

table { display: -webkit-flex; /* Safari / display: flex; -webkit-justify-content: center; / Safari / justify-content: center; -webkit-align-items: center; / Safari */ align-items: center; } This code worked for h2 and worked for a table. It would NOT work when an id was used as the selector.
Any id what might be wrong???/

Jaime Williams
Jaime Williams
8,926 Points

Specificity, id's are at the top of the ladder (well after inline). Most likely that id has been called before and it is my understanding you can only use an id reference once in a page.

CodePen Specificity

Smashing Magazine Specificity

Screwlewse id-selectors

Okay, the last reference you have to take into account the domain name but the writer does correctly attribute the id singleton issue and the statement about reusability.

Jonathan Etienne
Jonathan Etienne
24,572 Points

ID can only be reference once. If you want to reference multiple-times, use classes

For most things, I usually use this method from the css tricks guide:

<blockquote> If padding isn't an option for some reason, and you're trying to center some text that you know will not wrap, there is a trick were making the line-height equal to the height will center the text.

.center-text-trick { height: 100px; line-height: 100px; white-space: nowrap; } </blockquote>

Sorry still working on this markup thing.