Tips for containing a range slider within a div element in a flexbox container

I am currently using Javascript to generate a grid of images, and I want to include a range slider at the bottom of one of the images. Here is a simplified version of my code using flex-container:

<style>
  .flex-container {
   display: flex;
   flex-wrap: wrap;
   background-color: LightYellow;
  }

.flex-item {
  background-color: #f1f1f1;
  width: 300px;
  margin: 10px;
  text-align: center;
  line-height: 300px;
 font-size: 30px; 
 }

</style>
<body>
 <div class="flex-container">
   <div class="flex-item">1</div>
  <div class="flex-item"> 2 </div>
</div>
</body>

The grid layout with two images side by side works as intended:

https://i.sstatic.net/dWdcx.png

Now, I am trying to add a range slider to the second image. I have defined the styles for the slider container and slider elements:

  .slidecontainer {
    flex: 0 1 auto;
    order: 0;
    position: relative;
    align-items: center;
    width: 100%;
   }

 .slider {
     -webkit-appearance: none;
     width: 100%;
     height: 5px;
     border-radius: 5px;
     background: #d3d3d3;
     outline: none;
     opacity: 0.7;
     -webkit-transition: .2s;
     transition: opacity .2s;
   }

  .slider:hover {
    opacity: 1;
   }

  .slider::-webkit-slider-thumb {
     -webkit-appearance: none;
     appearance: none;
     width: 25px;
     height: 15px;
     border-radius: 50%;
     background: #4CAF50;
     cursor: pointer;
    }

 .slider::-moz-range-thumb {
    width: 25px;
    height: 15px;
    border-radius: 50%;
    background: #4CAF50;
    cursor: pointer;
  }

I then try to embed the range slider in the second div of the flex-container:

<div class="flex-item"> 2
   <div class="slidercontainer">
     <input type="range" min="1" max="100" value="50" class="slider" id="myRange">
     <p>Value: <span id="demo"></span></p>
  </div>
</div>

Unfortunately, this causes sizing issues with the images. Here is how it looks now:

https://i.sstatic.net/lQODD.png

Is there any way to maintain the dimensions of the images while adding the slider at the bottom of one of them within its div?

Thank you!

Answer №1

Below is the HTML code snippet:

<div class="flex-container">
        <div class="flex-item">
            <img src="https://placeimg.com/300/150/any" alt="">
        </div>
        <div class="flex-item">
            <img src="https://placeimg.com/300/150/any" alt="">
            <div class="slidercontainer">
                <input type="range" min="1" max="100" value="50" class="slider" id="myRange">
                <p>Value: <span id="demo"></span></p>
            </div>
        </div>
    </div>

Here is the corresponding CSS style rules:

img {
       max-width: 100%;
       object-fit: cover;
    }
        .flex-container {
            display: flex;
            flex-wrap: wrap;
            background-color: LightYellow;
        }

        .flex-item {
            background-color: #f1f1f1;
            width: 300px;
            margin: 10px;
            font-size: 30px;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: space-between;
        }

        .slidecontainer {
            flex: 0 1 auto;
            order: 0;
            position: relative;
            align-items: center;
            width: 100%;
        }

        .slider {
            -webkit-appearance: none;
            width: 100%;
            height: 5px;
            border-radius: 5px;
            background: #d3d3d3;
            outline: none;
            opacity: 0.7;
            -webkit-transition: .2s;
            transition: opacity .2s;
        }

        .slider:hover {
            opacity: 1;
        }

        .slider::-webkit-slider-thumb {
            -webkit-appearance: none;
            appearance: none;
            width: 25px;
            height: 15px;
            border-radius: 50%;
            background: #4CAF50;
            cursor: pointer;
            }

        .slider::-moz-range-thumb {
            width: 25px;
            height: 15px;
            border-radius: 50%;
            background: #4CAF50;
            cursor: pointer;
        }

Answer №2

The appearance seems exaggerated due to the excessive line-height: 300px; applied to the .flex-item element. Removing this should restore a more balanced look.

To maintain the images' original aspect ratio, ensure that the max-width matches your desired width and then set width: 100%;. It's also recommended to wrap the image in a div container since IE11 can sometimes present issues with images as flex items.

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

Best practices for securing passwords using Chrome DevTools in React development

React developer tool inspector Is there a way to prevent password values from appearing in the inspector as a state when handling form submissions in ReactJS, especially when using Chrome's React developer tool? ...

Is there a way to extract shared properties within a Material UI style declaration?

