ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

Position a div at the center of the page

I want to position a <div> element at the center of the screen irrespective of screen size. The space left on 'right' and 'left' sides should be equal. I would like to accomplish this with only CSS.

 

user-image
تم إضافة السؤال من قبل Mohammed Shaji Vattam Kandathil , Senior Developer , Emirates Arabian Horse Society
تاريخ النشر: 2015/03/26
Faizan Ahmed
من قبل Faizan Ahmed , Lead UI Engineer , Folio3 Private Limited

give the width to div and then give margin left and right 'auto'.

 

div{ width:400px; margin:0 auto; }

Yasir Hussain
من قبل Yasir Hussain , Digitizing Consultant , PROZTEC

give a margin0 and auto to that element it will be at the center

vishal sharaf
من قبل vishal sharaf , DBA & Tech Support , India IT Solution

div{

position:absolute;

left:30%;

top:30%;

width:100px;

height:100px;

}

and adjust this with your customization.

Sathishkumar N
من قبل Sathishkumar N , Web Developer , Admax communications

to position a element at the center of the screen

Just specify the element with the following css code

margin:auto

ramakrishna ramachadra
من قبل ramakrishna ramachadra , Technical Web Analyst , Happy Marketer

<!-- way1 --!>

<style type="text/css">

#mydiv {   

position:absolute; 

   top:50%;  

  left:50%;  

  margin:0 auto;

}

</style>

 

 

 

<!-- way2 --!>

<style type="text/css">

#mydiv

   width:100px; 

   margin:0 auto;   

clear:both;

}

</style>

Asteway Shitaye Woldehitsan
من قبل Asteway Shitaye Woldehitsan , Mobile Application Engineer , golfscape

to align a div center, the width of the child div and the parent div should be defined. for example:

 

========the css=====

.parent{

text-align:center;

width:100%;

}

.child{

width:50%;

text-align:left;

margin:Auto;

}

 

Text-align is used so the centering can also work on IE6 as IE6 doesn't support margin Auto