首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CSS根据屏幕大小更改内容

问CSS根据屏幕大小更改内容
EN

Stack Overflow用户
提问于 2018-10-09 09:57:25
回答 2查看 3.7K关注 0票数 0

有一次,我从一个使用CSS构建的Flash应用程序中看到了一个效果:当用户调整浏览器窗口的大小,使其超出最小可查看区域(例如小于500x800 px)时,将内容替换为一条消息,说明可视屏幕大小太小。

我尝试了@media指令,它能够隐藏原始的显示内容,但是我无法用消息替换它

代码语言:javascript
复制
@media screen and (max-width:900px), screen and (max-height:500px) {
    .wrapper { display: none !important; }
    .notice  { display: block; visibility: visible; }
}

.notice {
  display: none;
  visibility: hidden;
}

编辑:添加HTML代码

代码语言:javascript
复制
<body>
<div class="wrapper">
   <header class="header">My header</header>
   <aside class="sidebar">Sidebar</aside>
   <article class="content">
   <h1>2 column, header and footer</h1>
      <p>This example uses line-based positioning, to position the header
         and footer, stretching them across the grid.</p>
   </article>
   <footer class="footer">My footer</footer>
</div>
<div class="notice">notice</div>
</body>

当我将浏览器窗口的大小调整到给定大小之后,CSS中的上述代码可以隐藏div包装器,但无法显示通知div。当屏幕尺寸为预期尺寸时,应隐藏div通知。

在这里,我不想考虑任何CSS框架。主要是因为我刚接触过CSS,而且我也认为这是过头了。

我只能用IE11 -别问我为什么

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-09 10:12:14

您需要将@media css放在.notice css下面。

代码语言:javascript
复制
<div class="wrapper">
  i am wrapper
</div>
<div class="notice">
  i am notice
</div>
代码语言:javascript
复制
.notice {
  display: none;
  visibility: hidden;
}

@media screen and (max-width:900px), screen and (max-height:500px) {
    .wrapper { display: none !important; }
    .notice  { display: block; visibility: visible; }
}
票数 1
EN

Stack Overflow用户

发布于 2018-10-09 10:30:38

假设您的HTML如下所示:

代码语言:javascript
复制
<div class="wrapper">
     <div class="content">
          <--Your content-->
     </div>
     <div class="notice">
          viewable screen size is too small
      </div>
</div>

您可以添加以下css。

代码语言:javascript
复制
// Show content for bigger viewpoints
@media screen and (min-width:900px), screen and (min-height:500px) {
.wrapper.content {display:block;}
.wrapper.notice {display:none;}
}

// Show notice for smaller viewpoints
@media screen and (max-width:900px), screen and (max-height:500px) {
.wrapper.content {display:none;}
.wrapper.notice {display:block;}
}

希望对你有用,或者给你一个洞察力。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52718216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档