亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在調整窗口大小時使表格響應?

如何在調整窗口大小時使表格響應?

郎朗坤 2023-07-29 13:47:25
我有一張桌子,我正在努力使其具有響應能力。當我從右向左調整大小時,它完美收縮在小窗口中,我點擊刷新并嘗試將其大小調整回來這就是我現在為 CSS 所做的@media only screen and (max-width: 1555px) {    td:nth-child(9),th:nth-child(9)  {        display: none;    }    td:nth-child(8),th:nth-child(8)  {        display: none;    }    td:nth-child(7),th:nth-child(7)  {        display: none;    }}@media only screen and (max-width: 1080px) {    td:nth-child(6),th:nth-child(6)  {        display: none;    }    td:nth-child(5),th:nth-child(5)  {        display: none;    }}@media only screen and (max-width: 840px) {    td:nth-child(5),th:nth-child(5)  {        display: none;    }    td:nth-child(4),th:nth-child(4)  {        display: none;    }}注意:我已經在所有瀏覽器類型上嘗試過此操作:Chrome、Firefox、Safari,結果相同。
查看完整描述

4 回答

?
滄海一幻覺

TA貢獻1824條經驗 獲得超5個贊

我建議您使用 display: table-cell啟用所有表格數據 ( td) 和表格標題 ( )。th


@media (min-width: 1300px) and (max-width: 1555px) {


th, td {?

? display: table-cell;

}?


th {

? ? color:brown;

}


td:nth-child(9),th:nth-child(9)? {

? ? display: none;

}


td:nth-child(8),th:nth-child(8)? {

? ? display: none;

}

}



@media (min-width: 1200px) and (max-width: 1300px) {


th, td {?

? display: table-cell;

}


th {

? ? color:brown;

}


td:nth-child(9),th:nth-child(9)? {

? ? display: none;

}


td:nth-child(8),th:nth-child(8)? {

? ? display: none;

}


td:nth-child(7),th:nth-child(7)? {

? ? display: none;

}


}



@media (min-width: 840px) and (max-width: 1200px) {

? ? th, td {?

? display: table-cell;

}


? ? th {

? ? ? ? color:red;

? ? }




? ? td:nth-child(9),th:nth-child(9)? {

? ? ? ? display: none;

? ? }


? ? td:nth-child(8),th:nth-child(8)? {

? ? ? ? display: none;

? ? }


? ? td:nth-child(7),th:nth-child(7)? {

? ? ? ? display: none;

? ? }


? ? td:nth-child(6),th:nth-child(6)? {

? ? ? ? display: none;

? ? }


? ? td:nth-child(4),th:nth-child(4)? {

? ? ? ? display: none;

? ? }




}


@media (min-width: 520px) and (max-width:? 840px) {


? ? th, td {?

? display: table-cell;

}


? ? th {

? ? ? ? color:orange;

? ? }



? ? td:nth-child(9),th:nth-child(9)? {

? ? ? ? display: none;

? ? }


? ? td:nth-child(8),th:nth-child(8)? {

? ? ? ? display: none;

? ? }


? ? td:nth-child(7),th:nth-child(7)? {

? ? ? ? display: none;

? ? }


? ? td:nth-child(6),th:nth-child(6)? {

? ? ? ? display: none;

? ? }


? ? td:nth-child(4),th:nth-child(4)? {

? ? ? ? display: none;

? ? }



? ? td:nth-child(5),th:nth-child(5)? {

? ? ? ? display: none;

? ? }


}


