How to show text only on mobile with CSS?

杀马特。学长 韩版系。学妹 提交于 2020-02-04 09:31:04

问题


I have a text (in a div) which shows on desktop and mobile screens.

Expected

I want the text to only show in @media only screen and (max-width: 768px)

How to

  1. hide the div with display:none or

  2. is there any other solution?


回答1:


Use media query.

Set display:none to div and then apply display:block for mobile using max-width media query.

Stack Snippet

div {
  display: none;
}

@media only screen and (max-width: 768px) {
  div {
    display: block;
  }
}
<div>Text</div>

or you can use the min-width media query. Less code here

Stack Snippet

@media only screen and (min-width: 768px) {
  div {
    display: none;
  }
}
<div>Text</div>


来源:https://stackoverflow.com/questions/48358208/how-to-show-text-only-on-mobile-with-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!