Designing a responsive website is essential for providing a seamless user experience across devices. One of the best ways to ensure your website looks great on all screen sizes is by using CSS media queries. In this article, we’ll guide you through how to create CSS specifically tailored for different screen sizes, helping you optimize your site for mobile, tablet, and desktop users.

Why Create CSS for Different Screen Sizes?
Not all devices display your website the same way. A design that works well on a desktop may look cluttered or unreadable on a mobile phone. Using CSS media queries allows you to adapt your design so that it is responsive, ensuring that the layout adjusts according to the screen size.
For example, you may want larger text and fewer elements on a mobile screen, while on a desktop, you can afford to show more content with higher resolution images.
What Are CSS Media Queries?
CSS media queries are a feature in CSS that allows you to apply specific styles based on conditions such as screen width, height, orientation, and more. Media queries give you control over how your content is displayed on different devices.
Here’s an example of a basic media query targeting screens with a maximum width of 600px:
@media only screen and (max-width: 600px) {
body {
font-size: 16px;
}
}
In this case, the font size changes when the screen width is 600 pixels or smaller.
How to Write CSS for Different Screen Sizes
- Identify Your Breakpoints Breakpoints are the screen sizes at which your layout should change. Some common breakpoints to target are:
- Small devices (mobile): 480px or 600px
- Medium devices (tablets): 768px
- Large devices (desktops): 1024px or higher
- Use Media QueriesUse CSS media queries to apply styles for specific devices. Below are a few examples of CSS media queries for common screen sizes:
For Mobile Devices (Max width 600px):@media only screen and (max-width: 600px) {
body { background-color: lightgray; }}
For Tablets (Max width 768px):@media only screen and (max-width: 768px) {
body { background-color: lightblue; }}
For Desktops (Min width 1024px):@media only screen and (min-width: 1024px) {
body { background-color: white; }} - Test Across Devices After implementing your media queries, test your site on various devices to ensure that your CSS changes are applied correctly. You can use Chrome DevTools or other responsive design testing tools to simulate different screen sizes and resolutions.
Tips for Effective Media Queries
- Start with a Mobile-First Approach: Write your CSS for mobile devices first and then add media queries for larger screens. This ensures that mobile users get the most optimized version of your site.
- Keep Breakpoints Simple: Avoid using too many breakpoints. Focus on the major screen sizes to prevent overly complex CSS code.
- Use Relative Units: Instead of fixed pixel values, consider using relative units like em or rem for a more fluid, scalable design.
Common Breakpoints and a Practical Example
There is no single correct set of breakpoints, because the best values depend on your design. A mobile-first approach is a good starting point: write the base styles for small screens, then use min-width media queries to add styles for larger ones.
/* Base styles for small screens */
.container {
padding: 16px;
}
/* Tablets and larger */
@media (min-width: 768px) {
.container {
padding: 32px;
}
}
/* Desktops and larger */
@media (min-width: 1024px) {
.container {
max-width: 960px;
margin: 0 auto;
}
} Common starting points are around 480 pixels for large phones, 768 pixels for tablets, and 1024 pixels for small laptops. Instead of targeting specific devices, adjust the breakpoint wherever your layout starts to look awkward.
Other Useful Media Queries
- Orientation.
@media (orientation: landscape)lets you adapt the layout when a device is turned sideways. - Color scheme.
@media (prefers-color-scheme: dark)lets you offer a dark theme to visitors who prefer it. - Reduced motion.
@media (prefers-reduced-motion: reduce)allows you to limit animations for people who are sensitive to movement. - Print.
@media printlets you create a clean layout for printed pages.
Frequently Asked Questions
Should I use min-width or max-width?
Both work. Mobile-first designs usually use min-width, because the base styles apply to small screens and larger layouts are added progressively, which tends to keep the code simpler.
Do media queries affect page speed?
They add very little overhead. Performance is usually influenced more by image sizes, scripts, and fonts than by the number of media queries.
How to Test Your Media Queries
After you write your styles, confirm that they behave as expected. First, make sure your page includes the viewport meta tag in the head section, since mobile browsers need it to apply responsive styles correctly.
<meta name="viewport" content="width=device-width, initial-scale=1"> Then open your browser developer tools and use the responsive or device mode to resize the page. Drag the width slowly and watch for the point where the layout breaks. Finally, test on at least one real phone and one tablet. Try to keep the number of breakpoints small, since fewer breakpoints make the stylesheet easier to maintain.
Conclusion
Creating CSS for specific screen sizes is crucial in building a responsive website. By using media queries, you can ensure that your design looks great on any device, whether it’s a mobile phone, tablet, or desktop. Start by identifying breakpoints, writing mobile-first CSS, and testing across different devices to achieve a seamless user experience.
With these simple steps, you’ll be able to tailor your CSS to meet the needs of any screen size, improving both your website’s usability and SEO.
