Tips for maintaining text within an anchor tag in a fixed position while applying the border property

I want to implement Bootstrap's Scrollspy component with a <ul> element.

The problem arises when I apply the border-left property to an <li> element, causing the text in the anchor tag to shift slightly to the right.

Any suggestions on how to resolve this issue?

Here is the link to the Pen:

https://codepen.io/AshNaz87/pen/QmVJVY

.wrapper {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

li {
  list-style: none;
  margin-bottom: 0.5rem;
  padding-left: 1rem;
}

li:last-child {
  margin: 0;
}

.with-border {
  border-left: 3px solid grey;
}

a {
  text-decoration: none;
  color: #000;
}
<div class="wrapper">
  <ul class="nav">
    <li class="with-border"><a href="#">Fortune</a></li>
    <li><a href="#">Favours</a></li>
    <li><a href="#">The</a></li>
    <li class="with-border"><a href="#">Brave</a></li>
  </ul>
</div>

Answer №1

The issue with the inconsistent offset of text is due to the physical space taken up by the border of an element in the layout. To address this, you have a couple of options - either adjust for the space the border occupies (solution 1), or explore alternative approaches that maintain the document flow (solutions 2 and 3).

You can implement:

  1. A transparent left-border on all <li> elements,
  2. Use of a background-image to simulate a border visually, or
  3. An absolutely positioned pseudo-element.

Solution 1: Transparent left border

This method involves adding a transparent border to all <li> elements and changing the border-color property as needed:

li {
  border-left: 3px solid transparent;
}

li.with-border {
  border-color: grey;
}

.wrapper {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

li {
  border-left: 3px solid transparent;
  list-style: none;
  margin-bottom: 0.5rem;
  padding-left: 1rem;
}

li:last-child {
  margin: 0;
}

li.with-border {
  border-color: grey;
}

a {
  text-decoration: none;
  color: #000;
}
<div class="wrapper">
  <ul class="nav">
    <li class="with-border"><a href="#">Fortune</a></li>
    <li><a href="#">Favours</a></li>
    <li><a href="#">The</a></li>
    <li class="with-border"><a href="#">Brave</a></li>
  </ul>
</div>


Solution 2: Using background image for border

Alternatively, you can employ a linear gradient as a background image with a distinct border/breakpoint to mimic a border visually:

li.with-border {
  background-image: linear-gradient(90deg, grey 3%, transparent 3%);
}

.wrapper {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

li {
  list-style: none;
  margin-bottom: 0.5rem;
  padding-left: 1rem;
}

li:last-child {
  margin: 0;
}

li.with-border {
  background-image: linear-gradient(90deg, grey 3%, transparent 3%);
}

a {
  text-decoration: none;
  color: #000;
}
<div class="wrapper">
  <ul class="nav">
    <li class="with-border"><a href="#">Fortune</a></li>
    <li><a href="#">Favours</a></li>
    <li><a href="#">The</a></li>
    <li class="with-border"><a href="#">Brave</a></li>
  </ul>
</div>


Solution 3: Using Absolutely Positioned Pseudo-Element

This approach utilizes a generated pseudo-element that is absolutely positioned within the <li> element to replicate a border visually:

li {
  position: relative;
}

li::before {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: 3px;
  display: none;
  content: '';
  background-color: grey;
}

li.with-border::before {
  display: block;
}

.wrapper {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

li {
  position: relative;
  list-style: none;
  margin-bottom: 0.5rem;
  padding-left: 1rem;
}

li:last-child {
  margin: 0;
}

li::before {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: 3px;
  display: none;
  content: '';
  background-color: grey;
}

li.with-border::before {
  display: block;
}

a {
  text-decoration: none;
  color: #000;
}
<div class="wrapper">
  <ul class="nav">
    <li class="with-border"><a href="#">Fortune</a></li>
    <li><a href="#">Favours</a></li>
    <li><a href="#">The</a></li>
    <li class="with-border"><a href="#">Brave</a></li>
  </ul>
</div>

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 there a way to change the text (price) when I select an option?

<div class="single-pro-details"> <!--Customize in the CSS--> <h6>Home / Beats</h6> <h4>Unique Lil Tecca Type Beat - New Love</h4> <h2 id="price">$ ...

Disabling the submit button after submitting the form results in the page failing to load

I am encountering an issue with my HTML form that submits to another page via POST. After the form validates, I attempt to disable or hide the submit button to prevent double submission and inform the user that the next page may take some time to load. He ...

Ensure that site content remains visible above the table when refreshing the Ajax Table

Whenever I update the table, the content above it ends up below the table. This is frustrating because the content should not be affected by the refresh. How can I resolve this issue? <!DOCTYPE html> <html> <head> <titl ...

How to vertically align text within a container in Bootstrap?

Lately, I've been facing a challenge in my attempts to vertically center text while also incorporating a full-width background image. The goal is for the centered text to maintain its position regardless of the height of the enclosing container. This ...

What could be causing the extra space around my body on mobile devices?

I am currently delving into the world of responsive design, starting from square one. My aim is to create a minimalist webpage with no side margins. However, when viewing it on an iPhone, there still seems to be a significant white margin on either side. T ...

Is there a way to include a text file in HTML for referencing or posting on my website?

Managing a website where my colleagues frequently post updates can be time-consuming as I am the only one knowledgeable in html and css. I find myself constantly formatting their posts and creating new pages for them. I am exploring ways to simplify this ...

Align two tables horizontally in the center using HTML

Is there a way to center two tables side by side using CSS? I have been able to center a single table, but I am struggling to center two tables together. Can anyone provide a simple solution using CSS? Below are the codes I have used: @import url(ht ...

I'm encountering layout issues with my table when using the repeater in Bootstrap

I am experiencing an issue with nested repeaters inside a repeater while trying to incorporate Bootstrap for layout purposes. The layout appears distorted when placed within a div with the class "row". Even when isolated, the layout remains problematic. Re ...

How can I ensure that my data remains properly aligned in a row with 9 columns while utilizing bootstrap?

My approach to creating a row with 9 columns involved using custom CSS to set each column's width to 11.11%, resulting in equal distribution across the row. I then applied the "custom-column" class to populate the row data: .custom-column { widt ...

Using Javascript/JQuery to attach a javascript object to a form in order to perform a traditional form

On a page, there is a form that consists mostly of plain HTML. One section of the form creates a quite complex multidimensional object using JavaScript. Usually, if I wanted to include a variable in the POST request, I would add an <input type="hidden" ...

JavaScript functions cannot be applied to input fields in jQuery

I am having trouble saving values into a database where I need to calculate the total and grand total. I want to do the calculation in the input field, but my attempts have not been successful. It seems like the issue lies with $('.multTotal',thi ...

The "initialized" event in angular2-tree-component fires prior to the data being loaded

Utilizing the angular2-tree-component, my goal is to display an already expanded tree. According to Angular docs, the initialized event should be used for expanding the tree after the data has been received: This event triggers after the tree model has ...

Tips for displaying images dynamically in HTML using AngularJS

src={{logo}} var logo = 'localhost:3000/modules/images/default.png' I'm trying to display the image path dynamically, but it's not showing up in the HTML. Do I need to use quotes for src? Can someone please assist me with this issue? ...

Implement a vertical scrollbar within the Bootstrap navbar to accommodate a large number of items

I implemented a sidebar using Bootstrap within a React application. Below is the code snippet: import React from 'react'; import './style_sidebar.css'; export default class Sidebar2 extends React.Component { render() { ret ...

The footers on each page are not consistent

I have noticed that two pages utilizing the same CSS are displaying differently. Check them out here: 128.48.204.195:3000 128.48.204.195:3000/formats If you look closely, you'll see that below the footer, the /formats page appears to have no extra s ...

Incorporate new functions through the css class name "javafx"

On my dashboard, I have 10 labels that share the same CSS class for mouse over events. Now, I want to change the button style after it is pressed and remove the mouse over effect. How can I achieve this? ...

What could be causing the onclick function in the button tag to malfunction?

I have implemented a feature where clicking on a "Delete Photo" button would trigger the appearance of another delete button in the code. The code for this functionality is as follows: <button type="button" class="btn btn-danger" onc ...

Tips for adjusting the alignment of numbers in an ordered list to the right

Check out the code snippet below: The list items are correctly aligned to the right, but the numbers are not. How can we adjust the CSS so that the numbers align properly on the right as well? ol { width: 15vw; } <ol style="text-align:right;"> ...

Bootstrap does not show submenus on its navigation menus

I am currently designing a menu with Bootstrap, but for some reason, the submenu items are not showing up. https://i.stack.imgur.com/qZqyf.png After carefully reviewing the HTML code multiple times, I am unable to identify any issues. I am now questionin ...

Is there a way to extract all the aria-label attributes from elements that share a common class?

I am currently working on a program that tallies the number of reactions from Facebook posts, with each reaction stored in an aria label containing detailed information. I am encountering difficulty retrieving the values of these unique aria labels due to ...