Jump to content

Span destroys selection background


Traveller

Recommended Posts

One Definition and Usage for the HTML <span> Tag on w3schools is:

The <span> tag provides no visual change by itself.

 

This may be true for unselected text, but it is wrong for selected texts where selection background styles are applied to.

Here is a Fiddle were you can see what I mean. Please select the text in the Result window.

 

Is there a way to prevent this behaviour?

Link to comment
Share on other sites

Thank you both for your quick replies.I tried to create 2 new fiddles based on your suggestions.

 

According to Ingolme, "Apply the selection style to the span elements as well", I created this fiddle.

This seems to me to be a good workaround.

 

According to dsonesuk, "Move .selectionBgYellow::-moz-selection to a new outer container", I created another fiddle.

I don't know why, but the .selectionBgYellow::-moz-selection has no effect on the outer div.

 

 

Link to comment
Share on other sites

Thank you dsonesuk.

The space between .selectionBgYellow and ::-moz-selection makes it to a descendent selector, doesn't it?

I didn't know that before. I'm a beginner to CSS :sorry: .

Here I have tried this concept with 2 different background colors.

A realy nice solution.

The descendent selector makes the style to be applied to the span like to any other element in the same container.

Link to comment
Share on other sites

I’m satisfied with the proposed solutions.However, it’s still my opinion that the browsers have a faulty implementation of the definition “The <span> tag provides no visual change by itself”.When you have access to the sources of the web pages, it’s no problem to apply the proposed workarounds.When the sources are less accessible, e.g. when you make a browser add-on, I guess you would need JavaScript and libraries like JQuery just to implement the workarounds.

Link to comment
Share on other sites

Depends how you read

 

The ::selection selector matches the portion of an element that is selected by a user.

 

If you go by this! the portion of element you applied p::selection to was the paragraph element only, the span is a separate element which you had not targetted, While using .selectionBgYellow ::-moz-selection would target ALL child descendants elements of parent using class '.selectionBgYellow'. If you where to have <em>, <u> tags as well in your first example you would have the same problem.

 

https://jsfiddle.net/ox749jem/1/

Edited by dsonesuk
Link to comment
Share on other sites

Your jsfiddle shows very clearly, that other elements also have the same behavior like the span.

It’s not what I was expecting, but that's the way it is.Thank you very much for your help.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...