What is the best way to change a dynamically centered text alignment?

Is it possible to add a transition for a center-aligned text that changes due to an action? (codepen)

HTML :

<div id="container">Lorem ipsum dolor sit amet consectetur adipiscing elit</div>

CSS :

#container {
  width: 400px;
  height: 20px;
  background: #000;
  color: white;
  text-align: center;
}

(The codepen example demonstrates dynamic data, in my case, the data is changed by React states)

EDIT: Only the last part of the text changes, e.g.

Lorem ipsum dolor sit amet consectetur adipiscing elit
changes to Lorem ipsum dolor .... I want to display the shortened text moving to the center from the left (since the remaining text will be on the left side when cut off).

Answer №1

Here is an example

$("#box div").hover(function() {
  this.innerHTML = "This is a demonstration of dynamic text This is a demonstration of dynamic text This is a demonstration of dynamic text This is a demonstration of dynamic text This is a demonstration of dynamic text This is a demonstration of dynamic text This is a demonstration of dynamic text";
}, function() {
  this.innerHTML = "This is some sample dynamic text that doesn't do much This is some sample dynamic text that doesn't do much This is some sample dynamic text that doesn't do much";
});
#box {
  height: 20px;
  background: #000;
  color: white !important;
  text-align: center;
}
#box div {
  width: 400px;
  white-space: nowrap;
  max-width: 400px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: inline-block;
  transition: all ease-in-out .9s;
}
#box div:hover {
  width: 100%;
  max-width: 100%;
  white-space: normal;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="box">
  <div>This is a demo of dynamic text that doesn't do much This is a demo of dynamic text that doesn't do much This is a demo of dynamic text that doesn't do much</div>
</div>

Answer №2

#wrapper:after{
   content:'this is ever-changing';
}

otherwise, in jQuery:

document.getElementById("wrapper").innerHTML = "Text modified!"

Answer №3

Experience a seamless transition effect using just CSS. There are endless possibilities for creating various effects without the need for JavaScript.

#container {
  width: 100%;
  height: 20px;
  background: #000;
  color: white;
  position: relative;
}
#container:hover > .static {
  top: -20px;
}
#container:hover > .dynamic {
  top: 0px;
}
#container .static {
  top: 0px;
}
#container .dynamic {
  top: 20px;
}
#container > div {
  position: absolute;
  left: auto;
  right: auto;
  width: 100%;
  text-align: center;
  transition: all 0.3s linear;
  overflow-y: hidden;
}
<div id="container">
  <div class="static">This is an example of dynamic text that remains static</div>
  <div class="dynamic">This is an example of dynamic text that changes</div>
</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

Creating a popup window using HTML

I am attempting to create a pop-up window using HTML, but I'm struggling to get it to appear in the desired way. My goal is this: to have a pop-up show up when the profile image is clicked on. Here's the HTML <span class="profile"><im ...

Personalize the drop area in Filepond

Is there a way to customize the filepond droparea by adding custom HTML with placeholder images to guide users on what kind of images should be uploaded and allow multiple uploads? https://i.sstatic.net/VFGfJ.png I attempted to add placeholders in an abs ...

What is the process for deploying a React project located in a subdirectory of the main branch to Github Pages?

Currently, the project repository is structured like this: main: app/ README.md preview.jpg The app directory contains all the necessary files and folders for my react project. I am facing an issue with getting Github Pages to work properly with the sub- ...

Implement a Selection Feature in Angular

Currently, I am working on an application where users can add new rows with the same fields. One of the requirements is to allow users to add an option to a select element. While I have successfully implemented this in jQuery, I am facing challenges integr ...

Clickable Material UI tabs that can be moved around easily

I need help finding a way to drag and drop Material UI tabs in order to change their sequence, similar to the functionality of rearranging browser tabs by dragging them into different positions. I have searched online but haven't found much informatio ...

JSP form continues to submit despite JavaScript validation returning false

On my JSP page, I have a form named "deleteCont" and a JavaScript function called "validateDelete". When the user clicks the "Delete contact" button, the "validateDelete" function is triggered successfully, showing an alert message. Unfortunately, even whe ...

Within NextJS, the content may not immediately appear on the webpage, however, if the information is logged in the console, it will be

Whenever I use getInitialProps to fetch data, the information doesn't display on the website. However, if I check the console.log, the data appears as expected. {this.props.siswa.map(siswa => { {console.log(siswa)} <div className="flex& ...

Display or conceal a div element depending on the value of a row in a MySQL

Hey everyone, I'm facing an issue where I want to display or hide a div based on a MySQL value. Unfortunately, I can't seem to get it right. Can anyone help me figure out what I'm doing wrong? Here's my code. Thanks in advance for your ...

MUI Autocomplete refuses to accept a value

In my Autocomplete feature, I have a list of all users available. When a user clicks on a button from another site, they are redirected to this site and the user's ID is fetched. I want the user with the corresponding ID to be automatically selected, ...

How can you modify the starting point of data in jQuery flot?

Currently using Flot to create a graph displaying clicks per minute within the first 60 minutes of short URLs generated at . The graph currently displays data from minute 0 to minute 59. My query is about adjusting the data to start at 1 and end at 59, wh ...

Struggling to center a modal at the exact middle of the page using Bootstrap 5

My navigation bar has a button that, when clicked, triggers a modal to appear at the top of the page. I am trying to center it horizontally. Here is the HTML code: </button> <!-- </button> --> <div class="text-dark "> < ...

Using the jQuery AJAX function to modify the URL query string

I currently find myself on a webpage Is there a way to add a URL query string without reloading the page? For example, when I click a button, can the URL change to using jQuery ajax functions? ...

Find the position of an HTML5 canvas element within a webpage

I am currently facing an issue with a canvas within another canvas. <canvas id ='canvas2' height="718" width="1316"></canvas> The CSS for this canvas is as follows: #canvas2{ position:absolute; width :95%; height:90%; top:5%; lef ...

Customizing hyperlink styles with JavaScript on click

Hey there! I'm experimenting with something new. I've managed to change the background color of each link after it's clicked, but now I'm facing a challenge: How can I revert the original style when another link is clicked? Here's ...

How can we determine the dimensions of an absolutely positioned element within a relatively positioned element?

One thing I noticed is that when placing an absolutely positioned element within a relatively positioned element, the latter does not automatically inherit the width and height of the former unless manually set. Take this example: <!DOCTYPE html> ...

JavaScript - AngularJS HTML parser

I am working with an array that contains content along with HTML tags, and here is the code snippet: for (car in cars.parking[0]){ content.push('<br />'); for (token in cars.parking[0].now) { content.pus ...

Using cascading style sheets to switch a page into editing mode

Is it possible to change the view of a page after clicking a button without using javascript, and instead relying solely on CSS? I want to display a page with information where users can click an edit button and make changes within the same view rather th ...

Using PHP header function to redirect to a specific form on an HTML page: A step-by-step guide

I have a dilemma in my web project. In the index.php file, there are two forms that utilize transitions to switch between pages (Login and Register). When attempting to register a new user in the backend, I check if the user already exists. If they do exis ...

What is the best way to enable a disabled MUI MenuItem within a table that is being mapped, based on a specific item in the

In my table, I have a mapped object of users: {users.map((user,index) => { <TableRow key={index}> ... The final cell in the table contains a button that is linked to an MUI Menu. One of the menu items should be disabled if a specific aspect of ...

What is the reason file inputs do not trigger 'input' events, while 'change' events do fire?

Trying out a basic input: <input type="file"/> Noticing that the input event doesn't trigger when a new file is selected: $('input').on('input', function(event){ console.log('input value changed', event.targe ...