CSS Interview Questions
CSS Interview Questions
A list of top frequently asked CSS interview questions and answers are given below.
1) What is CSS?
CSS stands for Cascading Style Sheet. It is a popular styling language which is used with
HTML to design websites. It can also be used with any XML documents including plain XML,
SVG, and XUL.More details...
o CSS1
o CSS2
o CSS2.1
o CSS3
o CSS4
More details...
5) What are the advantages of CSS?
o Bandwidth
o Site-wide consistency
o Page reformatting
o Accessibility
o Content separated from presentation
o Bootstrap
o Foundation
o Semantic UI
o Gumby
o Ulkit
1. <style>
2. body {
3. background-color: linen;
4. }
5. h1 {
6. color: red;
7. margin-left: 80px;
8. }
9. </style>
10.
More details...
o Selector
o Property
o Value
1. <style>
2. img.trans {
3. opacity: 0.4;
4. filter: alpha(opacity=40); /* For IE8 and earlier */
5. }
6. </style>
1. <style>
2. * {
3. color: green;
4. font-size: 20px;
5. }
6. </style>
7.
15) Which command is used for the selection of all the elements
of a paragraph?
The p[lang] command is used for selecting all the elements of a paragraph.
1. <style>
2. h2,p{
3. background-color: #b0d4de;
4. }
5. </style>
6.
18) Name the property for controlling the image repetition of the
background.
The background-repeat property repeats the background image horizontally and vertically.
Some images are repeated only horizontally or vertically.
1. <style>
2. body {
3. background-image: url("paper1.gif");
4. margin-left:100px;
5. }
6. </style>
7.
19) Name the property for controlling the image position in the
background.
The background-position property is used to define the initial position of the background
image. By default, the background image is placed on the top-left of the webpage.
1. center
2. top
3. bottom
4. left
5. right
1. background: white url('good-morning.jpg');
2. background-repeat: no-repeat;
3. background-attachment: fixed;
4. background-position: center;
20) Name the property for controlling the image scroll in the
background.
The background-attachment property is used to specify if the background image is fixed or
scroll with the rest of the page in the browser window. If you set fixed the background
image, then the image not move during scrolling in the browser. Let's take an example with
the fixed background image.
1. background: white url('bbb.gif');
2. background-repeat: no-repeat;
3. background-attachment: fixed;
1. <style>
2. .center {
3. text-align: center;
4. color: blue;
5. }
6. </style>
7.
CSS Id Selector
1. <style>
2. #para1 {
3. text-align: center;
4. color: blue;
5. }
6. </style>
7.
More details...
Syntax
1. <htmltag style="cssproperty1:value; cssproperty2:value;"> </htmltag>
2.
Embedded: Embedded style sheets are put between the <head>...</head> tags.
Syntax
1. <style>
2. body {
3. background-color: linen;
4. }
5. h1 {
6. color: red;
7. margin-left: 80px;
8. }
9. </style>
10.
External: This is used to apply the style to all the pages within your website by changing
just one style sheet.
Syntax
1. <head>
2. <link rel="stylesheet" type="text/css" href="mystyle.css">
3. </head>
4.
28) What is the CSS Box model and what are its elements?
The CSS box model is used to define the design and layout of elements of CSS.
To understand its purpose and origin, let's take a look at its print display. In the print
display, an image is set into the page such that text wraps around it as needed.
More details...
The closest option is to use the 'initial' property value, which restores the default CSS
values, rather than the browser's default styles.
31) What is the purpose of the z-index and how is it used?
The z-index helps to specify the stack order of positioned elements that may overlap one
another. The z-index default value is zero and can take on either a positive or negative
number.
An element with a higher z-index is always stacked above than a lower index.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. h1.vis {
6. visibility: visible;
7. }
8.
9. h1.hid {
10. visibility: hidden;
11. }
12. </style>
13. </head>
14. <body>
15. <h1 class="vis">It is visible</h1>
16. <h1 class="hid">It is hidden</h1>
17.
18. <p>Note - Second heading is hidden, but it still occupy space.</p>
19. </body>
20. </html>
21.
display: none also hides the element but not occupy space. It will not affect the layout of
the document.
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <style>
5. h1.vis {
6. display: block;
7. }
8.
9. h1.hid {
10. display: none;
11. }
12. </style>
13. </head>
14. <body>
15. <h1 class="vis">It is visible</h1>
16. <h1 class="hid">It is hidden</h1>
17.
18. <p>Note - Second heading is hidden and not occupy space.</p>
19. </body>
20. </html>
21.
It gives the impression that the first image has smoothly evolved into the second one.
Apart from that, CSS3 contains new General Sibling Combinators which is responsible for
matching the sibling elements with the given elements.