简体   繁体   English

如何创建具有 flex-direction: column; 的表?

[英]How to create a table with flex-direction: column;?

My goal is to create a table with flex-direction: column;我的目标是创建一个带有flex-direction: column;的表flex-direction: column; . .

Ticker Price  --.--
  --   Volume    --

index.html索引.html

<div class="d-flex">
    <div class="p-1">
        Ticker
        <div id="stockSymbol" class="font-weight-bold display-4">--</div>
    </div>
    <div class="d-flex flex-column p-1">
        <div class="d-flex">
            Price
            <div id="stockPrice" class="p-1 font-weight-bold display-4">--.--</div>
        </div>
        <div class="d-flex">
            Volume
            <div id="stockVolume" class="p-1">--</div>
        </div>
    </div>
</div>

styles.css样式文件

.p-1 {
  padding: 1rem;
}

.d-flex {
  display: flex;
}

.flex-column {
  flex-direction: column;
}

.font-weight-bold {
  font-weight: bold;
}

.display-4 {
  font-size: 2rem;
}

My expected result: I can use the text-align: center;我的预期结果:我可以使用text-align: center; to make the stockPrice and stockVolume looks aligned.使stockPricestockVolume看起来对齐。

My actual result: the text-align: center;我的实际结果: text-align: center; does not affect the view.不影响观看。

What I've considered:我考虑过的:

  1. Use the HTML tables.使用 HTML 表格。 Per my knowledge, it's not mobile friendly, especially if the first column direction is to below, and the 2nd and 3rd column direction is to the right.据我所知,它不适合移动设备,尤其是当第一列方向在下方,第二列和第三列方向在右侧时。

Here you go!干得好! I used flex-direction column我使用了 flex-direction 列

I added quite a bit to the CSS, but that was just to demonstrate what the table is doing, so if you need any of the colors/margins removed;我在 CSS 中添加了很多内容,但这只是为了演示表格的作用,所以如果您需要删除任何颜色/边距; or anything changed let me know.或任何改变让我知道。

 html, body { height: 100%; margin: 0; } .grid2x2 { min-height: 60%; width: 60%; display: flex; flex-wrap: wrap; flex-direction: row; } .grid2x2 > div { display: flex; flex-basis: calc(50% - 40px); justify-content: center; flex-direction: column; } .grid2x2 > div > div { display: flex; justify-content: center; flex-direction: row; } .box { margin: 3px; } .box1 { background-color: red; } .box2 { background-color: orange; } .box3 { background-color: purple; } .box4 { background-color: grey; }
 <div class="grid2x2"> <div class="box box1"><div>Price</div></div> <div class="box box2"><div>--.--</div></div> <div class="box box3"><div>Volume</div></div> <div class="box box4"><div>--</div></div> </div>

Here's my take on your problem of using flex-direction: column to create a table.这是我对您使用flex-direction: column创建表格的问题的看法。 Through this approach you can use the div class="col" to append data columns to the right of Price-Volume column.通过这种方法,您可以使用div class="col"将数据列附加到价格-成交量列的右侧。

 .table { display: flex; column-gap: 5px; margin: 10px; border: 1px solid black; width: fit-content; width: -moz-fit-content; } .col { display: flex; flex-direction: column; justify-content: center; row-gap: 5px; } .col div { background: beige; } .align-center { /* align-self: center; */ text-align: center; } .align-right { /* align-self: flex-end; */ text-align: right; }
 <div class="table"> <div class="col"> <div class="align-center">Ticker</div> <div class="align-center">--</div> </div> <div class="col"> <div class="align-center">Price</div> <div class="align-center">Volume</div> </div> <div class="col"> <div class="align-right">--.--</div> <div class="align-right">--</div> </div> </div> <div class="table"> <div class="col"> <div class="align-center">Ticker</div> <div class="align-center">--</div> </div> <div class="col"> <div class="align-center">Price</div> <div class="align-center">Volume</div> </div> <div class="col"> <div class="align-right">98.56</div> <div class="align-right">20</div> </div> <div class="col"> <div class="align-right">72.03</div> <div class="align-right">13</div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM