Trying to display and conceal images based on the screen size of the device

Just starting out with responsive web design, I'm working on switching between a large image and a small one based on screen width.

It's all good when I resize my desktop browser, but when I check it on my mobile phone, the large image is still showing up.

This is the code I have:

HTML:

<div id="wrapper">
    <img src="lrgimg" id="lrgimg" alt="" />
    <img src="smlimg" id="smlimg" alt="" />
    <p>Some text with changing font size based on device width.</p>
</div>

CSS:

#wrapper{
    margin-right:0 auto;
    margin-left:0 auto;
    padding:10px;
    width:80%;
    border:1px solid red;
}
#smlimg{
    display:none;
    max-width:100%;
    max-height:100%;
}
#lrimg{
    max-width:100%;
    max-height:100%;
}

/* media queries */

@media screen and (max-width: 480px){

    #lrgimg{display:none;}
    #smlimg{display:inline;}

    p{
        font-size:30px;
    }

}

I thought using max-width would cover most phone screen widths, but apparently resolutions make a big difference even if the width stays the same.

So, my question is: Is there a standard @media query that will work for most modern mobile phone browsers?

Check out the demo

Answer №1

With different screen sizes, the width of 480px might still be too extensive. (In cases where the screen size is indeed 480, it will not cause a problem until

@media screen and (max-width: 479px){
because even a screen width of 480 falls within
@media screen and (max-width: 480px){

For additional statistics, visit: Mobile and desktop screen size statistics

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

Transfer the choices from a dropdown list to a different JSP page

In my current JSP code, I have a select element that looks like the following: <select name="withoutAccess" size="5"> <c:foreach var="item" items="${obj.withoutAccess}"> <option>${item}</option> </c:foreach> </sel ...

What is the reason behind the failure of the jquery.hide() function on this particular UL element?

Although the submenu displays correctly, it refuses to hide when using the mouseleave function. Oddly enough, a console.log() confirms that an event does trigger at the right moment when the mouse exits the <ul>, yet the submenu remains visible for ...

Ways to modify the text color of an inactive TextField in Material UI React JS

After researching how to change the disabled TextField font color on Stack Overflow, I implemented the solution using Material-UI. However, when I tried to create a new customized TextField based on the example provided, it did not work as expected and dis ...

BASH-SHELL SCRIPT: Display a specific portion of content from a file

Recently, I stumbled upon a text file containing a snippet of HTML code: >>nano wynik.txt which includes the following text: 1743: < a href="/currencies/lisk/#markets" class="price" data-usd="24.6933" data-btc= "0.00146882" My goal is to extr ...

Tips for modifying the value and refreshing the array

I retrieve data from $scope.roleinfo = {"QA":[{"White Box Testing":0},{"Black Box Testing":0}],"Development":[{"Server Side":0},{"UI":0},{"Back end":0}]}; Then, I present these values in a table. Now, I need to update the maxcount and create an array w ...

Tips for setting the background image of the body once the image has finished loading

On my website, I have decided to incorporate high-resolution images as the background for the body. However, when I use document.body.style.backgroundImage = "url('URL OF PICTURE')";, the image loads vertically down the page which is not ideal. I ...

Tips for Changing the CSS of an External Component with Material-UI-React

Is there a way to override the default CSS of an external component that is not developed in Material-UI or my project? In styled-components, I can simply take the root classes and replace them with my own custom CSS. How can I achieve a similar result wit ...

The 'OR' statement in jQuery (||) is not functioning correctly within an if condition

I am facing an issue with a dropdown menu and colorizing its options based on their values. Here is the scenario: If the Option value is "f1" OR "f2", then the dropdown color should be "blue". Otherwise, it should be "red". Below is the code snippet: &l ...

The functionality of the margin gets disrupted when the float property is not included

Could someone please explain why the margin stops working when I remove the float property? Is there a connection that I am missing? .header-image { float: left; width: 33%; margin-top: 1em; padding-right: 3em; text-align: right; } ...

Troubleshooting Select2 frontend design in Bootstrap 4

I've been working on implementing Select2 with Bootstrap 4 for my <select> elements. Unfortunately, I ran into an issue because there isn't an official Bootstrap 4 template for Select2. However, I came across this project which seems to be ...

I created a new game where I can select two images to display, but for some reason, it is only showing one

My rock paper scissors game functions well, except for one issue. I programmed it to display both the player's choice and the bot's choice along with an outcome message (win, tie, or lose). However, I encountered a problem where only the player&a ...

Issue with React router not functioning correctly when dealing with dynamically created anchor tags utilizing jquery

As a newcomer to React.js, I have incorporated jQuery into my project to handle mouse and click events for cloning navigation elements, specifically <li> tags within <a> tags. The cloned elements are displayed in a targeted div ID successfully. ...

Sneaky spam and ads embedded within Joomla template

Today, while examining the source code of a Joomla site I am developing, I stumbled upon some hidden links that seem to be spam. I have spent an hour searching through various template files but have been unable to locate them. The hidden links in questio ...

Erase the contents of a div by clicking a button

I am currently working on a game project that utilizes drag-and-drop functionality, similar to Blockly and Scratch. One of the key features I am trying to implement is the ability to clear the contents of a target container by clicking a Reset button. Desp ...

Encircle a particular row in a table with a border

I'm having trouble creating a border around only the top row of my table. Right now, the border is only displayed around the outside of the entire table. I also want to add a border specifically to the first row that contains 'Team, Correct Picks ...

How to Execute a Java Method Using a JavaScript Function?

I've been doing some research, but haven't come across a straightforward answer to my question. Imagine I have an HTML page with embedded Javascript code, as well as a java.class file located in a package named somePackage.someSubPackage.*; Is t ...

Sidebar navigation text shifting during transition

I attempted to modify the CSS and JavaScript, but unfortunately, it had no effect and actually caused more issues. I adjusted the position and display properties in the CSS, but it seems that wasn't the root of the problem. If anyone could offer assis ...

Using JQuery, eliminate the transition effect from a child CSS class

I am facing an issue with transitioning an ID that has a child class. My goal is to transition the ID only, without affecting the class within it. Despite going through CSS and jQuery documentation, I have been unable to achieve this. The transitions are c ...

Set restrictions on the element with a fixed position

My div has a position: fixed that scrolls correctly, but I want it to stop when it reaches specific y-axis boundaries. How can I achieve this without flickering and maintaining performance? The behavior of Twitter's right panel is similar to what I&ap ...

What is the best way to adjust the minimum width of the HTML5 progress element?

There is a similar question which addresses input elements having a minimum width determined by the size parameter. I am attempting to create a layout with an inline-flex element with a flex-direction: column property like this: .item { border: 1px s ...