Tips for adding a border to an element when hovered over

I am looking to achieve a hover effect that displays a border around an element without actually adding the border to the element itself, as shown in this image:

https://i.sstatic.net/iFpCA.png

The challenge I'm facing is that I want to allow users to change styles, so directly adding a border or outline to the element won't work as it will be affected by user style changes.

Here is my approach to solving this:

  1. Created an overlay div positioned using position: absolute
  2. Inserted a nested div within the overlay also set to absolute
  3. Implemented onmouseover and onmouseout event listeners on the overlay div to gather information about the element's position and size

However, the issue I encountered is that the overlay prevents events from firing on elements beneath it, which I need for the nested element's information. I've tried adjusting the z-index but it didn't work as expected.

So, what would be the best way to achieve this effect?

PS: The screenshot is from Webflow's visual builder, and I am unsure of their method for achieving this.

Here is the relevant code snippet:

var outlineContainer = document.querySelector('#content-container');
outlineContainer.onmouseover = outlineContainer.onmouseout = handler;

function handler(event) {
  var hoverOutline = document.querySelector('.hover-outline');
  if (event.type == 'mouseover') {
    console.log(event.target.tagName);
    var clientRects = event.target.getBoundingClientRect();
    hoverOutline.style.width = `${clientRects.width}px`;
    hoverOutline.style.height = `${clientRects.height}px`;
    hoverOutline.style.transform = `translate(${event.target.offsetLeft}px,${event.target.offsetTop}px)`;
  }
  if (event.type == 'mouseout') {
    hoverOutline.style.width = 0;
    hoverOutline.style.height = 0;
    hoverOutline.style.left = 0;
    hoverOutline.style.top = 0;
  }
}
#content-container {
  border: 2px solid black;
  background-color: white;
  padding: 50px;
  position: relative;
  height: 100%;
}

.overlay {
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: 100%;
  z-index: 2;
}

.hover-outline {
  position: absolute;
  border: 2px solid orange;
  z-index: 6;
  top: 0px;
  left: 0px;
  z-index: 3;
}

.content {
  z-index: 4;
}
<div id="content-container">
  <div class="overlay">
    <div class="hover-outline"></div>
  </div>
  <div class="content">
    <div class="component">
      <label>Hi</label>
    </div>
    <div class="component">
      <label>Text Field</label>
      <span class="wrapper">
            <input type="text" placeholder="Text Input Field" />
          </span>
    </div>
  </div>
</div>

Answer №1

It seems there may have been a misunderstanding in the requirements. Is it possible to simply adjust the border color on hover without the need for JavaScript?

#content-container {
  border: 2px solid black;
  background-color: white;
  padding: 50px;
  position: relative;
  height: 100%;
}
#content-container:hover {
  border: 2px solid red;
}

.overlay {
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: 100%;
  z-index: 2;
}

.hover-outline {
  position: absolute;
  border: 2px solid orange;
  z-index: 6;
  top: 0px;
  left: 0px;
  z-index: 3;
}

.content {
  z-index: 4;
}
<div id="content-container">
  <div class="overlay">
    <div class="hover-outline"></div>
  </div>
  <div class="content">
    <div class="component">
      <label>Hi</label>
    </div>
    <div class="component">
      <label>Text Field</label>
      <span class="wrapper">
            <input type="text" placeholder="Text Input Field" />
          </span>
    </div>
  </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

Leverage Angular to highlight updated items within a list

