Using Css3 to adjust styling based on device width and orientation

I am trying to create a background color that changes based on the orientation of the device. Here is the code I have been using:

<meta name="viewport" content="width=device-width">
...

@media all and (max-device-width: 360px) {
  div { background-color: green; } }      

@media all and (min-device-width: 361px) {
  div { background-color: blue; } }

However, this code does not seem to be effective. Surprisingly, when I replace max-device-width and min-device-width with max-width and min-width, it works perfectly fine. What could be the reason behind the failure of using max-device-width and min-device-width in the code?

Answer №1

Here is a helpful tip:

To make this work, include the following in your HTML document:

<meta name="viewport" content="width=device-width, initial-scale=1.0">

In addition, add the following code to your CSS stylesheet:

div {
  background-color: blue;
}

@media (max-width: 360px) {

  div{
    background-color: green;
  }
}

As a result, if the width of the window is less than 360 pixels, the background color will change to green; otherwise, it will remain blue.

Answer №2

The reason for displaying it as a device is to ensure proper recognition. This eliminates visibility on desktops.

I advise against using this method because not all browsing devices signal that the content is being viewed as a device, resulting in unpredictable design outcomes.

Stick with setting minimum width and height for safer results.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Importing ReactDOM alone does not result in the rendering of anything

Having just started delving into the world of React, I've been grappling with getting a React app up and running. Despite my efforts, all I can manage to see is a blank page. Can anyone offer some assistance? HTML Markup (index.html) <html> & ...

Ways to reduce the amount of time spent watching anime when it is not in view

My anime experiences glitches as the black container crosses the red, causing a decrease in duration. Is there a way to fix this glitch? I attempted to delay the changes until the red path is completed, but the glitches persist. delayInAnimeSub = ourVilla ...

React component for a background image with a gradient overlay

I'm facing an issue trying to incorporate a background image for a div along with a color overlay. In plain CSS, the code would typically look like this: .backgroundImage { background-image: linear-gradient(to bottom, rgba(245, 246, 252, 0.52), rg ...

Changing a URL parameter based on the element's width is a simple task when

I'm trying to display elements on a page by replacing a string in the URL parameter with the width of each element of a certain class. I'm new to JavaScript, so any help would be appreciated! Here's an example of the HTML for objects on the ...

Searching a JSON document to retrieve the position within an array

Attempting to query a JSON file in order to adjust the position of an array. For instance, here is a snippet from the JSON file: { "result": [{ "summonerLevel": "0", "summonerID": "0", "summonerName": "", "summonerIcon": ...

Sharing resources between different origins and the file:// protocol

I have been developing an HTML5 application that is collecting data from various sources using JSONP. So far, everything involving a GET request has been functioning perfectly. However, I have encountered a hurdle while attempting to make a POST request. T ...

Can you explain the significance of the role attribute being set to "button"?

While browsing through the Google+ Project's page, I noticed that all the buttons were created using div elements like: <div role="button"></div> I'm curious to know if this is done for semantic reasons or if it has an impa ...

The Div overlay vanishes once the Javascript function is finished running

Initially, take a look at this fiddle: http://jsfiddle.net/ribbs2521/Q7C3m/1/ My attempt to run JS on the fiddle has been unsuccessful, but all my code is present there, so we should be fine. I was creating my own custom image viewer, and everything was ...

Ways to customize the datetime-local format in an HTML input field

When dealing with the HTML input of datetime type, consider the following: Datefield: <input type="datetime-local" data-date="" data-date-format="DD MMMM YYYY, h:mm:ss"> The script below includes important code. $("input").val(moment().format(&apo ...

Show various pieces of information in my input field

I'm working with an input field: <div class="search-field search-field-date search-field-calendar ui-datepicker-calendar columns small-3"> <input type="text" data-ng-model="goDate" placeholder="Departure Date" data-mtx-datepic ...

Is it possible to generate an HTML element by utilizing an array of coordinates?

I have a set of 4 x/y coordinates that looks like this: [{x: 10, y: 5}, {x:10, y:15}, {x:20, y:10}, {x:20, y:20}] Is there a way to create an HTML element where each corner matches one of the coordinates in the array? I am aware that this can be done usi ...

Best practice for sending intricate variables to JavaScript via HTML

Steering away from PHP's htmlentities, I made a change in my code: <?php echo '<img ... onclick="MP.view(\''.$i->name.'\') />'; ?> However, I decided to go a different route by JSON encoding the ...

Creating HTML email content in PHP by merging long strings

I am currently working on a project that involves receiving a JSON webhook and using it to send email notifications. While I have successfully sorted the JSON data, I am unsure of how to create a large HTML email. It seems that attempting to use an If sta ...

Is there a method to prevent Apple OS from applying its default styles to form fields, scrollbars, and other elements?

Is it possible to remove the default Apple styling (such as the blue glow) applied to focused DOM elements in OS X or iOS using CSS or JavaScript? I've searched online but couldn't find any information on this topic. I'm starting to think t ...

Is there a way to automatically activate the "Add" button when I hit the enter key in the text box?

Being someone who relies on to-do lists, I implemented a system where you can input tasks into a textbox and click the add button to see them listed below. However, I found it cumbersome to keep clicking the add button every time I wanted to quickly add mu ...

Two variations of identical descriptions within a single div container

Is there a way for me to use an item, specifically an img, within the same div class but with different definitions? For example: section .row img { margin: 0 0 30px; width: 100%; border: 4px solid #18a00e; } section .row img { margin: 0 ...

Discover the amazing capabilities of Beautiful Soup's find function in extracting numerical data from Google

Just finished watching a tutorial on beautiful soup and decided to put it to the test by scraping some data from Google. After searching for "coronavirus UK" I aimed to extract the current number of confirmed cases. A table on the upper right side of the ...

Populate a database with information collected from a dynamic form submission

I recently created a dynamic form where users can add multiple fields as needed. However, I'm facing a challenge when it comes to saving this data into the database. You can view a similar code snippet for my form here. <form id="addFields" me ...

ERROR : The value was modified after it had already been checked for changes

Encountering an issue with [height] on the main component and seeking a solution Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: '753'. Current value: '731'. I have th ...

When the div tag exceeds its boundaries, apply a new class

I am working on a div with set dimensions, and I am populating it with content using ng-repeat. My goal is to apply a CSS class to this div when it exceeds its limits. I attempted to use the length property but without success. var app = angular.module( ...