2013-04-25 11 views
10

मैं सामग्री ए, सामग्री बी, और सामग्री सी कॉलम क्षैतिज रूप से केंद्रित होना चाहता हूं। मार्जिन:: मैं इस कोडक्षैतिज तालिका-कक्ष को कैसे केंद्रित करें

http://jsfiddle.net/hsX5q/24/

एचटीएमएल जोड़ने के लिए कोशिश कर रहा है .columns-कंटेनर के लिए 0 ऑटो और यह काम नहीं करता। क्या कोई मदद कर सकता है?

/************************* 
 
* Sticky footer hack 
 
* Source: http://pixelsvsbytes.com/blog/2011/09/sticky-css-footers-the-flexible-way/ 
 
************************/ 
 

 
/* Stretching all container's parents to full height */ 
 

 
html, 
 
body { 
 
    height: 100%; 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 
/* Setting the container to be a table with maximum width and height */ 
 

 
#container { 
 
    display: table; 
 
    height: 100%; 
 
    width: 100%; 
 
} 
 
/* All sections (container's children) should be table rows with minimal height */ 
 

 
.section { 
 
    display: table-row; 
 
    height: 1px; 
 
} 
 
/* The last-but-one section should be stretched to automatic height */ 
 

 
.section.expand { 
 
    height: auto; 
 
} 
 
/************************* 
 
* Full height columns 
 
************************/ 
 

 
/* We need one extra container, setting it to full width */ 
 

 
.columns-container { 
 
    display: table-cell; 
 
    height: 100%; 
 
    width: 300px; 
 
    margin: 0 auto; 
 
} 
 
/* Creating columns */ 
 

 
.column { 
 
    /* The float:left won't work for Chrome for some reason, so inline-block */ 
 
    display: inline-block; 
 
    /* for this to work, the .column elements should have NO SPACE BETWEEN THEM */ 
 
    vertical-align: top; 
 
    height: 100%; 
 
    width: 100px; 
 
} 
 
/**************************************************************** 
 
* Just some coloring so that we're able to see height of columns 
 
****************************************************************/ 
 

 
header { 
 
    background-color: yellow; 
 
} 
 
#a { 
 
    background-color: pink; 
 
} 
 
#b { 
 
    background-color: lightgreen; 
 
} 
 
#c { 
 
    background-color: lightblue; 
 
} 
 
footer { 
 
    background-color: purple; 
 
}
<div id="container"> 
 
    <header class="section"> 
 
    foo 
 
    </header> 
 

 
    <div class="section expand"> 
 
    <div class="columns-container"> 
 
     <div class="column" id="a"> 
 
     <p>Contents A</p> 
 
     </div> 
 
     <div class="column" id="b"> 
 
     <p>Contents B</p> 
 
     </div> 
 
     <div class="column" id="c"> 
 
     <p>Contents C</p> 
 
     </div> 
 
    </div> 
 
    </div> 
 

 
    <footer class="section"> 
 
    bar 
 
    </footer> 
 
</div>

+1

साइड-टिप्पणी केंद्र के लिए : चूंकि '। कॉलम-कंटेनर' टेबल-सेल के रूप में प्रदर्शित होता है, मार्जिन इसे लागू नहीं किया जा सकता है, यही कारण है कि यह चाल यहां काम नहीं करती है। – animuson

उत्तर

20

आप .columns-container के लिए घोषणाओं को text-align: center जोड़ देते हैं तो फिर वे केन्द्र संरेखित:

.columns-container { 
    display: table-cell; 
    height: 100%; 
    width:600px; 
    text-align: center; 
} 

/************************* 
 
* Sticky footer hack 
 
* Source: http://pixelsvsbytes.com/blog/2011/09/sticky-css-footers-the-flexible-way/ 
 
************************/ 
 

 
/* Stretching all container's parents to full height */ 
 

 
html, 
 
body { 
 
    height: 100%; 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 
/* Setting the container to be a table with maximum width and height */ 
 

 
#container { 
 
    display: table; 
 
    height: 100%; 
 
    width: 100%; 
 
} 
 