My goal is to synchronize data, so I have a data object that holds the current state. Whenever this state changes, I want to mark the object with an attribute for filtering purposes during syncing. Here is the structure of the object: data = { type1: [ ...

Techniques, modules and functions in Javascript

How should I properly document this code snippet: // Define a collection of colors with methods colors = { // Define method for color red "red" : function() { // Do something... } // Define object for color black "black" : { // Add ...

Setting the height of the text area in a three-column layout cannot be achieved using a percentage value

I'm working on creating a 3-column layout with two text areas. The issue I'm facing is that when I adjust the width of the text areas to create columns, the height shrinks. I need a solution that will be compatible with HTML5 browsers - support f ...

The byte order of integer literals in JavaScript

When writing the following line in Javascript: var n = 0x1234, is it always true that n == 4660? This question could also be phrased as follows: Does 0x1234 represent a series of bytes with 0x12 as the first byte and 0x34 as the last byte? Or does 0x1234 r ...

Utilize C# and SmtpClient to efficiently send HTML emails

Is there a way to send HTML emails instead of plain text using SmtpClient? I have been following the code provided in this solution, but the output always ends up as plain text. For example, the link in the sample message below is not displayed as an activ ...

When floating a left and right div block, they do not necessarily align on the same line

I am looking to create a nested menu where some of the menu items have key shortcuts that I want to align to the right side on the same line. I attempted to use float left/right, but encountered an issue where the shortcuts were shifted to the next line. H ...

Ways to acquire the reference of the parent component

Within my code, there is a parent component represented as: <Parent> <Child (click)=doSomething()></Child> </Parent> I am looking to establish a reference to the Parent component in order to pass it within a method that will trigg ...

How can you enhance functionality in Polymer without the need to create a custom element?

This is the code snippet from my index.html file in Polymer 1.1: <template is="dom-bind" id="app"> <paper-drawer-panel force-narrow id="the-drawer"> <paper-header-panel drawer> <paper-toolbar></paper-too ...

Steps for changing the direction of a dropdown menu to the left instead of the right

I'm having a bit of an issue with my inbox setup. I have a dropdown for it, but the dropdown is appearing off to the right and since it's at the end of my top navbar, it's not very visible. Here is the CSS code for the inbox: .notification ...

Can the arrangement of icons/buttons on the JW Player control bar be customized?

I am trying to customize the skin of my JWplayer like this: This is my current progress: I have been researching how to rearrange the order of icon / button on the controlbar. According to the jwplayer documentation, the buttons on the controlbar are div ...

Is it necessary for Angular Reactive Form Validator to convert types before checking the value for min/max validation?

Preface: My motivation for asking the questions below stems from my experience with form.value.purchaseCost. When the <input> field does not have type=number, I receive a string instead of a number. This required me to manually convert it to Number ...

Unable to choose the option from the modal that functions similarly to a drop-down menu

I need to choose a field that functions similarly to a dropdown box, but does not actually display as one. The select field reveals its values in a pop-up with values and sub-values that I need to select. Challenges : I'm struggling to locate the ob ...

Having trouble viewing a dynamically adjusting Angular NVD3 graph embedded in bootstrap

I have been utilizing the NVD3 library along with bootstrap 3.0 for my project. I have two divs with the classes "col-md-6 col-sm-12" positioned side by side. My goal is to showcase charts in both of these divs. To ensure that the charts are displayed corr ...

Instructions on adding a class to the parent element when the child has a particular class

Here is the html structure I am working with: <section> <div class="v-middle"> <div class="row"> <h5 class="heading">Heading goes here</h5> </div> </div> </section> ...

Different ways to update the AutoComplete Style attribute

MuiInput-formControl { margin-top: 16px; Is there a way to reset the marginTop property to zero? I attempted the following method, but it was not successful. MuiFormControlLabel: { marginTop: 0, }, <Autocomplete disabl ...

Key steps for effectively implementing a Substring-Filter on a JSON array in d3

Trying to apply filtering on a JSON data array structured like this: var data = [ { "key": "FirstGroup", "color": "#1f77b4", "values": [ { "label ...

Should You Ajaxify Your Website?

Absolutely loving the way Ajax can transform a web app into something that performs like a desktop application. The concern, however, arises when dealing with high volume sites. Currently working on an intranet-based database app meant for only 2-4 users a ...

The Truffle test encounters an issue: "Error: Trying to execute a transaction that calls a contract function, but the recipient address ___ is not a contract address."

Every time I run truffle test in the terminal, I encounter the following error message: Error: Attempting to run a transaction which calls a contract function, but the recipient address 0x3ad2c00512808bd7fafa6dce844a583621f3df87 is not a contract address. ...

Is it possible to create an AngularJS and jQuery Calendar Directive that activates by clicking on a Bootstrap glyphicon?

I have successfully created a directive for my calendar using AngularJS and jQuery. The datepicker pops up when the user selects the input box. Now, I am attempting to allow the user to click on Bootstrap's 'glyphicon glyphicon-calendar' to ...

Steps to create a horizontal animation using CSS3

I have 6 different CSS3 animations that I want to split between going right and left. Can these animations be applied separately or do they all have to be the same direction? Is it also possible to adjust the speed of each animation individually? Here is ...