@media (max-width: 520px) {


? ? th, td {?

? ? ?display: table-cell;

? ? }


? ? th {

? ? ? ? color:yellow;

? ? }



? ? td:nth-child(9),th:nth-child(9)? {

? ? ? ? display: none;

? ? }


? ? td:nth-child(8),th:nth-child(8)? {

? ? ? ? display: none;

? ? }


? ? td:nth-child(7),th:nth-child(7)? {

? ? ? ? display: none;

? ? }


? ? td:nth-child(6),th:nth-child(6)? {

? ? ? ? display: none;

? ? }



? ? td:nth-child(5),th:nth-child(5)? {

? ? ? ? display: none;

? ? }


? ? td:nth-child(4),th:nth-child(4)? {

? ? ? ? display: none;

? ? }


? ? td:nth-child(2),th:nth-child(2)? {

? ? ? ? display: none;

? ? }

? ? }

table {

? font-family: arial, sans-serif;

? border-collapse: collapse;

? width: 100%;

}


td, th {

? border: 1px solid #dddddd;

? text-align: left;

? padding: 8px;

}


tr:nth-child(even) {

? background-color: #dddddd;

}


@media (min-width: 520px) and (max-width:? 840px) {


? ? th, td {?

? display: table-cell;

}


? ? th {

? ? ? ? color:orange;

? ? }



? ? td:nth-child(3),th:nth-child(3)? {

? ? ? ? display: none;

? ? }


}


@media (max-width: 520px) {


? ? th, td {?

? ? ?display: table-cell;

? ? }


? ? th {

? ? ? ? color:yellow;

? ? }


? ? td:nth-child(2),th:nth-child(2)? {

? ? ? ? display: none;

? ? }


? ? td:nth-child(3),th:nth-child(3)? {

? ? ? ? display: none;

? ? }? ??

}

<h2>HTML Table</h2>


<table>

? <tr>

? ? <th>Company</th>

? ? <th>Contact</th>

? ? <th>Country</th>

? </tr>

? <tr>

? ? <td>Alfreds Futterkiste</td>

? ? <td>Maria Anders</td>

? ? <td>Germany</td>

? </tr>

? <tr>

? ? <td>Centro comercial Moctezuma</td>

? ? <td>Francisco Chang</td>

? ? <td>Mexico</td>

? </tr>

? <tr>

? ? <td>Ernst Handel</td>

? ? <td>Roland Mendel</td>

? ? <td>Austria</td>

? </tr>

? <tr>

? ? <td>Island Trading</td>

? ? <td>Helen Bennett</td>

? ? <td>UK</td>

? </tr>

? <tr>

? ? <td>Laughing Bacchus Winecellars</td>

? ? <td>Yoshi Tannamuri</td>

? ? <td>Canada</td>

? </tr>

? <tr>

? ? <td>Magazzini Alimentari Riuniti</td>

? ? <td>Giovanni Rovelli</td>

? ? <td>Italy</td>

? </tr>

</table>



查看完整回答
反對 回復 2023-07-29
?
UYOU

TA貢獻1878條經驗 獲得超4個贊

當您重新加載并且處于最小窗口大小時,第二個、第四個、第五個、第六個、第七個、第八個和第九個子級將設置為顯示:無。


當您擴大備份大小時,不會重置其中任何一個以實際顯示。


因此,您需要在較大的媒體尺寸中執行一些操作,將它們設置為在您返回時顯示。


以下是一些關于如何確保元素在較大尺寸下可見的想法。


請閱讀評論,尤其是關于孩子 3 的評論 - 它總是可見嗎?我已經放置了 display: block 來顯示子元素,但我無法知道這是否是元素中正常的設置。請替換為適合您的特定用例的任何內容。


/* DONT FORGET WE NEED TO COPE WITH A WIDE SCREEN SO I'VE ADDED THIS */

    

    td:nth-child,th:nth-child  {

        display: block;

    }

    

/* we need to make sure that child 7 comes back into view */

@media (min-width: 1300px) and (max-width: 1555px) {


    th {

        color:brown;

    }


    td:nth-child(9),th:nth-child(9)  {

        display: none;

    }


    td:nth-child(8),th:nth-child(8)  {

        display: none;

    }

    

    td:nth-child(7),th:nth-child(7)  {

        display: block;

    }

}


