Adding a border bottom to anchor tags that can be applied across multiple lines and with either inline block or block display properties

One design challenge I'm facing is ensuring that anchor tags on my site have a custom underline that fits the overall look.

The problem arises when the link text wraps to multiple lines, as the bottom border only applies to the last line or the very bottom of the anchor. One potential solution is setting the display property of the anchor links to inline, but this sacrifices the ability to add a top margin.

Is there a way to apply a 2px underline to links that spans across multiple lines while still being able to give them a top margin?

Here is an example fiddle showcasing the issue:

https://jsfiddle.net/mjxfzrwk/

For reference, below is the code snippet used in the fiddle:

.custom-underline {
  border-bottom: 2px solid red;
  display: inline-block;
  margin-top: 20px;
}
.standard-underline {
  text-decoration: underline;
  display: inline-block;
  margin-top: 20px;
}
.inline {
  display: inline;
}
.container {
  width: 100px;
}
a {
  text-decoration: none;
  line-height: 29px;
  font-size: 20px;
}
<div class="container">
  <a class="custom-underline" href="">This has a good underline</a> 
  <br/>
  <a class="standard-underline" href="">This has a standard underline</a> 
  <br />
  <a class="custom-underline inline" href="">This has a good underline but display inline removed top margin</a>
</div>

Answer №1

To achieve this effect, you can utilize the :before pseudo element as demonstrated in the code snippet provided below. For a live example, you can check out the updated fiddle here.

.inline:before {
  content: '';
  display: block;
  margin-top: 20px;
}

Answer №2

One way to enhance your text is by enclosing it in a span element and utilizing the border-bottom property on the span.

a {
  width: 100px;
  display: block;
  text-decoration: none;
  margin-top: 50px;
}
span {
  border-bottom: 2px solid red;
}
<a href=""><span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, quisquam.</span></a>

Answer №3

If you're looking to add color to text, you can achieve it using the following CSS:

a{
    text-decoration: underline;
    -moz-text-decoration-color: blue; /* Firefox specific */
    text-decoration-color: blue;
}

However, achieving custom spacing with text-decoration:underline is not possible.

Answer №4

Consider implementing the display inline technique as a final resort within your code. Additionally, experiment with incorporating the following styles into your CSS:

.inline:before{
     content: " ";
     height:25px;
     display: block;
 }

Answer №5

To achieve a multiline border effect for your text, follow these two steps:

  1. Apply the following 3 CSS properties to your element:

    .custom-underline {
        border-bottom: 2px solid blue;
        display: inline-block;
        text-decoration: none;
    }
    
  2. Ensure that the display property of the parent element is set correctly. Certain values of display can interfere with the multiline behavior of its child. Values such as block, flow, inline, and table will work, while flex, inline-flex, grid, and inline-grid will not work. Additionally, display: none will have no effect.

Keep in mind that changing the vertical position of an inline element using margin, padding, or transform + translateY may not be possible. In such cases, try adjusting the line-height or add margin or padding to the parent element instead.

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

Generate a unique class for each img element randomly

