Questions tagged [pseudo-class]

CSS pseudo-classes allow for more specific selection of elements within a document based upon an element's status, location in the DOM tree structure, user action, or other factors.

The pseudo-class (literally, "false-class") of CSS allows for more specific selection of elements within a document based upon an element's status, location in the DOM tree structure, user action, or other factors. Styling can then be applied to just those elements meeting the pseudo-class qualifications, similar to how one uses a CSS class to target an element. They are not to be confused with pseudo elements.

List of CSS Pseudo-Classes

This is a list of all pseudo-classes that are currently included in CSS specifications (not any editor's drafts or upcoming specifications).

  • :link and :visited
    Selects links which are either unvisited or visited, respectively, in order to apply additional styles to them. Visited links are often shown in an alternate color to signify to the end-user that they've already visited that page previously.

  • :active
    Selects an element which is currently active, or in the process of being activated. This occurs after a user presses down on a link, button, or other activating element, and before the user releases the click.

  • :hover
    Selects an element which is currently being hovered over by the user so that additional styles may be applied to it. Many elements which perform some action when clicked have a "hover effect" to signify to the end-user that it does something. It can also be used to reveal additional information to the user and many other purposes.

  • :focus
    Selects an element which currently has focus. For example: the user's cursor is currently positioned inside the input element (while they may be typing, etc).

  • :enabled and :disabled
    Selects a user interface element which is either enabled or disabled so that additional styles may be applied to them.

  • :checked
    Selects a radio button or checkbox which is currently in the "checked" state so that additional styles may be applied to it. Keep in mind that many browsers do not allow you to apply styles directly to the radio button or checkbox. This is mostly used to select additional elements immediately after the boxes when in the checked state.

  • :empty
    Selects an element which has no children or text inside it (and is hence, empty). Keep in mind that this cannot apply to replaced elements which do not have actual content (such as images and inputs).

  • :root
    Selects the root of the document.

  • :target
    Selects an element which is the target of the referring URI. This occurs when an element has an ID matching the comment part of the URL (#some-place-in-the-document).

  • :nth-child(), :nth-of-type(), :nth-last-child(), :nth-last-of-type(), :first-child, :last-child, :first-of-type, :last-of-type, :only-child, and :only-of-type
    These are all structural pseudo-classes which select elements based on their positioning within their parent element. They are useful for creating zebra-striped rows, styling specific elements which always appear first or last, and many other uses.

  • :lang()
    Selects an element which is using the language specified.

Replaced Elements

Any element whose appearance and/or dimensions are determined by some external resource is considered to be a replaced element. Some pseudo-classes cannot be applied to replaced elements because they have no "content" or get replaced with something (such as user interface controls). Replaced elements include images (<img>), inline frames (<iframe>), line breaks (<br>), horizontal rules (<hr>), plugins (<object>), form elements (<button>, <textarea>, <input>, and <select>), videos (<video>), audio sounds (<audio>), and canvases (<canvas>). Any other element is considered to be a non-replaced element.

968 questions
406
votes
7 answers

How can I write a ':hover' condition for 'a:before' and 'a:after'?

How can I write :hover and :visited condition for a:before? I'm trying a:before:hover, but it's not working.
Jitendra Vyas
  • 148,487
  • 229
  • 573
  • 852
379
votes
7 answers

What is the difference between :focus and :active?

What is the difference between the :focus and :active pseudo-classes?
Jitendra Vyas
  • 148,487
  • 229
  • 573
  • 852
206
votes
3 answers

Css pseudo classes input:not(disabled)not:[type="submit"]:focus

I want to apply some css for inputs elements and I want to do that only for inputs that are not disabled and are not submit type, below css is not working, maybe if someone can explain me how this must be added…
BurebistaRuler
  • 6,209
  • 11
  • 48
  • 66
170
votes
4 answers

CSS Pseudo-classes with inline styles

Is it possible to have pseudo-classes using inline styles? Example: Google I know the above HTML won't work but is there something similar that will? P.S. I know I should use…
Web_Designer
  • 72,308
  • 93
  • 206
  • 262
154
votes
12 answers

Setting CSS pseudo-class rules from JavaScript

I'm looking for a way to change the CSS rules for pseudo-class selectors (such as :link, :hover, etc.) from JavaScript. So an analogue of the CSS code: a:hover { color: red } in JS. I couldn't find the answer anywhere else; if anyone knows that this…
user39882
  • 1,549
  • 2
  • 11
  • 4
152
votes
2 answers

:after vs. ::after

Is there any functional difference between the CSS 2.1 :after and the CSS 3 ::after pseudo-selectors (other than ::after not being supported in older browsers)? Is there any practical reason to use the newer specification?
Parker Ault
  • 3,268
  • 3
  • 22
  • 24
148
votes
4 answers

CSS3 :unchecked pseudo-class

I know there is an official CSS3 :checked pseudo-class, but is there an :unchecked pseudo-class, and do they have the same browser support? Sitepoint's reference doesn't mention one, however this whatwg spec (whatever that is) does. I know the same…
Web_Designer
  • 72,308
  • 93
  • 206
  • 262
136
votes
4 answers

Is there an opposite CSS pseudo-class to :hover?

Is there a pseudo-class in CSS to specify :not(:hover) Or is that the only way to specify an item that is not being hovered? I went through several CSS3 references, and I see no mention of a CSS pseudo-class to specify the opposite of :hover.
135
votes
6 answers

Indent starting from the second line of a paragraph with CSS

How can I indent starting from the second line of a paragraph? I've tried p { text-indent: 200px; } p:first-line { text-indent: 0; } and p { margin-left: 200px; } p:first-line { margin-left: 0; } and (with position:relative;) p { …
Reuben
  • 2,701
  • 6
  • 31
  • 47
121
votes
4 answers

CSS :selected pseudo class similar to :checked, but for

Is there a way to style the currently selected