CSS Interview Questions For Freshers
1. What are the advantages of using CSS?
Ans: The main advantages of CSS are given below:
-
Separation of Content from Presentation
CSS allows content to be separated from its presentation, enabling the same content to be displayed in different formats across devices such as mobile, desktop, or laptop. -
Easy Maintenance
When CSS is structured efficiently, small changes can update the look and feel of an entire website. By modifying a single style rule, all associated elements across all web pages get updated automatically. -
Improved Bandwidth Usage
CSS files, when used properly, are cached by the browser and reused across multiple pages. This reduces the need to download the same style sheets repeatedly, improving load time and saving bandwidth.
2. How do you specify units in the CSS?. What are the different ways to do it?
Ans: There are various ways to specify units in CSS, such as px, em, pt, and % (percentage):
-
px(Pixels): Offers fine-grained control and ensures sharp alignment, as 1px or its multiples render precisely. However,pxis not relative and does not cascade with parent elements. -
em: A relative unit that scales based on the current font size of the element or its parent.1emequals the font size of the element it is applied to. If the root font size is set to 16px, then1em = 16px. Cascading can occur, which allows for responsive typography. A common practice is setting thebodyfont-size to62.5%(equal to 10px), making it easier to calculate sizes withem. -
pt(Points): Primarily used in print media.1pt = 1/72 inch. It is a fixed-size unit and not ideal for screen displays. -
%(Percentage): Sets the font size relative to the font size of the parent element (commonly thebody). To use percentages effectively, it’s important to define a reasonablebodyfont-size first.
3. What is the Box model in CSS? Which CSS properties are a part of it?
Ans: The CSS Box Model is a fundamental concept in web design and layout. It describes how every HTML element is essentially a rectangular box, and how the space around it is calculated and rendered.
Components of the Box Model:
Each box consists of the following (from inside out):
-
Content
-
The actual content of the box (text, image, etc.).
-
Affected by:
width,height.
-
-
Padding
-
Space between the content and the border.
-
Affected by:
padding(can bepadding-top,padding-right,padding-bottom,padding-left).
-
-
Border
-
The edge of the box, surrounding the padding (if any).
-
Affected by:
border(likeborder-width,border-style,border-color).
-
-
Margin
-
Space outside the border, separating the element from other elements.
-
Affected by:
margin(can bemargin-top,margin-right,margin-bottom,margin-left).
-
CSS Properties in the Box Model:
| Box Model Part | CSS Properties |
|---|---|
| Content | width, height, min-width, max-width, min-height, max-height |
| Padding | padding, padding-top, padding-right, padding-bottom, padding-left |
| Border | border, border-width, border-style, border-color, border-radius |
| Margin | margin, margin-top, margin-right, margin-bottom, margin-left |
4. What are the limitations of CSS?
Ans: Disadvantages of CSS are given below:
-
Browser Compatibility: Some CSS selectors are supported in certain browsers but not in others. Developers often need to check compatibility using the
@supportsrule. -
Cross-Browser Issues: Certain selectors or properties may behave differently across various browsers, which can lead to inconsistent designs.
-
No Parent Selector: CSS does not currently support selecting a parent element based on a child, limiting some styling possibilities.
5. How to include CSS in the webpage?
Ans: There are different ways to include CSS in a webpage:
1. External Style Sheet
An external file linked to your HTML document. Use the <link> tag to connect the stylesheet to the HTML page.
2. Embedded CSS (Using <style> tag)
A set of CSS rules included within your HTML page using the <style> tag, placed inside the <head> section.
Write your CSS rules between the opening and closing <style> tags, just as you would in a standalone CSS file.
3. Inline CSS
CSS rules applied directly within an HTML element using the style attribute.
4. Imported Style Sheet
An external CSS file imported into another CSS file using the @import rule.
This method is typically used within a CSS file to include additional styles from another stylesheet.
6. What are the different types of Selectors in CSS?
Ans: In CSS, selectors are patterns used to select and style HTML elements. There are several types of selectors, each serving a specific purpose. Here’s a breakdown of the most common types:
1. Universal Selector
-
Syntax:
* -
Description: Selects all elements on the page.
-
Example:
2. Type (Element) Selector
-
Syntax:
elementName -
Description: Selects all elements of a specific type.
-
Example:
3. Class Selector
-
Syntax:
.className -
Description: Selects all elements with the specified class.
-
Example:
4. ID Selector
-
Syntax:
#idName -
Description: Selects an element with the specific ID (must be unique).
-
Example:
5. Group Selector
-
Syntax:
selector1, selector2, ... -
Description: Groups multiple selectors to apply the same styles.
-
Example:
6. Descendant Selector
-
Syntax:
ancestor descendant -
Description: Selects all elements that are descendants of a specified element.
-
Example:
7. Child Selector
-
Syntax:
parent > child -
Description: Selects direct child elements only.
-
Example:
8. Adjacent Sibling Selector
-
Syntax:
element1 + element2 -
Description: Selects the first
element2immediately afterelement1. -
Example:
9. General Sibling Selector
-
Syntax:
element1 ~ element2 -
Description: Selects all
element2siblings afterelement1. -
Example:
10. Attribute Selector
-
Syntax:
element[attribute=value] -
Description: Selects elements with specific attributes.
-
Example:
11. Pseudo-class Selector
-
Syntax:
selector:pseudo-class -
Description: Targets elements based on state or position.
-
Examples:
12. Pseudo-element Selector
-
Syntax:
selector::pseudo-element -
Description: Styles parts of elements.
Examples:
