How can I stop an element from shifting when hovering over a border?

My HTML structure is set up like this:

ul {
  list-style: none;
  padding: 0;
  margin: 0;
}
li:hover {
  border: 2px solid;
}
<ul>
  <li>one</li>
  <li>two</li>
  <li>three</li>
</ul>

When the `:hover` event triggers, the position of the <li> element changes. How can I keep it constant (fixed)?

Note: I am able to achieve this using `box-shadow`, but I prefer not to as most of my website's visitors use older browsers. Are there any alternative solutions available?

Answer №1

To achieve a unique effect, try incorporating a transparent border around each li element within your HTML.

This technique will override any borders that are typically displayed when hovering over the elements.

ul {
  list-style: none;
  padding: 0;
  margin: 0;
}
li {
  border: 2px solid transparent;
}
li:hover {
  border: 2px solid;
}
<ul>
  <li>one</li>
  <li>two</li>
  <li>three</li>
</ul>

Answer №2

Another option is to utilize the outline property.

ul{
list-style:none;
padding:0;
margin:0;
}

li:hover{
outline: 2px solid;
}
<ul>
  <li>apple</li>
  <li>banana</li>
  <li>cherry</li>
</ul>

Answer №3

Apply a default border to the child elements that matches the background color of the parent element or is transparent, then change the border color on hover.

ul{
    list-style:none;
    padding:0;
    margin:0;
}
li{
    border:2px solid #fff;
}
li:hover{
    border-color:#000;
}
<ul>
  <li>one</li>
  <li>two</li>
  <li>three</li>
</ul>

Another approach would be to set the line-height and left padding of the child elements, then reduce them on hover like this:

ul{
    list-style:none;
    padding:0;
    margin:0;
}
li{
    line-height:24px;
    padding:0 0 0 7px;
}
li:hover{
    border:2px solid;
    line-height:20px;
    padding:0 0 0 5px;
}
<ul>
  <li>one</li>
  <li>two</li>
  <li>three</li>
</ul>

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

Is it possible to navigate to specific sections on a one-page website with an offset?

Creating a single page navigation with automatic active classes and smooth scroll is functioning properly, but I encountered an issue where the fixed navigation covers my headings slightly after scrolling. To address this problem, I included 'top - 7 ...

Tips for adjusting the color of the sidebar in an HTML email template

My attempt at modifying the background color of my HTML email template has been focused on changing the sidebar's white color, rather than altering the background of the email body itself. This is the CSS code I have: @import url('https://fonts. ...

Complete the form by following a hyperlink located on a different webpage

I recently developed three PHP pages. The first one consists of the following form: <form method="POST" name="go" action="search_form_all.php" > <input name="value" type="text" id="search_form_1" size="65" placeholder="enter name"/> <i ...

Creating a custom alarm sound with Vue.js

I have implemented a websocket using Vue.js in the following way: The socket initiates when it receives 'any' in real-time. It retrieves data from a REST API. I would like to play a short alarm sound, like 'dingdong', when data.ge ...

What is the best way to adjust a div's height to fill the remaining space of the viewport after the header's height

Is there a way to create a hero section that fills 100vh minus the height of the header, especially when the height of the header is not fixed? I initially used CSS with a height property set to calc(100vh - 310px), where 310px represents the height of t ...

I am looking to incorporate a dropdown feature using Javascript into the web page of my Django project

According to the data type of the selected column in the first dropdown, the values displayed in the columns of the second dropdown should match those listed in the JavaScript dictionary below, please note: {{col.1}} provides details on the SQL column data ...

Steps to correct alignment in a numbered list when the dot is removed using CSS

Currently, I'm working on developing a rule page for a game. The specific layout requirements dictate that the rules be presented in an ordered list without periods following each number: 1 Rule one 2 Rule two 3 Rule three To achieve this format ...

Swapping out the "select file" and "add file" buttons for a single unified button

I'm working on implementing file uploads in ReactJS and I want to streamline my UI by having just one "Upload" button instead of separate "choose file" and "upload" buttons. Here's the code snippet: import React, { useRef } from 'react' ...

Unable to set CSS image as background

Currently, I am in the process of developing a website using HTML 5, CSS, and JavaScript. However, I am encountering an issue with image manipulation on the site. I am attempting to make the image the background but every method I try seems to be lacking a ...

Issue with continuous loader malfunction

I integrated a 3-second mini-loading animation on my website. It shows up every time I refresh the site or navigate to another page. The issue I'm facing is that once I add the loading section, it never stops (it should only last for 3 seconds). Usua ...

Using a combination of a bootstrap class and a custom CSS class to enhance your website design

How do I apply two different classes? I have two HTML select fields where the user can choose either repair or another option in the first one. When the user selects one, the second field should appear. But it also needs to have a Bootstrap class: form-con ...

Adjusting a model using an input is causing a ValueError

Attempting to enable the user to modify a decimal value named 'amount'. Once the user clicks on 'edit', a decimal field will be displayed. Upon finishing the value change and clicking "done"... An error is thrown: Exception Type: Valu ...

Vue.JS is unable to parse JSON data

Recently, I decided to experiment with Vue JS and started working on a new page. One of the features I wanted to include was a button that would trigger a chat module to pop up. Here's the current code snippet I have: <!DOCTYPE html> <html l ...

Executing a bash script on the server

I am in the process of developing a webpage control panel for my remote server. My goal is to have the ability to execute local scripts by simply clicking buttons on this webpage. After taking some guidance from (stackoverflow), I have put together the f ...

How to Combine Various Conditions in a ngStyle Directive?

I am working with a NgStyle Tag and would like to incorporate multiple conditions, but I am encountering issues where it doesn't work or produces errors. Here is the code snippet that I have been experimenting with: <div class = "card" [ngStyle] ...

Setting the initial position for an SVG path

Working on a unique loading animation with 3 shapes that will scale as they follow a path. Each shape begins from a different starting point but follows a similar path. Shapes and paths were created in Illustrator and exported as SVGs. See below for an exa ...

Experience interactive 3D model viewer in action

I want to embed this 3D model viewer into an existing div instead of creating a new one. I've made some edits to the code but it's not working. Any assistance would be greatly appreciated. <script> // Here is where the model viewer cod ...

How can I save or export an image file containing HTML content?

Currently, I am working on a project where I am generating dynamic HTML content. My goal is to be able to export or save this HTML content as an image file using PHP, jQuery, and JavaScript (or any other method if applicable). Can anyone help with the im ...

Retrieving all elements that contain the specified search value with Jsoup

I am currently developing an Android app that involves working with HTML. Specifically, I need to extract part of an HTML body, retrieve attributes, and search for values containing a specific element. While I have successfully accomplished the first two t ...

Aligning float:left divs vertically

I have multiple divs with equal widths but varying heights that I need to fit together tightly. When I set them to float left, they don't align vertically but instead line up at the bottom of the row above. Even with the example below, I want to eli ...