Is it possible to utilize CSS to generate a full-width slider while maintaining the aspect ratio of the content?

I am currently utilizing the unslider plugin to design a full-width slider header on a webpage (). Although it functions well at the dimensions I set it up for (1920x450), issues arise when viewed on different screen resolutions. See below:

The background image scales proportionally to fit the width of the screen, but the actual li element remains fixed in height and does not adjust, resulting in the gap as shown in the image.

Below is the HTML code for setting up the slider:

<div class="banner" style="overflow: hidden; width: 1904px; height: 450px;">
    <ul style="width: 300%; position: relative; left: -200%; height: 450px;">
        <li style="width: 33.333333333333336%; background-image: url(http://fths.convoke.info/wp-content/uploads/2014/08/header450-02.jpg); background-size: 100%;"></li>
        <li style="width: 33.333333333333336%; background-image: url(http://fths.convoke.info/wp-content/uploads/2014/08/header450-01.jpg); background-size: 100%;"></li>
        <li style="width: 33.333333333333336%; background-image: url(http://fths.convoke.info/wp-content/uploads/2014/08/header450-03.jpg); background-size: 100%;"></li>
    </ul>
</div>

And here is the corresponding CSS:

.banner { position: relative; overflow: auto; }
.banner li { list-style: none; }
.banner ul li {
    float: left;
    height: 450px;
    -webkit-background-size: 100% 100%;
    -moz-background-size: 100% 100%;
    -o-background-size: 100% 100%;
    -ms-background-size: 100% 100%;
    background-size: 100% 100%;
    background-repeat: no-repeat;
}

I have tried experimenting with min-height and max-height properties but haven't found an ideal solution. Setting the height is crucial to prevent the collapsing of the li elements. While using background-size:cover seems like a good option, it results in poor image scaling on narrow screens.

To resolve this issue, I believe finding a way to set the height of the li element in proportion to the background images is necessary. Is there a CSS approach to achieve this, or would scripting be required? I came across this article suggesting the utilization of padding-top:x% to "set" the height, but I fear this method might create complications due to dynamic changes in the slider's width based on the number of slides present.

Answer №1

You can't achieve this using pure CSS alone, but you can do so by utilizing a simple JQuery function like the one shown in this example.

$('document').ready(function(){
  function adjustImageSize(){
    var bannerWidth = $('.banner').width();
    var imageHeight = 450 * bannerWidth/1903;
    $('.banner ul li').css('height', imageHeight.toString());
  }

  adjustImageSize();

  $(window).resize(function(){
    adjustImageSize();
  });

});

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

Information within specified dates shows all leaders

Looking to filter data based on Start Date, End Date, and HeadName. The current search query successfully filters between dates but does not properly filter the HeadName column, displaying all results instead. Sno HeadName Date Amount BillNo BillD ...

Just finished my first webpage - can't seem to get the footer to stay put!

After spending 3-4 months learning HTML and CSS, I am now working on my first web page. However, I'm facing an issue where the footer is stuck to the slideshow and I can't seem to figure out how to position it at the bottom of the page. Any tips ...

Storing the result of an Angular $http.get request in a variable

