Tips for achieving consistent line spacing and aligning text evenly with CSS

Struggling to create consistent lines between words without knowing their length in advance. Adding extra divs might be the solution, but I'm hesitant to clutter the DOM with unnecessary elements.

.progress-bar {
  display: flex;
  width: 100%;
  justify-content: space-between;
  margin-top: 50px;
}

.progress-bar p {
  display: block;
  position: relative;
  width: 100%;
  text-align: center;
  line-height: 0.1em;

}

.progress-bar p::after {
  content: "";
  z-index: -1;
  position: absolute;
  bottom: 0;
  top: 0;
  right: 0;
  width: 25%;
  height: 2px;
  background: black;
}

.progress-bar p:last-of-type::after {
  background: none;
  content: "";
}
<div class="progress-bar">
  <p>text one</p>
  <p> text two</p>
  <p>text three</p>
  <p>text four</p>
</div>

Answer №1

If you want your text to be part of a flexbox container, the easiest solution is to use display:contents.

.progress-bar {
  display: flex;
  justify-content: space-around;
  align-items:center;
  margin-top: 50px;
}

.progress-bar p {
  display: contents;
  text-align: center;
  line-height: 0.1em;
}

.progress-bar p:not(:last-of-type)::after {
  content: "";
  width: 8%;
  height: 2px;
  background: black;
}
<div class="progress-bar">
  <p>text one</p>
  <p> text two</p>
  <p>text three</p>
  <p>text four</p>
</div>

Another option is:

.progress-bar {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 50px;
}

.progress-bar p {
  display: contents;
  text-align:center;
  line-height: 0.1em;
}

.progress-bar p:not(:last-of-type)::after {
  content: "";
  width: 8%;
  margin:0 3%;
  height: 2px;
  background: black;
}
<div class="progress-bar">
  <p>text one</p>
  <p> text two</p>
  <p>text three</p>
  <p>text four</p>
</div>

Answer №2

Finding a suitable way to format using flex and space-between proved challenging as the information on the width and position of each text seemed to get lost in the process.

Instead, I have devised a method utilizing inline-blocks, padding, and margins.

The trick here is to create the inter-text lines as a continuous line, with each text element 'blanking out' portions of it to the left and right using padding and background color. This method ensures that the texts are evenly spaced regardless of their width.

This code snippet allows you to adjust 3 variables to customize the line width, gap between text and line, and the number of items in the progress bar, giving you control over the spacing.

The end result is responsive, meaning that longer texts will wrap onto multiple lines if needed while keeping the inter-text line centered among the texts.

.progress-bar {
  width: 100%;
  position: relative;
  margin-top: 50px;
  background-image: linear-gradient(black, black);
  background-size: 100% 2px;
  background-position: 0 calc(50% - 1px);
  background-repeat: no-repeat no-repeat;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  /* alter these to suit your use-case */
  --lineW: 6%;
  /* width of the lines between texts */
  --gapW: 3%;
  /* width of the gap between text and line */
  --n: 4;
  /* number of items in the progress bar */
}

p {
  display: inline-block;
  position: relative;
  background: white;
  text-align: center;
  padding: 0 var(--gapW);
  margin: 0 calc(var(--lineW) / 2);
  max-width: calc((100% - (var(--n) * (var(--gapW) + var(--lineW)))) / var(--n));
}

p:first-child::before,
p:last-child::after {
  content: '';
  display: inline-block;
  margin-right: 0;
  position: absolute;
  width: 100vw;
  background: white;
  z-index: 1;
  height: 100%;
}

p:first-child::before {
  right: 100%;
}

p:last-child::after {
  left: 100%;
}
<div class="progress-bar">
  <p>text one</p>
  <p>text two</p>
  <p>text three is noticeably wider</p>
  <p>text four</p>
</div>

Answer №3

To create spaces using gap syntax, you can utilize the following method in your CSS:

.progress-bar{
  display: flex;
  flex-orientation: column;
  gap: 2rem;
}

For more information on using flex properties, visit: https://www.w3schools.com/css/css3_flexbox.asp

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

HTML5 text enhanced with a dynamic shadow effect

Struggling with adding a shadow effect to text in my front-end development project. Using HTML5, CSS3, bootstrap, and AngularJS for the design elements, I want to achieve a shadow effect similar to what you would see in Photoshop on a text field. Can anyo ...

implement a new directive in AngularJS that references another directive in HTML

Check out the Fiddle here Upon button click, a modal window appears. Inside this modal, there is a <template-placeholder></template-placeholder>. When the button is clicked, an if-check is performed to ensure it's the correct button. If ...

The toggle button in the navbar takes its time to vanish completely

