Creating a horizontal line to the left of centered text using CSS styling

Is there a way to display a horizontal line to the left of text while keeping the text center aligned? Refer to the image below for clarification.

Below is the markup I am currently using, which includes Bootstrap 4:

https://i.sstatic.net/KF8WJ.png

     <div class="text-center nav-items">
    <ul>
      <div class="pb-5">
        <li>
          <a href="#"><h2 class="sidebar-first-item">About</h2></a>
        </li>
        <p>Behind the brand // Meet the team</p>
      </div>
      <div class="pb-5">
        <li>
          <a href="#"><h2>Our Work</h2></a>
        </li>
        <p>Take a look at our marvelous creations</p>
      </div>
      <div class="pb-5">
        <li>
          <a href="#"><h2>Services</h2></a>
        </li>
        <p>Learn more about what we can do for you</p>
      </div>
      <div class="pb-5">
        <li>
          <a href="#"><h2 class="sidebar-last-item">Contact</h2></a>
        </li>
        <p>Get in touch today. Let's make some magic</p>
      </div>
    </ul>
   </div>

Answer №1

To create this effect, utilize pseudo-elements:

.menu-item {
  position: relative;
}

.menu-item:before {
  content: '';
  position: absolute;
  width: 50%;
  height: 3px;
  background-color: blue;
  top: 50%;
  left: 0%;
}

CodePen Link: https://codepen.io/johndoe/pen/abc123xyz

Answer №2

Before making any further changes, it is essential to tidy up your HTML code. A <ul> list should only have children elements like <li>, <script>, or <template>. Therefore, remove the div elements inside the .</ul> and transfer their classes to the <li> tags.

To address your design issue, consider adding a wrapping element before and after your anchor links. You can adjust the width using percentages and set a minimum width for the anchors to ensure a responsive layout, especially on smaller devices. This technique will create an evenly spaced red line.

If you prefer the red line to align with the text's width, you can use non-breaking spaces in your anchors along with padding. This will bring the red line closer to the text according to the specified padding.

.redline,
.spacer {
  width: 100%;
  height: 3px;
  display: inline;
  background-color: red;
}

.spacer {
  height: 0px;
}

li {
  display: flex;
  align-items: center;
}

li a {
  margin: 0 auto;
  border: 0px solid gold;
  padding: 0 20px;
  white-space: nowrap;
}

