Place an animated object in the center with the display style set to

I'm trying to vertically center the second span within my H1.

After attempting to change from display: inline-block to display: flex, I found that this causes my animation to start at the beginning of the container rather than in the center.

I suspect the issue may be related to the various display properties assigned to the parents of the span elements?

HTML

<section class="container">
            <div id="description">
                <h1>
                    <span>
                        This element is good,</span>
                    <span>
                        But this one is not centered.
                    </span>
                </h1>
            </div>
            <div id="other-div">
            </div>
</section>

CSS

.container {
    margin: auto 30px;
    background-color: black;
    border-radius: 1rem;
    display: flex;
}

#description {
    flex: 1;
    display: flex;
    justify-content: center;
    flex-direction: column;
    color: white;
}

#description h1 span:first-child {
    display: flex;
    justify-content: center;
    font-family: "Croissant One";
    font-size: 3.2rem;
    color: white;
}

#description h1 span:nth-child(2) {
    font-family: "Croissant One";
    font-size: 3.2rem;
    display: inline-block;
    vertical-align: middle;
    overflow: hidden;
    color: black;
    background-color: rgb(255, 237, 36);
    padding: 0px 15px;
    border-right: 0.15em solid white;
    white-space: nowrap;
    animation: typing 2.5s steps(30),
    blink-caret 0.9s step-end infinite;
}

@keyframes typing {
from {
    width: 0
}

to {
    width: 52%
}
}

@keyframes blink-caret {
50% {
    border-color: transparent;
}
}

#other-div {
    flex: 1;
    display: flex;
    justify-content: center;
}

Please assist with resolving this issue. Thank you!

Appreciate your help!

Answer №1

To achieve perfect centralization of the second span and ensure that your animation starts from the center, you will need to make some style adjustments. Specifically, apply the following flex item style to the second span:

align-self: center;

Additionally, add the flex property to the h1 element to centralize the span and include a width property to execute the animation correctly:

#description h1{
display: flex;
width: 100%;
flex-direction: column; 
}

I have made edits to your styles and added comments for clarity:

.container {
    margin: auto 30px;
    background-color: black;
    border-radius: 1rem;
}

#description {
    flex: 1;
    display: flex;
    justify-content: center;
    color: white;
}

/* Adjusting the style of the H1 element is essential for proper animation execution. Modify the width value as needed! */
#description h1{
    display: flex;
    width: 100%;
    flex-direction: column;
}

#description h1 span:first-child{
    display: flex;
    justify-content: center;
    font-family: "Croissant One";
    font-size: 3.2rem;
    color: white;
    /* Horizontally aligning the first span can be achieved with this property */
    justify-content: flex-start;
}

#description h1 span:nth-child(2){
    font-family: "Croissant One";
    font-size: 3.2rem;
    /* Remove unnecessary properties */
    /* display: inline-block;
    vertical-align: middle; */
    overflow: hidden;
    color: black;
    background-color: rgb(255, 237, 36);
    padding: 0px 15px;
    border-right: 0.15em solid white;
    white-space: nowrap;
    animation: typing 2.5s steps(30),
    blink-caret 0.9s step-end infinite;
    /* Starting the animation from the center */
    align-self: center;
}

@keyframes typing {
from {
    width: 0
}

to {
    width: 52%
}
}

@keyframes blink-caret {
50% {
    border-color: transparent;
}
}

#other-div {
    flex: 1;
    display: flex;
    justify-content: center;
}

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

Is it possible to eliminate the border on an image input button?

Here is the code I've been working on: HTML: <!DOCTYPE html> ... <form> <input type="image" value=" " class="btnimage" /> </form> ... CSS: .btnimage { width: 80px; height: 25px; background:url('C:/Users ...

Creating an overflow effect for sliders using only CSS

Is it feasible to develop a slider that extends beyond its parent elements and the body? The concept involves having content within a section housed in a container. The slider would be part of this section, with slides overflowing past the parent element ...

Show singular array element upon click

I need help with a specific task. When a button is clicked, I want to display a random item from an array. Each array item should only be displayed once. Currently, my code is set up as follows: When the button is clicked, a random array item is displ ...

Can Three.js be used to create a compact canvas?

