Trouble aligning dynamic inline-block <h1> tag with text-align: center

One of my Angular directives has a feature that scrolls through words in an array and presents them as if they are being typed out in real-time. I'm facing an issue with aligning the h1 tag that displays these dynamically changing words to be centered using text-align: center. Despite my efforts, I can't seem to get it to work properly.

Below is the HTML code snippet...

<div class='intro-div'>
    <h1 class='intro-title'>Hi, I'm Erik. I build & design</h1>
    <div class='changing-text-space-holder'>
        <h1 i-do class='intro-title changing-text'></h1>
    </div>
    <button class='hire-me-btn'>AVAILABLE FOR HIRE</button>
</div>

And here is the accompanying CSS...

.intro-div{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateY(-50%) translateX(-50%);
    width: 55em;
}

.intro-title{
    font-family: 'WSReg';
    color: white;
    font-size: 3.5em;
    text-align: center;
}

.changing-text-space-holder{
    height: 3em;
    width: 100%;
}

.changing-text{
    border-right: lightgrey solid;
    display: inline-block;
    text-align: center;
}

.hire-me-btn{
    border: solid white .2em;
    border-radius: 75px;
    width: 80%;
    height: 2.5em;
    background-color: transparent;
    font-size: 3em;
    font-family: 'WSReg';
    color: white;
    position: relative;
    left: 50%;
    transform: translateX(-50%);
}

Additionally, this is how my directive is set up...

myModule.directive('iDo', function(){
    return function(scope){
        var element = angular.element('.changing-text');
        var words = ['front-end.', 'back-end.', 'anywhere between the "ends".', 'e-commerce websites.', 'landing-pages.', 'with Javascript.', 'with Python.', 'the internet.']
        var startStopBool = false;
        var word_count = 0;
        var letter_count = 0;

        function updateText(){
            if(words[word_count] === undefined){ //if no more words in array stop
                clearInterval(frontDisplay);
            }else if(element[0].innerHTML.length === words[word_count].length){  //switch to next word
                word_count++;
                letter_count = 0;
                clearInterval(frontDisplay);
                setTimeout(function(){restartInterval()}, 2000); //wait 1 second and then restart the text
            }else{
                element[0].innerHTML += words[word_count][letter_count]
                letter_count++;
            }
        }

        function restartInterval(){
            element[0].innerHTML = '';
            frontDisplay = setInterval(updateText, 200);
        }

        var frontDisplay = setInterval(updateText, 200)  //Starts text cycle
    }         
})

Answer №1

To ensure your .changing-text class is properly displayed, set it to display: block.

Unlike inline elements, block elements cannot have other elements positioned next to them naturally. For a detailed explanation, check out this excellent response on Stack Overflow.

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

How can you horizontally align a collection of elements using CSS?

I'm struggling to align a paragraph element with a group of button elements using jQuery and CSS. This is my issue: My goal is to have all these elements on the same horizontal line at the top of the screen to make the most of the pixel real-estate. ...

Karma and RequireJS fetching files beyond the base URL

My goal is to set up automatic testing using karma with the following file structure: /assests /css /font /img /js /collection /lib /model /plugin /spec /view test-main.js main.js /templates index.html karma.conf.js In my ...

Filter an object in Typescript and retrieve a single key

Managing a set of checkboxes is essential in assigning roles to new users. While it's possible to filter and retrieve only the checked checkboxes, extracting just the "name" key poses a challenge. The current method involves filtering with a for loop ...

What is the best way to determine if certain rows of data have been successfully loaded when working with Ext.data.operation and an ajaxProxy?

