Creating a dynamic text expansion feature triggered by the mouse hovering over

I am looking to create an expanding menu text similar to what can be found on this webpage: example

Below is my JavaScript code that currently works but has some drawbacks. The code is lengthy, not very professional, and lacks responsiveness. For instance, when the screen resolution is reduced, most of the letters extend beyond the screen.

I have placed each letter in a separate div element.

Question: Is there a way to group all the 'div's into an array and make them expand based on the width of their parent div ('NavPersonalBlog')?

Thank you for any suggestions and apologies for any language errors :)

$(".NavPersonalBlog").on("mouseenter", function() {
  // Animation code here
});
$(".NavPersonalBlog").on("mouseleave", function() {
  // Animation code here
});

Answer №1

No jQuery or JS code is necessary here. You can create the desired effect by utilizing a CSS transition with the letter-spacing property:

div {
  font: bold 2em arial;
  text-align: center;
  transition: letter-spacing 0.5s;
}
div:hover {
  letter-spacing: 2em;
}
<div>HEADING</div>

Answer №2

To achieve the desired outcome, the CSS property letter-spacing is a good place to start.

Although, as mentioned by @chazsolo in their response, this method does not provide perfect centering. By adding a non-breaking space at the beginning of the text, it can help create additional spacing between characters - yet still resulting in centering based on character count rather than width. (It also slightly shifts the alignment when not hovered.) Even with varying letter widths, the X remains fixed due to three letters on each side:

div {
  text-align:center; 
  transition: letter-spacing 1s;
}

div:hover {
  letter-spacing: 20px
}
<div>SAMPLE</div>
<div>&nbsp;MMMXIII</div>

This approach is straightforward and may suffice, but for more accuracy, consider following chazsolo's advice.

Answer №3

The answers provided are commendable, however, they do not meet my design standards in terms of centering. It is evident that the centering in the accepted answer falls between the characters D and I, while in the "SAMPLE" answer, the P serves as the center point. Below is a code snippet that will help achieve a more aesthetically pleasing center, although it may require a bit more structure (extra HTML) and setup.

div {
  display: flex;
  justify-content: center;
  align-items: center;
}

span {
  text-align: center;
  flex: 1;
  transition: all 1s ease-in-out 0s;
  max-width: 3%;
}

div:hover span {
  max-width: 7%;
}
<div>
  <span>H</span>
  <span>E</span>
  <span>A</span>
  <span>D</span>
  <span>I</span>
  <span>N</span>
  <span>G</span>
</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

The Fetch function seems to be malfunctioning in Next.js

An issue has arisen while working with Next.js. The problem arises when attempting to fetch data from an API, an error message as seen here: consola, pops up unexpectedly. Despite deleting the database of the API, the error persists for some reason. Upon t ...

The functionality of module.exports and require operation

My understanding of module.exports and require() in NodeJS is a bit unclear, leading me to seek help finding answers. I can export functions like: example1.js module.exports = { hello: function() { return 'Hello world'; }); } O ...

In which part of the DOM tree are Event/Callbacks typically located?

As I work on creating a one-page web application, the task of dynamically constructing nested Backbone.View components arises. My typical approach involves building an empty jQuery object, populating it with div elements, binding events to it, and then re ...

What is the optimal method for displaying dynamic HTML content?

Imagine a scenario where I have an HTML template file with the following string: <p> >>LEFT PART<< {{Data}} >>RIGHT PART<< </p> Suppose that neither the left nor right part of this string contain anything like {{Data}} ...

Obtaining MIME-TYPE from a Base 64 String: A Handy Guide

After receiving a base64 encoded string from the backend, I am decoding it in Javascript to display it on the browser. The content of this string could be any type of file such as .pdf, .img, .docx, .zip, etc. The base64 string I have does not contain th ...

Implementing an Ajax call within a loop

When trying to fetch data from a website using certain codes, I end up with a list of 330 categories. According to the WooCommerce REST API documentation, this endpoint typically only returns the first 100 categories. In order to get all the categories, ...

