What is the best way to expand/shrink elements smoothly when hovering with CSS?

As a beginner in the world of coding, I am eager to tackle something simple in CSS. My current project involves a landing page that is divided into two sections down the middle. The left side features a yellow div, while the right side showcases a grey div. My goal is to create an interactive effect where hovering over one div increases its width (from left to right for the left div and vice versa) while simultaneously decreasing the width of the other div to prevent any overlap.

The code I have implemented successfully achieves this effect for the left side div. However, when I hover over the right side, the intended reduction in width for the left side does not occur, resulting in an overlap between the two.

I have tried adjusting the parent/child relationships as suggested by some resources but have yet to find a solution. Any guidance or advice on improving my code would be greatly appreciated.

#leftside {
  /*this is the orange half*/
  height: 100%;
  width: 50%;
  position: absolute;
  left: 0;
  top: 0;
  opacity: 0.5;
  background-image: linear-gradient(rgba(255, 255, 0, 1), rgba(255, 165, 0, 1));
  transition: all 1s;
}

#leftside:hover {
  opacity: 1.0;
  width: 51%;
}

#leftside:hover+#rightside {
  width: 49%
}

#rightside {
  /*this is the grey half*/
  height: 100%;
  width: 50%;
  position: absolute;
  right: 0;
  top: 0;
  opacity: 0.5;
  background-image: linear-gradient(rgba(255, 255, 255, 0), rgba(160, 160, 160, 1));
  transition: all 1s;
}

#rightside:hover {
  opacity: 1.0;
  width: 51%;
}

#rightside:hover+#leftside {
  width: 49%
}
<div id="leftside">
  <a class="leftsidehome" href="page1.html"></a>
</div>
<div id="rightside">
  <a class="rightsidehome" href="page2.html"></a>
</div>

Answer №1

Your code faces an issue with #rightside:hover+#leftside as CSS is unable to look back. Here is a suggested solution utilizing flexbox.

.container {
  display: flex;
  width: 100%;
}

#leftside,
#rightside {
  flex-grow: 1; /* Allow each flex item to grow */
  flex-shrink: 1; /* Allow each flex item to shrink */
  width: 50%;
  height: 100px; /* Used only to make them visible */
  transition: width 1s; /* Animate the change of width */
}

#leftside {
  background: yellow;
}

#rightside {
  background: grey;
}

#leftside:hover {
  width: 51%;
}

#rightside:hover {
  width: 51%;
}
<div class="container">
  <div id="leftside">
    <a class="leftsidehome" href="page1.html"></a>
  </div>
  <div id="rightside">
    <a class="rightsidehome" href="page2.html"></a>
  </div>
</div>

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

Securing Your Content - Preventing Users from Right-Clicking and Using F12 on Your Website

I am looking to disable the Right-Click function on my website or display an error message saying "Protected Content!" when someone tries to right-click. I want to prevent others from viewing my Source Code. Although I know how to disable Right-Click, I am ...

I'm experiencing an issue where the line-height css property is not functioning as expected in a JEditorPane. Can anyone provide

Having an issue with the line-height attribute in a JEditorPane. It seems to be ignoring it, even though the font and color attributes are working fine. I've checked the HTML in Firefox and it looks good. Does anyone have any idea what might be wrong? ...

Using mySQLi to store query results in an array and display them in separate DIV containers

I am looking to efficiently query the content of multiple IDs (from 1 to 6) from a mySQL database using mySQLi. Once I retrieve the content, I want to display it in separate DIVs. Currently, I am using the following code which works perfectly: <?php $c ...

Creating a scrolling sidebar in Bootstrap 4

Today I learned about CSS Bootstrap 4 and created my template with a sidebar. However, when there are many menus, I want the sidebar to show a scrollbar. I am looking for help on how to make a scrollbar appear on my sidebar template. Below are my HTML and ...

The aesthetic of react-bootstrap and material ui aesthetics is distinctive and visually appealing

