我使用flex框創建表格視圖。 但不像html表格,其中行是單元格(列)的容器,我需要列作爲容器。對齊flex表中的行高度
這個要求完全來自響應式設計,當在較小的屏幕上時,可以在彼此堆疊的行上封裝行。
列數和行數將是已知的,但行內的內容將不同,因此行高將在相鄰列上不同。
我無法通過純CSS獲取行高度。我試過不同的flexbox解決方案,但都沒有提供100%的解決方案。我試圖避免JS解決方案。
我能夠得到的最接近的結果是使用基於flex和flex-grow的屬性,但必須爲flex基礎賦予任意值,並且使得某些單元格比它們必須大得多。另外flex框開始重疊下面的內容。
見我的小提琴:https://jsfiddle.net/k6bfyv1p/
$(function() {
$('#fix1').click(fix1Clicked);
function fix1Clicked() {
\t // Add or remove fix1 class to content-table div.
var cssClass = 'fix1';
var $button = $(this);
var $table = $('.content-table');
var hasFix1 = $table.hasClass(cssClass);
if (hasFix1) {
$table.removeClass(cssClass);
$button.html('Apply Fix1');
}
else {
$table.addClass(cssClass);
$button.html('Remove Fix1');
}
}
});
/*** FIX 1 ***/
.fix1 .row {
flex-grow: 1;
flex-shrink: 0;
flex-basis: 50%;
}
/*** Main CSS ***/
.content-table {
width: 500px;
display: flex;
flex-direction: row;
}
.col {
display: flex;
flex-direction: column;
width: 33.3%;
}
.row {
display: flex;
}
.row1 {
background-color: #d0d0d0;
}
.row2 {
background-color: #d0f0d0;
}
.row3 {
background-color: #d0d0f0;
}
.row4 {
background-color: #f0d0d0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3>Known (constant) properties of the table:</h3>
<ul>
<li>Number of columns</li>
<li>Number of rows</li>
</ul>
<button type="button" id="fix1">Apply Fix1</button>
<br><br>
<div class="content-table">
<div class="col col1">
<div class="row row1">
Column 1 Row 1
</div>
<div class="row row2">
Column 1<br />Row 2
</div>
<div class="row row3">
Column 1 Row 3
</div>
<div class="row row4">
Column 1<br /><br /><br /><br />Row 4
</div>
</div>
<div class="col col2">
<div class="row row1">
Column 2<br /><br /><br />Row 1
</div>
<div class="row row2">
Column 2<br/>Row 2
</div>
<div class="row row3">
Column 2<br /><br/>Row 3
</div>
<div class="row row4">
Column 2 Row 4
</div>
</div>
<div class="col col3">
<div class="row row1">
Column 3<br />Row 1
</div>
<div class="row row2">
Column 3<br/><br/>Row 2
</div>
<div class="row row3">
Column 3<br />Row 3
</div>
<div class="row row4">
Column 3<br>Row 4
</div>
</div>
</div>
<p>
Lorem ipsum dolor sit amet, modus invidunt intellegam pri te, possit tritani id nec. Cu summo oratio honestatis per. Eum essent accumsan qualisque id. Animal molestie mel te, nec at elit fierent omittam, nec option fabulas ea. Iudico moderatius ad usu, eu modo melius qui, homero malorum usu no.
Lorem ipsum dolor sit amet, modus invidunt intellegam pri te, possit tritani id nec. Cu summo oratio honestatis per. Eum essent accumsan qualisque id. Animal molestie mel te, nec at elit fierent omittam, nec option fabulas ea. Iudico moderatius ad usu, eu modo melius qui, homero malorum usu no.
</p>
令人驚歎的,謝謝!修改建議修正:https://jsfiddle.net/h0ykgnyp/1/ – Patrick