Can responsive image technique be utilized for adjusting both height and width simultaneously?

I am trying to set the height of a li element to be responsive based on the viewport, with a width of 20%. However, I want the height to increase on smaller screens. Inside the li element, there is a responsive image, but when I decrease the height, the image does not shrink accordingly. Is there a way to resize the width and height of the li element together? You can view the code on CodePen: http://codepen.io/anon/pen/QbbOrb

 *, *:before, *:after {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

ul {
  list-style:none;
  margin:0;
  padding:0;
}

li {
  width:20%;
  height:100px;
  display: inline-block;
  background: lightblue;
  padding: 15px;
}

img {
  max-width:100%;
  height:auto;
  display:block;
}

HTML

<ul>
  <li><div><img src="http://lorempixel.com/1450/600/"></div></li>
</ul>

Answer №1

If you're looking for a different method to resize images while maintaining the aspect ratio, consider using the background property of your li element with background-position set to center and background-size set to cover:

Check out this Demo

*,
*:before,
*:after {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}
ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
li {
  width: 20%;
  height: 100px;
  display: inline-block;
  border: 15px solid lightblue;
  background-size: cover;
  background-position: center center;
}
<ul>
  <li style="background-image:url(http://lorempixel.com/1450/600/);"></li>
</ul>

Answer №2

When you make this adjustment, it will function as desired.

li {
  width:20%;
  display: inline-block;
  background: green;
  padding: 15px;
}

img {
  max-width:100%;
}

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

Automatic Scrolling within Frameset

Currently, I am using a Frameset in my UI page and I am looking to implement auto-scrolling functionality for both vertical and horizontal directions. I specifically need the ability to scroll either horizontally, vertically, or both whenever necessary. B ...

Currently, I am in the process of constructing a DSpace repository on an Ubuntu 16.04

I have successfully set up a DSpace repository on Ubuntu 16.04 LTS by following the installation instructions provided in this manual. After deploying the webapps, I encountered an issue when trying to add new items. Upon clicking the search link, I recei ...

Show a substantial amount of text when the cursor hovers over

I have incorporated a pop-up tooltip from the following link: This feature enables a message to appear whenever I mouse-over a [?] sign. The script is structured as follows: <a href="#" class="hintanchor" onMouseover="showhint('Please choose a u ...

Retrieve the link's "href" attribute and its text if they match the specified text

Is there a way to extract the href link and text from a specific type of link? <a href="google.com">boom</a> I am looking to retrieve only the 'google.com' part and the text 'boom' Furthermore, how can I filter out other ...

Creating interactive maps with Leaflet using Vue.js 2.0 single-file components and webpack

I have been exploring vuejs for a few weeks now. Recently, I decided to work on implementing Leaflet maps. When I visited the official Leaflet website, they instructed me to prepare my page as follows: Include the Leaflet CSS file in the head section of ...

How to center a div with a overlay in CSS?

I have created an overlay with a div on top of it, and I am currently struggling to center the div over the overlay. I have tried adjusting the left and right properties, but nothing seems to be working. .request-estimate { position: absolute; ...

What steps can be taken to repair the footer area within this CSS document?

As a beginner ASP.NET developer, I am encountering CSS for the first time. The CSS file I have is fairly simple, but I am facing difficulties with the footer section. The current appearance of the footer is as follows: I am looking to make the following m ...

Adjusting the Height of the Video Jumbotron in Bootstrap

One of my main goals is to create a jumbotron with a video that plays, occupying 100% width and 70% height of the screen. However, I am facing an issue where the video follows the scrolling behavior when I don't want it to. Specifically, as I scroll d ...

What is the best way to retrieve text from a span within an input field when it is highlighted?

Is there a way to extract the text from a span that comes before a text input, and when that input is focused, retrieve the span associated with it? Consider this example structure: <div> <label for="id">...</label> <input type="text" ...

Is there a way to implement Font Awesome Icons within CSS? I'm looking to incorporate an icon following a heading on my webpage

Can you guide me on how to incorporate Font Awesome Icons in CSS? I am looking to add an icon after a heading for better customization. Below is the code I have tried: <!DOCTYPE html> <html lang="en"> <head> <meta chars ...

Split the output into two separate columns in a cshtml file using a foreach

Is there a way to modify my current code so that the output is displayed in two columns instead of one? Would using flex grid be the best approach, or is there another method I should consider? <div class="col-12 box-container"> <div class=" ...

Unable to make CSS footer stay at the bottom of the page following the utilization of V-for to display a list of items in

Footer seems to be misbehaving by not staying at the bottom and instead showing under the items rendered using v-for. This issue is only happening on this page while it is working fine on others. <template> <div> <!-- Item renderin ...

What is the best method to conceal the tooltip using the html <a> title attribute?

Is there a way to prevent the tooltip from displaying when hovering over links, while still keeping the title attribute for SEO purposes? I have come across suggestions like using jQuery with $('...').removeAttr('title'), but will this ...

How can I eliminate the CSS value that was inherited?

Looking for a way to eliminate the effect of an inherited CSS property with Jquery. .class1 #id1 div.class2 input.class-input{ border-color: #bbb3b9 #c7c1c6 #c7c1c6; } Can someone explain how to delete this "border-color"? Thanks. ...

Encountering ERR_BLOCKED_BY_XSS_AUDITOR while attempting to download a file through selenium

I am currently attempting to download a file using selenium by replicating a click on a download button, but Chrome is indicating an error with ERR_BLOCKED_BY_XSS_AUDITOR. Even when I try to bypass this by utilizing the "--disable-xss-auditor" argument, th ...

Dim the entire website

I am attempting to apply a grey overlay across my entire site when a file is dragged in for upload. The drag event and activation of the grey overlay are functioning correctly, but there are specific areas where it does not work as intended. There seems t ...

Tips on revealing the following div using jQuery

Can anyone help me figure out how to reveal the next div with a class of .hide when a Clickbox is clicked? Here is a link to the demo: http://jsfiddle.net/fvuqW/ This is the jQuery code I currently have: $(function(){ $('.form input[type=checkb ...

Using custom fonts in HTML on Bootstrap is causing issues

After struggling with a persistent problem for quite some time, I decided to delete my initial question since I was already utilizing Bootstrap. However, I am facing an issue where my custom fonts are not being loaded even though my main.css and bootstrap. ...

A fixed header list using CSS with a single scroll bar

I am looking to create a scrollable list with a fixed header, where the scroll bar extends the full height of the parent but only scrolls through the nav items (keeping the logo in place). My current setup involves: <div className="home-page-nav"> ...

Tips for creating a consistent header and footer across an HTML CSS web application

My previous projects involved using ASP.NET MVC and utilizing @Html.Partial("_header") to incorporate common static HTML across different pages. However, my current project is a pure HTML CSS and JS web app with no server-side technology. The la ...