Enlarging the current division while reducing the size of the others when hovering

Currently, I am working on a school project that involves creating a slideshow on a webpage. However, I have reached a point where I am unsure of how to proceed. Here is the progress I have made so far:

body {
  background-color: #252525;
}

#wrapper {
  width: 90%;
  margin: 0 auto;
  padding: 2%;
}

#images {
  width: 100%;
  height: 300px;
  text-align: center;
  overflow: auto;
}

#container-1 {
  display: inline-block;
  background-color: #fff;
  width: 100px;
  height: 300px;
  transition: .5s ease-in-out;
}

#container-1:hover {
  background-color: #189fff;
  width: 80%;
  height: 300px;
}

.container {
  width: 100px;
  height: 300px;
  background-color: #fff;
  display: inline-block;
  transition: .5s ease-in-out;
}

.container:hover {
  background-color: #189fff;
  width: 80%;
  height: 300px;
}
<div id="wrapper">
  <div id="images">
    <div id="container-1"></div>
    <div class="container"></div>
    <div class="container"></div>
    <div class="container"></div>
  </div>
</div>

The idea behind this design is for each image (or div) to expand when hovered, displaying the complete picture. There are two types of images - clipped and unclipped. Perhaps this division could cause some issues?

The class "container" serves as a placeholder to demonstrate the layout and provide background colors for the other divs. The current #container-1:hover setting may not align perfectly with future image sizes.

Without overflow: auto;, additional divs may get displaced below one another, which must be avoided.

The code functions somewhat according to my requirements. However, hovering over one div pushes the others aside, leading to conflicts. Is there a solution to prevent this behavior? Possibly adjusting the widths of surrounding divs while one is being hovered on?

I am new to JavaScript and still learning, so any feedback or suggestions would be greatly appreciated. Unfortunately, we are restricted from using jQuery or similar libraries.

To view a live demo, visit this fiddle: jsfiddle

Answer №1

The issue you are facing is that when one of the elements is hovered and expands, the combined width of all elements exceeds the width of the container. This causes the last one or two elements to be pushed below the others onto the next line.

To prevent this from happening using only CSS, you need to select specific width values where three default elements and one expanded (hovered) element do not exceed 100% of the container's width. You can see an example of this in action in the following JSFiddle:

https://jsfiddle.net/kju94h1n/

If you want the non-hovered elements to become narrower when another element is hovered, then Javascript would be required to achieve that effect.

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

Creating a dynamic configuration for service instantiation in Angular 4/5

Currently working on a library that has an AuthModule with an AuthService for managing OAuth2 authentication using oidc-client-js. I want the application using this library to be able to set up the configuration for the OAuth client. One way to do this is ...

Using HTML5 datetime-local to only accept dates in the format Y-m-d

I am currently working on a form that includes a datetime-local input field. <input type="datetime-local" id="datetime" /> After clicking on the today button, the date is automatically set to today's date like this: 2018-11-05 --:-- However ...

Error: The last line is missing a trailing comma

I'm struggling to understand why my tslint insists on having a trailing comma at the end of the last line in the objects. Is there a way to configure the ignore rule for the last line of objects? Appreciate any help! For example: settings = { ...

Adjusting the size of images in real time

Currently, I am in the process of creating a Fluid grid style website and I am looking to decrease the size of all images by 75% when the screen is below 1280px, 50% at 800px, and so forth. Is there a way to achieve this using the IMG tag? My attempt so ...

The Angular Material layouts demonstration is experiencing technical difficulties

I'm attempting to run the Angular Material grid layouts demo titled Flex Percent Values, which can be accessed here. Here are some snippets from my HTML code: <head> <meta charset="UTF-8"> <meta name="viewport" content="initial-sc ...

JavaFX text label does not adjust its font according to the CSS file

In my FXML file, the structure is as follows: ... <BorderPane id="header"> <right> <Label styleClass="title" text="A Label" /> </right> </BorderPane> ... I also have a CSS file with the following content: #h ...

