Having difficulty getting the h1:after element to properly float to the left and center

I am facing an issue with the styling of a :after class that creates a line under a floated h1 element. The h1 element is floated left, and I want to center the :after line underneath it.

Despite floating it left and attempting a text-center on the parent div, I have not been successful. Any suggestions?

Check out the Codepen here

.singleAfterLine {
  display: block;
  float: left;
  height: 2px;
  background-color: #55B4B0;
  /*Great way to give single line color; see under GET FOOD NOT FAST FOOD */
  content: " ";
  width: 100px;
  margin: 0 auto;
  margin-top: 20px;
  margin-bottom: 20px;
}
<div class="container-fluid greyBck" style="margin-top: 100px;" id="about">
  <div class="" style="padding-top: 50px; padding-bottom: 50px;">
    <div class="row" style="width: 80%; margin-left: 10%;">
      <div class="col-12 col-md-5">
        Duis faucibus, magna eu sodales viverra, nunc erat ullamcorper nibh, at varius leo ipsum eleifend lorem. Nam eu consectetur odio. Curabitur dolor libero, congue luctus gravida vitae, laoreet nec dolor. Donec sagittis lobortis odio, id hendrerit nunc molestie
        sagittis. Nam nec purus sapien. Nunc est purus, mattis ac magna maximus, pulvinar sodales lacus. Praesent eget pulvinar justo, in sodales arcu. Integer vel sagittis libero, id condimentum enim. Quisque sollicitudin tempus nibh pellentesque condimentum.
        Curabitur in posuere sem. Nulla aliquam in turpis ut molestie. In eu quam est.
      </div>
      <div class="col-12 col-md-7 bioText" style="float: left;">
        <h1 style="padding-bottom: 20px;">About Myself</h1>
        <span class="singleAfterLine"></span>
      </div>
    </div>
  </div>
</div>

Answer №1

To achieve the desired result, you must make adjustments to the css

.bioText h1 {
        padding-bottom: 20px;
        display: inline-block;
        position: relative;
    }
.bioText h1:after {
        display: block;
        float: left;
        height: 2px;
        background-color: #55B4B0;
        content: " ";
        width: 100px;
        margin: 0 auto;
        margin-top: 20px;
        margin-bottom: 20px;
        position: absolute;
        left: 0;
        top: 50px;
        right:0px;
    }
<div class="container-fluid greyBck" style="margin-top: 100px;" id="about">
  <div class="" style="padding-top: 50px; padding-bottom: 50px;">
    <div class="row" style="width: 80%; margin-left: 10%;">
      <div class="col-12 col-md-5">
        Duis faucibus, magna eu sodales viverra, nunc erat ullamcorper nibh, at varius leo ipsum eleifend lorem. Nam eu consectetur odio. Curabitur dolor libero, congue luctus gravida vitae, laoreet nec dolor. Donec sagittis lobortis odio, id hendrerit nunc molestie sagittis. Nam nec purus sapien. Nunc est purus, mattis ac magna maximus, pulvinar sodales lacus. Praesent eget pulvinar justo, in sodales arcu. Integer vel sagittis libero, id condimentum enim. Quisque sollicitudin tempus nibh pellentesque condimentum. Curabitur in posuere sem. Nulla aliquam in turpis ut molestie. In eu quam est.
      </div>
      <div class="col-12 col-md-7 bioText" style="float: left;">
        <h1>About Myself</h1>
      </div>
    </div>
  </div>
</div>

Answer №2

Should the centering focus be on the entire h1 or just its text? To effectively center the h1, you need to determine the width of the text within it. If you prefer to center the h1 as a whole, the process is much simpler. In cases where dynamic centering is needed, it's helpful to have a static reference point (such as its parent container). Create a separate container with the same dimensions as the h1's text and perform the alignment within that container using flex properties. Alternatively, for centering the line with the entire h1, utilize the flex property since the div's width will adjust dynamically based on its parent's width, similar to the behavior of an h1.

.singleAfterLine {
  display: block;
  float: left;
  height: 2px;
  background-color: #55B4B0;
  /*Great way to give single line color; see under GET FOOD NOT FAST FOOD */
  content: " ";
  width: 100px;
  margin: 0 auto;
  margin-top: 20px;
  margin-bottom: 20px;
}

.container {
  width: 200px;
  /*assuming the h1's text width is 200px, omit this if you want it to center with the whole h1*/
  display: flex;
  align-items: center;
  float: left
}
<div class="col-12 col-md-7  bioText" style="float: left;">
  <h1 style="padding-bottom: 20px;">About Myself</h1>
  <div class="container">
    <span class="singleAfterLine"></span>
  </div>
</div>

Answer №3

Let's start by correcting the usage of :after. You don't need to include a <span> element under the <h1>.

<h1>Introduction</h1> 


 h1:after {
    display: block;
    height: 2px;
    background-color: #55B4B0;
    content: " ";
    width: 100px;
    position: absolute;
    bottom: 10px;
}

h1 {
    padding-bottom: 20px;
    position: relative;
}

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

Having Trouble Accessing Full Website Source Code with Selenium and Python

