tags:

views:

1059

answers:

3

Can a CSS rule select the portion of a box which contains text (or an inline block)?

For example, an HTML fragment like <p>The quick brown fox jumped over the lazy dog</p> might be laid out like this:

+--------------------------+
|  The quick brown fox     |
|  jumped over the         |
|  lazy dog                |
+--------------------------|

If I create a CSS rule like p { background: red } then the whole box/rectangle will have a red background, including the "whitespace" at the end of each line.

Is there a way to specify a selector such that, on each line, only the actual text has a red background?

I notice that by default the cursor changes from 'arrow' to 'i-beam' when it's actually over text; when it's elsewhere within the paragraph box, not over text, then it's an arrow not an i-beam.

If I specify an explicit rule like p { cursor: crosshair } then it's effective everywhere within the rectangular box. Again, is it possible to have a rule that's selected only when the cursor is actually over text?

+3  A: 

What you can do is to wrap the text in a span element, and then set the background and cursor properties to it.

remi
+1  A: 

There's no way to do exactly what you want as far as I am aware because CSS selectors do not match text nodes. You'd have to wrap the text in a span and then style the span.

mishac
+1  A: 

despite css is a god idea , it´s actual implementation is ridiculous, it seems like it dont match whit the dom, end each broswer (render) css in thiferent way. it is something like you 2+2 and in firefox u get 3, ie get 6, etc... this is anything but an standart

PDT: span is the only and bad way.