Maintain the initial spacing when altering the font weight

Two divs were created with padding in each one.

div {
  display:inline;
  background-color:yellow;
  padding: 2px 2px 2px 2px;
}

div:hover {
  font-weight:bold;
}
<body style="font:15px arial,sans-serif;"> 
  <div>one</div>
  <div>two</div>
</body>

The CSS code above sets the font weight to bold on hover for the divs.

Once the first div is hovered over, the second div shifts slightly to the right due to the additional space needed for text with bold font.

Is there any way to prevent this shifting? Can we consider the extra space even when the font weight is normal (i.e., when the div isn't being hovered)?

Your assistance is greatly appreciated!

Answer №1

The div containers are designed to adjust and conform to the text within them, so as the text expands, the divs will also shift accordingly. For more stability, consider specifying a fixed width for the div containers to prevent any movement.

Answer №2

Adjust the width to a set measurement or decrease the padding during hover.

Answer №3

Try using float:left; instead of display : inline; and assign a specific width to the div elements for better layout control.

div {
  background-color: #FFFF00;
  float: left;
  padding: 2px;
  width: 41px;
}

div:hover {
  font-weight:bold;
}
<div>one</div>
<div>two</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

Difficulty with Flexbox in Internet Explorer 11 when utilizing a Bootstrap .badge element

At the moment, I am dealing with a flex-row that contains an element that grows with the available space and a badge that should be aligned to the end without growing. However, when using flex-grow: 0 in IE11, the span.badge does not even take up the width ...

How come the font size and div elements combine when I drag and drop the items?

Recently, I decided to create my own drag and drop game. The game is almost complete, but there's one issue. When I try to drop the items into the designated "Drop Items Here" area, their style changes abruptly to mimic the text. For example: https: ...

When I adjust the size of my browser, the elements on my HTML page shift around

I'm facing an issue where all my elements move when I resize my browser, and I cannot figure out why. How can I ensure that they stay in their respective positions even when the browser is resized? Is there a CSS solution for this? Thank you! body ...

Struggling to add custom styles to an Ionic radio button

I'm struggling to adjust the position of the icon in an Ionic radio button so that it sits a bit higher, but I can't seem to figure out how to do it. Below is the code snippet for reference: // HTML <ion-radio class="radio-input" mo ...

The Android Webview is experiencing difficulties with loading CSS styles and displaying the blockquote font color effect

I am facing an issue with loading HTML content in CSS through Android WebView. Despite having the code executed, I don't see any observable changes in font color for the text inside blockquote tags Here is the HTML code: <html> <head> ...

The Django Ajax Comment feature successfully updates and displays new comments, however, it seems to be unable to clear the content from

There was an issue with my comment form where the value wasn't being removed after submission. I had to refresh the page in order to comment again. I need assistance with this problem, specifically referring to the view function and Main JS part. Pl ...

Get rid of irritating photo borders

It's a mystery to me why no one seems to have the answer to this question. Take a look at . The spacer images are surrounded by borders. While I could use empty divs instead of spacer images, I'm using them here to make a point. This issue also ...

Information from contact forms gets inputted into the URL

Currently, I am in the process of developing a website and implementing a Contact Form that allows customers to email me directly. To test if the mail function is working correctly, I am using a Test Mail Server listening on Port 25 through a WAMP server v ...

What could be causing the NaN error when parsing a number in Javascript?

I'm having trouble figuring out why I keep getting a NaN when I try to print a number with JavaScript. This code snippet is used in multiple places on the website and usually works without any issues. The URL where this issue is occurring is: Here ...

The label element in a CSS form is not displaying correctly

After clicking submit on the form located at this link: I am experiencing an issue where the validation messages are not displaying as intended. I would like them to appear next to the input elements, inline with the form. I suspect that there may be a p ...

The padding on the button inside the v-card is not being applied as expected

I am facing an issue with the following code snippet: <v-card height="200"> <v-card-actions class="mb-0"> <v-btn flat color="orange">Share</v-btn> <v-btn flat color="orange">Explore</v-btn> & ...

Structuring PHP, HTML, and jQuery files on a website

I'm a beginner in web development and currently working on a school project to create a basic educational video platform. My next task involves turning my site into a single page application, which I plan to accomplish with the help of jQuery and AJAX ...

Ways to conceal text or a div element and substitute it with asterisks

I'm seeking assistance with a concept involving a simple toggle button that can hide an object and replace the empty area with ****. My initial idea was similar to a password form input where you can conceal the password by clicking an eye icon. Howe ...

Adding CSS styles to a pre-existing table structured like a tree

Can a CSS style be applied to a pre-existing HTML table structured as a tree? For instance, in Firefox, the Bookmarks Library table is set up as a tree. Is it feasible to add a CSS style to one specific column without affecting the others? While using tr ...

Place a date picker in the recently added row

How can I add a date picker to a newly appended row using jQuery? $(document).on('click', '.fa-plus-square', function () { var newRow = $('.installment').html(); $('.payment').append('<div class="form- ...

Problem encountered in Firefox using Selenium: Invalid expression SyntaxError when dealing with Div tags in XPath

I encountered an error while attempting to automate a sample Qlikview web report using Selenium Browser: Firefox Xpath returned by FirePath: .//*[@id='58']/div[2]/div/div[1]/div[4]/div[1] Exception: **Exception**: Exception in thread "main ...

Assistance required: The database is not found upon page reload. How can this be resolved?

My HTML5 code has created a database with create, add, delete, and print operations. However, the ObjectStore is re-created every time I load the page, and the additional values stored are not present upon reloading the page. What could be wrong with my ...

Utilizing jQuery's POST Method to Fetch JSON Data on a Website

I've developed a Java REST API and now I want to retrieve JSON data on my website (which is built using HTML & CSS) by utilizing jQuery. The method to be used is POST, and here is the structure of my JSON: { "sessionID" : "25574", "interactiv ...

Creating a triangle shape using Bootstrap to style a div element, similar to the image provided

Trying to achieve the look of the attached image with a few divs. I know I can use a background image like this: background:url(image.png) no-repeat left top; But I'm curious if there's another way to achieve this without using a background ima ...

Issue with the close button on ngb-alert not functioning properly

As I develop my website, I have incorporated an ngb-alert component to display an alert message to users upon login. While the alert itself is functioning properly, I have encountered an issue with the close button being misaligned, and I am struggling to ...