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?
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)
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.
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.
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.
What you can do is to wrap the text in a span element, and then set the background and cursor properties to it.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With