CSS媒体查询将图像压缩到容器中

时间:2015-06-19 11:26:01

标签: css css3 responsive-design media-queries

我被迫使用基于表格的布局,需要使其响应。 我正在努力处理图像:当图像宽度超过屏幕宽度时,它应缩小以适应屏幕。 在这里的几个附件中,我发现max-width被推荐,但不知怎的,我没有让它工作。

这是我的代码:

.headerImg { max-width: 100%; }
@media (max-width: 600px) {        
    .mainTabWidth {width: 100%;}   
    .halfWitdth   {width:  50%;}
    .qWidth       {width:  25%;}
}
@media (min-width: 601px)  {        
    .mainTabWidth {width: 594px;}   
     .halfWidth    {width: 236px;}
     .qWidth       {width: 113px;}
}
  <body style="font-family: Arial;font-size: 12pt;margin:0px;">
      <table align="center" border="0" cellpadding="0" cellspacing="0"
      style="border-collapse: collapse;" class="mainTabWidth">
        <tbody>
        <tr>
        <td style="padding: 10px;">
    Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
    Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.    
        </td>
        </tr>
          <tr>
            <td><img src="http://mbaas.de/wordle.png" class=
            "headerImg" /></td>
          </tr>
        </tbody>
      </table>
    </body>

我还设置了fiddle(全屏here)。

1 个答案:

答案 0 :(得分:1)

在这种情况下,更好地改变media queries的顺序,加上关于你的形象

  

当图像宽度超过屏幕宽度时,它应缩小以适应   在屏幕上。

这里你需要做什么:

&#13;
&#13;
.mainTabWidth {
  table-layout: fixed;
}
.headerImg {
  max-width: 100%;
}
@media (min-width: 601px) {
  .mainTabWidth {
    width: 594px;
  }
  .halfWidth {
    width: 236px;
  }
  .qWidth {
    width: 113px;
  }
}
@media (max-width: 600px) {
  .mainTabWidth {
    width: 100%;
  }
  .halfWitdth {
    width: 50%;
  }
  .qWidth {
    width: 25%;
  }
}
&#13;
<body style="font-family: Arial;font-size: 12pt;margin:0px;">
  <table align="center" border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse;" class="mainTabWidth">
    <tbody>
      <tr>
        <td style="padding: 10px;">
          <img class="headerImg" src="http://lorempixel.com/1000/300" />
        </td>
      </tr>  
      <tr>
        <td style="padding: 10px;">This text is embedded in a cell of table-row where the table is styled with a media-query, in the width is >607px, the table should be 594px wide and on smaller screens (max-width: 600px), the table should fill the available space to a 100%. But
          if you resize and observe, you will see the contrary: on large screens, the table takes all space and on small screens it takes 594px - so it seems the wrong query is selected...??? Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed
          diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem
          ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea
          takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo
          dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
          sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</td>
      </tr>
    </tbody>
  </table>
</body>
&#13;
&#13;
&#13;