/* at this, the 4th smallest, we need to make sure that child 6 comes back into view */

@media (min-width: 1200px) and (max-width: 1300px) {


    th {

        color:brown;

    }


    td:nth-child(9),th:nth-child(9)  {

        display: none;

    }


    td:nth-child(8),th:nth-child(8)  {

        display: none;

    }


    td:nth-child(7),th:nth-child(7)  {

        display: none;

    }

    

    td:nth-child(6),th:nth-child(6)  {

        display: block;

    }


}


/* at this 3rd from smallest we need to make sure that child 5 comes back into view */

@media (min-width: 840px) and (max-width: 1200px) {


    th {

        color:red;

    }




    td:nth-child(9),th:nth-child(9)  {

        display: none;

    }


    td:nth-child(8),th:nth-child(8)  {

        display: none;

    }


    td:nth-child(7),th:nth-child(7)  {

        display: none;

    }


    td:nth-child(6),th:nth-child(6)  {

        display: none;

    }


    td:nth-child(4),th:nth-child(4)  {

        display: none;

    }


    td:nth-child(5),th:nth-child(5)  {

        display: block;

    }


}

/* at the next to smallest the 4, 5, 6, 7 8, 9 children are display: none so we need to make sure that child 2 (and see comment below about child 3) is/are displayed */

@media (min-width: 520px) and (max-width:  840px) {



    th {

        color:orange;

    }



    td:nth-child(9),th:nth-child(9)  {

        display: none;

    }


    td:nth-child(8),th:nth-child(8)  {

        display: none;

    }


    td:nth-child(7),th:nth-child(7)  {

        display: none;

    }


    td:nth-child(6),th:nth-child(6)  {

        display: none;

    }


    td:nth-child(4),th:nth-child(4)  {

        display: none;

    }



    td:nth-child(5),th:nth-child(5)  {

        display: none;

    }

    

    td:nth-child(2),th:nth-child(2)  {

        display: block;

    }

    td:nth-child(3),th:nth-child(3)  { /* just to be on the safe side */

        display: block;

    }

    td:nth-child(2),th:nth-child(2)  {

        display: block;

    }


}

/* when very small the 2, 4, 5, 6, 7, 8, 9 children are display: none. DID YOU MEAN TO KEEP CHILD 2 DISPLAYED? */

@media (max-width: 520px) {



    th {

        color:yellow;

    }



    td:nth-child(9),th:nth-child(9)  {

        display: none;

    }


    td:nth-child(8),th:nth-child(8)  {

        display: none;

    }


    td:nth-child(7),th:nth-child(7)  {

        display: none;

    }


    td:nth-child(6),th:nth-child(6)  {

        display: none;

    }



    td:nth-child(5),th:nth-child(5)  {

        display: none;

    }


    td:nth-child(4),th:nth-child(4)  {

        display: none;

    }


    td:nth-child(2),th:nth-child(2)  {

        display: none;

    }


}



查看完整回答
反對 回復 2023-07-29
?
飲歌長嘯

TA貢獻1951條經驗 獲得超3個贊

對于那些喜歡庫的人,我可以說我強烈推薦 DataTable Library。將 CSS 、動態調整大小等問題留給庫。

查看完整回答
反對 回復 2023-07-29
?
有只小跳蛙

TA貢獻1824條經驗 獲得超8個贊

為什么您如此依賴于td:nth-child(x)影響代碼的可重用性,請使其:即使移動列,也不必觸及 css。


我建議創建實用程序類并將這些類添加到您的表列中。


例如:


.hidden-mobile 

@media (max-width: 450px) {

  display: none;

}

等等。如果您的 HTML/Table 不可訪問,那么不幸的是您必須通過使用 querySelector 的腳本來訪問它,這會影響您的可重用性。


查看完整回答
反對 回復 2023-07-29
  • 4 回答
  • 0 關注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號