CSS Properties: How to change the background-color if the viewport is 400 pixels wider
Solution:
HTML Code:
Explanation:
- This HTML document demonstrates the use of a media query in CSS to change the background color of the body based on the width of the viewport.
- CSS comments are added to explain each section of the code.
- The initial background color of the body is set to #3300FF.
- The @media rule is used to define a media query for screens with a minimum width of 400px.
- Within the media query, the background color of the body is changed to #00FFFF for screens wider than 400px.
- Users can resize the browser window to observe the effect of the media query on the background color.
Live Demo :
See the solution in the browser
Supported browser
![]() |
![]() |
![]() |
![]() |
![]() |
Yes | Yes | Yes | Yes | Yes |
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.