Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change CSS based on mobile device?

Tags:

In the following example, I want to display only one div, depending on the device.

I'm familiar with how to use @media to override a CSS class but not how to do a conditional.

<style type="text/css">    @media (max-width : 770px) {      ...    }    @media (max-width : 320px) {    ...   }   //do I need another for the desktop? </style>  //if desktop <div style="width: 400px; float: left;">     this is desktop     <a href="somepage.html"><img src="aLargeImage.png"/></a> </div>  //if mobile <div style="width: 200px; float: left;">     this is mobile     <a href="somepage.html">just text</a> </div> 

Any suggestions?

like image 256
4thSpace Avatar asked Apr 22 '13 20:04

4thSpace


People also ask

Does CSS work on mobile?

Yes, CSS works on Android devices. In most cases, CSS can be used to style websites that are viewed on Android devices just like they would be on any other type of device.

Does CSS work on Android?

If you want to style a native android app, there is no support for CSS. Instead android has it's own mechanism. The complete list of options is only available at the source code. You may use WebViews in your native Android app, to display HTML which is packaged with the app, then you can use CSS like in any HTML site.


1 Answers

Here's an example of how it can be done conditionally without javascript:

//CSS .visibledevice {display:none;} .visibledesktop {display:display;}  @media (max-width : 320px) {     .visibledevice {display:block;}     .visibledesktop {display:none;} }  //in the page <div class="visibledesktop">this displays for desktop and tablet</div>  <div class="visibledevice">this displays for mobile</div> 
like image 86
4thSpace Avatar answered Sep 21 '22 11:09

4thSpace