简体   繁体   English

@media在CSS上工作。 是否有适用于html的内容?

[英]@media works on css. Is there something for html?

The @media works in the css section of an html file. @media在html文件的css部分中起作用。 However, I want a centered < table > with one column if the screen is below 600px wide or with 2 columns if it is greater than 600px. 但是,如果屏幕宽度小于600像素,我想要居中的<table>带有一列;如果宽度大于600像素,我想要具有2列。

The exact same stuff will be in the table but arranged differently. 完全相同的内容将在表中,但排列方式不同。 Elements in the table (select boxes) will have the same id in both layouts. 表格中的元素(选择框)在两种布局中都具有相同的ID。

Is there any way of doing this? 有什么办法吗?

So in 600px or less I want: 所以我希望在600px或更小范围内:

<table>
    <tr>
        <td>Stuff 1</td>
    </tr>
    <tr>
        <td>Stuff 2</td>
    </tr>
    <tr>
        <td>Stuff 3</td>
    </tr>
    <tr>
        <td>Stuff 4</td>
    </tr>
</table>

But in greater than 600px I want: 但是我想要大于600像素:

<table>
    <tr>
        <td>Stuff 1</td>
        <td>Stuff 2</td>
    </tr>
    <tr>
        <td>Stuff 3</td>
        <td>Stuff 4</td>
    </tr>
</table>

You can do something like this and only use a single table since they will have the same content. 您可以执行类似的操作,并且仅使用一个表,因为它们将具有相同的内容。

 table, tr, td { display:block; } @media only screen and (min-width: 600px){ table { display:table; } tr { display:table-row; } td { display:table-cell; } } 
 <table> <tr> <td>Stuff 1</td> <td>Stuff 2</td> </tr> <tr> <td>Stuff 3</td> <td>Stuff 4</td> </tr> </table> 

Responsive grid approach: 响应式网格方法:

As Isherwood stated in his comment: 正如伊舍伍德在评论中所说:

Seems to me like a table is the wrong approach here anyway. 在我看来,无论如何这里的桌子都是错误的方法。 Tables are for data that have a row/column relationship. 表用于具有行/列关系的数据。 This apparently doesn't, and the OP should be using a responsive grid instead. 这显然不是,OP应该使用响应网格。 – isherwood –伊瑟伍德

This is probably best done without tables. 如果没有表,最好这样做。

 div {box-sizing:border-box; width:100%;} @media only screen and (min-width:600px) { .half { float: left; margin: 0; width: 50%; } .row:after { content:""; display:table; clear:both; } } 
 <div class="container"> <div class="row"> <div class="half">Stuff 1</div> <div class="half">Stuff 2</div> </div> <div class="row"> <div class="half">Stuff 3</div> <div class="half">Stuff 4</div> </div> </div> 

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

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