Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css content without pseudo-class

Tags:

css

I wrote a class with content but seems content just work for pseudo-classes.

How to modify my code?

http://jsfiddle.net/fuRKC/

button.follow{
    content: 'Follow';
}

button.followed{
    content: 'Unfollow';
}
like image 425
werva Avatar asked Sep 24 '13 08:09

werva


People also ask

How do you use non pseudo-elements in CSS?

The :not() pseudo-class requires a comma-separated list of one or more selectors as its argument. The list must not contain another negation selector or a pseudo-element.

Which is not a pseudo class in CSS?

The :not pseudo-class represents an element that is not represented by its argument.

Is there a class before pseudo?

::before (:before) In CSS, ::before creates a pseudo-element that is the first child of the selected element. It is often used to add cosmetic content to an element with the content property. It is inline by default.

Does CSS have a pseudo class?

The :has() CSS pseudo-class represents an element if any of the selectors passed as parameters (relative to the :scope of the given element) match at least one element.


2 Answers

By current CSS specifications, the content attribute only applies to :before and :after pseudo-elements. There is an old proposed extension, CSS3 Generated and Replaced Content Module, which would allow the property to apply to real elements, too. The proposal is however ten years old. It has only been implemented in Opera, except for the special case where the content value is an URL expression, interpreted as referring to an image – this is supported by Chrome and Safari, too.

Moreover, even the limited support wouldn’t do much good here, since the extension does not seem to work on Chrome when the element is a button, and on Opera, the generated content appears as such, not within a button widget.

Since your button elements have empty content, you don’t really need the extension, since for empty content, adding content is equivalent to replacing content. So in this case, you would just need to use button.follow:before, as in Kippie’s answer. Note, however, that this means that the button appears as completely empty when CSS is disabled.

So whatever might be the reason for using generated content, instead of button text in HTML, a more robust approach would be to change content with JavaScript. JavaScript can be disabled, of course, but you can use actual button text as fallback, e.g. <button id=follow>Follow</button>, and replace the content when JavaScript is enabled.

like image 146
Jukka K. Korpela Avatar answered Oct 31 '22 16:10

Jukka K. Korpela


You are correct. content only works with the :before and :after pseudo classes. Just make your css like this:

button.follow:before{
    content: 'Follow';
}

button.followed:before{
    content: 'Unfollow';
}
like image 25
Kippie Avatar answered Oct 31 '22 17:10

Kippie