2016-04-26 17 views
-2

Die Anforderung ist, gleiche Höhe und gleiche Breite der Spalte festzulegen. Ich habe CSS-Eigenschaft Display-Tabelle-Zelle für diese verwendet und es funktioniert gut in Bezug auf die gleiche Höhe, aber nicht gut mit, wenn es um gleiche Breite kommt. Ich weiß technisch, dass es gut funktioniert, aber ich möchte es gleich breit machen. Also, ob es vier Spalten oder eine Spalte in einer Tabelle gibt, sollte es mit einer Breite kommen.Gleiche Breite und Höhe Spalte von css

Ich habe auch nach flex gesucht, aber seine Unterstützung ist nicht in IE9 und hat einige Kompatibilitätsprobleme mit mobilen Browsern. Ich habe nachgedacht und es auf viele Arten versucht, aber ich habe keine Lösung gefunden. Hier ist fiddle, wenn Sie Ihre Hand darauf versuchen wollen.

HTML

.table { 
 
    display: table; 
 
    width: 100%; 
 
    height: 100% 
 
} 
 
.cell { 
 
    display: table-cell; 
 
    padding: 0 10px; 
 
    width: 23.8%; 
 
    height: 100% 
 
} 
 
.white-box { 
 
    border: solid 1px #ccc; 
 
    background: #eee; 
 
    height: 100% 
 
}
<div class="table"> 
 
    <div class="cell"> 
 
    <div class="white-box">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</div> 
 
    </div> 
 
    <div class="cell"> 
 
    <div class="white-box">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</div> 
 
    </div> 
 
    <div class="cell"> 
 
    <div class="white-box">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</div> 
 
    </div> 
 
    <div class="cell"> 
 
    <div class="white-box">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</div> 
 
    </div> 
 
</div> 
 
<br> 
 
<div class="table"> 
 
    <div class="cell"> 
 
    <div class="white-box">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</div> 
 
    </div> 
 
    <div class="cell"> 
 
    <div class="white-box">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</div> 
 
    </div> 
 
</div>

+0

Wissen Sie, wie viele Divs Sie insgesamt haben werden? – Bojan

+1

Ich denke nicht, dass es eine doppelte Frage ist. – Carlos

+1

Was genau stimmt nicht mit deiner Geige? –

Antwort

1

Sie können die Breite usine vw Einheiten angeben. Diese Einheit hat jedoch einige Macken. Zum Beispiel enthält es Bildlaufleisten und Rand/Padding auf Körper.

Wenn Sie also auf 25% der verfügbaren Breite ausgerichtet sind, müssen Sie etwas wie calc(100vu - 17px - 20px)/4 tun.

.table { 
 
    display: table; 
 
    height: 100%; 
 
} 
 
.cell { 
 
    display: table-cell; 
 
    padding: 0 .5vw; 
 
    width: 22vw; 
 
    height: 100%; 
 
} 
 
.white-box { 
 
    border: solid 1px #ccc; 
 
    background: #eee; 
 
    height: 100%; 
 
}
<div class="table"> 
 
    <div class="cell"> 
 
    <div class="white-box">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</div> 
 
    </div> 
 
    <div class="cell"> 
 
    <div class="white-box">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</div> 
 
    </div> 
 
    <div class="cell"> 
 
    <div class="white-box">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</div> 
 
    </div> 
 
    <div class="cell"> 
 
    <div class="white-box">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</div> 
 
    </div> 
 
</div> 
 
<br> 
 
<div class="table"> 
 
    <div class="cell"> 
 
    <div class="white-box">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</div> 
 
    </div> 
 
    <div class="cell"> 
 
    <div class="white-box">Lorem Ipsum is simply dummy text of the printing and typesetting industry.</div> 
 
    </div> 
 
</div>

Here is a fiddle mit genaueren Berechnungen.

+0

funktioniert es in IE 9 und mobilen Browser? – Carlos

+0

Warum Breite in Prozent funktioniert nicht? – Carlos

+0

Breite in Prozent wird funktionieren, wenn (i) Tabelle eine Breite hat (ii) Summe der Spaltenbreiten gleich 100% ist. Für zwei Spalten wäre es 50% und der Browser greift auf seine eigenen Berechnungen zurück. –

1

Damit es funktioniert, benötigen Sie 3 Ebenen: Tabelle - Tabelle - Zeile - Tabelle - Zelle.

<div class="table"> 
    <div><!-- style="display:table-row" assumed --> 
     <div class="cell"> 
      <div class="white-box>.....</div> 
     </div> 
     <div class="cell"> 
      <div class="white-box>.....</div> 
     </div> 
    </div> 
    <!-- repeat rows --> 
</div> 
Verwandte Themen