军哥哥傻乎乎 發表於 2026-5-3 22:16:45

CSS3媒体查询实现不同宽度的下不同内容的展示功能

<h3>前言</h3>
<h3>CSS3 多媒体查询实例</h3>
<p>本章节我们将为大家演示一些多媒体查询实例。</p>
<p>开始之前我们先制作一个电子邮箱的链接列表。HTML 代码如下:</p>
<div class="jb51code"><pre class="brush:xhtml;">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;style&gt;
ul {
    list-style-type: none;
}
ul li a {
    color: green;
    text-decoration: none;
    padding: 3px;
    display: block;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a data-email="johndoe@example.com" href="mailto:johndoe@example.com"&gt;John Doe&lt;/a&gt;&lt;/li &gt;
&lt;li&gt;&lt;a data-email="marymoe@example.com" href="mailto:marymoe@example.com"&gt;Mary Moe&lt;/a&gt;&lt;/li &gt;
&lt;li&gt;&lt;a data-email="amandapanda@example.com" href="mailto:amandapanda@example.com"&gt;Amanda Panda&lt;/a&gt;&lt; /li&gt;
&lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div>
<p><code>注意 data-email 属性。在 HTML 中我们可以使用带 data- 前缀的属性来存储信息。</code></p>
<h3>520 到 699px 宽度 - 添加邮箱图标</h3>
<p>当浏览器的宽度在 520 到 699px, 邮箱链接前添加邮件图标:</p>
<div class="jb51code"><pre class="brush:css;">@media screen and (max-width: 699px) and (min-width: 520px) {
    ul li a {
      padding-left: 30px;
      background: url(email-icon.png) left center no-repeat;
    }
}</pre></div>
<h3>700 到 1000px - 添加文本前缀信息</h3>
<p>当浏览器的宽度在 700 到 1000px, 会在邮箱链接前添加 &quot;Email: &quot;:</p>
<div class="jb51code"><pre class="brush:css;">@media screen and (max-width: 1000px) and (min-width: 700px) {
    ul li a:before {
      content: "Email: ";
      font-style: italic;
      color: #666666;
    }
}</pre></div>
<h3>大于 1001px 宽度 - 添加邮件地址</h3>
<p>当浏览器的宽度大于 1001px 时,会在链接后添加邮件地址接。</p>
<p>我们会使用 data- 属性来为每个人名后添加邮件地址:</p>
<div class="jb51code"><pre class="brush:css;">@media screen and (min-width: 1001px) {
    ul li a:after {
      content: " (" attr(data-email) ")";
      font-size: 12px;
      font-style: italic;
      color: #666666;
    }
}</pre></div>
<h3>大于 1151px 宽度 - 添加图标</h3>
<p>当浏览器的宽度大于 1001px 时,会在人名前添加图标。</p>
<p>实例中,我们没有编写额外的查询块,我们可以在已有的查询媒体后使用逗号分隔来添加其他媒体查询 (类似 OR 操作符):</p>
<div class="jb51code"><pre class="brush:css;">@media screen and (max-width: 699px) and (min-width: 520px), (min-width: 1151px) {
    ul li a {
      padding-left: 30px;
      background: url(email-icon.png) left center no-repeat;
    }
}</pre></div>
<p>代码</p>
<div class="jb51code"><pre class="brush:xhtml;">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;style&gt;
      ul {
            list-style-type: none;
      }
      ul li a {
            color: green;
            text-decoration: none;
            padding: 3px;
            display: block;
      }
      @media screen and (max-width: 699px) and (min-width: 520px) {
            ul li a {
                padding-left: 30px;
                background: url(email-icon.png) left center no-repeat;
            }
      }
      @media screen and (max-width: 1000px) and (min-width: 700px) {
            ul li a:before {
                content: "Email: ";
                font-style: italic;
                color: #666666;
            }
      }
      @media screen and (min-width: 1001px) {
            ul li a:after {
                content: " (" attr(data-email) ")";
                font-size: 12px;
                font-style: italic;
                color: #666666;
            }
      }
      @media screen and (max-width: 699px) and (min-width: 520px), (min-width: 1151px) {
            ul li a {
                padding-left: 30px;
                background: url(email-icon.png) left center no-repeat;
            }
      }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a data-email="johndoe@example.com" href="mailto:johndoe@example.com"&gt;John Doe&lt;/a&gt;&lt;/li&gt;3
      &lt;li&gt;&lt;a data-email="marymoe@example.com" href="mailto:marymoe@example.com"&gt;Mary Moe&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a data-email="amandapanda@example.com" href="mailto:amandapanda@example.com"&gt;Amanda Panda&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div>
<h2>后言</h2>
頁: [1]
查看完整版本: CSS3媒体查询实现不同宽度的下不同内容的展示功能