CSS3 Interview Questions

CSS3 Interview Questions

1. How to write conditional statement in CSS?

Following is the example of conditional statement in CSS.

 <head>

        <style type="text/css">

           body

            {

                color:blue;

            }

        </style>

        <!--[if IE 7]>

        <style type="text/css">

        body {

            background-color:red;

        }

        </style>

        <![endif]-->

 </head>

If this code will run in IE7 browser, the background color of the page will be red, for other browser it will be default color (white).

2. How to write styles for all html elements of the same type?

If we want to maintain uniformity in the look and feel of all same type of elements on the page, we can write CSS class with the element name.eg. 

If we want to change the look and feel of all table and h1 element on the page, we can write like this.

table

{

                    font-size:10pt;

        font-family: Arial;

}

h1

{

    font-size:14pt;

    padding-left:5px;

    margin:0px;

    color:#094BBB;

}

The first class "table" will apply to all the tables on the page and second class "h1" will apply to all the h1 element of the page. 

Note that the name of the class is not prefixed with the .(dot) as it happens with normal css class name.

3. What are the possible values of the "Position" attributes?

The possible value of the "Position" attributes are 

absolute 
fixed 
inherit 
relative 
static 

By default, relative value is considered.

4. How to display a link without underline and display underline when mouseover on the link using CSS?

Write following css class.

a

{

    text-decoration:none;

}
a:hover

{

    text-decoration:underline;

}

The first class will force all anchor tag (link)to not display any docoration (underline) and second class will force all anchor tag (link) to display text decoration as underline when mouse over it (ie. display underline when mouse over).

5. How to float the image left side and let the page content fill right side and bottom in CSS?

 

Wrap the div element with fload:left style.

<div style="float: left">

<img src="fsdaf.gif" />Your contents goes here.

</div>

To reverse, ie float the image in the right side and let the content fill the space at the left and further down, specifyfloat:right style.

6. How to line break in CSS?

Use display:block style with span.

<span style="display:block;" />

7. How to page break after an html element in CSS?

Use following code snippet

<p style="page-break-after: always">Place your text</p>

After above code, the rest content will appear in the next page. (It will not be visible as next page in browser but on the printer and in Print Preview, you will see them as next page)

8. State some limitations of style sheets?

Style sheets do have its own share of limitations some of them are as follows: - 
1) Inconsistent browser support 
2) Vertical control limitations 
3) Margin collapsing, float containment, control of element shapes, etc 
4) Lack of column declaration and variables are some of the limitations present in CSS.

9. How do I center block-elements with CSS1?

There are two ways of centering block level elements: 

1. By setting the properties margin-left and margin-right to auto and width to some explicit value: 

BODY {width: 30em; background: cyan;} 
P {width: 22em; margin-left: auto; margin-right: auto} 

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: 

TABLE {margin-left: auto; margin-right: auto; width:400px;} 
In most legacy browsers, a table's width is by default determined by its content. In CSS-conformant browsers, the complete width of any element (including tables) defaults to the full width of its parent element's content area. As browser become more conformant, authors will need to be aware of the potential impact on their designs.

10. What does CSS stand for?

NOTE: This is objective type question, Please click question title for correct answer.

11. Explain inline, embedded and external style sheets .

There are three ways of inserting a style sheet: 

1. External style sheet 
2. Internal style sheet 
3. Inline style 

External Style Sheet : 
An external style sheet is ideal when the style is applied to many pages. 
With an external style sheet, you can change the look of an entire Web site by changing one file. 
Each page must link to the style sheet using the <link> tag. The <link> tag goes inside the head section: 
<head> 
<link rel="stylesheet" type="text/css" href="mystyle.css" /> 
</head> 

Internal Style Sheet : 
An internal style sheet should be used when a single document has a unique style. Internal styles sheet needs to put in the head section of an HTML page, by using the <style> tag, like this: 
<head> 
<style type="text/css"> 
hr {color:sienna} 
p {margin-left:20px} 
body {background-image:url("images/back40.gif")} 
</style> 
</head> 

Inline Styles : 
If only a small piece of code has to be styled then inline style sheets can be used. 
An inline style loses many of the advantages of style sheets by mixing content with presentation. 
To use inline styles you use the style attribute in the relevant tag. 
The style attribute can contain any CSS property. 
The example shows how to change the color and the left margin of a paragraph: 
<p style="color:sienna;margin-left:20px">This is a paragraph.</p>

12. What are the values of "Position" attribute in CSS?

Possible values are 
static, relative, absolute, fixed, inherit

13. What is the default value of "position" attribute incss?

Default value is "static".

Display and visibility properties are used to hide and show elements in any page. Then how they are different from each other?

As said Both the properties are used to hide and show elements but they are different in the way they both work.visibility property, set to hidden will still occupy the space in the layout but display:none does not take up the space in the page.

14. Can you specify more than one css class for any HTML element?

