Constructing a horizontal slider using vertically floating divs

Struggling to create a basic horizontal image slider using overflow:hidden and floating divs. Despite efforts, the divs keep stacking vertically instead of horizontally. Numerous online examples have been attempted without success.

HTML:

<div id="slidingWindow">
    <div class="slidingSection clearfix">Something something</div>
    <div class="slidingSection clearfix">Again something</div>
</div>

CSS:

#slidingWindow {
overflow:hidden;
width: 470px;
height: 500px;
background-color: red;
}

.slidingSection {
margin: 5px;
background-color: green;
width: 470px;
height: 400px;
float: left;
}

.clearfix:after { 
content: "."; 
visibility: hidden; 
display: block; 
height: 0; 
clear: both;
}

View this JSFiddle for a simplified version of the issue: http://jsfiddle.net/v4udd47t/

Answer №1

If your website supports IE10+ and doesn't need to worry about Opera Mini compatibility, you can utilize display: flex. This eliminates the need for extra markup, floats, or clearfix.

When using flex, ensure to set a min-width on the slides that equals the container width minus any margins and padding. Consider any margins and padding applied to the slides within the container (e.g., 5px margin).

HTML:

<div id="slidingWindow">
    <div class="slidingSection clearfix">Something something</div>
    <div class="slidingSection clearfix">Again something</div>
</div>

CSS:

#slidingWindow {
    overflow:hidden;
    width: 480px; /* slide width + left and right margins */
    height: 500px;
    background-color: red;
    display: -ms-flex;
    display: -webkit-flex;
    display: flex;
}

.slidingSection {
    margin: 5px;
    background-color: green;
    width: 470px;
    min-width: 470px; /* required */
    height: 400px;
}

Below is an adjusted version of your code with reduced size and hover animation to demonstrate functionality:

#slidingWindow {
overflow:hidden;
    width: 260px;
    height: 300px;
    background-color: red;
    display: -ms-flex;
    display: -webkit-flex;
    display: flex;
}

.slidingSection {
    margin: 5px;
    background-color: green;
width: 250px;
    min-width: 250px;
height: 200px;
    -webkit-transition: -webkit-transform 750ms;
transition: transform 750ms;
}

#slidingWindow:hover > .slidingSection {
    -webkit-transform: -webkit-translate3d(-260px, 0, 0);
    transform: translate3d(-260px, 0, 0);
    -webkit-transition: -webkit-transform 750ms;
    transition: transform 750ms;
}
<div id="slidingWindow">
    <div class="slidingSection">Something something</div>
    <div class="slidingSection">Again something</div>
</div>

Answer №2

If you're wondering why your sliders aren't behaving as expected, it could be due to how they are wrapped within another div. To fix this issue, ensure that each slider is wrapped in a parent div with a width equal to the total number of slides multiplied by their individual width. I've updated your example and added overflow: scroll so you can witness the difference for yourself http://jsfiddle.net/v4udd47t/1/

<div id="slidingWindow">
    <div class="slider-container">
        <div class="slidingSection clearfix">Something something</div>
        <div class="slidingSection clearfix">Again something</div>
    </div>
</div>


.slider-container {
    width: 1000px;    
}

To dynamically set the width, use the following code snippet:

var number_of_slides = $(".slidingSection").length;

$(".slider-container").css('width', number_of_slides * 470 + "px");

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

All browsers experiencing issues with autoplay audio function

While creating an HTML page, I decided to include an audio element in the header using the code below: <audio id="audio_play"> <source src="voice/Story 2_A.m4a" type="audio/ogg" /> </audio> <img class= ...

The utilization of the <span> tag featuring a {float:right] property causes container expansion in Internet Explorer 7

Within my HTML code, I have an A tag button that contains a Span element to hold icons. This setup functions correctly in most browsers, except for IE7. When I attempt to float the Span to the right side using CSS, it causes issues specifically in IE7, cau ...

Several treeviews for selecting data

I need some help with a specific assignment. The back end code is all set, but I'm struggling with the UI component. The task requires two tree views, one on the left and one on the right. The left tree view will contain states with multiple children, ...

An effective method for targeting a specific button within a CSS file

