How to keep the webpage CSS styles only up to certain device width or maximum width?
Tags: css
Published: 2022-10-20 01:14:35

---
  title: How to keep the webpage CSS styles only up to certain device width or maximum width?
  published: true
  tags: CSS
  date: Thu Oct 20 2022 06:44:35 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/keep-webpage-css-styles-only-upto-certain-device-width-or-maximum-width
  cover_image: https://melvingeorge.me/_next/static/images/main-c398fcbc03e4b0d638dd7eb9165293bb.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/keep-webpage-css-styles-only-upto-certain-device-width-or-maximum-width)
  
To keep the webpage CSS styles only up to certain device width or maximum width, you can use the `@media` media query syntax followed by `()` symbols (opening and closing brackets). Inside the brackets, you can write the keyword `max-width` followed by the `:` symbol (colon) and then by writing the width with its unit.

The CSS styles you write inside this CSS block will be displayed till the device or the window width you provided.

### TL;DR

```html
<html>
  <body>
    <p>Hello World!</p>
  </body>

  <!-- CSS styles -->
  <!-- 
    Using the `@media` and the `max-width` syntax
    we can define the CSS styles to be triggered
    till the window or the device width reaches `1000px`.
  -->
  <style>
    body {
      background-color: black;
    }

    p {
      color: white;
    }

    @media (max-width: 1000px) {
      body {
        background-color: white;
      }

      p {
        color: black;
      }
    }
  </style>
</html>
```

For example, let's say we have a webpage where the background color of the `body` is black and the `paragraph` text is white by default.

It may look like this,

```html
<html>
  <body>
    <p>Hello World!</p>
  </body>

  <!-- CSS styles -->
  <style>
    body {
      background-color: black;
    }

    p {
      color: white;
    }
  </style>
</html>
```

The output will look like this,

![webpage with black background color and text in white color](https://melvingeorge.me/_next/static/images/default-webpage-ac8bc6ccaa13722f127841659b20ae4f.png)

We aim to change this webpage's `body` background color to `white` and paragraph text color to `black` till the window or the device width is at `1000px`.

To do that we can use the `@media` media query syntax followed by the `()` brackets symbol. Inside the brackets, we can write the `(max-width: 1000px)` code to define that we need to trigger the CSS styles till the window width reaches `1000px` or the maximum width of the window is at the `1000px` mark.

It can be done like this,

```html
<html>
  <body>
    <p>Hello World!</p>
  </body>

  <!-- CSS styles -->
  <!-- 
    Using the `@media` and the `max-width` syntax
    we can define the CSS styles to be triggered
    till the window or the device width reaches `1000px`.
  -->
  <style>
    body {
      background-color: black;
    }

    p {
      color: white;
    }

    @media (max-width: 1000px) {
      body {
        background-color: white;
      }

      p {
        color: black;
      }
    }
  </style>
</html>
```

The visual representation of CSS styles getting applied is shown below,

![webpage background color is white and text color is black till widow width reaches 1000px](https://melvingeorge.mewebpage-change-styles)

See the above code live in [codesandbox](https://codesandbox.io/s/keep-webpage-css-styles-only-upto-certain-device-width-or-maximum-width-o5mjsh?file=/index.html).

That's all 😃!

### Feel free to share if you found this useful 😃.

---