Ways to prevent the movement of changing centered text

After recently downloading a text rotator plugin, I've run into a small issue. Whenever the text changes, the entire heading shifts because it's centered.

Here's a simple example: (Don't worry about the js code, it's just a basic representation of the plugin)

HTML

<h1>
Heading <span class="change">Change</span>
</h1>

CSS

h1 {
  text-align: center;
}

JS

var itr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var  interval = 1000; //one second
itr.forEach((itr, index) => {
setTimeout(() => {
    $('.change').text('Car');
            setTimeout(() => {
            $('.change').text('Boat');
                    setTimeout(() => {
                    $('.change').text('Helicopter');
                    }, index * interval
                   );
          }, index * interval
         );
  }, index * interval
 );
})

Give it a try here: https://jsfiddle.net/teku3a0w/1/
Is there a way to prevent the heading from shifting when the text changes?

A fixed margin could be a solution, but I prefer a responsive approach.

Answer №1

After some experimentation, I discovered a solution that works perfectly.

.modify{
  display: inline-block;
    width: 200px;
    text-align: left;
}

I decided to add a specific width to the element that is being modified.
Keep in mind that the width should be sufficient to accommodate the longest word in the content.

Feel free to test it out here: https://jsfiddle.net/1aBcDeFg/

Answer №2

Below are the modifications I implemented in the HTML and CSS:

In the HTML:

<h1>
  <span>Main Title</span>
  <span style = 'padding-left: 6px' class="edit">Edit Title</span>
</h1>

In the CSS:

h1 {
  display: flex;
  justify-content: center;
  width: 400px;
  margin: 0 auto;
  flex-direction: column;
  align-items: center;
}

@media screen and (min-width: 768px) {
  h1 {
    flex-direction: row;
    justify-content: center;
  }

  span.edit {
    width: 150px
  }
}

Considering the fixed widths, this solution may lack flexibility. A suggestion would be to adjust the width of span.edit based on the longest word in order to enhance adaptability.

The media query at 768px was chosen to accommodate tablet-sized screens and higher dimensions.

Although not ideal, this approach provides a starting point for improvement.

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

Does Vue.js interfere with classList.remove functionality?

When working with Vue.js, I encountered an issue where elements would briefly flash curly braces to the user before being hidden. To combat this problem, I implemented the following solution: HTML: <div class="main hide-me" id="my-vue-element"> ...

Using a Javascript method to access a sibling property within an object

Is there a way to access a sibling property from a method in JavaScript? This seemingly simple task has proven challenging for me. Take a look at the sample code below. let f = { a: 3, printMyBrother() { console.log(X) } }.printMyBrother f() ...

Dealing with multiple submit buttons in a form with Angular JS: best practices

I'm using AngularJS and I have a form where the user can enter data. At the end of the form I want to have two buttons, one to "save" which will save and go to another page, and another button labeled "save and add another" which will save the form an ...

Troubleshooting the sidebar pin-unpin problem using Jquery and CSS

I have created a single side panel that allows me to toggle between opening and closing the sidebar by hovering on it. I can also pin and unpin the sidebar by clicking on the pin image. Now, I want to open the sidebar onClick instead of onHover and remove ...

Web Scraping ASU Courses using Node.js

I'm a beginner when it comes to Node.js, so please forgive me if I sound confused. Currently, I am attempting to scrape course information from ASU's course catalog (https://webapp4.asu.edu/catalog/) using tools like Zombie, Node.IO, and the HTT ...

Incorporating a loading animation with an AJAX call

I've come across various solutions to this issue, one of the most enlightening being a response on Stack Overflow suggesting to "display it as soon as you initiate the AJAX call" and then "hide the image once the request is complete." But how would I ...

Tips for transferring custom data on a form without relying on input fields are an effective way

Is there a way to submit a form with both name and quantity without having an input field for the second variable? I want the quantity get variable to be passed when the form is submitted, but I also need to pass the name quantity that is already displayed ...

We encountered a ReferenceError stating that 'dc' is not defined, despite having already imported d3, dc, and crossfilter in

In my current angular project, I have included the necessary imports in the component.ts file in the specific order of d3, crossfilter2, dc, and leaflet. Additionally, I have added the cdn for dc-leaflet.js in the index.html file. Despite these steps, wh ...

Comparing AJAX to a source script request

As I was exploring the Google API today, I came across their sample code where they simply request a URL using: <script src="src="https://www.googleapis.com/customsearch/v1?key=AIzaSyCVAXiUzRYsML1Pv6RwSG1.."></script> Before seeing this, I ha ...

Are elements loaded and hidden by ng-hide and ng-show, or does loading only occur with ng-show?

Is this method of programming effective for handling large elements such as 10 mb images? Are there alternative solutions that would work better? ...

Revamp the Side Navigation Feature to Identify the Currently Viewed Section of the Page

My website currently features a side navigation bar that dynamically updates based on the user's scroll position. When the user reaches a specific height, a class is added to a div in the sidebar, turning it orange and indicating which part of the pag ...

An issue with Axios request in a cordova app using a signed version

Currently, I am in the process of developing a Cordova application utilizing Axios and React. The interesting part is that everything runs smoothly when I build the app with Cordova and test it on my phone using the APK. However, once I sign, zipalign it, ...

push one element to fit in between two adjacent elements

Is there a way to make my responsive webpage ensure that the full-screen element appears between sibling elements when viewed on mobile devices? Check it out on desktop or on mobile here. I have three (div) elements: two in one row and another in a separa ...

Encountering an issue when trying to use multiple selections in a drop down list with Angular.js

I am facing a major issue. I need to be able to select multiple values from a drop down list, so I implemented bootstrap-multiselect.js. However, I encountered the following error: angularjs.js:107 TypeError: a.forEach is not a function at u.writeValu ...

CAUTION: Attempted to load angular multiple times while loading the page

I encountered a warning message while working on my project, causing errors in calling backend APIs due to duplicate calls. Despite attempting previously suggested solutions from the forum, I am stuck and seeking assistance. Can anyone provide guidance? Be ...

What is the best method to choose a random color for the background when a button is pressed?

Does anyone know how to create a button that changes the background color of a webpage each time it is clicked? I've been having trouble with the javascript function in my code. I've tried multiple solutions but nothing seems to work. Could someo ...

The styles are not being rendered on the Angular application

I have successfully implemented a Modal service with working HTML/CSS (Check it out on StackBlitz) div.cover { align-items: center; background-color: rgba(0, 0, 0, 0.4); bottom: 0; display: flex; justify-content: center; left: 0; ...

Increasing and decreasing the display of content using JQuery based on height rather than character count

I'm attempting to create a show more/show less link with a set height of 200px that, when clicked, will reveal the rest of the content. Anything exceeding 200px will be hidden, and there will be a "show more" link to display the remaining text. I&apos ...

Using AJAX (jQuery) to process and refine JSON data through filtration

I need assistance with filtering a JSON array using AJAX but I'm unsure of how to proceed. { posts: [{ "image": "images/bbtv.jpg", "group": "a" }, { "image": "images/grow.jpg", "group": "b" }, { "image": "images/tabs.jpg", ...

Material-ui 4.11.4 - Grid: For items to be displayed correctly in a column, it is necessary to set the container direction to 'row' (??)

I have a requirement to display multiple divs in a column layout, centered along the page axis and stretched in width based on the available space. I am currently using the latest version (4.11.4) of material-ui which includes both the <Grid> contain ...