Yes, you can. Just provide a space between both the class names. 

like..

<div class="class1 class2">

</div>

 

15. What is the difference between specifying css class with # and .? i.e. #Class1 or .Class1?

Stylesheeet class declared with # applies to items which have same Id value as of the class name. 

like

#div1

{

   font-weight:bold;

}

<div id="div1">Styled Container</div>

Where css class declared with . can be used for any html element.

.class1

      {

         position:relative;

         left:50px;

      }

<div class="class1">Styled Container</div>

Cascading Style Sheets (CSS) is not case sensitve. However, font families, URLs to images, and other direct references with the style sheet may be. 

If your page uses an XML declaration and an XHTML DOCTYPE then the CSS selectors will be case-sensitive for some browsers, if your page uses a HTML DOCTYPE then your CSS selectors will be case-insensitive. 

It is a good idea to avoid naming classes where the only difference is the case, for example: 
div.myclass { ...} 
div.myClass { ... }

16. What is embedded style? How to link?

The HEAD area, where the TITLE and META tags are found, is also used to store CSS commands. 
These are called embedded CSS. Any embedded CSS command will over-ride an external CSS command of the same tag. Embedded commands are more specific to the page. 

Embedded CSS codes are placed within the HEAD area of the page code. That is anywhere after the <HEAD> tag and before the </HEAD> tag. NOT in the HEAD tag itself. 

<style type="text/css" media=screen> 
<!-- 
p {font-family: georgia, serif; font-size: x-small;} 
hr {color: #ff9900; height: 1px } 
a:hover {color: #ff0000; text-decoration: none} 
--> 
</style> 

Now, whenever any of those elements are used within the body of the document, they will be formatted as instructed in the above style sheet.

17. What is CSS rule 'ruleset'?

There are two types of CSS rules: ruleset and at-rule. Rulesetidentifies selector or selectors and declares style which is to be attached to that selector or selectors. For example P {text-indent: 10pt} is a CSS rule. CSS rulesets consist of two parts: selector, e.g. P and declaration, e.g. {text-indent: 10pt}. 

P {text-indent: 10pt} - CSS rule (ruleset) 
{text-indent: 10pt} - CSS declaration 
text-indent - CSS property 
10pt - CSS value

18. What is ID selector?

ID selector is an individually identified (named) selector to which a specific style is declared. Using the ID attribute the declared style can then be associated with one and only one HTML element per document as to differentiate it from all other elements. ID selectors are created by a character # followed by the selector's 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. 

#abc123 {color: red; background: black} 

<P ID=abc123>This and only this element can be identified as abc123 </P>

19. How to write conditional statement in CSS?

Following is the example of conditional statement in CSS.

 <head>

        <style type="text/css">

           body

            {

                color:blue;

            }

        </style>

        <!--[if IE 7]>

        <style type="text/css">

        body {

            background-color:red;

        }

        </style>

        <![endif]-->

 </head>

If this code will run in IE7 browser, the background color of the page will be red, for other browser it will be default color (white).

20. How to write styles for all html elements of the same type?

If we want to maintain uniformity in the look and feel of all same type of elements on the page, we can write CSS class with the element name.eg. 

If we want to change the look and feel of all table and h1 element on the page, we can write like this.

table

{

                    font-size:10pt;

        font-family: Arial;

}

h1

{

    font-size:14pt;

    padding-left:5px;

    margin:0px;

    color:#094BBB;

}

The first class "table" will apply to all the tables on the page and second class "h1" will apply to all the h1 element of the page. 

Note that the name of the class is not prefixed with the .(dot) as it happens with normal css class name.

21. What are the possible values of the "Position" attributes?

The possible value of the "Position" attributes are 

absolute 
fixed 
inherit 
relative 
static 

By default, relative value is considered.

22. How to display a link without underline and display underline when mouseover on the link using CSS?

Write following css class.

a

{

    text-decoration:none;

}

a:hover

{

    text-decoration:underline;

}

The first class will force all anchor tag (link)to not display any docoration (underline) and second class will force all anchor tag (link) to display text decoration as underline when mouse over it (ie. display underline when mouse over).

23. How to float the image left side and let the page content fill right side and bottom in CSS?

Wrap the div element with fload:left style.

<div style="float: left">

<img src="fsdaf.gif" />Your contents goes here.

</div>

To reverse, ie float the image in the right side and let the content fill the space at the left and further down, specifyfloat:right style.

24. How to line break in CSS?

Use display:block style with span.

<span style="display:block;" />

 

25. How to page break after an html element in CSS?

Use following code snippet

<p style="page-break-after: always">Place your text</p>

After above code, the rest content will appear in the next page. (It will not be visible as next page in browser but on the printer and in Print Preview, you will see them as next page)

26. State some limitations of style sheets?

Style sheets do have its own share of limitations some of them are as follows: - 
1) Inconsistent browser support 
2) Vertical control limitations 
3) Margin collapsing, float containment, control of element shapes, etc 
4) Lack of column declaration and variables are some of the limitations present in CSS.