Is it feasible to generate/save a JSON file to a hard drive using an HTML application and pure JavaScript without the need for Node.js or a server?

Imagine having an HTML document with forms and input fields, along with a submit button. Here's the challenge: when the user clicks the submit button, the goal is to create a JSON file containing the form data, all done without the need for Node.js o ...

Tips for adding style to the first list element and first children using jQuery

Looking at the following markup: <ul> <li> // this <ul> <li> <div></div> </li> <li> <div></div> </li> </ul> </li> <li> ...

Ways to access and delete the canvas using ref steps?

Having a canvas in a react component, I utilized react refs to access the node and implemented a destroy() method. However, I encountered an error: TypeError: canvasRef.current.destroy is not a function How can we properly access the node (canvas) using r ...

Activate the parent navigation link when on sub-pages

I want to ensure that the parent navigation item is highlighted when a user is on a child page based on the URL. For example, if the user is currently viewing foo1-child and the parent is foo1, I need to apply the active class to Foo 1: http://foo.com/foo ...

Locate elements in a 2D Array with Google Apps Script

I am working with an array containing the values: [8:00 AM, 9:00 AM] //result of another function My goal is to locate these values within a 2-Dimensional Array: [ [8:00 AM], [9:00 AM], [10:00 AM], [11:00 AM], [12:00 NN], [1:00 PM], [2:00 P ...

What is the best way to add <li> to every element in an array?

I had a tough day today trying to figure out my code. My English isn't the best, so explaining my issue is hard. I decided to illustrate my problem in HTML and specify the kind of styling I need to achieve with JS. I also included an example using the ...

Completes a form on a separate website which then populates information onto a different website

Creating a website that allows users to login and view various complaint forms from government websites or other sources. When a user clicks on a link to file a complaint, they will be redirected to the respective page. However, I am looking for a way to ...

Error Encountered: Unable to utilize $(...).mask function with jQuery in ASP.NET using C#

I have encountered an issue while working on a task involving jQuery masked for date. When running the task in HTML, it works perfectly fine. However, when attempting to run it in ASP.NET, I face the problem where 'mask is not a function'. Below ...

Challenges with Selenium Extraction: Delays in Wait Times/Struggles with Locating

When working in both Chrome and Firefox, everything goes smoothly until the moment I try to extract text. At that point, an error is triggered: h3 = next(element for element in h3s if element.is_displayed()) StopIteration I attempted to address this issu ...

What is the best way to utilize JavaScript methods for retrieving dynamic data?

My variable needs to return data based on an array of objects that contain dynamic dummy values. There are 3 sets of dummies structured as follows --> Example: dummy1_A, dummy1_B, dummy1_C, ... dummy2_A, dummy2_B, dummy2_C, ... dummy3_A, dummy3_B, du ...

What are effective ways to work around the CORS policy while making get/post requests from within React JS?

My React JS application is encountering an issue with CORS policy while trying to fetch data from servers in other domains. Despite using http-proxy-middleware and setting up the necessary proxy, I am still unable to make successful get/post calls to the e ...

Ways to save an array in my database with each value occupying a single row

Is it possible for the form to be submitted with an array of checked values when a user clicks on a checkbox and saves? Each checked value should be saved in the database as a single row. I'm not sure if this is achievable, but I believe you might hav ...

The issue of CSS transitions flickering in Internet Explorer

Can someone help me troubleshoot an issue with this code in Internet Explorer? CSS: .nav-fillpath a { width: 100px; height: 100px; position: absolute; display: block; bottom: 0%; left:0; right:0; color: #3e3e3e; margin ...

How can I prevent a horizontal scrollbar from appearing in HTML when using margin:-4px and what are some possible solutions to this

I am facing an issue with a <div> element that contains a grid row element from semantic UI. Despite setting the width to 100%, there is a small space visible on all sides of the row element. I tried using margin: -4px to eliminate the white space, b ...