Every time I try to close my navbar, it seems to take forever for the navbar to disappear. Below is the code snippet: HTML <!-- logo --> <div class="logo"> <img src="assets/logo.png" alt="logo g's s ...

Is it possible to customize the default styling options in Tailwind?

I am currently working on a blog using NextJS, and I have encountered an issue with Tailwind's list style type. It seems that the default styling for list style type is set to list-none, resulting in my <ul> <li> elements not being styled ...

Arrange elements based on the selected dropdown menu

I'm currently utilizing Twitter Bootstrap 2.3.2 along with the chosen plugin for select boxes. I have a question regarding how to align other elements based on the box with selected items. Below, you can see two select boxes and content (elements) po ...

Can you tell me the best way to render a partial to a specific location in Ruby on Rails using the button_to method?

As a newcomer to Ruby on Rails, I am currently working on creating a basic messaging application. So far, I have successfully implemented user creation/login and have managed to render the inbox. However, I am facing difficulty in rendering a partial conta ...

Designing a rounded border radius for various child elements within a layout

I am currently working on creating a circular container that houses an image and a description overlaid at 50% opacity. Here is what I have achieved so far: https://i.stack.imgur.com/pIkO1.png My goal is to apply a uniform border-radius to the entire div ...

The background color disappears when viewed on mobile devices

I have created a table using HTML and CSS. The issue is that the background colors of the table display correctly on a desktop, but on a mobile phone, they appear completely transparent while the text remains visible. I am using Bootstrap 4 for this websi ...

Employing JavaScript for fading divs in and out sequentially

Hey there! I'm currently struggling with implementing transitions for my tool tips. Any assistance would be greatly appreciated! I am looking to have my "fader" divs fade in and out on click of a button, with each transition lasting 5 seconds. It&apo ...

Adjust the width of CSS elements dynamically

Here are the shortcodes I am using on a page or post: [custom_width width='500' color='red'] [custom_width width='600' color='blue'] The purpose of these shortcodes is to display 2 buttons with different widths. H ...

Save the session ID in localStorage instead of a cookie

After successfully logging into my PhoneGap app, everything functions properly. I can send requests within the current session and am authenticated. However, if I completely close the app and reopen it, my session is lost. The cookie containing connect.sid ...

Place the div at the center within the header

I'm struggling to center the logo in my media query for screens below 479px. Whenever I use margin auto, the image gets pushed to the right by the browser. Here is what I have so far and any assistance would be greatly appreciated. HTML <div clas ...

What are the advantages of importing CSS files from JS source code with webpack, rather than building CSS in isolation as traditionally done?

If you're looking for information on how to load CSS with webpack, check out the documentation here: https://webpack.js.org/guides/asset-management/#loading-css1 The webpack documentation suggests importing CSS files from JavaScript code and using ex ...

Simple Javascript does not appear well on the screen (Mean stack)

I am facing a seemingly simple issue with my code, but for some reason it is not working as expected. I am trying to create a basic website using Mean Stack for personal development purposes. The code I have written is similar to examples in AngularJS docu ...

Is JSX Babel being rejected for execution?

I have recently started learning React and encountered an error while trying to run this page: Refused to execute script from 'https://unpkg.com/browse/[email protected]/babel.min.js' because its MIME type ('text/html') is not exec ...

The code snippet 'onload='setInterval("function()",1000)' is not functioning as expected

I need to continuously load the contents of an XML file into a specific HTML div every second. Here is the JavaScript code that I am using to parse the XML file: function fetchEntries() { if (window.XMLHttpRequest) req = new XMLHttpRequest(); ...

What is the most effective method for locating and capturing a specific element (such as an input box) in Python Selenium using either Xpath or CSS selector

I am currently using selenium to scrape a website, primarily relying on xpath or CSS selectors to extract elements. However, I have noticed that these selectors are dynamic, which contradicts what I have read online about CSS selectors being stable. As a b ...

Using image paths in CSS for styling purposes

As I develop an Asp.net website, I've encountered a surprising issue. Here is my folder structure: - Styles - master.css - Images -webPages -scripts When trying to access an image in the Images folder from the CSS file, I receive a ...

Having trouble implementing HTML font-family in C# code due to spaces

I am encountering a minor issue with setting the font-family in my C# code behind on an aspx page in Visual Studio Express 2015 for Web. I have written code using HtmlTextWriter to enable printing of a gridview when a button is clicked. The problem arises ...

Images in the JavaScript menu are not remaining fixed in place

Can someone help me with my website? I'm having trouble with the menu for different pages, it should stay gray for the active page. It was working fine before I switched to Wordpress, but now I'm not sure what the issue is. Could someone take a ...