CSS Interview Questions and Answers Set
CSS Interview Questions and Answers Set
CSS Interview Questions and Answers Set
Set - 1
What is CSS?
1. CSS stands for Cascading Style Sheets and is a simple styling language which allows
attaching style to HTML elements. Every element type as well as every occurrence of a
specific element within that type can be declared an unique style, e.g. margins,
positioning, color or size.
3. CSS is a language that adds style (colors, images, borders, margins…) to your site. It’s
really that simple. CSS is not used to put any content on your site, it’s just there to take
the content you have and make it pretty. First thing you do is link a CSS-file to your
HTML document. Do this by adding this line:
The line should be placed in between your <head> and </head> tags. If you have several
pages you could add the exact same line to all of them and they will all use the same
stylesheet, but more about that later. Let’s look inside the file “style.css” we just linked
to.
h1 {
font-size: 40px;
height: 200px;
}
.warning {
color: Red;
font-weight: bold;
}
#footer {
background-color: Gray;
}
4. Cascading Style Sheets (CSS) is a simple mechanism for adding style (e.g. fonts,
colors, spacing) to Web documents. This is also where information meets the artistic
abilities of a web-designer. CSS helps you spice up your web-page and make it look neat
in wide variety of aspects.
1. By setting the properties margin-left and margin-right to auto and width to some
explicit value:
In this case, the left and right margins will each be four ems wide, since they equally split
up the eight ems left over from (30em - 22em). Note that it was not necessary to set an
explicit width for the BODY element; it was done here to keep the math clean.
Another example:
If background and color should always be set together, why do they exist as separate
properties?
There are several reasons for this. First, style sheets become more legible -- both for
humans and machines. The background property is already the most complex property in
CSS1 and combining it with color would make it even more complex. Second, color
inherits, but background doesn't and this would be a source of confusion.
What is class?
Class is a group of 1) instances of the same element to which an unique style can be
attached or 2) instances of different elements to which the same style can be attached.
1) The rule P {color: red} will display red text in all paragraphs. By classifying the
selector P different style can be attached to each class allowing the display of some
paragraphs in one style and some other paragraphs in another style.
2) A class can also be specified without associating a specific element to it and then
attached to any element which is to be styled in accordance with it's declaration. All
elements to which a specific class is attached will have the same style.
To classify an element add a period to the selector followed by an unique name. The
name can contain characters a-z, A-Z, digits 0-9, period, hyphen, escaped characters,
Unicode characters 161-255, as well as any Unicode character as a numeric code,
however, they cannot start with a dash or a digit. (Note: in HTML the value of the
CLASS attribute can contain more characters). (Note: text between /* and */ are my
comments).
CSS
P.name1 {color: red} /* one class of P selector */
P.name2 {color: blue} /* another class of P selector */
.name3 {color: green} /* can be attached to any element */
HTML
<P class=name1>This paragraph will be red</P>
<P class=name2>This paragraph will be blue</P>
<P class=name3>This paragraph will be green</P>
<LI class=name3>This list item will be green</LI>
It is a good practice to name classes according to their function than their appearance;
e.g. P.fotnote and not P.green. In CSS1 only one class can be attached to a selector. CSS2
allows attaching more classes, e.g.:
P.name1.name2.name3 {declaration} <P class="name1 name2 name2">This paragraph
has three classes attached</P>
What is grouping ?
Grouping is gathering (1) into a comma separated list two or more selectors that share the
same style or (2) into a semicolon separated list two or more declarations that are
attached to the same selector (2).
1. The selectors LI, P with class name .first and class .footnote share the same style, e.g.:
LI {font-style: italic}
P.first {font-style: italic}
.footnote {font-style: italic}
To reduce the size of style sheets and also save some typing time they can all be grouped
in one list.
LI, P.first, .footnote {font-style: italic}
2. The declarations {font-style: italic} and {color: red} can be attached to one selector,
e.g.:
H2 {font-style: italic}
H2 {color: red}
and can also be grouped into one list:
H2 {font-style: italic; color: red}
It is a good idea to avoid naming classes where the only difference is the case, for
example:
div.myclass { ...}
div.myClass { ... }
If the DOCTYPE or XML declaration is ever removed from your pages, even by mistake,
the last instance of the style will be used, regardless of case.
//////////////////////////////////////////////////////////////////////
Question:
What is a DTD? What DTD do you generally use? Why? Pros and cons.
Answer
Answer Rating:
Question
Font sizes should be declared using relative measurement values, such as ems, via a style
sheet, without the use of the term !important. There are issues with browser font size
enlarging which can be rectified via CSS.
Answer Rating
Question
a) What are the possible values for the display attribute that are supported by all
browsers?
b) What is the default value for the display attribute for the image element? (what is the
difference between inline and block level elements)
c)What does display: run-in do?
d) Difference between "visibility:hidden" and "display:none"? What are the pros and cons
of using display:none?
Answer
Answer Rating
1. Doesn’t know
2. Knows the answer to A
3. Knows the answer to A and D
4. Knows the answer to A, B and D
5. Knows the answer to C too!
Question
Answer
Answer Rating
1. Doesn’t know
2. Knows 4 out of 5 answers in part A
3. Knows A & B
4. Knows A, B & C
5. Knows A-D
6. Knows E too
Question:
Write a snippet of CSS that will display a paragraph in blue in older browsers, red in
newer browsers, green in IE6 and black in IE7
Possible Answer:
#content p{color:blue}
html>body #content p {color:red}
* html #content p{color:green}
html>body #content p {*color:black;}
Answer Rating
1. Doesn’t know
2. Knows how to declare one color, but no hacks
3. knows the html>body hack and * html hack
4. Knows all the hacks, but doesn’t validate or uses conditional comments in the
HTML
5. Gives you the right answer and explains why the CSS won’t validate, or, uses a
valid hack, other than conditional IE comments, instead of the above answer.
Question:
Answer:
Answer Rating:
Question
Are the following all equal, and, if so, what would your code look like to make the
following all equal the same thing:
alert(document.forms["myform"].elements["field"].value);
alert(document.forms[1].elements[1].value);
alert(document.myform.field.value);
answer:
<form name="myform" method="post" action="something">
<input name="anything" value="anything" type="something" />
<input name="field" value="something" type="something" />
</form>
Answer includes knowing that the form is the second form on the page, and that the
field input element is the second element within that form.
Answer Rating
1. Doesn’t know how to code forms and doesn’t know that the first index of an array
is 0.
2. Knows either how to code forms with valid XHTML or that array starts at 0, but
not both.
3. Knows how to code forms but not correctly, but omits something like doesn’t
know that the form needs to be the second one on the page, and the element is the
second one in the form. Would know how to do it if they actually put thought into
it.
4. Codes the form correctly, but uses ID instead of name
5. Codes everything correctly
Question:
Possible Answer:
newParagraph = document.createElement('p');
newParagraph.setAttribute('class', 'myClass');
newText = document.createTextNode('this is a new paragraph');
newParagraph.appendChild(newText);
myLocation = document.getElementById('parent_of_new_paragraph);
myLocation.appendChild(newParagraph);
Answer Rating:
Q: How do you organize your CSS? How do you come up with id and class names (what
naming conventions do you use)?
A: While there are no right answers, there are best practices. Issues to look for are not
having div mania, no inline CSS, no presentational markup, minimal use of classes,
understanding the CSS cascade.
Q: What do you think of hacks? When should you use them? If you use them, how do
you maintain them? What can be done to avoid needing to use box-model hacks? (if they
aren’t pros, you can ask them what is the issue with x-browsers and the box model)
Q: What are the pros and cons of using tables for layout? Do you use tables? What are
the pros and cons of tableless design? How do you generally layout your pages?
A: check for them NOT using tables
Q: Check to ensure that they separate structure and semantics first from presentation
later? Do not ask about this during HTML, but do in webstandards.
Q: What are some deprecated elements and attributes that you use, and in what instances
do you use them?
A: List of deprecated elements and attributes.
Q: What is involved in making a website accessible? What are arguments you use to
convince others to invest in making their web site accessible.
A: See Making the web Accessible. Making sites accessible also makes them more
search engine friendly (saves money), makes your pages accessible to the 20% of the
population that has some type of disability (so you can make more money) and it’s the
law in many places.
Q: Define what web standards mean to you? How do you implement web standards?
Q: In CSS, how can you make a form elments background-color change when the user is
entering text? will this work in all browsers?
Q: How can you target an element in your HTML using the DOM?