Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Viewport meta tags vs Media Queries?

Tags:

html

css

I would love to know, to optimize your website for Tablets, desktops and smarthpones, what is best to use: Media Queries or the Viewport meta tags? see code:

<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> 

vs

/* Smartphones (portrait and landscape) ----------- */ @media only screen  and (min-device-width : 320px)  and (max-device-width : 480px) { /* Styles */ }  /* Smartphones (landscape) ----------- */ @media only screen  and (min-width : 321px) { /* Styles */ }  /* Smartphones (portrait) ----------- */ @media only screen  and (max-width : 320px) { /* Styles */ }  /* iPads (portrait and landscape) ----------- */ @media only screen  and (min-device-width : 768px)  and (max-device-width : 1024px) { /* Styles */ }  /* iPads (landscape) ----------- */ @media only screen  and (min-device-width : 768px)  and (max-device-width : 1024px)  and (orientation : landscape) { /* Styles */ }  /* iPads (portrait) ----------- */ @media only screen  and (min-device-width : 768px)  and (max-device-width : 1024px)  and (orientation : portrait) { /* Styles */ }  /* Desktops and laptops ----------- */ @media only screen  and (min-width : 1224px) { /* Styles */ }  /* Large screens ----------- */ @media only screen  and (min-width : 1824px) { /* Styles */ }  /* iPhone 4 ----------- */ @media only screen and (-webkit-min-device-pixel-ratio : 1.5), only screen and (min-device-pixel-ratio : 1.5) { /* Styles */ } 
like image 476
Dexter Schneider Avatar asked Jul 16 '12 18:07

Dexter Schneider


People also ask

What is the purpose of viewport meta tag?

Setting the viewport meta tag lets you control the width and scaling of the viewport so that it's sized correctly on all devices.

Where do I put a viewport meta tag?

Generally, meta elements (including viewport) should be placed in the document's <head> . CSS rules should either be added to a CSS stylesheet and referenced with a <link> element or, if you're not using stylesheets for some reason, in a <style> element (also in the document's <head> ).

What is a viewport in CSS?

The viewport is the user's visible area of a web page. The viewport varies with the device, and will be smaller on a mobile phone than on a computer screen. Before tablets and mobile phones, web pages were designed only for computer screens, and it was common for web pages to have a static design and a fixed size.

Why do we need media query?

Media queries are a key part of responsive web design, as they allow you to create different layouts depending on the size of the viewport, but they can also be used to detect other things about the environment your site is running on, for example whether the user is using a touchscreen rather than a mouse.


1 Answers

Both are necessary.

Media queries are used to have different css for different devices its like the if condition for different devices.

Viewport meta tag is to set tell the device to take the width according to this tag. Its like a reset for devices if its not used device will adjust the layout according to its default settings.

like image 70
SVS Avatar answered Sep 25 '22 19:09

SVS