NoChance
NoChance

Reputation: 5752

How to select loose text between tags

In cases you find text in HTML code that is not embeded in any tags such as:

<sometag> ex1 </sometag>

I know many ways to select the text using a p tag or using a class or an id on the div, etc. but my question is: how do you write a css rule for this code as is? Also, I want the rule to be specific for the text so:

sometag {...}

is not desirable because it is more generic.

Thanks for all in advance.

Upvotes: 2

Views: 5864

Answers (4)

WEB LAZER
WEB LAZER

Reputation: 31

You can use html:

html {
 color: red;
}

or body:

body {
 color: red;
}

or select everything with *:

* {
 color: red;
}

Upvotes: 0

nullability
nullability

Reputation: 10675

Text nodes cannot have styles applied to them, so anything you need style applied to must be in an element. If you want some of the text inside of your element to be styled differently, wrap it in a span or div, for example.

The wrapping element can then be selected using CSS based on the element type, ID, class, or document order, but not by content.

You can, however, use jQuery to select by content using the :contains selector: http://api.jquery.com/contains-selector/

Upvotes: 1

Carrie Kendall
Carrie Kendall

Reputation: 11225

Simply put, no. There is currently no selector to select TEXT only with CSS. You can (and should) use markup to get the result you're looking for:

HTML:

<sometag>
    <span>text here</span>
    <img src='..' />
    some other stuff i don't want to select in my CSS
</sometag>

CSS:

sometag span{
    /* do something here */
}

Upvotes: 2

Vinicius Lima
Vinicius Lima

Reputation: 544

If "your tag" is a reserved name, like input, div, etc, you can use this:

e.g

sometag {
  border-width: 1px;
}

of course, every tag can receive an id or class, so, you still use for id or class:

id

<sometag id="myid"></sometag>

#myid {
  border-width: 1px;
}

class

<sometag class="myclass"></sometag>
.myclass {
  border-width: 1px;
}

Upvotes: 0

Related Questions