Guide on how to selectively apply the transform effect to a box-shadow

I am working on a navigation menu that utilizes the :hover pseudo classes to create a box-shadow effect.

This is how a normal nav item looks and this is the same item under :hover state.

To achieve a chunky, overlapping, slanted underline appearance, I am applying a skew transformation to the box-shadow.

.link:hover {
    box-shadow: 0 -0.5em 0 #7ed6df inset;
    transform: skew(-20deg);
}

The skew effect works as intended; however, it also distorts the text of the .link when I only want the box-shadow to be skewed.

I attempted to define a parent style using @apply like this:

--link-underline: {
    box-shadow: 0 -0.5em 0 #7ed6df inset;
    transform: skew(-20deg);
}

...

.link:hover {
    @apply --link-underline;
}

Unfortunately, there was no noticeable change when hovering over the link. I also experimented with using an ::after pseudo-element to add the shadow to a hidden text element but could not find a solution that worked. Any advice would be greatly appreciated. Thank you!

Answer №1

After experimenting with pseudo elements and classes further, I successfully implemented a functional solution!

To begin, I introduced these two properties to my original .link class to ensure text stability:

display: inline;
position: relative;

Next, I crafted an ::after pseudo element for the .link class:

.link::after {
    content: '';
    position: absolute;
    z-index: -1;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    box-shadow: 0 -0.5em 0 #7ed6df inset;
    transform: skew(-20deg);
    opacity: 0;
}

This pseudo-element is positioned beneath the original text using z-index and remains hidden with 0 opacity. The box shadow and skew effects are applied to this ::after element without affecting any text since its content is empty. All that was left was to implement the :hover function, which sets the pseudo-class opacity to 1:

.link:hover::after {
    opacity: 1;
}

I appreciate the suggestions provided, as I specifically wanted to utilize box-shadow for this task while also aiming to animate a swiping motion.

Answer №2

After some experimentation, I managed to come up with a solution that aligns with your request. While I can't guarantee it's the most optimal approach, it does seem to get the job done. By adjusting the value of top: 11%;, you should be able to fine-tune the positioning based on how your browser renders it. Hopefully, this provides some assistance for your needs.

hr.underline {
  border: 5px solid #7ed6df;
  transform: skew(-20deg);
  display: none;
}

.hover:hover .underline{
    display: flex;
    width: 5%;
    position: absolute;
    top: 11%;
    z-index: -1;
}
<div class="hover">
  <h3 class="link">Why</h3>
  <hr class="underline">
</div>

Answer №3

Check out this creative approach utilizing multiple background layers:

.message {
  display:inline-block;
  padding:5px 10px 5px;
  background:
    linear-gradient(-225deg,transparent 10px,lightblue 10px) bottom left /51% 15px,
    linear-gradient(-45deg ,transparent 10px,lightblue 10px) bottom right/51% 15px;
  background-repeat:no-repeat;

/* Removing the following section as it is not essential unless hover effect is required */
  background-size:0 0;
}
.message:hover {
  background-size:51% 15px;
}
<div class="message">some message here</div>
<div class="message">message</div>
<br>
<div class="message">loooooooooooooooooooong message</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

What could be causing the list-group and list-group-item classes to not take effect in this situation

Currently diving into Bootstrap 5, I decided to play around with lists. Here is a snippet of the code I'm working with: <ul class="list-group"> <li class="list-group-item">Cras justo odio</li> <li class="l ...

Ways to show text on top of an image using CSS

Seeking help! I'm attempting to overlay some text on top of an image, here's the link: http://jsfiddle.net/5AUMA/31/ The challenge is aligning this text to the bottom part of the image. I've tried using this CSS: .head_img p { position ...

Stopping CSS animations at a specific point along the path without using timing functions can be achieved by implementing a targeted

Is there a way to pause an animation at the 50% mark for 2 seconds and then resume? P.S. The setInterval method is not recommended! function pauseAnimation() { document.getElementById("0").style.animationPlayState = "paused"; }; var pauseInterval = set ...

Placing a form amidst two div containers

I am delving into the world of CSS and Bootstrap, attempting to recreate a simple layout. However, I'm encountering some challenges. My goal is to stack 2 divs on top of each other with a form that spans both divs. Can you provide insight on how to a ...

