CSS Interview Questions Flashcards
(98 cards)
What are the components of a CSS Style?
A style rule is made of three parts −
Selector − A selector is an HTML tag at which a style will be applied. This could be any tag like <h1> or </h1> etc.
Property − A property is a type of attribute of HTML tag. Put simply, all the HTML attributes are converted into CSS properties. They could be color, border etc.
Value − Values are assigned to properties. For example, color property can have value either red or #F1F1F1 etc.
Type selector quite simply matches the name of an element type. To give a color to all level 1 headings −
h1 {
color: #36CFFF;
}
Rather than selecting elements of a specific type, the universal selector quite simply matches the name of any element type −
- {
color: #000000;
}
This rule renders the content of every element in our document in black.
What is Descendant Selector?
Suppose you want to apply a style rule to a particular element only when it lies inside a particular element. As given in the following example, style rule will apply to <em> element only when it lies inside <ul> tag.</em>
ul em {
color: #000000;
}
</ul></em>
What is class selector?
You can define style rules based on the class attribute of the elements. All the elements having that class will be formatted according to the defined rule.
Can you make a class selector particular to an element type?
You can make it a bit more particular. For example −
h1.black {
color: #000000;
}
This rule renders the content in black for only <h1> elements with class attribute set to black.</h1>
What is id selector?
You can define style rules based on the id attribute of the elements. All the elements having that id will be formatted according to the defined rule.
#black { color: #000000; } This rule renders the content in black for every element with id attribute set to black in our document.
Can you make a id selector particular to an element type?
You can make it a bit more particular. For example −
h1#black {
color: #000000;
}
This rule renders the content in black for only <h1> elements with id attribute set to black.</h1>
What is a child selector?
Consider the following example −
body > p {
color: #000000;
}
This rule will render all the paragraphs in black if they are direct child of element. Other paragraphs put inside other elements like <div> or would not have any effect of this rule.</div>
What is an attribute selector?
You can also apply styles to HTML elements with particular attributes. The style rule below will match all the input elements having a type attribute with a value of text −
input[type = “text”]{
color: #000000;
}
The advantage to this method is that the element is unaffected, and the color applied only to the desired text fields.
How to select all paragraph elements with a lang attribute?
p[lang] : Selects all paragraph elements with a lang attribute.
How to select all paragraph elements whose lang attribute has a value of exactly “fr”?
p[lang=”fr”] - Selects all paragraph elements whose lang attribute has a value of exactly “fr”.
How to select all paragraph elements whose lang attribute contains the word “fr”?
p[lang~=”fr”] - Selects all paragraph elements whose lang attribute contains the word “fr”.
How to select all paragraph elements whose lang attribute contains values that are exactly “en”, or begin with “en-“?
p[lang|=”en”] - Selects all paragraph elements whose lang attribute contains values that are exactly “en”, or begin with “en-“.
What are the various ways of using CSS in an HTML page?
There are four ways to associate styles with your HTML document. Most commonly used methods are inline CSS and External CSS.
Embedded CSS − The Element: You can put your CSS rules into an HTML document using the element.
Inline CSS − The style Attribute: You can use style attribute of any HTML element to define style rules.
External CSS − The <link></link> Element: The <link></link> element can be used to include an external stylesheet file in your HTML document.
Imported CSS − @import Rule: @import is used to import an external stylesheet in a manner similar to the <link></link> element.
How CSS style overriding works?
ollowing is the rule to override any Style Sheet Rule −
Any inline style sheet takes highest priority. So, it will override any rule defined in … tags or rules defined in any external style sheet file.
Any rule defined in … tags will override rules defined in any external style sheet file.
Any rule defined in external style sheet file takes lowest priority, and rules defined in this file will be applied only when above two rules are not applicable.
What is the purpose of % measurement unit?
% - Defines a measurement as a percentage relative to another value, typically an enclosing element.
p {font-size: 16pt; line-height: 125%;}
What is the purpose of cm measurement unit?
cm − Defines a measurement in centimeters.
div {margin-bottom: 2cm;}
What is the purpose of em measurement unit?
em − A relative measurement for the height of a font in em spaces. Because an em unit is equivalent to the size of a given font, if you assign a font to 12pt, each “em” unit would be 12pt; thus, 2em would be 24pt.
p {letter-spacing: 7em;}
What is the purpose of ex measurement unit?
ex − This value defines a measurement relative to a font’s x-height. The x-height is determined by the height of the font’s lowercase letter.
p {font-size: 24pt; line-height: 3ex;}
What is the purpose of in measurement unit?
in − Defines a measurement in inches.
p {word-spacing: .15in;}
What is the purpose of mm measurement unit?
mm − Defines a measurement in millimeters.
p {word-spacing: 15mm;}
What is the purpose of pc measurement unit?
pc − Defines a measurement in picas. A pica is equivalent to 12 points; thus, there are 6 picas per inch.
p {font-size: 20pc;}
What is the purpose of pt measurement unit?
pt − Defines a measurement in points. A point is defined as 1/72nd of an inch.
body {font-size: 18pt;}