/* All sections (container's children) should be table rows with minimal height */ 
 

 
.section { 
 
    display: table-row; 
 
    height: 1px; 
 
} 
 
/* The last-but-one section should be stretched to automatic height */ 
 

 
.section.expand { 
 
    height: auto; 
 
} 
 
/************************* 
 
* Full height columns 
 
************************/ 
 

 
/* We need one extra container, setting it to full width */ 
 

 
.columns-container { 
 
display: table-cell; 
 
height: 100%; 
 
width:600px; 
 
text-align: center; 
 
} 
 
/* Creating columns */ 
 

 
.column { 
 
    /* The float:left won't work for Chrome for some reason, so inline-block */ 
 
    display: inline-block; 
 
    /* for this to work, the .column elements should have NO SPACE BETWEEN THEM */ 
 
    vertical-align: top; 
 
    height: 100%; 
 
    width: 100px; 
 
} 
 
/**************************************************************** 
 
* Just some coloring so that we're able to see height of columns 
 
****************************************************************/ 
 

 
header { 
 
    background-color: yellow; 
 
} 
 
#a { 
 
    background-color: pink; 
 
} 
 
#b { 
 
    background-color: lightgreen; 
 
} 
 
#c { 
 
    background-color: lightblue; 
 
} 
 
footer { 
 
    background-color: purple; 
 
}
<div id="container"> 
 
    <header class="section"> 
 
    foo 
 
    </header> 
 

 
    <div class="section expand"> 
 
    <div class="columns-container"> 
 
     <div class="column" id="a"> 
 
     <p>Contents A</p> 
 
     </div> 
 
     <div class="column" id="b"> 
 
     <p>Contents B</p> 
 
     </div> 
 
     <div class="column" id="c"> 
 
     <p>Contents C</p> 
 
     </div> 
 
    </div> 
 
    </div> 
 

 
    <footer class="section"> 
 
    bar 
 
    </footer> 
 
</div>

हालांकि, यह आवश्यक है कि आप .column तत्वों को text-align: left पर रीसेट करें (मान लें कि चाहते हैं कि वे बाएं-गठबंधन हों, जाहिर है (JS Fiddle demo)।

+0

ठीक काम करता है, धन्यवाद :) – Daniel

+0

इसने मुझे एक पल के लिए उलझन में डाल दिया क्योंकि मैं एक ही चीज़ को हासिल करने की कोशिश कर रहा हूं, लेकिन उपरोक्त वर्ग की चौड़ाई अनावश्यक है, यह सेल वास्तव में 100% चौड़ाई है। टेबल-सेल्स दुर्भाग्य से जितना संभव हो उतना क्षैतिज कमरा लेते हैं। – Dave

+1

भूलें कि इसके लिए काम करने के लिए, आपको '.column' को 'डिस्प्ले' होना चाहिए: इनलाइन-ब्लॉक;'। ओपी के पास पहले से ही था, लेकिन अन्य सोच रहे होंगे कि उत्तर उनके लिए केवल 'टेक्स्ट-एलाइन: सेंटर' के साथ क्यों काम नहीं कर रहा है? – poshest

1

बस भावी आगंतुकों के लिए

.column p{ 
    text-align:center; 
} 
1

लघु स्निपेट अपने सीएसएस में इस वर्ग को जोड़ने - कैसे क्षैतिज तालिका सेल (+ खड़ी)

html, body { 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 

 
.tab { 
 
    display: table; 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 

 
.cell { 
 
    display: table-cell; 
 
    vertical-align: middle; 
 
    text-align: center; /* the key */ 
 
    background-color: #EEEEEE; 
 
} 
 

 
.content { 
 
    display: inline-block; /* important !! */ 
 
    width: 100px; 
 
    background-color: #00FF00; 
 
}
<div class="tab"> 
 
    <div class="cell"> 
 
    <div class="content" id="a"> 
 
     <p>Content</p> 
 
    </div> 
 
    </div> 
 
</div>

संबंधित मुद्दे