As I develop my web application, I have incorporated react-bootstrap for its React components based on Bootstrap. However, wanting to give my project a customized look inspired by Material design, I came across bootstrap-material-design. I am curious if I ...

What is the best way to create a circular div that complements a circular navbar design?

Is there a way to make the red div align perfectly with the corners and height of the navbar? I have been experimenting with Bootstrap components and trying to customize them according to my needs. If using a button instead of a div is an option, that woul ...

Tips for updating the value within a textfield in HTML

I am looking to dynamically update the value displayed in my Revenue textfield by subtracting the Cost of Goods from the Sales Price. I have included an image of the current layout for reference, but I want the Revenue field to reflect the updated value af ...

Can ngFor be utilized within select elements in Angular?

I'm facing an interesting challenge where I need to display multiple select tags with multiple options each, resulting in a data structure that consists of an array of arrays of objects. <div class="form-group row" *ngIf="myData"> <selec ...

What is the best method to make a hyperlink within an IFrame open in a new window?

Let me share the code I've been working on: <!DOCTYPE html> <html> <head> <base target="_blank"> </head> <body> <div style="border: 2px solid #D5CC5A; overflow: hidden; margin: 15px auto; max-width: 800px; ...

Creating custom styles with makeStyles in Material UI

How can I properly write the following CSS value as a string in a makeStyles function within Material UI? background: linear-gradient(rgba(0, 0, 0, 0.1), rgba(0, 0, 0, 0.8)), url(../assets/pexels-pixabay-41949.jpg), I attempted to do it this way but i ...

Encountering some difficulties while setting up my development tool (specifically webpack)

I have embarked on a journey to learn modern JavaScript with the help of Webpack and Babel. As a beginner in this field, my instructor is guiding me through the principles of modern JavaScript by building an app called Forkify - a recipe application. While ...

What is the best way to target specific elements within my array using Jquery?

I have a Wordpress website where the posts contain images with the .aligncenter class. I want to style the images that are less than 400px in width by adding the display:inline-block; property and reducing their size to 40%. Here is the approach I tried: ...

Encountering issues with custom.css.scss not being properly overridden in Rails while using Bootstrap

My Configuration I made modifications to the .col-lg-4 class in Bootstrap for Rails and added them to my custom.css.scss file after the import statements to remove padding completely. The Issue at Hand I am trying to override the styling of .embed-respo ...

Steps to deactivate a JavaScript function once the page has undergone a Page.IsPostBack event

My current setup involves a simple div with the display set to none. Upon page load, I use $("#MyDiv").show(); to display the div after a delay, allowing users to enter information into the form and submit it using an asp.net button. After submitting the ...

Error: "Bootstrapsudio is experiencing a missing option in the col-md-

Recently, I was following a tutorial on Bootstrap Studio that showcased the design of a bakery named "Mom & Pop's Bakery". The tutorial highlighted how we could adjust the size of a column within a container by manipulating the column size on the righ ...

Align component within material-ui grid

Looking to center align the same cards but not just the grid component, I need the content itself to be equally distant from the borders and each other. I've searched and tried different solutions without luck. https://i.stack.imgur.com/aZj7H.png htt ...

Issue with Email Signature on replies

Recently, I had some email signatures designed and they appear great when sent out, but encounter issues upon receipt or reply: The logo gets distorted and occasionally enlarges significantly during replies. The font color switches to black when receivin ...

Develop a custom script function for every instance of a @foreach loop

I have a challenge where I need to style automatically generated table rows by clicking on a button. Currently, my code appears as follows: @foreach($tours as $tour) <tr id="{{$tour->id}}"> <td> {{$tour->tournr}} &l ...

Can users be prevented from bookmarking a particular web page?

I'm working on a Python (Django) webpage and I need to prevent users from being able to bookmark a certain page. Is there a way to do this? ...

What is the best way to arrange my crimson blocks to sit next to each other?

Is there a way to align the red boxes side by side with some space in between them for separation, and how can I ensure that the signin/signup button appears against the blue background of the header? I attempted using float but encountered issues. The d ...