I am working with a specific set of style rules that define different statuses: status_in_progress: { fontSize: 14, backgroundColor: theme.palette.success.dark, padding: 2, paddingLeft: 5, paddingRight: 5, display: "inline-bl ...

Enhancing user interfaces with jQuery by updating DOM elements

Can anyone help me figure out how to update a webpage so it functions as a report that multiple people can contribute to? I'm using forms to collect data and want it to instantly display under the correct category headings. Currently, I'm using j ...

Using CSS, center a div element both vertically and horizontally, and position the footer at the bottom of the page

I am encountering some unexpected behavior for a task that I thought would be simple. I have two main objectives in mind. Firstly, I want the footer to be displayed at the bottom of the page as an actual footer. Secondly, I need the div with the ".center-d ...

nsIProcess - Launch with Background Execution and Deferred Activation

Currently, my method of launching Firefox is as follows: var exe = FileUtils.getFile('XREExeF', []); //this provides the path to the executable var process = Cc['@mozilla.org/process/util;1'].createInstance(Ci.nsIProcess); process.init ...

The size of the search input and textarea in HTML decreases when viewed on an iPad device

Currently utilizing Bootstrap 3 and AngularJs for this project. Implementing the following markup for the input field with type 'search': <input type="search" class="form-control" id='roomSearch' placeholder="Search" ng-model=&apo ...

Is there a method to achieve a similar effect as col-md and col-sm but for adjusting height instead?

I'm looking for a way to adjust the height based on the device size like how sm, md, and lg classes work. I'm designing a login form that looks great on cell phones, but appears too tall with excessive spacing when viewed on an iPad. There are la ...

What could be causing the alignment issue with this html photo gallery?

Struggling to center a photo library on my webpage is really frustrating me. Does anyone have any tips? I've experimented with different ways to center it in the code, but nothing seems to do the trick :/ HTML: <div style="display: inline-block; ...

What is the best way to implement JavaScript code that can modify the layout of a website across all its pages?

I am facing an issue on my website where, on the index page, clicking a button changes the background color to black. However, this change is not reflected on other pages even though I have linked the JavaScript file to all HTML documents. How can I make i ...

Prevent the tab key from exiting the input field and instead direct it to a designated element on the webpage

I have customized a select menu for user interaction, but I am facing issues with normal keyboard behaviors not working as expected. Specifically, I want to enable tab functionality to navigate to my styled select menu just like when clicking tab to cycle ...

Updating items within an array in a MongoDB collection

I am facing a challenge where I have to pass an array of objects along with their IDs from the client-side code using JSON to an API endpoint handled by ExpressJS. My next task is to update existing database records with all the fields from these objects. ...

Issue encountered: Unable to add MongoDB object to database

Recently, I have encountered an issue with a script that looks like this: use first_db; advertisers = db.agencies.find( 'my query which returns things correctly ); close first_db; use second_db; db.advertisers.insert(advertisers); This error mess ...

An instructional HTML/JS dialogue for a linked page

On my website, there is a link that, when clicked, opens a new tab with a page that I don't control. I want to guide the user on what to do next after they are redirected to this new page ("Now please press the green button on this page"). Ideally, I ...

Determine the data type of an object's key

I have a XInterface defined as: export interface XInterface { foo: (() => Foo[]) | Foo[], bar: string, baz: number } When declaring an object using this interface, I want the type of foo to be Foo[], like so: const myObj: XInterface = { ...

Navigate to link based on selected option in Bootstrap select menu

How can I make a page redirection based on the selected option value from a bootstrap select? Here is the HTML code: <select class="selectpicker"> <option value="https://example.com/">Home</option> <option value="https://exam ...

Sophisticated Arrangement of Div Elements

Here's a scenario where Two divs are dynamically styled from the Database. I am attempting to align two divs in the layout shown here: https://i.stack.imgur.com/nYI7v.png Can this be achieved using Bootstrap's CSS class col-? In responsive mo ...

Rotating Tetris pieces around an axis using HTML5 Canvas

I am currently working on a project and I have encountered a problem. When you press the UP key and hold it down, you will see what I mean: My goal is to make the object rotate around its axis instead of its current behavior. Please take a look at the co ...

The hamburger menu in Bootstrap collapses along with the navigation items when viewed on a mobile device

How can I resolve the issue of the hamburger menu icon collapsing with the nav-items when clicked? Navbar <header> <div class="container-fluid"> <div class="row"> <div class="col-12 col-sm-12"> ...

Issue with rendering Backbone subview correctly

Today, I delved into the world of website development using backbone.js. Surprisingly, after a whole morning of trying to crack a puzzling problem, I find myself stuck. Let me focus on the crucial bits of code here. Initially, I have a View named Navigat ...

What is the best way to divide a list in a Django template?

Currently, I am working on pagination in Django and looking to display the page numbers that come after the current page. The code snippet I am using is as follows: {% for page in blogs_page.paginator.page_range|slice:"0:{{ blogs_page.number }}" %} Howev ...