सीएसएस लेआउट - क्षैतिज और लंबवत संरेखण
केंद्र तत्व
क्षैतिज और लंबवत
केंद्र संरेखित तत्व
एक ब्लॉक तत्व को क्षैतिज रूप से केन्द्रित करने के लिए (जैसे <div>), उपयोग करें margin: auto;
तत्व की चौड़ाई निर्धारित करने से वह अपने कंटेनर के किनारों तक फैलने से रोकेगा।
तत्व तब निर्दिष्ट चौड़ाई लेगा, और शेष स्थान को दो मार्जिन के बीच समान रूप से विभाजित किया जाएगा:
यह div तत्व केंद्रित है।
उदाहरण
.center
{
margin: auto;
width: 50%;
border: 3px solid green;
padding: 10px;
}
नोट:width
यदि गुण सेट नहीं है (या 100% पर सेट है) तो केंद्र संरेखण का कोई प्रभाव नहीं पड़ता है।
केंद्र संरेखित पाठ
किसी तत्व के अंदर टेक्स्ट को केंद्र में रखने के लिए, उपयोग करें text-align: center;
यह पाठ केंद्रित है।
उदाहरण
.center {
text-align: center;
border: 3px solid green;
}
टिप: टेक्स्ट को अलाइन करने के तरीके के बारे में अधिक उदाहरणों के लिए, CSS टेक्स्ट चैप्टर देखें।
एक छवि केन्द्रित करें
एक छवि को केन्द्रित करने के लिए, बाएँ और दाएँ हाशिये को सेट करें auto
और इसे एक block
तत्व में बनाएँ:
उदाहरण
img
{
display: block;
margin-left: auto;
margin-right: auto;
width: 40%;
}
बाएँ और दाएँ संरेखित करें - स्थिति का उपयोग करना
तत्वों को संरेखित करने के लिए एक विधि का उपयोग करना है position: absolute;
:
मेरे छोटे और अधिक कमजोर वर्षों में मेरे पिता ने मुझे कुछ सलाह दी थी कि मैं तब से अपने दिमाग में बदल रहा हूं।
उदाहरण
.right
{
position: absolute;
right: 0px;
width: 300px;
border: 3px solid #73AD21;
padding: 10px;
}
नोट: पूर्ण स्थित तत्व सामान्य प्रवाह से हटा दिए जाते हैं, और तत्वों को ओवरलैप कर सकते हैं।
बाएँ और दाएँ संरेखित करें - फ्लोट का उपयोग करना
तत्वों को संरेखित करने का एक अन्य तरीका float
संपत्ति का उपयोग करना है:
उदाहरण
.right
{
float: right;
width: 300px;
border: 3px solid #73AD21;
padding: 10px;
}
क्लियरफिक्स हैक
नोट: यदि कोई तत्व उसके साथ वाले तत्व से लंबा है, और वह तैरता है, तो वह अपने कंटेनर के बाहर बह जाएगा। आप इसे ठीक करने के लिए "क्लियरफिक्स हैक" का उपयोग कर सकते हैं (नीचे उदाहरण देखें)।
क्लियरफिक्स के बिना
क्लियरफिक्स के साथ
फिर हम इस समस्या को ठीक करने के लिए क्लीयरफिक्स हैक को युक्त तत्व में जोड़ सकते हैं:
उदाहरण
.clearfix::after {
content: "";
clear: both;
display: table;
}
लंबवत केंद्र - पैडिंग का उपयोग करना
There are many ways to center an element vertically in CSS. A simple solution is to use top and bottom padding
:
I am vertically centered.
Example
.center {
padding: 70px 0;
border: 3px solid
green;
}
To center both vertically and horizontally, use padding
and text-align: center
:
I am vertically and horizontally centered.
Example
.center {
padding: 70px 0;
border: 3px solid
green;
text-align: center;
}
Center Vertically - Using line-height
Another trick is to use the line-height
property with a value that is equal
to the height
property:
I am vertically and horizontally centered.
Example
.center {
line-height: 200px;
height: 200px;
border: 3px solid green;
text-align: center;
}
/* If the text has multiple lines, add the
following: */
.center p {
line-height: 1.5;
display: inline-block;
vertical-align: middle;
}
Center Vertically - Using position & transform
If padding
and line-height
are not options, another solution is to use positioning and the transform
property:
I am vertically and horizontally centered.
Example
.center {
height: 200px;
position: relative;
border: 3px solid green;
}
.center p {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Tip: You will learn more about the transform property in our 2D Transforms Chapter.
Center Vertically - Using Flexbox
You can also use flexbox to center things. Just note that flexbox is not supported in IE10 and earlier versions:
Example
.center {
display: flex;
justify-content: center;
align-items: center;
height: 200px;
border: 3px solid
green;
}
Tip: You will learn more about Flexbox in our CSS Flexbox Chapter.