EzDevInfo.com

css-selectors interview questions

Top css-selectors frequently asked interview questions

What is the difference between * and *|* in CSS?

In CSS, * will match any element.

Frequently, *|* is used instead of * to match all elements. This is generally used for testing purposes.

What is the difference between * and *|* in CSS?


Source: (StackOverflow)

Which characters are valid in CSS class names/selectors?

What characters/symbols are allowed within CSS class selectors? I know that the following characters are invalid, but what characters are valid?

~ ! @ $ % ^ & * ( ) + = , . / ' ; : " ? > < [ ] \ { } | ` #

Source: (StackOverflow)

Advertisements

CSS Selector that applies to elements with two classes

Is there a way to select an element with CSS based on the value of the class attribute being set to two specific classes. For example, let's say I have 3 divs:

<div class="foo">Hello Foo</div>
<div class="foo bar">Hello World</div>
<div class="bar">Hello Bar</div>

What CSS could I write to select ONLY the second element in the list, based on the fact that it is a member of both the foo AND bar classes?


Source: (StackOverflow)

Select element based on multiple classes

I have a style rule I want to apply to a tag when it has two styles. Is there any way to perform this without JavaScript? In other words:

<li class='left ui-class-selector'>

I want to apply my style rule only if the li has both .left and .ui-class-selector classes applied.


Source: (StackOverflow)

Is there a "previous sibling" CSS selector?

+ is for the next sibling. Is there an equivalent for the previous sibling?


Source: (StackOverflow)

Is there a CSS parent selector?

I would like to select the <li> element that is a parent (which immediately precedes the anchor tag, if that helps…) according to some attribute of the anchor tag.

i.e. my CSS would be something like this:

li < a.active {
    property: value;
}

Obviously there are ways of doing this with JavaScript but I'm hoping that there is some sort of workaround that exists native to CSS 2.

The menu that I am trying to style is being spewed out by a CMS so I can't move the active tag to the <li> element... (unless I theme the menu creation module which I'd rather not do)

Any ideas?


Source: (StackOverflow)

Why do browsers match CSS selectors from right to left?

CSS Selectors are matched by browser engines from right to left. So they first find the children and then check their parents to see if they match the rest of the parts of the rule.

  1. Why is this?
  2. Is it just because the spec says?
  3. Does it affect the eventual layout if it was evaluated from left to right?

To me the simplest way to do it would be use the selectors with the least number of elements. So IDs first (as they should only return 1 element). Then maybe classes or an element that has the fewest number of nodes — e.g. there may only be one span on the page so go directly to that node with any rule that references a span.

Here are some links backing up my claims

  1. http://code.google.com/speed/page-speed/docs/rendering.html
  2. https://developer.mozilla.org/en/Writing_Efficient_CSS

It sounds like that it is done this way to avoid having to look at all the children of parent (which could be many) rather than all the parents of a child which must be one. Even if the DOM is deep it would only look at one node per level rather than multiple in the RTL matching. Is it easier/faster to evaluate CSS selectors LTR or RTL?


Source: (StackOverflow)

CSS '>' selector; what is it? [duplicate]

Possible Duplicate:
What does “>” mean in CSS rules?

I've seen the "greater than" (>) used in CSS code a few times, but I can't work out what it does. What does it do?


Source: (StackOverflow)

What does the "~" (tilde/squiggle/twiddle) CSS selector mean?

Searching for the ~ character isn't easy. I was looking over some CSS and found this

.check:checked ~ .content {
}

What does it mean?


Source: (StackOverflow)

Select elements by data attribute in CSS

Is it possible to select elements in CSS by their HTML5 data attributes (for example, data-role)?


Source: (StackOverflow)

Multiple NOT selector

Creating simple CSS to apply to all inputs (now with HTML5, there are a lot), except radio and checkbox.

Many people have shown that you can put multiple arguments in :not, but using type doesn't seem to work anyway I try it.

form input:not([type="radio"], [type="checkbox"]) {
  /* css here */
}

Any ideas?


Source: (StackOverflow)

What does the "+" (plus sign) CSS selector mean?

For example:

p + p {
  /* Some declarations */
}

I don't know what the + means. What's the difference between this and just defining a style for p without + p?


Source: (StackOverflow)

CSS selector for first element with class

I have a bunch of elements with a class name red:

<p class="red"></p>
<div class="red"></div>

I can't seem to select the first element with the class="red" using the following CSS rule:

.red:first-child{
  border:5px solid red;
}

What is wrong in this selector and how do I correct it?

UPDATE:

Thanks to the comments, I figured out that the element has to be the first child of its parent to get selected which is not the case that I have. I have the following structure:

<div class="home">
  <span>blah</span>
  <p class="red">first</p>
  <p class="red">second</p>
  <p class="red">third</p>
  <p class="red">fourth</p>
</div>

and this rule fails as mentioned in the comments:

.home .red:first-child{
  border:1px solid red;
}

How can I target the first child with class red?


Source: (StackOverflow)

Select every Nth element in CSS

Is it possible to select, say, every fourth element in a set of elements?

Ex: I have 16 <div> elements... I could write something like.

div:nth-child(4),
div:nth-child(8),
div:nth-child(12),
div:nth-child(16)

is there a better way to do this?


Source: (StackOverflow)

What does the ">" (greater-than sign) CSS selector mean?

For example:

div > p.some_class {
  /* Some declarations */
}

What exactly does the > sign mean?


Source: (StackOverflow)