Ensure that the ::after element matches the size and position of a flex child element

I've been exploring ways to enhance the box-shadow effect on my flex elements, taking inspiration from this insightful article.

Unfortunately, I'm running into issues as the implementation is not functioning correctly. Here's a demo of the problem: example.

One attempt I made was setting the position: relative property on the nav element, but it resulted in the ::after element occupying the entire navbar.

<nav>
  <div class="nav-left">
    <a href="#">
      hello
    </a>
  </div>
  <div class="nav-center"></div>
  <div class="nav-right"></div>
</nav>
nav {
  display: flex;
  min-height: 5rem;
  background-color: #402424;
  align-items: stretch;
}

nav a,
nav .brand {
  text-decoration: none;
  align-items: center;
  display: flex;
  color: #AFAFAF;
}

nav a::after {
  content: '';
  position: absolute;
  z-index: -1;
  width: 100%;
  height: 100%;
  opacity: 0;
  border-radius: 5px;
  box-shadow: 0 5px 15px rgba(0,0,0,0.3);
  transition: opacity 0.3s ease-in-out;
}

nav a:hover::after {
  opacity: 1;
}

Answer №1

Here is an alternative approach:

Include the following in nav a

nav a{position:relative;}

Additionally, add the following to nav a::after

nav a::after{
  top:0;
  right:0;
  left:0;
  bottom:0;
}

body {
  background: #20262E;
  padding: 0;
  margin: 0;
  font-family: Helvetica;
}

nav {
  display: flex;
  min-height: 5rem;
  background-color: #402424;
  align-items: stretch;
}

nav a,
nav .brand {
  text-decoration: none;
  align-items: center;
  display: flex;
  color: #AFAFAF;
  position:relative;
}

nav a::after {
  content: '';
  position: absolute;
  z-index: 1;
  opacity: 0;
  top:0;
  right:0;
  left:0;
  bottom:0;
  border-radius: 5px;
  box-shadow: 0 5px 15px rgba(0,0,0,0.3);
  transition: opacity 0.3s ease-in-out;
}

nav a:hover::after {
  opacity: 1;
}
.nav-left,
.nav-center,
.nav-right {
  display: flex;
  flex: 1;
}

.nav-left {
  justify-content: flex-start;
}

.nav-center {
  justify-content: center;
}

.nav-right {
  justify-content: flex-end;
}
<nav>
  <div class="nav-left">
    <a href="#">
      hello
    </a>
  </div>
  <div class="nav-center">
  
  </div>
  <div class="nav-right">
    World
  </div>
</nav>

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

Prevent the border from shrinking upon being clicked

I'm currently working on customizing an accordion using jQuery for the animation effects. However, I am facing an issue where clicking on the accordion header causes the border around the plus sign to shrink in size as well. My goal is to only have th ...

Steps for splitting a numbered list and adding an image above each item:

I have a challenge I'm trying to tackle with my list: My goal is to create a long, numbered list that is divided into different sections I also want to include an image within each list item and have it display above the numbered title of the sectio ...

Is it recommended to employ @media print for a webpage dedicated exclusively to printing purposes?

Imagine a scenario where I have a specific page featuring a print button. Clicking on this button redirects me to a separate page with the same content, optimized for printing purposes. Instead of using @media print directly on the original page due to n ...

Whenever I adjust the zoom on my HTML page, it either scrolls upwards or downwards

Is there a way to prevent the page from scrolling when using zoom-in or zoom-out? I attempted using overflow: hidden in the body, but it did not work. <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

Show one line of text at a time with a pause in between each one

On my HTML page, I am looking to showcase text lines in succession with a delay. The unique condition is that when the second line appears, the color of the first line should change. Then, as the third line emerges, the color of the second line should also ...

Photo not completing the table

Hey there, I'm currently working on creating a table to showcase some products but I am struggling with ensuring that the images display at the correct size. Any assistance on this matter would be greatly appreciated. #tbldesktops { width: 100%; ...

The bootstrap carousel is unable to load at the moment

I've been attempting to create a carousel using Bootstrap, but it's not displaying correctly in Firefox or Google Chrome on Windows 8. Here's the code I used, could someone please help me identify what I might have done incorrectly? <htm ...

What are the top recommendations for implementing typography in SCSS?

As I ponder the best approach to take, I find myself torn between two options and wondering what the 'best practice' in this situation might be. My objective is to implement a selector that removes the standard global margin of 20px that I have ...

How can you easily center content vertically on a web page?

There has been plenty of back-and-forth regarding this issue, with proposed solutions ranging from pure CSS to pure HTML. Some can get quite complex, involving deeply nested divs and intricate CSS rules. I'm looking for a simple and uncomplicated answ ...

Extract attributes from a string of HTML containing name-value pairs

Here is a sample string that I need to work with '<img id="1" data-name="test" src="img_01.jpg" />' My goal is to extract the attribute name and value pairs from the string, create the element, and apply these attributes. I have come up ...

problem arising from the box shadow and transparent areas of an image

I'm facing an issue while attempting to apply a box-shadow effect on my image. The problem arises due to certain transparent parts of the image causing unexpected results with the box-shadow. To better understand the situation, refer to the code snipp ...

Trouble with table class functionality persists following insertion of SQL database

After successfully setting up the table layout and connecting it to a database, I encountered an issue with the class not working as expected. Despite being able to retrieve records and display them in the table, the desired layout was missing. <bo ...

Mastering the precise placement of Angular-UI Bootstrap popovers: A step-by-step guide

I have created a simple Angular-UI popover in my application. However, the predefined popover-placement options are not meeting my requirements. I want the placement of my popover to be at the bottom, as indicated in my HTML notes below. Unfortunately, t ...

Keeping a dangerouslySetInnerHTML div separate from the global CSS to prevent any potential risks

As I render an email HTML string with all its styles defined in-line in my NextJS React app, I notice discrepancies in the styling: <div id='emailContent' dangerouslySetInnerHTML={{ __html: sanitizedHtml }} /> It seems like there is a clas ...

Struggling to make @font-face function properly

I am attempting to incorporate a custom font into my website. In my CSS file, I have the following: body { font-family: HurmeGeometricSans4_SemiBold; } @font-face { font-family: HurmeGeometricSans4_SemiBold; src: url(resources/Hurme_Design_-_ ...

Using the scale transformation with an iframe on Google Chrome

It's puzzling why iframes seem to lose quality with large transform:scale values in Chrome's latest stable version (67). Do you think it's a bug or a feature? Thankfully, other browsers don't seem to have this issue. In Chrome: https:/ ...

The cascading style sheet used by a lightbox

I constructed a lightbox using the following HTML code: <a id="show-panel" href="#">Show Panel</a> <div id="lightbox-panel"> <h2>Lightbox Panel</h2> <p>You can add any valid content here.</p> <p align="center" ...

Position a div to float in the top right corner without overlapping its sibling header

Is there a way to position a div in the top right corner of a section without overlapping the text of a h1? This is the HTML code: <section> <h1>some long long long long header, a whole line, 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6</h1> < ...

What is the best way to target a class within a span using CSS?

I need help with hiding an icon from the mobile menu. I am unsure about how to target this specific class within the menu structure. <a class="funiter-menu-item-title" title="Nieuw">Nieuw<span class="icon fa fa-chevron-down"></span>< ...

Interacting with jQuery and HTML: Revealing sub dvi box content by clicking on the current div box ID

This is my unique jQuery and HTML code. My objective is to display the corresponding sub_box when clicking on box1, box2, or box3. For example: Clicking on box1 should reveal sub_box_1. I have attempted some jQuery code but haven't achieved the desi ...