CSS interview questions and answers

CSS knowledge is a common requirement for any web developer job. The job seekers have to face many types of CSS questions in their job interviews and this article will help them to prepare for CSS interview. Both basic and advanced level CSS interview questions and answers are described in this article. 

#01. What is CSS?

CSS is a styling language that is used to change the look of the HTML and make the page attractive for the user. The full form of CSS is Cascading Style Sheets. It is also used in XHTML.

Note:

The full form of XHTML is Extensible Hypertext Markup Language. It is a part of the XML language. It is almost similar to HTML.

#02. What is the origin of CSS?

In 1980, Standard Generalized Markup Language marked the beginning CSS.

#03. What are the limitations of CSS?

Limitations are:

  • Ascending selectors is not possible
  • The use of vertical control limited
  • No expressions and column declaration supported
  • Pseudo-class not controlled by dynamic behavior
  • Use of rules, styles, targeting specific text not possible

#04. What are the advantages of CSS?

Advantages are:

  • It is faster
  • It maintains site-wide consistency
  • Page reformatting is easier
  • It is more accessible
  • It is easier to separate the content from the presentation

#05. What are CSS frameworks?

It is a pre-planned library. The standards-compliant web page styling and formatting can be done easily by using CSS frameworks.

#06. How block elements can be centered with CSS?

Block-level elements can be centered by using setting auto to margin-left and margin-right properties.

Example:

h1 {

width: 20em;

margin-right: auto;

margin-left: auto

}

Note:

em unit is mainly used to set the computed font size for any HTML element. If no font size is assigned for the element then the default value of the one em is equal to 16px.

#07. Who maintains the CSS specifications?

World Wide Web Consortium maintains the CSS specifications.

#08. How many ways can a CSS be integrated as a web page?

CSS can be integrated into three ways:

Inline:

Style attribute of the element is used to add CSS code.

Example:

<p style=”color:red;”> Hello</p>

Embedded:

Style tag is used within the head tag to declare embedded CSS in the HTML document.

<script>

body{

font-size: 10px;

}

</script>


Linked/ Imported:

CSS code can be placed in an external CSS file and can be linked via link element or @import rule.

Example:

@import url(“style.css”);

#09. What benefits and demerits do External Style Sheets have?

Benefits:

  • Multiple documents having different styles can be controlled from one file.
  • Different HTML elements can have many styles, by using classes.
  • Selector and grouping can be used to group styles in a different situation.

Demerits:

  • It is not efficient for simple style definitions.
  • The style files need to download for using an external stylesheet.
  • The external style sheet should be loaded to render the document.

#10. Discuss the merits and demerits of Embedded Style Sheets?

Merits:

  • Multiple tag types can be created in a single document.
  • Styles, in complex situations, can be applied by using Selector and Grouping methods.
  • No need for any extra download.

Demerits:

  • Multiple documents cannot be controlled from one file.

#11. What does CSS selector mean?

The style declarations that are linked with some HTML particular elements is called CSS selector.

Example:

