如何使用css避免显示特定div元素的块

时间:2016-08-18 17:37:03

标签: html css html-table responsive

在我的媒体查询中,我设置了要显示的表:block。所以现在显示:block现在适用于网站的所有元素。如何避免显示:阻止所以它不适用于一个特定的div元素。

<div id="content">
  <h1>Article Question is</h1>
  <p>Below I have table inside the article. How to avoid display block for the table below on mobile screens?</p>
<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>
</div>  
{{1}}

2 个答案:

答案 0 :(得分:2)

这样的事可能吗?

@media only screen and (max-width: 460px) {

 /* Force table to not be like tables anymore  */
    table, th, tbody, td, thead { 
        display: block; 
        width: 100%;
    }

    #content table, #content th, #content tbody, #content td, #content thead {
        display: initial;
    }

}

答案 1 :(得分:2)

您也可以使用:not来避免#content表。

@media only screen and (max-width: 460px) {


 /* Force table to not be like tables anymore  */
    table:not(#content table),
    th:not(#content th),
    tbody:not(#content tbody),
    td:not(#content td),
    thead:not(#content thead) { 
        display: block; 
        width: 100%;
    }

#content {


    }

 }

这个或@ KodieGrantham的解决方案应该工作;哪一个可能是一个偏好的问题。