-1

The titles in my webpage are also a link that anchor to themselves, for accessibility reasons, I cannot really change that.

However, most people would like something in the likeness of Title with only the being a clickable link.

So my html looks like

<h2 id="myid"><a href="#myid">Title</a></h2>

And my CSS (so far):

h2 a::before { content: " "; }

However, I am far from having the wizardry to know what to do next, I tried looking at this question but I don't really know how to "save" the ::before part from being affected.

PatJ
  • 5,996
  • 1
  • 31
  • 37

1 Answers1

2

It's a bit tricky but you could set a fixed width for the character and overlap the h2::after pseudoelement for the entire length of the element minus the width of the a::before pseudoelement so the text can't be clicked.

It's worth noting that this will reduce the usability of the page so be careful on using this approach

h2 {
  position: relative;
  display: inline-block;
}

h2::after {
  position: absolute;
  z-index: 1;
  content: "";
  right: 0;
  height: 100%;
  width: calc(100% - 1.25em);
}

h2 a::before {
  content: "";
  display: inline-block;
  border-bottom: 1px dashed yellowgreen;
  width: 1.25em;
}

h2 a {
  text-decoration: none;
  color: inherit;
}
<h2 id="myid"><a href="#myid">Title</a></h2>
Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177