Tips for Avoiding Database Overflow Due to Excessive AJAX Spam Requests

Let's consider a simple scenario: A create album button is used to input album data into the database. I disable the button while the request is being processed, then re-enable it upon completion. Once the processing is finished, ...

What is the best way to iterate over an indexed attribute in PHP?

Here is my ajax code snippet: $.ajax({ type: "POST", url: "bee_sesi_edit.php", data: 'serv_ruang='+ serv_ruangx +'&who='+names +'&sesi_d1='+ sesi_d1 +&apos ...

Change the order of the table data elements using the nth-child CSS selector

I need the second td in my table to appear on top of the first one when I resize the screen. @media screen and (min-width: 200px) and (max-width: 872px) { td :nth-child(1) {display:bottom} td :nth-child(2) {display:top} } <table border="0" cell ...

Issue with Aligning Text Inputs and Images in Firefox

I am really struggling with this issue and it's driving me crazy. The problem lies with an image positioned at the end of a text input, styled using CSS. Here is the code snippet: .text_input{ background:url(../images/forms/text_input_back.png) t ...

What is the reason behind Firefox opting for a 2x image while Chrome goes for a 1x image when the devicePixelRatio is 1

When deciding between 1x and 2x options: Firefox will opt for 2x if devicePixelRatio is less than 1 Chromium will choose 2x if devicePixelRatio is less than 1.5 Testing this in Safari is tricky as devicePixelRatio remains constant even when scaling t ...

Using Vuex: Delay dispatch of action until websocket response received

Let's look at the given scenario and premises: To populate a chat queue in real time, it is necessary to establish a connection to a websocket, send a message, and then store the data in a websocket store. This store will handle all the websocket sta ...

"Encountering a Prisma type error while attempting to add a new record

I am currently utilizing Prisma with a MySQL database. Whenever I attempt to create a new record (School), an error of type pops up in the console. Additionally, I am implementing a framework called Remix.run, although it does not seem to be causing the is ...

The HTTPOnly cookie is not accessible within the getServerSideProps function in Next.js

Why isn't the jid cookie available in getServerSideProps using Next JS? Here's the scenario: https://i.stack.imgur.com/FLRGk.png The jid cookie is generated by an Expressjs API at https://api-dev.example.com. I'm attempting to retrieve thi ...

Error Encountered During JavaScript Form Validation

Currently, I am troubleshooting a website that was created by another developer. There is a form with JavaScript validation to ensure data is accurately entered into the database. However, I am puzzled as to why I keep receiving these alert messages. Pleas ...

Retrieve the XPath for the elements that combine to create a specific string within an HTML document

Issue: Searching for the xpath of node(s) that contribute to a text string in an HTML document. Using Python language (lxml to parse the document) To demonstrate, let's examine the following document: <HTML> <HEAD> <TITLE>samp ...

issue with logging in, token verification failed

My current project involves creating a login system with authorization, but for some reason the token is not being transferred properly. const path = require('path'); const express = require('express'); const bodyParser = require(' ...

Issue with flex item not expanding to fill entire height within flex container

My markup contains the following code: .container { display: flex; width: 500px; } .col1 { background: red; height: 100%; width: 50px; } .col2 { background: blue; flex: 1; height: 200px; } <div class="container"> <div class= ...

The CSS header remains the same size regardless of the size of the dynamic table

Is there a way to set the header in an XHTML page to be as wide as the page itself? I used width:100%, but the issue is that I have a dynamic table and when it grows bigger, the header doesn't expand. How can I solve this problem? #header{ top: 0 ...

Using Observables to assign values received from API calls to each element during loop iterations

As I loop through using a foreach loop, I need to call functions that will make async API calls and return values to be rendered in the HTML. The first function getCurrentValue() will return the currentTemperatureRef which should then be assigned to recei ...