Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

center a panel on screen

I'm using bootstrap and creating a panel. I'm trying to center my panel in the middle of the screen. It's inside a container.

<div class="container body-content">
    <div class="panel panel-default col-md-6">
        <div class="panel-title text-center">Log in</div>
        <div class="panel-body text-center">
             <h6>Use a local account to log in.</h6> 
        </div>
    </div>
</div>
like image 589
user1186050 Avatar asked Feb 05 '15 23:02

user1186050


People also ask

How do I fix a div in the center of the screen?

You basically need to set top and left to 50% to center the left-top corner of the div. You also need to set the margin-top and margin-left to the negative half of the div's height and width to shift the center towards the middle of the div.


2 Answers

Here is one approach:

Example Here

.container .panel {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
}

If you're interested in other approaches, see this answer.

like image 180
Josh Crozier Avatar answered Sep 28 '22 06:09

Josh Crozier


For others, like me, who came here looking to center panel only horizontally and not vertically, col-xx-offset is a quick way to center a panel horizontally.

<div class="panel panel-default col-md-6 col-md-offset-3">

See this answer

like image 22
Anupam Avatar answered Sep 28 '22 04:09

Anupam