How can we prevent the text from shifting when the border width is adjusted

I've run into an irritating issue. My left menu text keeps shifting when I expand the border using jQuery. I've tried various solutions to prevent the text from moving, but nothing seems to work. Any suggestions?

Could it be a CSS problem with the left menu?

.main .left {
    width: 220px; 
    margin-right: 20px; 
    float: left;
}
.main .box {
    margin-top: 10px;
}
.main .box .bname {
    font-size: 15px; 
    background: url(../images/sepa.png) no-repeat left center; 
    padding: 5px 0 5px 15px;
}
.main .box ul {
    margin: 10px 0 0 0; 
    padding: 0; 
    list-style: none;
}
.main .box ul li {
    padding-left: 10px; 
    margin-bottom: 1px; 
}
.main .box ul li a {
    display: block; 
    padding: 3px;
}

.main .box ul li a:hover{}

Here's my jQuery code for the animated border:

jQuery(document).ready(function($) {
    $(".box ul li").hover(function() {
        $(this).animate({borderWidth: 20}, "fast");
    }, function() {
        $(this).animate({borderWidth: 2}, "fast");
        });
});

The issue arises when the border expands ( borderWidth: 20 ), causing the text to shift to the right. I'm looking to keep the text in place. Here's a snippet of my HTML:

<div class="box">
   <div class="nadpis">Dokumenty</div>
   <ul>
       {foreach $MENU['USER1'] as $row}
           <li>
               <a href="{!$row->odkaz}" {widget  rsLista 'MENU',$row->idecko,$MENU_WEB['USER1'],$MENU_SLOZKA['USER1'],$row->nazev}>{$row->nazev}</a>
           </li>  
       {/foreach}
   </ul> 
</div>

Answer №1

To improve the appearance of the text, consider adjusting the element padding within the animation:

CSS

.content .container ul li{padding:20px; margin-bottom: 2px;}

JavaScript

$(".container ul li").hover(function() {
   $(this).animate({borderWidth: 22, padding:5}, "fast");
 }, function() {
   $(this).animate({borderWidth: 3, padding:20}, "fast");
});

View the demonstration http://jsfiddle.net/8GhFt/

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

Having trouble with changing images or background images in JavaScript?

Encountering issues with dynamic elements. When I click a button, the following JS function is executed. It displays a stylish CSS alert upon postback and then runs the function. function scrollToTop(sender, args) { window.scrollTo(0, 0); document. ...

Exploring JavaFX and FXML TreeTableView customization for column and header styles

I've been working on a JavaFX project and I'm having trouble customizing the style of the TreeTableView header or columns, like changing the color, width, font size, etc. I've tried using various codes like these but none seem to work. ...

Simple method to restrict duration of video uploads using HTML5's capture attribute

I'm working on a website that allows users to take photos and videos with their smartphones. Everything is running smoothly with HTML5 and some javascript, but I want to set a maximum time limit for the videos without requiring users to start recordin ...

Flexbox divs that adapt to different screen sizes

Hello everyone, I need help with a school project where I have to make blocks responsive and change their layout based on screen width. Specifically, above 1024px they should be in 2 horizontal rows, and below 1024px in 2 vertical rows, always spelling out ...

What is causing the left scroll to not work with negative values?

My design features three blocks stacked on top of each other with an additional block at the bottom below the middle. Left <--------> Middle<---------->Right -----------------Bottom------------------ I have a couple of queries. Why is scr ...

Cannot load JavaScript in Ajax tabs

I have implemented this ajax tabs on my website. However, the JavaScript code inside the content.htm file seems to be malfunctioning. Below is the code snippet I am using: Javascript $(document).ready(function(){ $("button").click(function(){ ...

Troubleshooting: Issue with changing the name of a cloned object in Internet Explorer?

Using jQuery, I have created a dynamic clone of an input checkbox object and then modified its name with the following code snippet: row.find('[name="ACCCHB_CDARM"]').attr("name",id); The original attribute's name that I am cloning is ...

Styling Divs Beside Each Other Using CSS

I'm attempting to display the "left" and "right" divs side-by-side in the following example. However, they are not appearing next to each other as expected. Can someone point out my mistake? Thank you. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...

Shapes and their corresponding encoded representations compiled in a comprehensive catalog

Can anyone provide me with a comprehensive list of HTML escaped codes for displaying various shapes on web pages? I need codes that are universally compatible with all browsers. For instance, I would like to display a square using an escaped code in one ...

Conceal or reveal radio buttons according to information in a table

How can I dynamically add and remove values from a table based on radio button selections? My goal is to add the selected value to the table and hide it from the radio button list, and if the value is deleted from the table, I want to show it back in the r ...

Contrasting WebSQL and SQLite in terms of their utility in mobile applications and web browsers

Could you confirm if WebSQL and SQLite are the same? Are both WebSQL and SQLite available in PhoneGap? Will the JavaScript code used for WebSQL in a web browser be the same for a mobile app, or will we need different code? What advantages does WebSQL ha ...

Leveraging the power of the YouTube Iframe API

I'm currently working on integrating the Youtube player API to enable video toggling when I close a modal box. While the iframe is being created as expected in my code, the button does not seem to have control over the iframe through the API. <di ...

Creating an HTML Page and Hosting it on a Server

Currently, I am in the process of learning how to set up an HTML page on a server. Although I have managed to do so, the problem is that an index page appears by default. I do not want this page to show; rather, I would prefer my home page to be visible. Y ...

Retrieving text snippet from an HTML document using .NET

Users input HTML content using a richtext editor, allowing for a variety of elements. Here is an example of such content: <h1>Header 1</h1> <p>Some text here</p><p>Some more text here</p> <div align=right><a hr ...

Angular material is experiencing an issue where content is being cut off or

I am currently working on a project using AngularJS for a web application. I have encountered an issue where some of the content in the md-content element is being clipped. For instance, the body tag has the style: overflow: hidden, and the child md-conte ...

Is there a JavaScript equivalent to the explode function in PHP with similar functionality

I'm facing an issue while attempting to split my string in JavaScript, here is the code I am using: var str = 'hello.json'; str.slice(0,4); //output hello str.slice(6,9); //output json The problem arises when trying to slice the second str ...

Submit form using jQuery after making an AJAX request with jQuery's $

I'm working on the following code: $.get(link, function(data, status) { document.getElementById("test").value = data; }); $('#formtest').submit(); Everything works fine in Firefox, but in Google Chrome the submit is done before t ...

Adjust the hyperlink color of <a> to change when hovering over <tr>

When I hover over a <tr> tag, I want the color of the associated <a> element to change to white. I attempted to achieve this using jQuery in the following way: <script> $('tr').hover(function(){ $('a').cs ...

Is it possible to generate a form dynamically in a Vue.js HTML file based on JSON data? Check out the provided f

Currently, I am diving into the world of vue.js and experimenting with building a dynamically created form. To explain further, I have JSON files that define what input types should be included in the form; now I am figuring out how to implement this usin ...

Troubleshooting issue: Unable to send file data using jQuery AJAX request

I'm having trouble sending data with a file using jQuery ajax. The code snippet I'm using is as follows: <script> function uploadImage() { var form = document.getElementById("table").value + "-form"; alert(form); ...