Currently, I am attempting to extract data from the following website: https://script.google.com/a/macros/cprindia.org/s/AKfycbzgfCVNciFRpcpo8P7joP1wTeymj9haAQnNEkNJJ2fQ4FBXEco/exec Utilizing selenium and python for this task, I am encountering a challe ...

Unique twist on the Bootstrap grid: perfectly centered

I'd like to design a 12-column Bootstrap grid with specific colors. The header should be light blue, the body in green, and the footer orange against a grey background. I want the grid to be centered on the screen with horizontal light blue stripes m ...

Why isn't the image showing up as a list style?

I can't seem to get the image to show up on my page. Can anyone help me figure out what's wrong? ul li { background-image: url(logo.png); background-repeat: no-repeat; padding-left: 0px; } This is what I'm currently seeing: This is what ...

Adjust the header width and column alignment when freezing the header in a gridview interface

Looking to implement a gridview with a fixed header? I've tried various solutions including this link and this one. While I've managed to get it working, the main issue lies in the alignment of header width and column width. The scroll and freez ...

What steps can I take to prevent my menu items from overlapping in the mobile navigation menu?

I am currently working on creating a mobile menu, but I'm facing an issue where the menu items overlap when hovered over. Instead, I want the menu items to move downwards when hovered upon to prevent the text from overlapping. Below is the HTML code ...

JavaScript failing to retrieve JSON information from PHP

I seem to be encountering an issue when trying to read JSON data that I create in PHP and send back to my JavaScript. The problem is puzzling me. Here's the PHP code: header("content-type: text/json"); curl_setopt($ch,CURLOPT_URL, $url); curl_setop ...

jQuery not getting desired response from AJAX GET request

I'm currently working on a project that involves displaying the member list of all users using AJAX get request. Everything seems to be functioning properly, but the content appended to my HTML is not responding to another jQuery script. Is there a w ...

Adding data into a SQL Server database using HTML controls

I am looking to extract the content from a <h1> tag containing a news title and insert it into a SQL Server 2008 database using C#. Can anyone guide me on how to achieve this? ...

Modifying Copyright Feature in Footer

I am attempting to implement this function within my dark-colored footer: import Typography from '@material-ui/core/Typography'; function Copyright() { return ( <Typography variant="body2" color="textSecondary" align="center"> ...

Is there a way to modify the background shade of an HTML meter element?

Can the background color of an HTML meter be customized? I noticed that by default, it has a green background. Is there a way to change this green background to a different color? I attempted using the style attribute with a red background color, but it ...

What is the best way to reduce the height of the navbar in Bootstrap 4?

Check out my code snippet for implementing Bootstrap 4: <nav class="navbar navbar-collapse navbar-toggleable-md navbar-light bg-faded "> <div class="collapse navbar-collapse" id="navbarText"> <ul class="navbar-nav mr-auto"> ...

Tips on changing the text alignment in ant design's Select with search field component within a Form item to support various languages

Is there a way to dynamically change the text direction in a search field based on the language being typed by the user? For example, switch the direction to rtl when typing in Arabic and to ltr while typing in English. I need to achieve this functionalit ...

How can you identify if JavaScript is turned off without relying on the noscript tag?

While working on designing a pop-up notification for cases when JavaScript is disabled, I incorporated html, css, and bootstrap elements into my project. An issue arose as I pondered if there exists a method to identify whether the html class within my p ...

The focus state persists even after the DOM element has been modified

I am currently utilizing Vue along with Bulma. My challenge lies in updating the DOM upon a button click event. Even after the update, the button still retains its focus state. I aim to have the updated button without the focus state. What would be the m ...

Adjust the height of the div container and implement a vertical scroll feature on the fixed element

I created a fiddle and was hoping to enable scrolling, but I have been unable to find a solution. http://jsfiddle.net/sq181h3h/3/ I tried both of these options, but nothing seems to be working: #league_chat { overflow-y:scroll; } #league_chat { ...

How come my DHTML navigation bar is displaying incorrectly in Internet Explorer 7?

This particular issue has me completely baffled. Normally I can navigate through most IE7 CSS bugs with some clever adjustments here and there, but this one has really stumped me! Take a look at the example page in IE7 and you'll notice that when hov ...

Arrangement of Phone Numbers in RTL Orientations

When inserting a phone number such as + 972-123888555 into a layout that reads from right to left, the plus sign will be placed at the end of the number. https://i.sstatic.net/thmY3.png ...

Prevent the insertion of HTML elements into the DIV using jQuery

Check out this code snippet: <div id="whatever"> /* Setting up a button to add HTML code in this div using jQuery */ /* Now looking to create another button that will prevent further additions to the div with the existing HTML intact */ </div> ...

Center the content within a div by utilizing either the auto or percentage values

Hey there, I'm currently facing some issues that I can't seem to solve. I've tried various solutions but none of them are working for me. My goal is to center the content of one div inside another. I prefer using only auto or % values as I ...

Struggling with setting up a search bar for infinite scrolling content

After dedicating a significant amount of time to solving the puzzle of integrating infinite scroll with a search bar in Angular, I encountered an issue. I am currently using Angular 9 and ngx-infinite-scroll for achieving infinity scrolling functionality. ...