Hover effect affecting adjacent divs on left and right sides

My navigation bar, known as .navbar, consists of 7 direct child elements called navbar-item.

Currently, I have implemented a hover effect where the width and height of a navbar item increase by 20px upon hovering. However, my goal is to enhance this interaction further so that when an item is hovered over, its dimensions grow by 20px while its neighboring divs (to the left and right, excluding edge cases) expand by 10px.

An ideal reference for the desired outcome would be the macOS app bar effect.

Here's a snippet of the code:

body {
  background-color: #D3D3D3;
}

.navbar {
    width: 600px;
    height: 50px;
    border-radius: 999px;
    background-color: white;
    padding: 5px;
    display: flex;
    align-items: center;
    justify-content: space-around;
}

.navbar-item {
    border: 1px solid black;
    height: 50px;
    width: 50px;
    border-radius: 50%;
}

.navbar-item:hover{
    width: 70px;
    height: 70px;
}
<div class="navbar">
  <div class="navbar-item"></div>
  <div class="navbar-item"></div>
  <div class="navbar-item"></div>
  <div class="navbar-item"></div>
  <div class="navbar-item"></div>
  <div class="navbar-item"></div>
  <div class="navbar-item"></div>
</div>

I attempted using

.navbar-item:hover + .navbar-item
, but it only affected the div to the immediate right of the hovered element.

After reviewing a similar question here: How to affect other elements when one element is hovered, I found that it did not provide a solution to my specific query.

Answer №1

:has() selector can be useful in this scenario. Instead of using width/height, I am utilizing scale for demonstration purposes.

body {
  background-color: #D3D3D3;
}

.navbar {
  width: 600px;
  height: 50px;
  border-radius: 999px;
  background-color: white;
  padding: 5px;
  display: flex;
  align-items: center;
  justify-content: space-around;
}

.navbar-item {
  border: 1px solid black;
  height: 50px;
  width: 50px;
  border-radius: 50%;
  transition: .3s;
}

.navbar-item:hover { /* main element */
  scale: 1.5;
}
/* direct neighbors */
.navbar-item:hover + *,
.navbar-item:has(+ .navbar-item:hover){
  scale: 1.2;
}
<div class="navbar">
  <div class="navbar-item"></div>
  <div class="navbar-item"></div>
  <div class="navbar-item"></div>
  <div class="navbar-item"></div>
  <div class="navbar-item"></div>
  <div class="navbar-item"></div>
  <div class="navbar-item"></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

Creating a web form with the ability to select multiple images using Javascript

Currently, I am in the process of developing an HTML form that enables users to select an image, a URL, and text that will be inserted as a <li> into a webpage. However, I am facing an issue where I want users to create multiple <li> per input. ...

Is it possible for jQuery to navigate to a different page, extract around 10 links, and then incorporate them into the original page?

Apologies if the title of my question is unclear, allow me to clarify. I'm interested in exploring the possibility of a specific task and would appreciate guidance on how to proceed. While I haven't attempted it myself yet, I am eager to learn mo ...

The pie chart generated by Google may be small in size, but it certainly packs

Below, you will find my page layout created using Bootstrap 4 and Google Charts. The layout consists of three black boxes, with the one on the right displaying a Google pie chart. However, I'm facing an issue where the area allocated for the chart is ...

How can I replicate the functionality of the span element using Javascript?

Using Javascript, I am able to display a paragraph without the need for HTML. By adding it to an HTML id, I can manipulate individual words within the text. My goal is to make specific words cursive while keeping the entire paragraph in its original font s ...

Is it possible to integrate a Neo4j Graph Visualization running on a virtual machine into my flask user interface?

My current setup involves running Neo4j on an Azure Virtual Machine and accessing the graph visualization through a web browser. In addition, I have developed a UI using Python with Flask that is currently running locally. I am interested in embedding th ...

Activate a change or response when the input value in Javascript is altered

