Mám nastaveno toto @media
:
HTML:
<head>
<meta name="viewport" content="width=device-width, user-scalable=no" />
</head>
CSS:
@media screen and (min-width: 769px) {
/* STYLES HERE */
}
@media screen and (min-device-width: 481px) and (max-device-width: 768px) {
/* STYLES HERE */
}
@media only screen and (max-device-width: 480px) {
/* STYLES HERE */
}
S tímto nastavením to funguje na iPhonu, ale nefunguje to v prohlížeči.
Je to tím, že v meta už mám device
a možná místo toho mám max-width:480px
?
Zjistil jsem, že nejlepší metodou je napsat výchozí CSS pro starší prohlížeče, protože starší prohlížeče včetně 5.5, 6, 7 a 8. Nelze'číst @media. Když používám @media, používám ho takto:
<style type="text/css">
/* default styles here for older browsers.
I tend to go for a 600px - 960px width max but using percentages
*/
@media only screen and (min-width: 960px) {
/* styles for browsers larger than 960px; */
}
@media only screen and (min-width: 1440px) {
/* styles for browsers larger than 1440px; */
}
@media only screen and (min-width: 2000px) {
/* for sumo sized (mac) screens */
}
@media only screen and (max-device-width: 480px) {
/* styles for mobile browsers smaller than 480px; (iPhone) */
}
@media only screen and (device-width: 768px) {
/* default iPad screens */
}
/* different techniques for iPad screening */
@media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:portrait) {
/* For portrait layouts only */
}
@media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:landscape) {
/* For landscape layouts only */
}
</style>
Ale s @media si můžete dělat, co chcete, Toto je jen příklad toho, co se mi nejlépe osvědčilo při vytváření stylů pro všechny prohlížeče.
Také! Pokud'hledáte možnost tisku, můžete použít @media print{}
.
Základním problémem je použití max-device-width
oproti starému max-width
.
Použití klíčového slova "device" se zaměřuje na fyzický rozměr obrazovky, nikoli na šířku okna prohlížeče.
Například:
@media only screen and (max-device-width: 480px) {
/* STYLES HERE for DEVICES with physical max-screen width of 480px */
}
Proti
@media only screen and (max-width: 480px) {
/* STYLES HERE for BROWSER WINDOWS with a max-width of 480px.
This will work on desktops when the window is narrowed. */
}
Správná hodnota atributu content
by měla místo toho obsahovat initial-scale
:
-- begin snippet: js hide: false console: true babel: false -->
<meta name="viewport" content="width=device-width, initial-scale=1">
^^^^^^^^^^^^^^^