Eliminate the extra space surrounding text when it is selected

One way to eliminate the space above and below text in this example is by adjusting the line-height and height properties.

@import url(http://fonts.googleapis.com/css?family=Roboto:300,400,500,700);
span {
    font-size: 50px;
    font-weight: 600;
    font-family: 'Roboto';

    display: inline-block;
    vertical-align: top;
    height: .75em;
    line-height: .75em;

    background-color: gold;
}

.noselect{
  user-select: none;
}

.upper{
  font-size:20px;
  position:relative;  //EDIT 2
}

.lower::selection { background: none; } //EDIT 1
<div><span class='upper'>UPPER</span></div>
<div><span class='lower'>LOWER</span></div>

However, an issue arises when selecting the text 'LOWER', as it covers part of the 'UPPER' text. This makes it difficult to select the upper text within the covered area. I have considered using z-index to bring the UPPER text to the front but wondered if there was a solution using line-height alone or another method.

I also attempted to use user-select: none to prevent text selection altogether, however, this prevented any text from being selected, including the highlighted area.

Thank you

EDIT: Making adjustments such as adding

.lower::selection { background: none; }
and position:relative; to .lower resolved this issue.

Answer №1

Upon further discussion in the comments, a proposed solution involves combining

::selection { background: none; }
with the usage of position: relative by the original poster to effectively address the issue within this specific scenario:

@import url(http://fonts.googleapis.com/css?family=Roboto:300,400,500,700);
span {
  font-size: 50px;
  font-weight: 600;
  font-family: 'Roboto';
  display: inline-block;
  vertical-align: top;
  height: .75em;
  line-height: .75em;
  background-color: gold;
}

.noselect {
  user-select: none;
}

.upper {
  font-size: 20px;
  position: relative;
}

.lower::selection {
  background: none;
}
<div><span class='upper'>UPPER</span></div>
<div><span class='lower'>LOWER</span></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

Unable to hear sound on Mozilla Firefox

I am facing an issue with playing an audio file using the audio tag inside the body element. The audio plays perfectly in Chrome once the body has finished loading, but it fails to play in Mozilla Firefox. I even attempted to download the audio file and pl ...

Attempting to create a dynamic grid layout utilizing CSS to ensure optimal responsiveness

I am working on creating a webpage layout similar to the one in the link below: The maximum width for the layout is set to 1600px. However, I am facing challenges in making it fully responsive. This is because all the boxes are given widths in percentages ...

What is causing the label's color to remain the same?

Once the page has loaded, the label color (which reads "enter your name") is supposed to change to red. However, despite the script being in place, the color remains unchanged. What could be the reason for this? SCRIPT window.onload = initiateScript; fu ...

The hover effect in CSS animations is turned up too high

Check out my Reddit news feed design: https://codepen.io/Teeke/pen/YxXXaE When hovering over articles, the headlines get displayed. But if the article text is too long, part of the headline may go off-screen. Instead of changing the line-height, I' ...

Issue with locating JavaScript controller in Angular HTML file [Node.js/Angular]

Here are all the files and folders in my project located within the same directory. If you want to check out the project on Github, you can find it here: https://github.com/JohnsCurry/learnAngular. Just to confirm, they are all in the same folder. Unfortu ...

Executing a PHP function within the same file using AJAX: A step-by-step guide

I've been exploring online tutorials to learn about ajax as it's a new technology for me. My main goal is to understand how to utilize two functions, create() and population(), in a php file. The create() function generates a form like this: ma ...

Incorporate a class into a link within the wp_list_pages function

I've been experimenting with creating a hover effect on list items using the wp_list_pages() function in my Wordpress theme. Despite my efforts, I've hit a roadblock in getting the hover effect to work properly. As a beginner in web development, ...

What is the most efficient way to fill an input field using jQuery with only two inputs?

I am working with a set of five input elements, all sharing a class called "cheque". Is there a way to selectively fill only one of these inputs using jQuery? Unfortunately, I am unable to utilize the validator jQuery library for this task. The following ...

Modification of webpage content when URL hash changes

I am experiencing an issue with my script. Whenever I type something with a spacebar, like google map, it automatically changes in the input box to google+map, which is not ideal. Additionally, when I try to submit again, it further complicates the situati ...

Avoid using CSS browser prefixes when selecting plugins or addons

I tried searching on Google for a solution, but I couldn't find anything! Is there a trick or library available to avoid repeating CSS extensions for browsers? Instead of using -webkit-, -moz-, and -o- prefixes? -webkit-animation: NAME-YOUR-ANIMATI ...

Preventing page refresh when typing in a form input: Tips and tricks

I'm currently puzzled by a small issue. In my web application, I have a chat box that consists of an input[type='text'] field and a button. My goal is to send the message to the server and clear the input field whenever the user clicks the b ...

It appears that jQuery is interfering with the CSS webkit hover effect and causing it

I've implemented multiple divs with the class .note and applied the following CSS code to them: .note{ background: #eff9ff; -webkit-transition: background .5s; } .note:hover{ background ...

The text area is not increasing in size when using the " " for a new line

I have a code that enables Autocomplete input to add values chosen from the list to a textarea, with each value separated by a new line. While manually adding rows to the textarea by pressing Enter allows it to expand and fill in the new row and value, us ...

Assigning a specific data type value to an element as it enters the top of the viewport

I have a unique color code stored for each section, and when a section reaches the top of the screen (specifically -180px for the header), I want to dynamically change the text color of the header element as you scroll through the sections. Despite no erro ...

Use JQuery to constantly monitor for any changes in HTML Div

I am dealing with a browser-based chat application where separate div elements are generated for each chat conversation. An external module oversees the entire chat process, and I do not have access to its underlying code. It is important to note that the ...

What is the best way to input a value into the search bar within an Iframe?

I've created a basic website using node and a pug page where I inserted an iframe from wikipedia.com. My goal is to have the search input in the Wikipedia iframe populated with a value based on a button click within the same pug page. Is this feasible ...

Video background in JavaScript

Can anyone help me with using the jQuery plug in videoBG to fill a top div with a video that has 100% width and height? I want the #top-video to fill the #top-content but nothing is showing up. Any suggestions would be appreciated. Thanks! <!DOCTYPE ...

Build a table with consistent columns and flexible rows utilizing CSS and HTML

Just starting out with CSS/HTML and looking for guidance on creating a table with a variable number of rows. Any tips on how to achieve this using CSS? I'm aiming to replicate a table with a similar structure, but struggling to figure out how to defin ...

Using JavaScript or another programming language to relocate files on a desktop

Is there a way to move pictures from the desktop to a folder called "pictures" using JavaScript? If not, perhaps through Ajax, PHP, or HTML? Is it possible to achieve this task by any means? Edit: I prefer not to do it on my server for web users. Is it f ...

`finding difficulty in presenting components in react`

I am encountering issues with an outdated version of CRUD where some codes are no longer functioning as expected. Visual Studio Code does not flag them as errors, so the page continues to load but my content box remains empty. Can someone assist me with th ...