Problem

You designed a web page and a particular element needs to appear only when the user visits the page from their Desktop. If the user visits the page from a smaller device, like a mobile or tablet, the element needs to be hidden.

Solution

CSS has a feature called media rules. Media rules allow us to accomplish this task. We can use a rule that specify how wide the screen is and apply some CSS based on that logic. For this example, a small screen with width of 600px will act as a device to hide the item.

So the code will be as follows:

@media only screen and (max-width: 600px) {
    /*you CSS here*/
}

The code above says to the web browser to check the media (device) and if it’s screen and the width less than 600px, then apply the CSS rules enclosed by the curly brackets. A good approach to test that is working is to change the background color.

Let’s add the CSS to change background color to yellow.

@media only screen and (max-width: 600px) {
    body {
        background-color: yellow;
    }
}

Let’s suppose an empty HTML file as follows:

<html>
	<head>
		<style>
		    @media only screen and (max-width: 600px) {
				body {
					background-color: yellow;
				}
			}
		</style>
	</head>
	<body>

	</body>
</html>

Now, if I open that to the a web browser I would expect to have the background page to white at the beginning when the window is full size. While I resize the page to shrink, less than 600px then the yellow should be applied.

We can see when the window resizes and gets smaller than 600px, the background color changes. Now, let’s add an item to the body and hide it when the window resizes.

For example, let’s add a header message to the <body>.

<body>
    <h1>This will disappear when screen < 600px<h1>
</body>

Let’s update the media rule to hide the header element.

h1 {
    display: none;
}

The complete code looks as follows:

<html>
	<head>
		<style>
		    @media only screen and (max-width: 600px) {
				body {
					background-color: yellow;
				}
				h1 {
					display: none;
				}
			}
		</style>
	</head>
	<body>
		<h1>This will disappear when screen < 600px<h1>
	</body>
</html>

Let’s open it in the web browser and resize it once again:

It works

Conclusion

In this post we saw how to apply CSS media rules and hide an element when a screen has less than 600px width. Check out other posts in CSS. You can download the example file below.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

Google Analytics Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.

HotJar Cookies

We use Hotjar in order to better understand our users’ needs and to optimize this service and experience. Hotjar is a technology service that helps us better understand our users’ experience (e.g. how much time they spend on which pages, which links they choose to click, what users do and don’t like, etc.) and this enables us to build and maintain our service with user feedback. Hotjar uses cookies and other technologies to collect data on our users’ behavior and their devices. This includes a device's IP address (processed during your session and stored in a de-identified form), device screen size, device type (unique device identifiers), browser information, geographic location (country only), and the preferred language used to display our website. Hotjar stores this information on our behalf in a pseudonymized user profile. Hotjar is contractually forbidden to sell any of the data collected on our behalf.

For further details, please see the ‘about Hotjar’ section of Hotjar’s support site.