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 can i center my button?

I am working on an page and i want to center my button in middle of the screen. https://gyazo.com/ca4bf6447d1500fae9f6f1b158d88171

.btn {
    border: none;
    font-family: inherit;
    font-size: inherit;
    color: inherit;
    background: none;
    cursor: pointer;
    padding: 25px 80px;
    display: inline-block;
    margin: 15px 30px;
    text-transform: uppercase;
    letter-spacing: 1px;
    font-weight: 700;
    outline: none;
    position: relative;
    -webkit-transition: all 0.3s;
    -moz-transition: all 0.3s;
    transition: all 0.3s;
}

.btn:after {
    content: '';
    position: absolute;
    z-index: -1;
    -webkit-transition: all 0.3s;
    -moz-transition: all 0.3s;
    transition: all 0.3s;
}

/* Button 1 */
.btn-1 {
    border: 3px solid #fff;
    color: #fff;
}

/* Button 1e */
.btn-1e {
    overflow: hidden;
}

.btn-1e:after {
    width: 100%;
    height: 0;
    top: 50%;
    left: 50%;
    background: #fff;
    opacity: 0;
    -webkit-transform: translateX(-50%) translateY(-50%) rotate(45deg);
    -moz-transform: translateX(-50%) translateY(-50%) rotate(45deg);
    -ms-transform: translateX(-50%) translateY(-50%) rotate(45deg);
    transform: translateX(-50%) translateY(-50%) rotate(45deg);
}

.btn-1e:hover,
.btn-1e:active {
    color: #0e83cd;
}

.btn-1e:hover:after {
    height: 260%;
    opacity: 1;
}

.btn-1e:active:after {
    height: 400%;
    opacity: 1;
}

Mod Note: Edited code to include CSS markup

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Try applying margins of 50% and auto to your css. So you'll have the browser automatically count the margin to the left and right of the containing element and center it vertically too.

.btn {
    margin: 50% auto;
}
Amrit Pandey
Amrit Pandey
17,595 Points
body{
  display:flex;
  justify-content:center;
}
.btn{
  align-self:center;
}