p { color:#fff;}

#cid{ font-size:30px; }

.cl {font-style:italic; }

Here, Three types of selectors are declared. p is tag selector, #cid is id selector and .cl is class selector.

#12. What is the function of media types in CSS?

The design and customization of documents are rendered by media types in CSS. By applying media control over the external style sheets, the document can be retrieved and used by loading it from the network.

#13. Differentiate logical tags from physical tags?

Physical tags are used for presentation. Logical tags are not used presentation.

Physical tags are used in newer versions. Logical tags are used for the old version and concentrate on the content.

#14. Differentiate the Style Sheet concept from HTML?

HTML is used to display the information and style sheet is used to formate the information.. Moreover, style sheets have better browser capabilities and formatting options than HTML

#15. What is CSS ‘rule’ or ‘ruleset’?

A “rule” or “rule set” is a statement that tells browsers how to render particular elements on an HTML page. A rule set consists of a selector followed by a declaration block.

P {font-size;10px}

Here, P is selector and the statement with curly bracket is declaration.

#16. Is CSS case-sensitive?

Normally CSS is not case-sensitivity except some properties, such as font-family, URLs of images, etc. Only when XML declarations along with XHTML DOCTYPE are being used on the page, CSS is the case -sensitive.

#17. What is the declaration block in CSS?

Any CSS declaration with curly braces consisting of property, colon, and value is called declaration block.

Syntax:

Selector {property name: property value }

Example:

P { color:white;}

Here, { color:white;} is a declaration block.

#18. Define various fonts attributes.

Fonts attributes of CSS are:

  • Font-style
  • Font-variant
  • Font-weight
  • Font-size/line-height
  • Font-family
  • Caption
  • Icon

#19. Why is it easy to insert a CSS file by importing it?

Importing CSS files enable combining external sheets to insert in many sheets.

Syntax:

@import notation, used with <Style> tag.

Example:

<style>

@import “styles.css”;

</style>

Here, styles.css is a external css file that may contain other external css as content.

#20. What is the usage of the Class selector?

Selectors that are unique to a specific style, are called CLASS selectors. Declaration of style and association with HTML can be made through this. The class name can be A-Z, a-z or digits.

Example:

/* class selector*/

.top {font: 14em ;},

/* This class is associated with element body tag*/

<Body class= “top”>

Note:

You can apply the class selector to any HTML document by using class attribute. This type of selector is beneficial if you want to apply the same style for a group of different elements.

#21. Differentiate Class selector from ID selector?

the class selector can be applied to different types of HTML elements and ID selector can be used for applying style on a particular element that has the corresponding id value. In other words, ID is unique while classes are not. An element may have both class and ID.

Example:

div{

background-color:red;

}

#myid

{

background-color:blue;

}

<div>Content of div 1</div>

<div id=”mydiv”> Content of div 2</div>

<div> Content of div 3</div>

Here, the background color of first and third div will be red for tag selector and the second div will be blue for ID selector.

#22. Can multiple declarations be added in CSS?

Yes, it can be achieved by using a semicolon.

Example:

p{

background-color:white;color:red;

}

Here, background-color and color properties are used for p.

#23. What is Pseudo-elements?

Pseudo-elements are used to add special effects to some selectors. CSS is used to apply styles in HTML mark-up. In some cases when extra mark-up or styling is not possible for the document, then there is a feature available in CSS known as pseudo-elements. It will allow extra mark-up to the document without disturbing the actual document.

Example:

div::first-letter {color: blue;font-size: 10em;text-transform: uppercase;}

Here, first-letter is a pseudo-element.

#24. How to overrule underlining Hyperlinks?

A hyperlink is displayed by underline in the HTML document by default. Underline can be replaced by over-line, line-through and none by using the text-decoration property of CSS property.

Example:

a {

text-decoration: none;

}

<a href=”f1.html” style=”text-decoration: line-through”>link 1</a>

<a href=”f2.html” >link 2</a>

Here, the first link will be displayed by line-through by inline CSS and the second link will be displayed without any formatting by embedded CSS.

#25. What happens if 100% width is used along with floats all across the page?

1 pixel is added for the float declaration whenever it is used in the form with the border, and even more, the float will be allowed after that.

#26. Can default property value be restored through CSS? If yes, how?

Default property value can’t be restored through CSS directly, but the property value can be re-declared to achieve the default value.

#27. Mention various Media types.

Different media has different properties. These are:

  • Aural – for sound synthesizers and speech
  • Print – gives a preview of the content when printed
  • Projection- projects the CSS on projectors.
  • Handheld- uses handheld devices.
  • Screen- computers and laptop screens.

#28. What is CSS Box Model and what are its elements?

The CSS box model defines the design and layout of elements. The four elements of this box are:

Margin: It indicates the topmost layer where the overall structure is shown.

Border: It indicates the portion before the padding and content part. The background color affects the border.

Padding: It indicates the space between the border and the content.

Content: It indicates the space where the actual content is shown.

#29. What is a contextual selector?

The selector that is used to select special occurrences of an element is called the contextual selector. Space separates the individual selectors. Only the last element of the pattern is addressed in this kind of selector.

For example:

td p text {color: blue}

#30. Compare RGB values with Hexadecimal color codes.

A color can be specified in two ways:

A color can be represented by 6 characters by hexadecimal data. The color code is a combination of numbers and letters which is preceded by the # symbol.

Example:

p {color: #00AABC}

A color can be represented by a mixture of red, green and blue values. The value of a color can also be specified as rgb(r,g,b). RGB values can be any decimal number between the integers 0 and 255. The values of RGB can also be set by the percentage value of red, green and blue.

Example:

Div { background-color: rgb(180, 230,79); }

H2 { color: rgb(45%,23%,70%);}

#31. Define Image sprites with context to CSS.


When a set of images are combined as a single image, then it is called ‘Image Sprites’. As the loading every image on a web page consumes more time, using image sprites, images can be loaded faster in web pages.

Example:

img.add { width: 60px; height: 55px; background: url (image.png) 0 0; }

Here, only the required portion of the image will be displayed. The user can save substantial margin and time through this.

#32. Compare Grouping and Nesting in CSS.

Grouping: Selectors can be grouped having the same values of property and the code can be reduced by using grouping.

Example:

h1 { color: blue; }

h2 { color: blue; }

p { color: blue; }

It can be seen from the code that every element shares the same property. Rewriting can be avoided by writing each selector separated by a comma.

h1, h2, p { color: blue; }

Nesting: Specifying a selector within a selector is called nesting.

Example:

P{

color: red;

text-align: left;

}

.marked{

background-color: blue;

}

.marked p{

color: green;

}

The text content of the HTML document can be aligned in various ways by using the text-align property of CSS.

#33. How can the dimension be defined as an element?

Dimension properties can be defined by:

  • Height
  • Width
  • Max-height
  • Max-width
  • Min-height
  • Min-width

#34. Define the float property of CSS.

By float property, the image can be moved to the right or the left along with the text that is wrapped around it. The elements declared before this property do not change their formatting.

For Example:

p {

float: left;

width: 250px;

}

According to the above style, The width of all paragraphs of the document will be 250px and the paragraphs will be set on the left side of the document. For the float property, other elements can be placed in the empty part of the right side.

#35. How does the z-index property work?

Overlapping may occur while using CSS for positioning HTML elements. Z index helps in specifying the overlapping element. It is a number that can be positive or negative, the default value is zero. Which element will display in the top and which element will display in the bottom among the overlapping elements are set by Z-index property.

For Example:

img {

z-index: 1;

}

div {

z-index: 2;

}

Here, the div tag will get the highest priority for higher z-index.

#36. What is graceful degradation?

When any web page displays the output properly even any component is failed to load then it is called graceful degradation. The latest browsers are developed to display web pages properly. If any component of the page is not available in any browser then it enables the basic functionality that helps to use the web application to a wider audience.

For example, when the image is unavailable for viewing then a text can be displayed by using the alt attribute.

#37. What is progressive enhancement?

It’s an alternative of graceful degradation. The functionality is the same, but it provides an extra edge to users having the latest bandwidth. It is useful for mobile internet connections.

#38. How can the gap under the image be removed?

All images are worked as inline elements and they are treated the same as texts, so there is a gap left, which can be removed by applying proper CSS code.

Example:

img { display: block ; }

display property has other values to display the element as inline or block or inline-block element.

#39. Why @import is used only at the top?

@import is preferred to use only at the top, to avoid any overriding rules. Generally, this ranking order followed by most programming languages such as Java, Modula, etc.

#40. Which among the following is more precedent: CSS properties or HTML procedures?

CSS is more precedent over HTML procedures because when the browser does not have CSS support then it displays HTML attributes.

#41. How comments can be added to CSS?

The comments in CSS can be added by /* and */.

#42. Define Attribute Selector.

A set of elements and, the attribute names and or values can be defined by an attribute selector.

Example:

input[type=”text”] {

display: block;

color: blue;

}

According to the above code, all text elements of the document will be displayed as a block element and the font color will be blue.

Note:

Many other attributes like href, title, src etc. can be used as an attribute selector to control the style of the document.

#43. What is a CSS property?

A style, that helps to influence CSS to apply the style in the HTML document is called CSS property.

Example:

Border: 1px solid #AB04DF;

Here, three property values are declared. These are border-width, border-style and border-color.

#44. Are quotes mandatory in URLs?

Quotes are optional in URL’s, and they can be single or double.

For Example:

background-image: url(“bg.jpg”);

background-image: url(bg.jpg);

Both of the above declaration are correct and display the same output.

#45. What is at-rule?

Rule, which is applicable in the entire sheet and not partly, is known as at-rule. It is preceded by @ followed by A-Z, a-z or 0-9.

Example:

@charset “UTF-8”;

The above rule defines the character set that will be used by the browser.

 You can check the following article link for javascript interview questions and answers.

JavaScript Interview Questions and Answers