Misha M
Misha M

Reputation: 11299

Style HTML element based on its title using CSS

Is it possible to apply a style to an HTML element using only its title as a unique identifier? For example:

<div class="my_class">
    <a href="some site" title="MyTitle">My Link</a>
</div>

I would like to write a rule that will apply only to link element within a div of class my_class and the link title MyTitle.

I do not have the ability to change page layout, however, I can use a custom CSS file that is included automatically.

Thank you

Upvotes: 20

Views: 27822

Answers (5)

ezra
ezra

Reputation:

Although it is possible, using attribute selectors (see http://www.w3.org/TR/CSS2/selector.html#attribute-selectors ) Internet Explorer 6 does not support it (see http://kimblim.dk/css-tests/selectors/ )

An example from the W3C site: H1[title] { color: blue; }

Upvotes: 1

womp
womp

Reputation: 116977

CSS specifications identify a number of "selectors" that may help you here. For example, you could apply a rule such as the following:

.my_class a[title="MyTitle"]
{
   ...
}

You can see a detailed specification of CSS "selectors" here:

http://www.w3.org/TR/2001/CR-css3-selectors-20011113/

Upvotes: 3

Ward Werbrouck
Ward Werbrouck

Reputation: 1462

What you are looking for is css attribute selectors:

http://www.w3.org/TR/CSS2/selector.html#attribute-selectors

a[title] { ... }

Upvotes: 3

i_am_jorf
i_am_jorf

Reputation: 54600

Yes you can:

http://www.w3.org/TR/CSS2/selector.html#attribute-selectors

You would say A[title="MyTitle] I believe.

Upvotes: 3

John Feminella
John Feminella

Reputation: 311496

It sure is, using what's called attribute selectors; these are part of CSS2. In your particular case, you'd use:

div.my_class a[title="MyTitle"] { color: red; }

You can read more about attribute selectors here: http://www.w3.org/TR/CSS2/selector.html#attribute-selectors

Upvotes: 40

Related Questions