I have multiple button tags in my code, but I need to style a specific one using CSS. How can I target this particular button and apply styles only to it while leaving the others unchanged? Do I need to assign the button to a variable and reference that va ...

Unlocking the potential of data inputted in a JQuery Autocomplete form

Seeking assistance with extracting data from a form to utilize for additional purposes <form onsubmit="return false;"> Enter a School: <input id="schoolsearch" type="text" /> <INPUT TYPE=SUBMIT VALUE="GO"> </form> I am struggling ...

Issue with resizing keyboard/dropdown popup in Android hybrid app when navigating offscreen

Here is the offscreen navigation menu I have set up. When I open a keyboard or dropdown, the offscreen menu changes and exhibits a small gap on the left side after the popup (highlighted in red). It seems like the menu is shifting further away from the ed ...

Current page with animated CSS3 menus

I found a cool menu example on this webpage: http://tympanus.net/Tutorials/CreativeCSS3AnimationMenus/index10.html. I want to modify it so that the current page's menu item has a different color from the others. For example, when I'm on the home ...

Tips for hiding the frame of a React MUI Select component

I am having trouble figuring out how to remove the border around the <Select> component from the React Material UI library. In the image below, you can see that when the <Select> component is not selected, it has a black border/frame. https:// ...

Incorporate a JSON file into the Webpack output separately from the bundle.js file

Is there a way for my webpack application to read a JSON file at runtime without including it in the bundle.js after packaging? I want the JSON file to be available in the package folder but not bundled with the rest of the code. How can I achieve this? ...

Is it unnecessary to use both "width: inherit" and "float: none

When learning about responsive design, one pattern that is frequently seen is: @media screen and (max-width: 480px){ .class1{ width: inherit; float: none; } .class2{ width: inherit; float: none; } f ...

How to add an 'alt' text tag to a background image sprite

I have a button that opens a new browser window, so I've added alternate_text and title tags to notify the user that a NEW window will open when hovered over. <img class="mp3PlayBtn" alt="Mp3 player opens in popup window" title="Mp3 player opens i ...

Missing or Lost Values in Object-Oriented PHP Arrays

I am fairly new to object-oriented PHP and PHP in general. I have a class where I assign array values in the constructor, but later when I try to access the array, it shows as null. Any ideas why this might be happening? Could it be an issue with scope? c ...

Execution of Ajax call fails to occur synchronously

I have created a unique weather website that utilizes the flickr API as well as the yahoo API to gather weather data. However, I am facing an issue where the ajax call from the yahoo API does not retrieve the necessary data in time for the content of the p ...

Applying the 'overflow: scroll' property creates a scroll bar that remains fixed and cannot be scrolled

Looking to showcase the seating arrangement in a cinema hall. If the seats occupy more than 50% of the page, I want a scroll bar to appear. Attempted using "overflow-scroll" without success. View result image <div class="w-50"> <div ...

Utilize CSS to prioritize the image and ensure it is responsive

I'm utilizing the 'featurette' feature in bootstrap. Currently, the page displays text on the left and an image on the right. As you reduce the browser size, they stack on top of each other, with the text appearing above the image. How can I ...

Animate the background of a table cell (td) in React whenever a prop is updated

I have a dynamic table that receives data from props. I would like the background animation of each cell (td) to change every time it receives new props. I've tried creating an animation for the background to indicate when a cell is updated, but it on ...

How to retrieve the value from an editable td within a table using Jquery

I am working with a dynamic table that looks like this: <table> <tbody> <tr> <td>1</td> <td contenteditable='true'>Value1</td> </tr> <tr> ...

Challenges arise when using Bootstrap 4 for country selection

There have been reports that bootstrap 4 country select is not functioning properly. In an effort to troubleshoot, I delved into the documentation to find a solution. To make bootstrap-select compatible with bootstrap 4, refer to: Bootstrap 4 beta-2 ...

The JavaScript functions are not loading within the onload event handler

I've got an HTML document that contains some Script sections. I've consolidated all the functions within a single Script Tag. Now, I want to be able to utilize these methods in both the onload and onclick events. Below is the HTML file with all t ...

Position two div elements side by side

Here is the HTML code snippet that I am working with: <div class="demand page"> <div id="crumby-title"> <div class="page-icon"> <i class="fa fa-line-chart"></i> </div> ...