简体   繁体   English

根据屏幕宽度将表格单元格或列表项目移动到第二行

[英]Move table cells or list items to second row depending on screen width

My question is simple. 我的问题很简单。

First off, take a look at this code: 首先,看看这段代码:

http://jsfiddle.net/Jk8Nr/ http://jsfiddle.net/Jk8Nr/

All I have is a very simple table with some padding, borders, text-alignment. 我只有一个非常简单的表,有一些填充,边框,文本对齐。

If you visit the jsFiddle above, you can increase the width of the results window and all of the table items will site comfortably. 如果您访问上面的jsFiddle,您可以增加结果窗口的宽度,并且所有表项都将舒适地放置。 However, if you decrease the width of said window, I want the table cells to move the the second row in order so that side-scrolling isn't required. 但是,如果减小所述窗口的宽度,我希望表格单元格按顺序移动第二行,以便不需要横向滚动。

I don't mind using <ul><li> for this instead of tables, but I do want to be able to do this. 我不介意使用<ul><li>代替表格,但我确实希望能够这样做。

If anyone is familiar with a solution, please let me know. 如果有人熟悉解决方案,请告诉我。 I don't mind using jQuery/JS/CSS, but I'm not familiar with much else. 我不介意使用jQuery / JS / CSS,但我不熟悉其他的东西。

Update 更新

Here's the updated jsFiddle with working solution: 这是更新的jsFiddle与工作解决方案:

http://jsfiddle.net/Jk8Nr/3/ http://jsfiddle.net/Jk8Nr/3/

add

border-collapse: separate;

to your TABLE-style 你的TABLE风格

and

display: inline-block;

to your TD/TH-Style... 你的TD / TH风格......

cheers. 干杯。

I would strongly suggest using a list for this, ordered or unordered as you see fit; 我强烈建议您根据自己的需要使用清单,有序或无序; if only because of the semantics (a navigation menu is a list of pages to which you may wish to go), whereas a table should present data in a tabular format. 如果只是因为语义(导航菜单是您可能希望去的页面列表),而table应以表格格式显示数据。 That said, I'd propose: 那就是说,我建议:

<header>
    <ul>
        <li>Home</li>
        <li>Gallery</li>
        <li>Location</li>
        <li>Rates</li>
        <li>Contact</li>
    </ul>
</header>​

With the CSS: 使用CSS:

header ul {
    width: 100%;
    font-size: 0; /* to remove the white-space gaps between the li elements */
    text-align: center; /* to center the list, amend as appropriate */
}

header ul li {
    font-size: 16px; /* to explicitly reset the font-size and
                        override that of the parent */
    display: inline-block; /* to allow for wrapping */
    width: 19%; /* a baseline width, if the monitor is wide enough to allow it */
    min-width: 7em; /* a minimum width, if the 19% of the width of the ul is less than
                       7em, the li elements will wrap to the next line, rather than
                       becoming too small to be functional */
    border: 1px solid #000;
    -webkit-box-sizing: border-box; /* To explicitly include the border in the width
                                       of the element */
    -moz-box-sizing: border-box;
    -o-box-sizing: border-box;
    -ms-box-sizing: border-box;
    box-sizing: border-box;
}

JS Fiddle demo . JS小提琴演示

Please use some semantic markup like this: 请使用这样的语义标记:

<header>
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">Gallery</a></li>
            <li><a href="#">Location</a></li>
            <li><a href="#">Rates</a></li>
            <li><a href="#">Contact</a></li>
        </ul>
</header>

And then use css media queries to style your page for smaller screens. 然后使用css媒体查询为较小的屏幕设置页面样式。

header ul {
    width: 100%;
    font-size: 0;
    text-align: center;
}

header ul li {
    font-size: 16px;
    display: inline-block;
    width: 19%;
    min-width: 7em;
    border: 1px solid #000;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    -o-box-sizing: border-box;
    -ms-box-sizing: border-box;
    box-sizing: border-box;
}
@media all and (max-width: 564px) and (min-width: 100px){
    header ul li {
        width:100%;
        display:block;
    }
}

Here is a fiddle http://jsfiddle.net/Jk8Nr/6/ 这是一个小提琴http://jsfiddle.net/Jk8Nr/6/

I agree with the other posters suggesting you use a list instead of a table. 我同意其他海报建议您使用列表而不是表格。 You can then use a media query to force a wrap at a given device-width if you want that level of control over the layout. 然后,如果您希望对布局进行该级别的控制,则可以使用媒体查询强制在给定设备宽度处进行换行。 Here's an example setting a breakpoint at 600px (markup then css) 这是一个设置600px断点的示例(标记然后是css)

<header>
    <ul>
        <li>Home</li>
        <li>Gallery</li>
        <li>Location</li>
        <li>Rates</li>
        <li>Contact</li>
    </ul>
</header>​

ul {
    width:100%;
}
li {
    width:118px;
    float:left;
    clear:left;
    padding:5px 0;
    border: 1px solid #000; 
    border-bottom:none; 
    text-align:center;            
}
li:last-child {
    border-bottom:1px solid #000;
}

@media (min-width:600px) {
    li {
        clear:none;
        border-bottom:1px solid #000;        
        border-right:none;
    }
    li:last-child {
        border-right: 1px solid #000;
    }
}

http://jsfiddle.net/Jk8Nr/8/ http://jsfiddle.net/Jk8Nr/8/

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

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