Click here to Skip to main content
15,890,690 members
Articles / Web Development / HTML
Tip/Trick

How to make DIV center of the screen using pure HTML and CSS

Rate me:
Please Sign up or sign in to vote.
4.58/5 (12 votes)
2 May 2014CPOL 90.1K   16   23
This is simple tricks which allows you to make your DIV on center of the screen without using Javascript.

Introduction

This is simple tricks which allows you to make your DIV (which are either Modal Dialog, Popup, Notification, IFrame etc.) on center of the screen without using jQuery or JavaScript. This is purely used HTML and CSS.

Using this code it will make your Div center of the screen Horizontally and Vertically.

Using the code

Following are the Style (CSS):

CSS
<style type="text/css">
.dialog-background{
    background: none repeat scroll 0 0 rgba(255, 0, 25, 0.5);
    height: 100%;
    left: 0;
    margin: 0;
    padding: 0;
    position: absolute;
    top: 0;
    width: 100%;
    z-index: 100;
}
.dialog-loading-wrapper {
    background: none repeat scroll 0 0 rgba(0, 0, 0, 0);
    border: 0 none;
    height: 100px;
    left: 50%;
    margin-left: -50px;
    margin-top: -50px;
    position: fixed;
    top: 50%;
    width: 100px;
    z-index: 9999999;
}
.dialog-loading-icon {
    background-color: #FFFFFF !important;
    border-radius: 13px;
    display: block;
    height: 100px;
    line-height: 100px;
    margin: 0;
    padding: 1px;
    text-align: center;
    width: 100px;
}
    </style>

Following are the HTML:

HTML
<div class="dialog-background">
    <div class="dialog-loading-wrapper">
        <span class="dialog-loading-icon">Loading....</span>
    </div>
</div> 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Infostretch Ahmedabad-Gujarat
India India
Aspiring for a challenging carrier wherein I can learn, grow, expand and share my existing knowledge in meaningful and coherent way.

sunaSaRa Imdadhusen


AWARDS:

  1. 2nd Best Mobile Article of January 2015
  2. 3rd Best Web Dev Article of May 2014
  3. 2nd Best Asp.Net article of MAY 2011
  4. 1st Best Asp.Net article of SEP 2010


Read More Articles...

Comments and Discussions

 
QuestionShow/Hide Pin
Member 111861556-Aug-15 22:59
Member 111861556-Aug-15 22:59 
GeneralMy vote of 1 Pin
sieunhantanbao11-May-14 15:06
sieunhantanbao11-May-14 15:06 
GeneralMy vote of 2 Pin
Anh Tu Nguyen7-May-14 20:25
Anh Tu Nguyen7-May-14 20:25 
GeneralMy vote of 1 Pin
JB03014-May-14 15:38
professionalJB03014-May-14 15:38 
GeneralRe: My vote of 1 Pin
Sunasara Imdadhusen6-May-14 1:39
professionalSunasara Imdadhusen6-May-14 1:39 
QuestionTotal crap Pin
Florian Rappl3-May-14 23:59
professionalFlorian Rappl3-May-14 23:59 
This is not the solution to the simple problem. The following CSS does that (just look at the center class):

CSS
body  {
  position: relative;
}
.absolute {
  position: absolute;
}
.center {
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
}
.sample-box {
  width: 200px;
  height: 200px;
  background: red;
  color: white;
}


applied e.g. to the following HTML:

HTML
<body><div class="absolute center sample-box"></div></body>


Quite simple, or ? The solution you post is usually the one I see from CSS beginners. Sorry for strong words, but I really think we do not need more posts from people who do not understand CSS. This will give beginners the wrong guidance.

Understanding CSS is more than just knowing the basic syntax and some properties with a bunch of values. Fully understanding CSS is only possible if you understand how a browser uses the CSS rules for rendering content. To achieve this you will have to go over the W3C specification.
AnswerRe: Total crap Pin
Sunasara Imdadhusen6-May-14 1:41
professionalSunasara Imdadhusen6-May-14 1:41 
GeneralRe: Total crap Pin
Florian Rappl6-May-14 22:44
professionalFlorian Rappl6-May-14 22:44 
GeneralRe: Total crap Pin
Sunasara Imdadhusen6-May-14 22:48
professionalSunasara Imdadhusen6-May-14 22:48 
QuestionLess code Pin
Gary Henning2-May-14 3:38
Gary Henning2-May-14 3:38 
AnswerRe: Less code Pin
Nitij2-May-14 6:54
professionalNitij2-May-14 6:54 
GeneralRe: Less code Pin
Gary Henning5-May-14 7:00
Gary Henning5-May-14 7:00 
GeneralRe: Less code Pin
Sunasara Imdadhusen6-May-14 1:55
professionalSunasara Imdadhusen6-May-14 1:55 
GeneralRe: Less code Pin
Nitij6-May-14 2:36
professionalNitij6-May-14 2:36 
QuestionFYI Pin
Wombaticus2-May-14 2:53
Wombaticus2-May-14 2:53 
AnswerRe: FYI Pin
Sunasara Imdadhusen2-May-14 2:57
professionalSunasara Imdadhusen2-May-14 2:57 
GeneralRe: FYI Pin
Florian Rappl4-May-14 0:07
professionalFlorian Rappl4-May-14 0:07 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun1-May-14 21:58
Humayun Kabir Mamun1-May-14 21:58 
GeneralRe: My vote of 5 Pin
Sunasara Imdadhusen1-May-14 22:51
professionalSunasara Imdadhusen1-May-14 22:51 
GeneralNice tips Pin
Asutosha1-May-14 8:01
professionalAsutosha1-May-14 8:01 
GeneralRe: Nice tips Pin
Sunasara Imdadhusen1-May-14 20:00
professionalSunasara Imdadhusen1-May-14 20:00 
GeneralMy vote of 4 Pin
Mahesh@Dev1-May-14 5:58
Mahesh@Dev1-May-14 5:58 
GeneralRe: My vote of 4 Pin
Sunasara Imdadhusen1-May-14 19:37
professionalSunasara Imdadhusen1-May-14 19:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.