I've successfully implemented a three.js scene on my website where users can drag to rotate an object. However, I don't want the scene to take up the entire webpage. I tried adjusting the field parameters with the following code: renderer.setSiz ...

Utilize jQuery to apply a border and background color to a table row when hovering over it

Is there a way to change the border of a table row along with its background color when the mouse hovers over it? Currently, I can only modify the background color using the following code: $(document).ready(function() { $(function() { $(&apos ...

determine the selection based on its numerical index

I'm looking to leverage the robot framework for automating user actions involving hovering over a bar chart. Is there a method to identify the bar chart by its index? Or what would be the appropriate selector for using the robot framework to hover o ...

What is the process of generating an animation, such as an MPEG movie, using plots from a text file without the need to save them separately on your computer?

Recently, I've been dealing with output data stored in a .txt file generated by a separate code. This code takes a final matrix and saves it to a file, with each line representing the solution to the 1D heat equation at a specific timestep. The number ...

what methods do you use to recall codes?

Hey there! I've recently started diving into the world of HTML, CSS, and Php. I'm curious, how exactly do experienced coders manage to remember so many functions? Is it something that comes with time and practice? As a beginner myself, this quest ...

modifying the child divs contained in the main div

Hey there! I'm currently using Bootstrap to design an application and I've got the entire form inside a container. However, as I add more child elements within the container div, I would like the parent div to expand automatically to show all the ...

Having trouble with Safari on your iPhone? Seeing image outlines peeking through radius borders?

Has anyone encountered a similar issue before? I'm experiencing this problem specifically on Safari for iPhone. Problem: https://i.sstatic.net/ffqWz.jpg Solution: https://i.sstatic.net/z1bUB.png Below is the HTML code snippet: <div class="row"& ...

Difficulty with implementing CSS Sprites in a jQuery-driven menu

Facing issues with a code base that was previously owned by someone else. Deadline is approaching fast and struggling to solve a particular issue. This code includes a table in the top section of the page, featuring a drop-down menu using CSS sprites for ...

Input form with multiple fields. Automatically display or hide labels based on whether each field is populated or empty

I am attempting to create a form where the placeholders move to the top of the input when it is focused or filled. However, I have encountered an issue with having multiple inputs on the same page. Currently, my JavaScript code affects all inputs on the pa ...

What distinguishes between using ul#test and #test ul in CSS?

Could someone help clarify the distinction between these two types of CSS declarations: ul#test { } #test ul { } Despite my searching, I am unable to pinpoint the difference, but they exhibit different behaviors when implemented on a test page. To me, i ...

How can the top height of a jquery dialog be reduced?

Just starting out with jquery, I've got a dialog box and I'm looking to decrease the height of this red image: https://i.sstatic.net/BuyQL.png Is there a way to do it? I've already made changes in the jquery-ui.css code, like so: .ui-dia ...

What is the process for transforming an ms3d model into a format compatible with three.js?

I am attempting to convert this specific model into the three.js model format: So far, I have imported the ms3d file into Blender using the default addon. While animations and mesh look accurate in Blender, the bones are only displayed as lines. Subsequen ...

How to truncate text in a cell of a Vuetify data table using CSS styling

Is there a way to truncate text in a cell using CSS without it wrapping around or overflowing? The ideal CSS code should include: .truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } However, the current implementation caus ...

What is the process for configuring text on an md-Switch?

I need help setting informational text on an md-switch element from Angular Material design. I want the user to be able to easily understand the current state of a field value. Here is a visual example of what I'm trying to achieve: https://i.sstatic. ...

How come my image isn't cooperating with text-align and vertical-align?

Hey everyone! I'm a newcomer to this community and a newbie when it comes to HTML. :D I encountered a problem recently and discovered that Stackoverflow is full of amazing developers. That's why I've decided to share my problem here and am ...

2 unique personalized classes with individualized modifications

Is it feasible to create 2 distinct custom (a tag) classes, each with unique class names? For Instance: When I try to use these in my HTML code, I struggle to create 2 different styles for (a tag) with different names! <a class="type1" href="#">te ...

Utilizing Print Styles in an Angular 7 Application: A Step-by-Step Guide

I'm trying to print an html form within my Angular7 App with minimal margins. I've attempted different solutions such as adjusting the margins manually in Chrome's print preview box and adding @media print styles & @page styles in both the c ...