Currently, I am working on a small project using JQuery and facing an issue when trying to remove error classes from HTML elements. To handle the removal of the error classes, I am utilizing $('selector').on('input') event to target th ...

The :before element is not displaying when using jQuery's slideToggle() method

When using only CSS, the .nav-main:before{} element is displayed, but with jQuery, it does not toggle that element and always keeps it hidden. Disclaimer: This issue has been observed on mobile devices (tested in Android Chrome). jQuery $('.menu-to ...

Trouble with implementing conditional styles in Next JS

<label className={`${darkModeActive ? 'text-brand-five' : 'text-brand-seven'} ${errors.invoiceDate ? 'text-[#EC5756]' : ''}`} htmlFor="invoiceDate"> Invoice Date </label> I am encountering a ...

Issue with Bootstrap Carousel Interval Setting not Functioning as Expected

I recently added Twitter Bootstrap-Carousel to a website with the intention of using it to navigate through different sections of a module. However, I'm encountering an issue where setting the interval to false does not work as expected. When I set an ...

Retrieve the CSS attributes within a directive specifically designed for child elements

Currently, I am iterating through the child elements of the element to which the directive is attached, and adding mouseup and mousedown callbacks: var dragDroppables = element[0].children; for (var elementIdx = 0; elementIdx < dragDroppables. ...

Fetching the jquery variable within an HTML document: A step-by-step guide

Here is the content of my check.js JavaScript file, which includes a function: function fun2(userid) { $.ajax({ type: 'POST', url: 'ex.php', data: {userid: userid}, success: function (data) { ...

A guide to changing the height of a column in Bootstrap

I've utilized Bootstrap to create features in the HTML, and I'm attempting to make each box's height consistent. Check out a screenshot of the boxes here #hr-1 { width: 100px; margin-left: 20%; } #features { background-color: #f0 ...

Tips for adjusting a div to perfectly fit your screen height

When I try to display images or videos in modal divs, they end up being taller than my screen size: Here is my code snippet: <div class="modal fade" id="showMedia" tabindex="-1" role="dialog" aria-hidden="true"> < ...

Set up a trigger to activate when a WordPress User Role is selected, or alternatively, execute JavaScript

Looking for a solution to detect when a new user is created with a custom User Role set to lessee. Options include adding a trigger through WordPress filters or actions, or detecting the change in JavaScript using an event listener. I have identified the ...

Switch up the position of an element every time the page is refreshed

I have a webpage containing 5 images, each measuring 48px by 48px. I would like these images to be displayed in random positions on the page every time it is loaded. While I am aware that I will need to use CSS and JavaScript for this task (specifically f ...

Styling Tip: Removing a header from the initial page of a chapter using CSS

Using CSS to generate HTML to PDF, I have successfully added page numbers at the bottom and chapter names at the top of each page using @top-center in the CSS and HTML code below. The rendering is done with Prince. However, I need a way to exclude the hea ...

What's the reason for the font weight property in CSS not affecting the font family?

The code structure in my HTML file includes a link to Google Fonts with the following call: <link href="https://fonts.googleapis.com/css?family=Lato:400,700" rel="stylesheet"> Following that, I specify the font family and weight in the CSS section: ...

top margin is functioning properly in Internet Explorer, but not in Google Chrome

margin-top is behaving differently in IE compared to Google Chrome. Two menus are supposed to be displayed one above the other in my design. The issue lies in the line margin-top:30%; within .anothermenu ul. In Chrome, the second menu appears above the f ...

Tap, swipe the inner contents of a <div> element without being inside it

(Make sure to check out the image below) I'm facing an issue with a <div> on my website. It's not taking up the full width of the page, and the scrolling functionality within the <body> is not working as expected. I just want the cont ...

Manipulate component properties using CSS hover in React with Material-UI

I am attempting to modify the noWrap property of a Typography element when hovering over it. I have defined a custom CSS class for the parent element and the hover functionality is working correctly. However, I am unsure how to adjust the noWrap property u ...