Is there a way to assign each image a unique random class instead of giving all the images the same random class? Any help would be appreciated. $(document.body).ready(function () { bgImageTotal = 5; randomNumber = Math.round(Math.random() * (b ...

Is there a way to choose the superior element excluding any offspring?

Help needed: trying to style only the h1 element without affecting the span using CSS HTML: <h1> <span>hello from spam</span> <br> hello from h1 </h1> ...

Issue with Jquery Date picker functionality on Master Page not functioning as expected

My issue is with the master page <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- ...

The animated 3D cube is specifically compatible with Firefox

After working on a 3D animated cube that rotates infinitely in two angles, I encountered some browser compatibility issues. The cube can be found on the home page of our new company website (next to the Software title when scrolling down) at test website. ...

Styling with CSS: The Art of Showcasing Initials or Images of Individuals

By following this elegant HTML and CSS example, I am able to showcase my initials over my photo. While this is wonderful, I would like the initials to be displayed only if the image does not exist; if the image is present, the person's initials shoul ...

Tips for achieving a design aesthetic similar to that of the JQuery UI website

On my website, I am using jQuery UI along with the "Smooth Default" CSS theme. However, I have noticed that everything appears larger compared to the jQuery UI website itself. For instance, when looking at the buttons here: http://jqueryui.com/button/ The ...

Is there a way to eliminate the shadow effect appearing on product photos in BIG CARTEL?

While using the Luna theme on my Big Cartel store, I noticed that the products are hard to see on mobile devices because of the box shadow effect. I found a solution on a Big Cartel help page recommending to add a specific code to the CSS, but when I tried ...

Website Navigation Bar Template

Can someone assist me, please? I've been working on this project for a few hours now. As a beginner in html and css, I've been doing extensive research but haven't found a solution that works perfectly for my needs. The only thing that some ...

Can anyone recommend any offline editors for HTML, CSS, and JavaScript similar to JSFiddle, jsbin, or codepen?

Is there an alternative to JSFiddle.net that allows users to experiment with Javascript, HTML, and CSS offline in a similar way? ...

File bootstrap.min.css is currently experiencing compatibility issues

I am currently working on a website where I have two images that are displaying vertically. However, I would like these images to be displayed horizontally with animation. I attempted to use Bootstrap to achieve this horizontal layout, but when I include ...

Managing the Navigation Bar

I am currently using Bootstrap and ReactJS, and I am facing an issue where the active tab on the navigation bar does not change when I scroll down the page. At the moment, the active tab only changes when I click on a specific section. Each section corresp ...

Stylesheet for a React component

Why is the CSS code in my React component file not working even though I have imported it? import React from 'react'; import { Carousel } from 'react-bootstrap'; import './Banner'; ...

Activating development mode for LessCSS in Node.js

I'm encountering some major difficulties identifying LESS parse errors within a large CSS compilation. The error messages aren't providing much help (at least not for me). Here are my questions: 1. Is there a method to activate more debugging ...

Opting for a CSS framework instead of building custom CSS from scratch

In my quest to achieve the best CSS practices on my website, I initially relied on my own custom CSS. However, I then discovered the option of using CSS frameworks like Bootstrap or Foundation Zurb. While I believed that using a framework alone would meet ...

Configuring the color of the active page link in the navbar using Python Flask

In order to highlight the current page that the user is on, I want the link of the current page to have a different color compared to the other page links. This will help the user easily identify which page they are currently viewing. I am implementing thi ...

Script to alter div style based on the day of the week

Check out my project: http://jsfiddle.net/yL0o1cy0/ .calendar { width: 850px; height: 500px; background-color: #141414; border-radius: 10px; padding-top: 10px; margin-top: 15px; margin-left: 15px; } #calendartest1 { width: 778px; heigh ...

Navigating within fixed-positioned divs

I'm attempting to create something similar to the layout seen on: The desired effect is to have fixed content on the right side, with only the left side scrolling up to a certain point before the entire page scrolls normally. However, in my implement ...

I have decided to integrate Laravel with Vite for building CSS and JS. However, when I attempted to run `npm run dev`, it appeared to execute but was accompanied by an error in the background that

Hi there, I'm new to Laravel and I've created a small app that primarily uses CSS and JS scripts. Everything was working fine in my development environment, so I decided to push it to a production server. However, after installation, my code does ...

Static checks do not apply to CSS variables

Our webtech class assignment is to create a static webpage that meets the criteria of the jigsaw w3c css validator. We've encountered a roadblock while trying to resolve the final warning message: Due to their dynamic nature, CSS variables are curre ...

Flexible design for your website content wrapper

When I display the content on my website, it sometimes overlaps the footer because the wrapper does not adjust its size based on the content. I set the height to an absolute value and now I need to find a more flexible option to prevent this issue. I' ...