27. How do I center block-elements with CSS1?

There are two ways of centering block level elements: 

1. By setting the properties margin-left and margin-right to auto and width to some explicit value: 

BODY {width: 30em; background: cyan;} 
P {width: 22em; margin-left: auto; margin-right: auto} 

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: 

TABLE {margin-left: auto; margin-right: auto; width:400px;} 
In most legacy browsers, a table's width is by default determined by its content. In CSS-conformant browsers, the complete width of any element (including tables) defaults to the full width of its parent element's content area. As browser become more conformant, authors will need to be aware of the potential impact on their designs.

28. What does CSS stand for?

NOTE: This is objective type question, Please click question title for correct answer.

29. Explain inline, embedded and external style sheets .

There are three ways of inserting a style sheet: 

1. External style sheet 
2. Internal style sheet 
3. Inline style 

External Style Sheet : 
An external style sheet is ideal when the style is applied to many pages. 
With an external style sheet, you can change the look of an entire Web site by changing one file. 
Each page must link to the style sheet using the <link> tag. The <link> tag goes inside the head section: 
<head> 
<link rel="stylesheet" type="text/css" href="mystyle.css" /> 
</head> 

Internal Style Sheet : 
An internal style sheet should be used when a single document has a unique style. Internal styles sheet needs to put in the head section of an HTML page, by using the <style> tag, like this: 
<head> 
<style type="text/css"> 
hr {color:sienna} 
p {margin-left:20px} 
body {background-image:url("images/back40.gif")} 
</style> 
</head> 

Inline Styles : 
If only a small piece of code has to be styled then inline style sheets can be used. 
An inline style loses many of the advantages of style sheets by mixing content with presentation. 
To use inline styles you use the style attribute in the relevant tag. 
The style attribute can contain any CSS property. 
The example shows how to change the color and the left margin of a paragraph: 
<p style="color:sienna;margin-left:20px">This is a paragraph.</p>

What are the values of "Position" attribute in CSS?

 

Possible values are 
static, relative, absolute, fixed, inherit

30. What is the default value of "position" attribute incss?

Default value is "static".

Display and visibility properties are used to hide and show elements in any page. Then how they are different from each other?

As said Both the properties are used to hide and show elements but they are different in the way they both work.visibility property, set to hidden will still occupy the space in the layout but display:none does not take up the space in the page.

31. Can you specify more than one css class for any HTML element?

Yes, you can. Just provide a space between both the class names. 

like..

<div class="class1 class2">

</div>

 

32. What is the difference between specifying css class with # and .? i.e. #Class1 or .Class1?

Stylesheeet class declared with # applies to items which have same Id value as of the class name. 

like

#div1

{

   font-weight:bold;

}

<div id="div1">Styled Container</div>

Where css class declared with . can be used for any html element.

.class1

      {

         position:relative;

         left:50px;

      }

<div class="class1">Styled Container</div>

Cascading Style Sheets (CSS) is not case sensitve. However, font families, URLs to images, and other direct references with the style sheet may be. 

If your page uses an XML declaration and an XHTML DOCTYPE then the CSS selectors will be case-sensitive for some browsers, if your page uses a HTML DOCTYPE then your CSS selectors will be case-insensitive. 

It is a good idea to avoid naming classes where the only difference is the case, for example: 
div.myclass { ...} 
div.myClass { ... }

33. What is embedded style? How to link?

The HEAD area, where the TITLE and META tags are found, is also used to store CSS commands. 
These are called embedded CSS. Any embedded CSS command will over-ride an external CSS command of the same tag. Embedded commands are more specific to the page. 

Embedded CSS codes are placed within the HEAD area of the page code. That is anywhere after the <HEAD> tag and before the </HEAD> tag. NOT in the HEAD tag itself. 

<style type="text/css" media=screen> 
<!-- 
p {font-family: georgia, serif; font-size: x-small;} 
hr {color: #ff9900; height: 1px } 
a:hover {color: #ff0000; text-decoration: none} 
--> 
</style> 

Now, whenever any of those elements are used within the body of the document, they will be formatted as instructed in the above style sheet.

34. What is CSS rule 'ruleset'?

There are two types of CSS rules: ruleset and at-rule. Rulesetidentifies selector or selectors and declares style which is to be attached to that selector or selectors. For example P {text-indent: 10pt} is a CSS rule. CSS rulesets consist of two parts: selector, e.g. P and declaration, e.g. {text-indent: 10pt}. 

P {text-indent: 10pt} - CSS rule (ruleset) 
{text-indent: 10pt} - CSS declaration 
text-indent - CSS property 
10pt - CSS value

CSS3 Interview Questions CSS3 Interview Questions Reviewed by Evergreen on 23:10 Rating: 5

No comments:

Powered by Blogger.