CSS exclusive - achieving a horizontal scrolling list with all list items in a single row

I am working with a div that has a fixed width. Inside this div, there is an unordered list (ul) containing a random number of list items (li) with varying widths. My project utilizes the Bootstrap framework. Below is an example of the code structure: & ...

"jQuery enables hover effects to be applied to elements that are not directly related

I'm attempting to achieve an effect where an element will show or hide when I hover over a completely different element. Currently, my approach involves using lists and indexes so that the nth item in list 2 changes when the nth item in list 1 is hov ...

In what way does a child's width vary when set to "100%" or "inherit" from its parent element?

Can a Child element, nested within a Parent, have a width that is different from the parent's width when: 1) the Child's Width is set to 100% 2) the Child's Width is set to 'inherit'? I am experiencing both scenarios. It is challe ...

JavaScript Deviance

I am facing an issue with my JS code while trying to send form data to a .php file via AJAX. The problem occurs when the input fields are filled - for some reason, my client-side page refreshes and the php file does not get executed. However, everything wo ...

Inquiry about Date and Time Selection Tool

I am working on a PHP project that includes two textboxes: one for selecting dates and the other for choosing a specific time. What I need assistance with is disabling any times before the selected date in the second timepicker textbox if today's dat ...

Utilizing Background Images in CSS/HTML

Just starting out in the front end world! :D I'm having trouble setting the background of my login page. It's not extending to cover the entire page. Here's how it looks:https://i.sstatic.net/bQQLv.png This is the HTML code I'm using: ...

The red solid outline of 1px is not functional when attempting to enlarge on hover

Recently, I stumbled upon a PSD design that really caught my eye. https://i.sstatic.net/R46Ko.jpg It inspired me to create a website similar to . One feature I particularly liked was how a circle appeared when hovering over a link. Plus, I plan on using ...

Angular 2 Dropdown Multiselect Plugin - Fully Customize Width to 100%

Currently working on integrating the angular-2-dropdown-multiselect component: https://www.npmjs.com/package/angular-2-dropdown-multiselect The component functions correctly, but I am looking to adjust its width to fit the container... I believe simply ...

Placing Bootstrap nav-items outside of the justify-content-center alignment

Having trouble positioning a button in the nav container to the right side of the screen while keeping all other nav items centered using justify content center. I've tried the following solutions: Using margin-left:auto on the button, but it shifts ...

AngularJS - Organize Item Hierarchy with Separate Containers for Each Group

My goal is to incorporate a $scope variable within an AngularJS controller that has the following structure: $scope.hierarchy = { name: 'bob', selected: true, children: [ { name: 'frank' }, { name: 'spike' ...

Experiencing difficulties with my image slider and struggling to determine the cause

Trying to create an image slider, but I'm encountering numerous challenges. It seems like everyone finds it easy to do, but for me, it's a struggle. I've been following along with this tutorial. Watch Tutorial Here However, the buttons don ...

<a> slightly off-centered horizontal alignment

I've been trying to center a link using different methods like text-align:center and display:inline-block, but it seems to be slightly off-center. I've attached images along with my code below for reference. Any assistance would be greatly apprec ...

Hide panel when button is clicked - bootstrap

Is it possible to make a panel collapse by clicking a regular bootstrap button? Below is the code snippet: <div class="panel panel-primary" style="border-color: #464646;"> <div class="panel-heading" style="border-color: #BBBBBB; height: 35px; ...

Can you help me figure out how to make a header that sticks to the top of the page as you scroll horizontally, and a sidebar that stays in place as you scroll vertically with

I am currently working on developing a layout that includes a sticky header and sidebar for my content list. My goal is to have the sidebar scroll down as I move through the page content, while also ensuring that the header scrolls horizontally along with ...

tips for showing search outcomes with Angular

I'm currently working on a Mat-Chips Angular project and I am curious about how to display the user input results. At the moment, I have a user input field and data within the TS file. Beneath the search box, my goal is to show the number of items t ...

Bootstrap: Ensuring vertical stacking of columns on mobile devices for responsive design

Here is the code snippet I am working with: <div class='carousel'> <div class='data-ng-repeat='{{ repeat }}'{% endif %}> <div class='col-xs-12 col-sm-3 vdivide valign-wrap' data-ng-repeat=&apos ...