Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a CSS selector to select text (inline blocks) within a rectangular box?

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?

like image 644
ChrisW Avatar asked Aug 14 '09 02:08

ChrisW


People also ask

What are the 3 main ways we can select an item through CSS?

Simple selectors (select elements based on name, id, class) Combinator selectors (select elements based on a specific relationship between them) Pseudo-class selectors (select elements based on a certain state)

What is inline-block CSS?

inline-block It's formatted just like the inline element, where it doesn't start on a new line. BUT, you can set width and height values. block The element will start on a new line and occupy the full width available. And you can set width and height values.

What is Section element selector in CSS?

The element selector is a way to select all the elements with a given tag name in a document, and apply the same styles to each element with the tag name. Note that you should only write the tag name, and not write brackets around the tag name — h1 , not <h1> . 2.

What is display inline CSS?

The display CSS property sets whether an element is treated as a block or inline element and the layout used for its children, such as flow layout, grid or flex.


2 Answers

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

like image 77
remi Avatar answered Sep 22 '22 02:09

remi


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.

like image 43
mishac Avatar answered Sep 19 '22 02:09

mishac