Here is the provided code snippet: Ext.define('Book', { extend: 'Ext.data.Model', fields: [ {name: 'id', type: 'int'}, {name: 'title', type: 'string'}, {name: &apo ...

Looking for image src attribute within a string using JavaScript and appending a function to it

When using ng-bind-html in AngularJS to render an HTML string, I encountered the need to add an ng-click function to every img element. For example, here is the HTML string: $scope.html="<--some html content--> <img class='some ...

SignalR does not include cookies in the connection request

While working on a web application using ASP.NET Web API and SignalR, I encountered an issue that is proving to be quite challenging despite my understanding of HTTP. Currently, I have successfully set a cookie on all AJAX requests to the API, and subsequ ...

Is it possible to apply a styling to a table data cell based on the table header relationship

Let's say I have a table header with content like this... <th> <div class="text-left field-sorting " rel="local_inventory"> Local inventory </div> </th> <th> <div class="text-left field-sorting " rel="something el ...

Appending a property to a JSON object using JavaScript

What is the method for adding a new field to a JSON object in JavaScript? I have seen examples using arrays, but I specifically need it with a JSON object. Essentially, how can I transform this: { "hello":"this is cool" } into this ...

Obtain Blob using Uint8Array in JavaScript

Hello, I am currently facing an issue while trying to retrieve data from a WebSocket using JS and VUE. Here's the problem I'm encountering: In order to receive data, I am utilizing the following code snippet: created() { console.log('Starti ...

Weird State / Unusual Effectiveness with NextJS Links

I'm encountering some unusual behavior in a personal project I'm working on. The project is a recipe website with buttons at the top that lead to different pages querying Firebase for recipe data. In the Index.js file, Firestore queries pass pro ...

trigger a CSS animation using JavaScript upon a specified event

I am attempting to incorporate a class for an autogrow animation effect to a textarea element. Here is a demo: http://jsfiddle.net/d0kmg7d3/15/ var tx = document.getElementsByTagName('textarea'); for (var i = 0; i < tx.length; i++) { tx[i] ...

Utilizing Angular: Importing Scripts in index.html and Implementing Them in Components

Currently, I am attempting to integrate the Spotify SDK into an Angular application. While I have successfully imported the script from the CDN in index.html, I am encountering difficulties in utilizing it at the component level. It seems like there may be ...

Transform buffer information into a visual representation

How can I convert the buffer data into an image so that when I loop through the results and render it in the img src, the user will be able to see the image? I am currently using ejs for rendering. <span> <img class="user-with-avat ...

Reorganizing the layout of grid items

Currently, I am working on a task involving GRID CSS. The main objective of the task is to utilize GRID specifically, rather than other methods like flex. I have successfully set up two columns in my GRID, each with its own divs. However, what I am struggl ...

ordering an array based on a boolean property in TypeScript

I'm currently working with angular 10 and I need help figuring out how to sort this array: var dic = [ { state: false, id: 1 }, { state: true, id: 2} , { state: false, id: 3 }, { state: true, id: 4 }, { state: false, id: 5 } ] My goal is to ...

When the user clicks on an organizational chart, a new organizational chart will appear in a modal popup

Currently, I am developing a project with Highcharts where I have a specific requirement to display a modal popup when a node on an org chart is clicked. The popup should also contain another org chart. Below is the code snippet I am working with: [link to ...

Optimal methods for handling Ajax requests in the present day

Recently, I revisited some websites I co-built with a friend and was working on getting them functional again. It's been a while since I've done any AJAX work, and I'm realizing that there aren't many resources available to help trouble ...

Rotating an object in Three.js: Animate back and forth along two different azimuth angles

My three.js project features a 3D object that is meant to be viewed from the front only, as it appears as a single plane and is transparent from the back... Using orbitControls, I have restricted movement of both azimuth and polar angle... To enhance the ...

invoking a PHP function within a JavaScript script

In my collection of essential functions, there is one that I am particularly interested in: //the functions file //........ function user_exists($username){ $username = sanitize($username); $query = mysql_query("SELECT COUNT('user_id') F ...

Javascript /// <field type=?> used to define properties for individually customized objects

Exploring the XML javascript comment syntax in Visual Studio and encountering some confusion. Specifically, I have a query regarding custom types. For example, consider a custom type like... namespace.types.User = function(_id, _name) { /// <field ...