Instructing CSS to establish a maximum width of 220 pixels, or 50% of the view if 220px exceeds half of the total viewport size

Is there a way to ensure that my images have a width of 220px, unless on devices with smaller screens where it would exceed 50% of the screen width?

For instance, if I set the image width to 220px maximum, it may take up more than 50% of the screen width on an iPhone 6 4.7". In such cases, I would like the CSS rule to be max-width: 50%, rather than 220px.

What is the best approach to achieve this responsive design challenge?

Answer №1

What do you think of this solution?

img {
  width: 60%;
  max-width: 250px;
  height: auto;
}

Answer №2

If you're looking to make your images responsive, CSS media queries can help achieve that:

img {
    width: 220px;
}
@media only screen and (max-device-width: 439px) {
    img {
        width: 50%;
    }
}

This approach ensures all <img> elements have a predefined width of 220px. However, on narrower devices where 220px would take up more than half the screen width (2 x 220 - 1), the second rule kicks in to set the width of <img> elements to 50%.

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

Applying transition effects to elements external to the VueJS transition component

Vue Version 2.6.10 To view the reproduction, please click on the link provided. <router-link to="/a"><a name="/a">[a]</a></router-link> <router-link to="/b"><a name="/b">[b]</a></router-link> <router-lin ...

Ways to limit the width of content

While I could use a div container to solve this issue, I'm exploring the possibility of achieving it without one. My goal is to create a layout that is flexible between 640px and 1280px, but remains fixed beyond that range. The colored div I have cur ...

Positioning a div absolutely within a targeted div

Is there a way to set a div as absolute within a specific relative div rather than just the parent? For instance, I have a div that's nested inside of a row. However, I would like it to be positioned absolutely in the section instead of the row. Both ...

Easy selector for choosing jquery css backgrounds

Here is a simple background selector that I created. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <hea ...

When clicked on, Bootstrap creates a stylish border of blue lines around the designated

Hey there, I've noticed a strange issue that only seems to occur in Google Chrome on both desktop and mobile devices. To better illustrate the problem, I have attached a picture. A blue line unexpectedly appears when I interact with certain sections ...

Avoiding mistakes in class and id names with preventative tools

Is there a simple way to spot typos in HTML/CSS/JavaScript code? I'm looking for basic debugging tools that can catch mistakes like "#fron" instead of "#from" in class names and ids. I'm aware of more advanced IDEs, but I'm curious if there ...

Tips for employing clamp CSS to modify font size while maintaining alignment within a paper-inspired CSS grid

Currently, I am working on creating a responsive text section with a grid-like horizontal line resembling paper, and the challenge lies in aligning the text properly within this grid. While I can achieve this when the font size remains constant, I am curi ...

Tips on achieving consistent division through CSS where both columns and rows have equal spacing

In my website's code, I am trying to display two blogs per row. While the width is divided correctly, there is an issue with the height. This is because some blogs have large titles and others have small titles, resulting in uneven content layout. In ...

Is there a Unicode character substitution happening?

There are certain mobile browsers that appear to have trouble supporting all unicode characters, such as the down arrow icon like this: span.icon:after { content:"\25be"; } Currently, no symbol is shown. Is there a way for me to identify this i ...

Elevate the placeholder in Angular Material 2 to enhance its height

I want to make material 2 input control larger by adjusting the height property of the input using CSS <input mdInput placeholder="Favorite food" class="search-grid-input"> .search-grid-input { height:30px!important; } As a result, the image o ...

Creating a color gradient for hyperlinked text when hovered over: a tutorial

How can I achieve this effect using CSS only? I want the link color to change from #00C to #000 when hovered over, with a gradient transition taking 1 second. ...

Header with a dropdown select option

In the process of developing a monitoring system, I am in need of a feature that allows users to select varying numbers of days. Here is the desired style: Previous [Dropdown Selection] Days https://i.sstatic.net/ysk6X.png Ideally, I do not want any bor ...

React Images: Carousel display issue with images not centered

Is there a way to center the selected image instead of it appearing on the left-hand side? Here is the current behavior: https://i.stack.imgur.com/SUWOK.jpg I am using the following packages in a sandbox environment with Next.js 11 and TailwindCSS 2.2.4: ...

Is there a way for me to keep an image stationary on the page while allowing overlaying text to move?

Currently, I am exploring the process of coding a website that mimics the style of this particular webpage: . I am particularly interested in learning how to create a scrolling effect for text within a fixed area, while keeping the accompanying images st ...

Ways to conditionally display a component in Next.js without the issue of caching CSS styles

I'm a newcomer to Next.js and I'm still trying to wrap my head around how the caching works. Let's take a look at this simplified example: An index page that displays either Test1 or Test2 components, based on whether the current minute is ...

Modifying webpage design using JavaScript for styling

Is there a way to change the css style(defined in the page source) dynamically with Java? I know it is possible to do it with JavaScript. If there isn't, are there other situations where JavaScript is the only choice developing a web app? ...

Issue with aligning the image product in Bootstrap 4

I'm currently working on creating a basic online shopping page for my PHP training, and I've run into a bit of a roadblock with the HTML portion. Even after using Bootstrap, I'm still struggling to simplify things. When I view the result of ...

In VUE, switching colors upon clicking is undone once the cursor moves away from the element

I need help with changing the color of a div when it is clicked. Here's how the div is structured: <li v-for="monomer in monomers"> <div :style="monomerButton" @mouseover="hover = true" @mouseleave="hov ...

What is the best way to initiate animation on a child element once the parent element has reached 75% completion of its animation

Is there a way to use JavaScript to determine when an animation is 75% complete? I have many nested HTML elements that need to be animated with an animation delay property, where the nested animation should start when the parent element is 75% complete. I ...

Set a default selection for radio buttons in Angular template-driven forms

I'm having an issue with setting a default selection for a radio button in my template-driven form. Here's the relevant part of my code: <div class="donut-form-promos"> <span>Promo:</span> <div class=&quo ...