When elements are hovered over, they shift due to the addition of borders. Contrary to suggestions from elsewhere, padding does not seem to have

I have a dilemma with my testing website. Whenever I hover over a certain area, a border gets added which causes other elements to move around. I tried adjusting the padding as suggested in some posts, but it didn't solve the issue because it collided with other content.

Here is where I set the border:

#logo:hover {
  border: 2px solid cyan;
}

And this is the section it was applied to:

#logo {
    width: 200px;
    height: 200px;
    position: relative;
    top: 80px;
    line-height: 12em;
    border-radius: 200px;
    margin: 0 10px 0 10px;
    overflow: hidden;
}

If anyone has a solution or advice on how I framed my question, please let me know.

#logo {
    background-color: black;
    width: 200px;
    height: 200px;
    position: relative;
    top: 80px;
    line-height: 12em;
    border-radius: 200px;
    margin: 0 10px 0 10px;
    overflow: hidden;
}

#logo:hover {
  border: 2px solid cyan;
}
<div id=logo></div>

Best regards, TheWombatGuru

Answer №1

Another option is to incorporate box shadow:

 #logo{
    box-shadow: inset 0 0 0 1px cyan;
 }

 #logo:hover {
    box-shadow: inset 0 0 0 2px cyan;
 }

Answer №2

Feel free to include:

* {  box-sizing: border-box;}

in your CSS file or specifically in the #logo ID.

You can learn more about this (in my opinion, an incredible property) here

(Just so you know... the code mentioned applies the property to all HTML elements on your website, a practice I've been using in my recent projects with no regrets)

Answer №3

Consider incorporating the following into your #logo style:

border: 2px solid transparent;

This adjustment will result in:

#logo {
width: 200px;
height: 200px;
position: relative;
top: 80px;
line-height: 12em;
border-radius: 200px;
margin: 0 10px 0 10px;
overflow: hidden;
border : 2px solid transparent;
}

An alternative option is to utilize box-sizing: border-box

Hope this suggestion proves beneficial!

Cheers!

Answer №4

Consider including border:1px solid transparent; for elements that are not currently being hovered over.

Answer №5

Using box-sizing: border-box may not always result in a perfect circle when adding borders, despite its usefulness. To achieve the desired effect, consider updating your CSS as follows:

#logo:hover {
border-left: 3px solid cyan;
border-right: 3px solid cyan;
padding: 0;
margin: 0 6px 0 6px;
}

Additionally, make adjustments to your #logo class margins with the following code:

{margin: 0 9px 0 9px;}

While this solution may seem a bit unconventional, it provides the intended outcome that I personally find preferable.

Answer №6

To ensure the padding is included within the width of the container, make sure to include box-sizing: border-box in your CSS for the relevant element.

Check out this codepen example to see it in action: http://codepen.io/himmel/pen/LVPPvg

Answer №7

Instead of relying solely on box-sizing: border-box;, you have the option to utilize the calc function to subtract a specific value.

#header:hover {
    width: calc(300px - 10px);
    height: calc(300px - 10px);
    border: 5px solid magenta;
}

This technique can be quite handy, especially when working with percentages. It's definitely a nifty workaround for scenarios like this.

Answer №8

Consider using the CSS property position: absolute; as it allows elements to be positioned independently without affecting other elements. Ensure that the parent element is assigned a position of relative. Here is an example:

` #container { position:relative;

}

#image { position:absolute; }`

Answer №9

If you want to keep elements stationary when hovering, add the following line of code to #logo.

#logo {
  border: 2px solid transparent;
}

Adding a transparent border to prevent elements from resizing on hover is key when applying borders to initially borderless elements.

However, be cautious with adjacent elements as adding a transparent border may create gaps between them. To avoid this, ensure that the border color matches the background color of the element.

Here is the original code snippet for reference:

#logo {
    background-color: black; /* Background */
    width: 200px;
    height: 200px;
    position: relative;
    top: 80px;
    line-height: 12em;
    border-radius: 200px;
    margin: 0 10px 0 10px;
    overflow: hidden;
}

To implement the recommended change, add the following line:

 #logo {
        border: 2px solid black;
    }

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

How to use Iframe for "bookmarks" without causing the main browser to jump?

I currently have multiple iframes on my main page, each with a src attribute set to an empty value. I've used jQuery to create a function that when a button is clicked, the src for the corresponding iframe fills up with website.com/#title1. My issue ...

Missing arrow icon in Bootstrap 4 navbar menu and sub menu

I am attempting to use the navbar component from Twitter Bootstrap 4 with a nested sub menu, but the arrow in the menu item that has a sub menu is not appearing at all, and I am unsure why. Here is where the arrow appears: https://i.sstatic.net/J6xI4.jpg ...

