सबसे अच्छा समाधान (मेरी राय में) पूर्ण स्थिति का उपयोग करने के 50 में तत्व के ऊपर बाईं जगह है %/50%, फिर नकारात्मक मार्जिन का उपयोग कर तत्व को केंद्र में वापस लाएं। एकमात्र कमी यह है कि आपको तत्व की चौड़ाई और ऊंचाई निर्दिष्ट करना है।यहाँ एक उदाहरण है:
HTML:
<div id="centerme">
Hello, world!
</div>
सीएसएस: http://jsfiddle.net/UGm2V/
तुम सच में केंद्रित सामग्री की आवश्यकता होती है एक के लिए:
#centerme
{
position: absolute;
left: 50%;
top: 50%;
/* You must set a size manually */
width: 100px;
height: 50px;
/* Set negative margins equal to half the size */
margin-left: -50px;
margin-top: -25px;
background-color: cornflowerblue;
}
यहाँ jsfiddle पर एक प्रदर्शन है गतिशील ऊंचाई, एक और अधिक उन्नत समाधान है। सावधान रहें कि यह पुराने आईई ब्राउज़र में काम नहीं करेगा। एचटीएमएल निम्नानुसार है:
<div id="outter-container">
<div id="inner-container">
<div id="centred">
<p>I have a dynamic height!</p>
<p>Sup!</p>
</div>
</div>
</div>
बाहरी कंटेनर को पृष्ठ की चौड़ाई और ऊंचाई को कवर करने की आवश्यकता है। यह पूर्ण स्थिति के साथ एक ब्लॉक तत्व है।
आंतरिक कंटेनर वास्तव में एक टेबल है! यह display: table
सीएसएस संपत्ति द्वारा तय किया गया है। यहां जीत यह है कि आपको वास्तव में किसी भी तालिका HTML की आवश्यकता नहीं है।
# केंद्रित div अंतिम आवश्यक तत्व है। इसमें अभी भी पृष्ठ की चौड़ाई और ऊंचाई का 100% शामिल है, लेकिन इसके अंदर रखी गई कुछ भी लंबवत और क्षैतिज रूप से केंद्रित होगी। यह सीएसएस की जरूरत है, स्पष्टीकरण के साथ है:
/*
An outter container is needed because the table
won't cover the page width and height on it's own
*/
#outter-container
{
position: absolute;
top: 0px;
right: 0px;
bottom: 0px;
left: 0px;
}
/*
The inner container is a table which is set to
cover the width and height of the page.
*/
#inner-container
{
display: table;
width: 100%;
height: 100%;
}
/*
This table cell will cover 100% of the page width
and height, but everything placed inside it will
be placed in the absolute centre.
*/
#centred
{
display: table-cell;
vertical-align: middle;
text-align: center;
}
और बेशक, यहां इसके साथ जाने के लिए एक jsfiddle प्रदर्शन है: http://jsfiddle.net/N7ZAr/3/
क्या इससे मदद मिलती है? http://stackoverflow.com/questions/9656362/div-horizontally-center-and-vertically-middle –
http://stackoverflow.com/questions/49350/practical-solution-to-center-vertically-and का संभावित डुप्लिकेट -horizontally-in-html-that-works-in-f, हालांकि यहां कोई जवाब नहीं है। –
यदि आप उस तत्व की ऊंचाई को जानते हैं जिसे आप केंद्र में रखने की कोशिश कर रहे हैं तो ऊर्ध्वाधर केंद्र (जावास्क्रिप्ट के बिना) केवल तभी संभव है। यदि आपके पास केवल टेक्स्ट की एक पंक्ति है, तो आप उसे भी केंद्र में रख सकते हैं, फिर भी, आपको 'लाइन-ऊंचाई' का उपयोग करके ज्ञात ऊंचाई होगी। – Sparky