Home > Article > Web Front-end > How to Apply Responsive CSS Styles Exclusively to Mobile Devices?
When crafting responsive CSS styles, it's crucial to ensure they are applied only to the intended devices. If you're targeting mobile devices but are having difficulties preventing them from interfering with the desktop presentation, consider the following approach.
Instead of using single media query breakpoints, employ a range of breakpoints. This allows you to specify specific width ranges for which the mobile styles should apply, while excluding other devices.
Here's an example of a media query range that targets devices with widths between 480px and 1024px:
<code class="css">@media all and (min-width: 480px) and (max-width: 1024px) { /* Mobile styles go here */ }</code>
To cover most common device sizes, consider the following ranges:
<code class="css">@media all and (min-width: 0px) and (max-width: 320px) @media all and (min-width: 321px) and (max-width: 480px) @media all and (min-width: 569px) and (max-width: 768px) @media all and (min-width: 769px) and (max-width: 800px) @media all and (min-width: 801px) and (max-width: 959px) @media all and (min-width: 960px) and (max-width: 1024px)</code>
To ensure your styles scale well regardless of the device's resolution, avoid using px units. Instead, opt for EM or % units. EM units are relative to the font size of the parent element, while % units are relative to the total width of the element.
Here's an updated version of your code using the media query range approach and EM units:
<code class="css">/* Regular desktop styles */ /* Mobile only styles for devices between 480px and 1024px */ @media all and (min-width: 480px) and (max-width: 1024px) { /* Mobile styles using EM units */ }</code>
This code ensures that the mobile styles are applied exclusively to devices within the specified width range while preserving the separation from desktop styles.
The above is the detailed content of How to Apply Responsive CSS Styles Exclusively to Mobile Devices?. For more information, please follow other related articles on the PHP Chinese website!