As a newcomer to Angular, I am exploring how to retrieve a JSON object containing a list of individuals from CouchDB using a $http.get call. The JSON structure includes an id, names, and quotes for each person: { "total_rows": 2, "offset": 0, ...

Deploying CSS/JS files in Magento 2 is a crucial

Hello, I recently set up magento2 with the sample data included. After attempting to deploy static css/JS using the command php bin/magento setup:static-content:deploy, I didn't receive any errors but the issue persists. Additionally, I am unable to l ...

Manipulate Input Classes by Adding and Removing Them

I am currently using masked JS in some input fields, specifically for telephone numbers. However, I am facing an issue where phone numbers can vary in length, either 10 or 11 digits. I need to switch the class based on whether a checkbox is checked. Here i ...

Creating a customized ASP.NET AJAX control that utilizes templating for dynamically showing or hiding items within a placeholder

I am dealing with a custom templated control (toolbar) that includes a custom user control (button). The button utilizes jQuery for styling and managing various states, postbacks, and non-postbacks. Within this setup, some of the buttons are initially hid ...

Discovering a value that aligns with one of the values in an array using Javascript

Just a heads up: this quiz is super simple and only has one input field for each question. This block of Javascript code is used to check if the answer entered in the input field is correct or not. In this case, it checks if the answer entered is 'en ...

Issues with Jquery Autocomplete feature when using an Input Box fetched through Ajax requests

When a user selects an option from the drop-down list, an input box is dynamically added to the page using AJAX. document.getElementById("input_box").innerHTML ="<input id='ProjectName'/>"; However, there seems to be an issue with jQuery ...

Unable to choose from the dropdown menu

I'm having trouble with selecting options on a selectbox when the select option is overlapping my menu. The menu I'm using is a jQuery Plugin. When the select box overlaps the menu, the options are hidden. I tried adjusting the z-index of my div ...

Discovering the Power of Search Suggestions in Clojure using Jquery with Clojure/Compojure

I've been struggling to implement a search suggestion or autocomplete feature in compojure using JQuery. Here's what I have so far, but it's not working as expected. In my `routes.home`, I'm able to view the output of `list` obtained f ...

Apply a chosen style to the element that was clicked on, while removing the style from the rest

Here is an example of my ul li structure: <div id="categoryTree"> <ul> <li id="cat_15"> <a class="hasSubCat" href="javascript:void(0);"><img src="images/icons/folder.gif" border="0" alt="Folder" title=" Folder ">N ...

Sending form information through AjaxPassing information from a form using

Currently, I am working on a project where one page opens a thickbox of another page that contains a form. Once the form is submitted and data is written to the database, I need the parent page of the thickbox to update specific rows of the form that have ...

Flip the direction of this vertical CSS motion blur effect

Is there a way to modify this code so that the object can have a vertical motion blur instead of a horizontal one? I want it to move from top to bottom and then back up. Can anyone assist me with this? http://jsfiddle.net/db8gr4y6/ #outer { posit ...

Trouble arising when implementing media queries alongside jQuery

When the screen size is 768px, I need to trigger the code below by clicking on the next button. However, an issue arises when trying to revert back to the original style after changing the screen size: $(document).ready(function() { alert(); $ ...

Display elements that are positioned absolutely within a container with scrolling overflow

My modal contains a scrollable list with many items that have absolute positioned elements causing overflow within the modal. How can I ensure that the absolute positioned elements are visible in this scenario? .modal { width: 300px; border: 1px ...

Element failing to adhere to CSS grid layout

I am currently working with grid layout and aiming to position the following content at the bottom and center, but it is currently aligning to the left: .body { margin: 0; background-color: rgb(255, 237, 237); display: grid; grid-template-rows: ...

Why is my md-button in Angular Material displaying as full-width?

Just a quick question, I created a simple page to experiment with Angular Material. On medium-sized devices, there is a button that toggles the side navigation, but for some reason, it expands to full width. There's no CSS or Material directives causi ...

What is the best way to ensure an AJAX get-request waits for the page to finish rendering before providing a response?

I've been working on a Greasemonkey Script for a specific section of this website (Site1). Site1 offers various deals and discounts, and my script is designed to perform the following task: When a user visits an offer on Site1, the script checks with ...

The structure becomes disrupted when the Material Ui grid is enclosed within a div container

I currently have a responsive dashboard built with Material Ui's Grid elements. One of the grid items is wrapped in a div element, causing the layout to break. Check out the playground with the div element here: https://codesandbox.io/s/basicgrid-mat ...

Bring the element to the top of the page by clicking on the anchor within the element or anywhere within the specified div ID

I am looking to implement a functionality where the page scrolls to the top of the navigation div ID when a link inside the navigation div is clicked, or ideally even when clicking anywhere within the div itself that contains the navigation links. After r ...