How to show text only on mobile with CSS?
25,178
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>
Author by
Admin
Updated on January 21, 2020Comments
-
Admin over 4 years
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
hide the div with
display:none
oris there any other solution?
-
denmch over 6 yearsI'd simplify this by putting
display: none
in the media query triggered atmin-width: 768.1px
. This way you only need the media query, and don't have (1) to reset the default display on the element only to (2) reverse it back with a media query. -
Admin over 6 years@denmch thank you it worked! How to set the question as answered?
-
Bhuwan over 6 years@denmch Agreed....I have used the
max-width
because OP had mentioned in the question that he want to usemax-width
only....BTW I have also added themin-width
solution... -
Admin over 6 yearsThank you @BhuwanBhatt I am inexperienced and I am having a hard time dealing with media queries. Have a great day.
-
denmch over 6 years@ElPistolero The two options are functionally equivalent, so Bhuwan Bhatt should keep the accepted answer. Good luck moving forward and keep asking questions!