Ways to emphasize text in a h2 header without affecting other elements

One particular HTML block is causing me some trouble:

<h2><span class="numbers">1.1 </span>header text</h2>

It's important to note that I did not write this HTML code, so my options for changing it are limited. However, I can apply some CSS styling.

My goal is to underline the text "header text" without affecting the 1.1.

When attempting to achieve this with CSS and using

h2 {text-decoration: underline;}

The entire line gets underlined, which poses a challenge since I cannot override the text-decoration of h2. This has left me unsure how to proceed.

(An example of the issue is presented below)

h2   { text-decoration: underline; }
span { text-decoration: none;      }
<h2><span class=numbers>1.2 </span>header text</h2>

Answer №1

If you want to create an effect where the numbers appear underlined but blend in with a white background, you can use this CSS code:

h2 {
  text-decoration: underline;
}

.numbers {
  text-decoration: underline;
  text-decoration-color: white;
}
<h2><span class="numbers">1.1 </span>header text</h2>

Answer №2

It may seem like using text-decoration is not the solution here (UPDATE: Actually, you can achieve this with just text-decoration), especially if you want to underline text only. In that case, you can try using border-bottom in a creative way like so:

h2 {
  border-bottom:2px solid; 
  display: inline-block;
}
span {
  display: inline; 
  background-color: #fff; 
  padding: 3px 0;
}

Check out this example for reference: https://jsfiddle.net/DeepakKamat/eqjsg0nL/

To conceal the border on the parent element, apply a background color and vertical padding to the span.

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

Invoke a function using the output of a different function

There is a function whose name is stored in the value of another function, and I need to invoke this function using the other one. The function I need to call is popup() random() = 'popup()' if ($.cookie('optin-page')) { } I attemp ...

Is it possible to extract only the Bolded Text and store it in a String Array within a Spanned String

I need help with extracting bolded text from my paragraph-long stories that are saved as spanned strings in an EditText. The regular text is interspersed with a few bolded words here and there, and I want to be able to search through the spanned string a ...

Material UI Grid Items not stretching to fill the entire available width

I'm currently working with a nested Grid system in Material UI, but the Grid items are only occupying a fixed width and leaving some empty space. The issue arises when this fixed space is used up and instead of adjusting their internal free space, the ...

Issue with functionality of Bootstrap template generated by Visual Studio 2013

I've set up a new ASP.NET MVC project using the latest Bootstrap 3 template, but I'm encountering an issue. There doesn't seem to be any spacing between the header navbar and the content displayed below by Renderbody(). I've attempted t ...

Do double forward-slash comments work in CSS code?

Is using a double-forward slash (//) for single-line comments in CSS considered reliable and supported by mainstream browsers and CSS interpreters according to the specification? ...

How can I convert an HTML table into a PDF using TCPDF while ensuring that all content is contained within a single div in PHP?

I have successfully converted my JSON API data into an HTML TABLE format and now I am trying to generate a PDF from this HTML TABLE using TCPDF in PHP. Despite trying various methods related to PDF generation using TCPDF, I am facing issues with my code wh ...

the process of triggering animation to start automatically when a button is clicked in Web Development

I'm looking to create a React component that triggers an animation when clicked. I have a few options in mind: If the props are changed in the middle of the animation, it should restart the animation. The props can be changed by clicking a button on ...

Switching from real pixels to CSS pixels

The details provided in Mozilla's documentation on elementFromPoint clarify that the coordinates are specified in "CSS pixels" rather than physical pixels. This raises a question about what exactly CSS pixels entail. Many assume that a pixel in CSS is ...

The Laravel href link will fail to work when the URL does not include the string 'http'

I am encountering an issue with a particular hyperlink tag <a href="{{ $organization->website }}">Link</a> When the URL in $organization->website does not start with http:// or https://, the link fails to direct me properly. Instead, i ...

How can I include multiple search forms on a single page in TYPO3 Ke_Search without any conflicts between them?

In the header of my website, I have a search form that is generated as a cObject from a content element containing a ke_search searchbox. When this form is submitted, it redirects to a /search page. In addition to this, I have subpages that are meant to se ...

Optimize the size of div elements using jQuery

I am looking for a way to minimize certain div elements on my webpage using symbols from font-awesome instead of the traditional "-" and "+". However, I am having trouble inserting the classes of the icons into my code. I attempted replacing the .html meth ...

What is the best way to update the div id by extracting the last digits from a number?

Is there a way to change the div ids using a function? Before: <div id="a_1">a1</div> <div id="b_1">b1</div> <div id="c_1">c1</div> <div id="d_1">d1</div> <button onclick="change()">Change</button& ...

What is the best way to retrieve the parent div's ID with JavaScript and Selenium?

My current project involves utilizing webdriverjs, and I am faced with the challenge of retrieving the parent id of a specific webelement among multiple elements with similar classes. Each element has a different id that gets dynamically generated when a m ...

What is the best way to retrieve the value of an object in PHP?

There seems to be an issue with retrieving the value of an object. When attempting to get the object value, it appears empty. However, when simply echoing the object, there is content and not null. I'm struggling to understand how to access that value ...

How can you fix a navbar that won't stay in place?

I'm facing an issue with this example: When I scroll, the navbar moves along due to the fixed property, which affects the overall look. How can I make it disappear when scrolling, while keeping the logo intact? Can someone suggest if this layout is ...

Creating a flexible grid layout in CSS without relying on any pre-made frameworks

Suppose I am working with the following HTML structure <div class="option"> <input type="checkbox" /> <label>Checkbox</label> </div> <div class="option"> <input type="checkbox" /> <label>Chec ...

What is the best way to transfer a variable from the Cold Fusion back-end page to the HTML front-end section within a CFM file using jQuery Ajax?

My cfm file contains the necessary logic to extract a PDF and convert it into an image file. Now I am looking to pass the file path of this converted image to the front-end CFM so that it can be set as the source for the image tag below. <img id="image ...

Making adjustments to a Jquery data attribute can have a direct impact on CSS

My CSS is using a data attribute, but when I try to change it with jQuery, the change is not reflected on the screen or in the DOM. $('#new-1').data('count', 5); .fa-stack[data-count]:after { position: absolute; right: -20%; to ...

Adjust the background color of alternate elements

I am looking to customize the background colors of every other element within the structure provided below: <div class="dets"> <div>Simple Layout</div> <div>Few Pictures/Links</div> <div>Element Uniformity</div> ...

What is the best way to invert the positioning of the li elements to move upwards?

https://i.stack.imgur.com/mZaoS.png Seeking assistance on adjusting the height of bars to start from the bottom and go upwards instead of starting from the top position and going downwards. The JavaScript code below is used to generate the li elements and ...