When initializing a new instance of Stripe using cartalyst/stripe-laravel, an error occurs stating "The Stripe API key has not been properly defined!"

I am eager to explore Stripe and learn how to process payments. Currently, I am utilizing the cartalyst/stripe-laravel package to create a new instance of Stripe following the instructions provided here. The code snippet should resemble this: $stripe = ...

Changing the font-family to 'Symbol' and using Windows-1252 character encoding

I have a collection of HTML documents that contain basic text encoded in Windows-1252, but scattered throughout the content are various instances of span elements styled with font-family: Symbol. For instance: <span style='font-family:Symbol& ...

Prompt for user confirmation when a button is clicked using jQuery

I'm facing a situation that I haven't found an answer for yet. I have a button on my webpage that triggers an ajax call to perform a certain action when clicked. However, I want to add a confirmation popup with the message "Are you sure you want ...

The challenges of $location.search().name and base href in application URLs

I am working on an Angular app and my URL appears as http://localhost:8080/personal?name=xyz. To extract the 'xyz' value in JavaScript, I am using $location.search().name. Here is a snippet of my code: app.js app.config(function ($locationProv ...

What causes a dot to display when hovering over a link in a list?

Good day. [Click here to access the test page](http://89.111.180.28/CatalogOfProductsAndServices.php) Code: <ul> <li> <a href="#">Бытовая техника</a> <ul> <li><a href="CatalogOfProductsAndServices.php ...

Is there a way to only refresh the div specifically for the calendar with an id of "

$(document).ready(function() { $("#client-list").on("change", function() { var selectedValue = $(this).val(); location.reload(); }); }); Is there a way to refresh only the div with id='calendar' without refreshing the entire pa ...

Executing a function in JQuery with care

Hey there, I have a total value output and I am trying to add a function that links to it. Unfortunately, I am encountering some issues while trying to put it together. The console keeps showing NaN or the "not defined" error. Here is the function I want ...

The right-aligned navigation bar elegantly shifts downward when it exceeds the screen width

I've been struggling to create a Right Aligned Navigation Bar with a search bar and a login icon that stay in one row on the page. No matter how I try, it keeps jumping down instead of aligning properly. Here is an image of what I'm trying to ach ...

Using PHP to Populate HTML Elements Depending on the Attribute of the Parent Class

I maintain a personalized TV schedule that I manually update using a static <table>. Each <tr> consists of 5 <td> elements with the following content in this sequence: date, time, competition, match, and TV channel. <table> <t ...

Function is raising an error with the wrong value or text

I am currently testing a form that I am in the process of developing. My goal is to have different values returned each time I click on a different item from the dropdown menu. If this explanation isn't clear, please take a quick look at my pen for cl ...

Center the title vertically APEXCHARTS

I created this chart using Apex charts https://i.sstatic.net/j34Wc.png However, I am facing an issue where the 'Chart' title and caption are not properly aligned. The title should align with the graph horizontally, and the caption vertically. ...

Eliminate the error notification on the login page if it corresponds to the input

I am facing an issue with my login page and the API for password validation. Even when the password matches, an error message is still being displayed. This is because I am looping through the data and need to break the loop once a match is found. How can ...

How can you find the index of an HTML element while excluding any clearfix divs?

In the process of developing a CMS, I find myself in need of determining the index of the element I am currently working on. Consider the following stripped-down HTML structure: <div class="row"> <article class="col-md-4 col-sm-4 project" &g ...

steps for making a dropdown search bar

I am currently working on integrating a search form into my navigation bar. My goal is to have a small magnifying glass icon in the navigation bar, which, when clicked, will reveal a dropdown search form (similar to how it's done on this site - ). I&a ...

Styling elements with CSS and using it to toggle the visibility of divs based on radio

When the radio button #my102 is selected, the div #navi102 should be visible and all other #navi??? divs should be hidden. Similarly, if the radio button #my7 is selected, the div #navi7 should be visible and others hidden. What would be the best approach ...

Turn off Appbar padding at the top and bottom

I am looking to create a customized box within the Appbar that spans the full height, as illustrated in the image below: https://i.stack.imgur.com/CFMo0.jpg However, the default Appbar provides padding from all sides to its internal elements, leading to ...

Learn how to toggle between two different image sources with a single click event in JavaScript

If the button is clicked, I want to change the image source to one thing. If it's clicked again, it should change back to what it was before. It's almost like a toggle effect controlled by the button. I've attempted some code that didn&apos ...

What are the steps for capturing video using openCV in a python program?

I am facing a challenge in figuring out how to capture a video using openCV. While I have managed to display the video on an html template with the existing codes, I am uncertain about which codes are needed to actually record the video. -camera.py- i ...