li p {
  width: 100%; margin:-50px 0 50px;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<div class="text-center nav-items">
  <ul>
    <li class="pb-5">

      <span class="redline"></span>
      <a href="#">
        <h2 class="sidebar-first-item">About</h2>
      </a>
      <span class="spacer"></span>

    </li>
    <li>
      <p>Take a look at our marvelous creations</p>
    </li>



    <li class="pb-5">
      <span class="redline"></span>
      <a href="#">
        <h2>Our Work</h2>
      </a>
      <span class="spacer"></span>     
    </li>
    <li>
      <p>Behind the brand // Meet the team</p>
    <li>



  </ul>
</div>

Answer №3

Utilizing a CSS pseudo element can enhance the design of your elements! A pseudo element allows for the addition of decorative elements before or after an existing element.

I included a new class named horizontal-line to style your h2 element.

<div class="text-center nav-items">
<ul>
  <div class="pb-5">
    <li>
      <a href="#"><h2 class="horizontal-line sidebar-first-item">About</h2></a>
    </li>
    <p>Behind the brand // Meet the team</p>
  </div>
  <div class="pb-5">
    <li>
      <a href="#"><h2>Our Work</h2></a>
    </li>
    <p>Take a look at our marvelous creations</p>
  </div>
  <div class="pb-5">
    <li>
      <a href="#"><h2>Services</h2></a>
    </li>
    <p>Learn more about what we can do for you</p>
  </div>
  <div class="pb-5">
    <li>
      <a href="#"><h2 class="sidebar-last-item">Contact</h2></a>
    </li>
    <p>Get in touch today. Let's make some magic</p>
  </div>
</ul>

This is how the CSS styling would appear:

.horizontal-line {
 position: relative;
}

.horizontal-line::before {
  content: "";
  display: block;
  width: 60px;
  height: 2px;
  position: absolute;
  top: 50%;
  left: 35%;
  border-radius: 30px;
  background-color: #DE657D;
}

By implementing the pseudo element, it automatically adds the specified design before any element with that class name. In this instance, we are using an empty content value for the decorative effect.

Visit this link for reference.

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

Form control named 'startTime' and 'endTime' do not have a valid value accessor

The app.module file contains the following code: import { TimepickerModule, TimepickerConfig } from 'ngx-bootstrap/timepicker'; import { getTimepickerConfig } from './TimepickerConfig'; imports: [ BsDatepickerModule.forRoot(), Tim ...

What is the best way to ensure that my two sections stay vertically aligned, even when they have varying amounts of content and are scaled

I'm struggling to align two sections horizontally with varying content lengths, even when scaled. I attempted using display:flex in the .section part of my CSS, but it didn't yield the desired effect. Changing the flex direction also didn't ...

Event firing in child components of Vue.js can occasionally fail to trigger

When using component A to render multiple instances of component B using v-for, an animationend event is fired from component B to A. However, there are cases where the animationend event fails to fire. View the code sandbox for more information ...

Aligning a picture with a heading

I'm currently working on a website header design where I am struggling to align the logo and site title horizontally. As someone who is new to CSS, any guidance or tips would be greatly appreciated! The logo image has the following styling: .logo { ...

Issue with Flexbox alignment: Second row does not line up with first row

When dealing with a flex container that has 4 items and you want 3 per row while keeping the container centered, it can be tricky. Using justify-content to center the container may result in the fourth item on the second row also being centered. To address ...

What could be causing the Navbar Collapse feature to malfunction in my Bootstrap setup?

Hey there, I'm facing an issue with the Bootstrap navbar collapse button not working. I've tried everything, followed all the steps in a tutorial, but still can't get it to function properly without altering the Signup and Login buttons. Ca ...

Tips for adjusting the width of dynamic list boxes according to user input

When a list box is clicked, it will be removed and replaced by 'n' new list boxes of the same type. The value of 'n' is provided by the user in a prompt. This process can be repeated. The width of the newly generated list boxes should ...

What is the best way to adjust the width and height of react-flip-page through CSS for different media queries?

Incorporating the react-flip-page npm package to add animation to a book on my web application has been exciting. However, I've encountered an issue with adjusting the width when using media queries due to the available props for setting size and colo ...

How can I fill the space around a centrally aligned div?

I'm looking to create a centered div with additional divs or spans on each side to fill the empty space. Currently, I am using margining to center the div and you can see the implementation in this example. HTML Code: <div id='A> <d ...

Creating a custom design for a dropdown menu in the Bootstrap navbar

My Bootstrap navbar has a unique color scheme for the navbar itself and the letters, as indicated in the following CSS: .jumbotron { background-color:#2d4c60 !important; } nav.navbar { background-color: #2d4c60; } .nav.navbar-nav.navbar-ri ...

Images and CSS are functioning properly when viewed locally, but are not displaying as expected on GitHub pages

There seems to be some issues with transferring my website from a custom domain on Hover. When I try to import it to anthematics.github.io, only the HTML loads while images and CSS are missing. <link rel="stylesheet" href="theme.css" type="text/css"> ...

Radio buttons that are arranged vertically with spaces in between (Bootstrap 4)

I am looking to style radios as buttons in a vertical arrangement, similar to the examples shown on the Bootstrap documentation for buttons and button groups. I tried using '.btn-group-vertical' from Bootstrap but it seems to leave gaps between t ...

What is preventing window.scrollTo() from being executed?

I have implemented Bootstrap's Buttons plugin to toggle the state of a custom checkbox. When the button is toggled on, a hidden element should appear at the top of the page and then the page should scroll to the top. Similarly, when the button is togg ...

Ways to dynamically manipulate HTML elements in Angular 5

Recently, I've been attempting to programmatically transform an HTML element. Strangely, when I update the transform value in the console tab, it changes successfully, but for some reason it doesn't reflect in the element tab of the browser. onD ...

Formatting text and images in CSS

Looking to align images at the bottom of each column in a div with 3 columns, but struggling due to varying text lengths. Any tips on how to achieve this? https://i.sstatic.net/fuPgl.png Thank you, Carolin #content_row{ margin-top:150px; marg ...

Embed JavaScript within the Material-UI style components

I was hoping to incorporate some conditional logic within my MUI styles Currently, I am using inline styles which are functional <div className={classes.form} style={{alignItems: ((Code.length>10 || Description.length>100) ? 'start' : & ...

Why won't applying a binding style affect the appearance of my Vue component?

const EventLevelBoard = { name: "EventLevel", data: { levelStyle: { display: "block" }, levelStyleinner: [ { display: "block" }, { display: "block" }, { display: "block&qu ...

Content that is partially concealed when scrolling through div elements

I have been searching high and low for similar questions, but to no avail. So, I am laying out my problem here in the hopes that you could guide me on what I might be doing wrong and how to rectify it. The scenario I am trying to achieve is having two divs ...

The animation for the Bootstrap modal is not functioning correctly

Whenever I click the button to open the modal window, it just pops up without any animation or fade effect. I attempted to add classes like .fade and .modal-fade to the modal, but nothing changed. You can see it in action on my test page. Simply click th ...

Icons disappear from the navbar toggle when clicked on a small screen

Using Django and Bootstrap 4, I've created a navbar with a toggle feature. However, when viewed on a small screen, the toggle button does not display the icons as expected. Instead, it shows empty lines. Strangely, when the phone is rotated, the toggl ...