Tips for changing the appearance of selectable text in a way that differs from the displayed text

I am seeking to implement a unique design for a piece of text that will not show a particular character visually, but includes the character for other functional purposes. For instance, the interface might display something like

<sup><sub>$</sub></sup>123<sup><sub>45</sub></sup>
, while the actual text content is $123.45.

  • If you highlight and copy the displayed text using Ctrl+C, the clipboard should store the underlying text.
  • If you right-click on the displayed text, the context menu should offer an option such as Search Google for "<em>X</em>" where X represents the hidden text (if supported by the browser).
  • Both screen readers and search engine crawlers should be able to access the concealed text.

This is the approach I'm currently using:

body {
  font-family: sans-serif;
}

.price {
  line-height: 4.4rem;
  font-size: 4rem;
}

.cc, .cf {
  font-size: 0.6em;
  vertical-align: super;
  margin: 0 2px;
}

.cs {
  opacity: 0;
  margin: 0 0 0 -.28em;
}

.ci {
  font-size: 4.4rem;
}
<span class="price" aria-label="$123.45">
  <span class="cc">$</span><span class="ci">123</span><span class="cs">.</span><span class="cf">45</span>
</span>

However, I have concerns about using an invisible element with opacity: 0—it feels somewhat deceptive and could possibly be flagged by certain advanced crawlers. Additionally, the -.28em adjustment may not be universally effective across different fonts or font sizes.

I am considering changing the style of .cs to display: inline-block; width: 0 to address the margin issue, but this could lead to challenges when selecting parts of the text. Keeping .cs inline appears crucial in ensuring seamless selection of the entire text block.

Thus, my question revolves around determining the most semantically appropriate method, utilizing HTML5 and CSS, to manage distinct content for visual and interactive purposes.

Answer №1

If you're looking for a different approach, consider using a font size of 0 for the period.

.cs {
 font-size: 0;
}

This method will ensure that the information remains accessible on both screen readers and when copied to the clipboard.

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

Calling the ColdFusion component with $.ajax results in returning plain text instead of HTML

Apologies if this question has already been addressed. I am working on setting up a pagination system that involves sending variables to a ColdFusion function query in order to fetch a specific number of elements and display those records on a web page wi ...

What is the best way to add a space line after a word in a sentence using HTML and CSS?

Inquiry: Sentence that needs completion This question pertains to HTML/CSS. I am looking to replicate the image provided, where I need to insert a line to fill the empty space after certain words. The length of the words may vary. For instance, if the ...

Using a series of identical divs to dynamically update the image URL

Greetings! I am a newcomer to the world of web development and I have decided to hone my skills by creating a small website for my mother! My goal is to replicate a specific div multiple times while changing only the image URL and the heading caption. < ...

Transform a hexadecimal string into a hexadecimal color representation within SCSS

I have a plethora of colors stored in JSON format. I am utilizing rootbeer with gulp to convert them into Sass maps for processing by SCSS: { "blue": "33A2FF" } into $colors: ( "blue": "33A2FF" ); I am able to use ...

Identify the descendant of the `<li>` tag

I need help creating a interactive checklist. The idea is that when you click on an item in the list, a hidden child element should become visible. <div id="list_one"> <h2>LIST TITLE</h2> <div id="line"></div> < ...

Creating and adding nested div elements with the power of JavaScript

My goal is to utilize JavaScript for updating the CSS layout as the webpage loads. Below is the code I have been working on: var container = 0; // Total UI Container var containerTitle = 0; // Title Container var article = 0; var articleTitle = 0; var ...

CSS3 Rounded Corners

What is the most effective method to create this style of border using only CSS3? PS: The width should be 100%, matching the size of the screen. ...

Having trouble with JavaScript not working when clicking an image and toggling a div?

Why isn't the onclick image and toggle div functionality working with JavaScript? I made the change from: <input type="button" id="Showdiv1" name="Showdiv1" value="Show Div 1" onclick="showDiv('div1')" /> to: <img src="https://d ...

Unable to reset disabled styling for CSS select box

My Windows 8 app consists of two pages: Page1.html and Page2.html. Page1 references ui-light.css and page1.css, while Page2 references ui-light.css and page2.css. In the ui-light.css file, there is a rule defined for the disabled state of select boxes. I ...

Single-Page Design Ascend

I'm facing a dilemma with my one-page website layout project. Instead of the conventional scroll down effect, I'd like to implement a design similar to www.xsbaltimore.com where users have to scroll up to view other sections. However, I'm un ...

Using pseudoclasses in combination with BEM: A beginner's guide

How can I effectively utilize pseudo-classes while adhering to the principles of BEM methodology? I'm facing a specific challenge with creating a custom checkbox functionality. Normally, I would use the input:checked + label selector. However, when t ...

How to create collapsible and expandable HTML table columns using the <th> element?

I've searched far and wide for a solution to this conundrum and come up empty-handed. I have a HTML table with a total of 13 columns. The 7th column, labeled Number of Tops, is a sum of columns 8-11: # Short-sleeve, # Long-sleeve, # Sweaters, # Jacket ...

Retrieve the value from another select element

Is there a way to create a function in pure JavaScript that changes the selected options in two select tags based on each other? For example, if I choose a French word in one select tag, the English equivalent automatically changes in the other select tag ...

Enhance user experience with JQUERY - MouseEnter, Focus, Hover, and Blur

Here is the current code snippet for showing and hiding a footer banner. Everything seems to be working fine except for an issue with MouseOver functionality. The MouseOver feature works as expected, but when a user clicks within the area, the highlight d ...

What are the steps to generate an image upon clicking a button?

Here's what I have now: <script> function display() { var element = document.getElementById('display'); element.innerHTML='<img src=\"display.png\">'; } </script> And the HTML code: <input type ...

Is it possible to eliminate the *:after effect for specific elements in my CSS?

I struggle with CSS, so I decided to use a library that includes the following CSS code: *, *:before, *:after { box-sizing: inherit ; } While this code works well for some views, it messes up others and is unnecessary for some. For instance, in this b ...

Is there a way to determine in JavaScript whether an HTML element contains no visible data, such as text or images?

In my web application, I have a feature that eliminates specific elements from a webpage. After the initial filtering, I am looking to remove all divs that previously held content but no longer do. For instance, after the first pass, an element may appear ...

What is the best way to eliminate the default background color applied to the body automatically?

I'm currently developing a Covid-19 tracking web application and I am using chartJS to showcase data from an API. In my CSS, I have assigned a background color to all elements with an asterisk (*), but for some reason there are empty spaces around the ...

Matching an apostrophe in an AutoHotkey script using regex

My autohotkey script is designed to search for a word in a bilingual dictionary when I double click on any word within a webpage. However, I am facing an issue where the script also copies the characters preceding the apostrophe when clicking on words like ...

The table does not move up or down when using the mousewheel over the rows, but only when

I'm encountering an issue with a table inside a div that has overflow-y: scroll applied to it. While the scrollbar appears when there is content overflow, I am unable to scroll using the scrollwheel. It only works when the cursor is directly on top o ...