CSS Interview Questionnaire
CSS Interview Questionnaire
CSS (Cascading style sheet) is a style sheet language which is used to do formatting
and adding beauty to HTML elements.
Its primary purpose is to make the appearance of application presentable.more about CSS
is it also help us to control the styles for multiple pages
As we add whole CSS into a single file it makes easier to manage and update styles.
Inline CSS – Adding style directly to an HTML element using style attribute
Example = <h4 style = “color: red; background-color: green ;”> Hello Sahil</h4>
Internal CSS – Adding style tag in head section of HTML and apply style logic in this
tag.
Example =
<DOCTYPE html>
<html>
<head>
<title>Internal CSS</title>
<style>
P
{
Color: red;
Background-color: green;
}
</style>
</head>
<body>
<p>This is a paragraph</p?
</body>
</html>
External CSS – In this type we create a separate CSS file and link the CSS file using
link tag .Add the logics of styling in the CSS file.
Example =
p <DOCTYPE html>
<html>
{ <head>
<title>Internal CSS</title>
color: red;
<link rel=”stylesheet”
background-color: green; href=”style.css”>
} </head>
<body>
<p>This is a paragraph</p?
</body>
</html>
index.html
style.css
In CSS we are always use dot + class_name for selecting an element having specific
class
Example = Suppose we have a div having a class box
.box
{
Color: red;
Background-color: black;
}
4. What is the box model in CSS?
Box model is a structure of an element .It contains the width , height , border, margin
, padding of an element
MARGIN
padding BORDER
ELEMENT
width x height
6. What is the precedence order of CSS while applying style by inline , internal and
external CSS?
Syntax- background-color:#f3f3f3;
Add this property while styling the element and add the color you want .
I) Padding : This property defines space between the content and its border. It
generally create extra space around the element . It also included while we are setting the
background-image or background-color
Ii) margin: This property defines space around the outside of an element , between
element and its surroundings . It is transparent in color and if we even apply background-
color or image to element it won’t influence the margin.
13. How do you apply CSS styles to only the first letter of paragraph ?
For styling first letter of paragraph we can use ::first-letter pseudo element. It allows
you to style and target the first letter of a text-element
Example
P::first-letter
{
Color : blue;
}
C- em: The em unit is relative to the font size of the element itself or the nearest parent
element with a specified font size. 1em is equal to the current font size, so if the font-size
of the document is 16px, then 1em will equal 16px.
D- rem: Similar to em, the rem unit is relative to the font size. However, rem is always
relative to the base (root) font size of the document, usually defined on the element. This
unit provides a consistent way to scale elements based on the overall font size of the page,
without being affected by the font size of parent elements.
In CSS, you can set the height and width of an element using the height and width
properties. These properties can be applied to most elements, except non-replaced
inline elements and table columns.
.my-element {
Width: 200px;
Height: 100px;
}
24. Explain the difference between position: relative; and position: absolute;.
Position: relative; and position: absolute; are both CSS positioning properties that
determine how an element is positioned in a webpage. They offer different methods
for controlling an element's placement:
Position: relative: When an element has position: relative;, its position is calculated
based on its normal position in the document flow (i.e., its static position). You can then
use the top, right, bottom, and left properties to adjust the element's position relative to its
original location, without affecting the placement of other elements around it.
Position: absolute: When an element has position: absolute;, it is taken out of the
normal document flow and positioned relative to the nearest positioned ancestor element
(the nearest element with a specified position other than static). If it does not have a
positioned ancestor, the element is positioned relative to the initial containing block,
usually the initial viewport or document body. You can use the top, right, bottom, and left
properties to set the position of the absolute element relative to its positioned ancestor.
Ul {
List-style-type: none;
}
The CSS opacity property is used to control the transparency of an element. It accepts
a value between 0 (completely transparent) and 1 (fully opaque). It can be used to
create subtle transparency effects or fade animations.
27. How do you align text to the center of an element in CSS?
To align text to the center of an element in CSS, you can use the text-align property
with the value center. This will horizontally center the text within the specified
container element. Here's an example:
CSS:
.center-text {
Text-align: center;
}
HTML:
<div class="center-text">
This text is horizontally centered within the div.
</div>
Both :nth-child() and :nth-of-type() are CSS pseudo-class selectors that allow you to
target specific elements based on their position in a group of siblings. However, they
differ in the way they determine which elements to select.
:nth-child() selects elements based on their position in the group of all sibling
elements, regardless of their type (e.g., < div >, < p >, < li >). The syntax is :nth-
child(an+b), where a and b are integers and n is a counter that starts from 0.
:nth-of-type() selects elements based on their position in a group of siblings with the
same element type. The syntax for :nth-of-type() is almost identical to :nth-child(): :nth-
of-type(an+b).
29. How do you style all links within a specific container in CSS?
To style all links within a specific container in CSS, you can use the descendant
selector. This allows you to target all the anchor elements () that are descendants of
the container element, regardless of their nesting level.
.container a {
/* Your styles here */
Color: red; /* For example, change the link color to red */
}
30. What is the CSS box-shadow property used for?
The CSS box-shadow property is used to add a shadow effect to an element. It allows
you to control the shadow's color, size, blur, and position. By applying a box shadow,
you can give depth and dimension to elements on the page.
HTML:
<nav>
<ul class="navigation-menu">
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></-li>
<li><a href="#services">Services</a>li>
<li><a href="#contact">Contact</a></-ti>
</ul>
</nav>
CSS:
.navigation-menu a {
Display: block; /* Make the link fill the entire list item */
Padding: 10px; /* Add padding around the link text */
Text-decoration: none; /* Remove the default link underline */
Color: #000; /* Set link color */
}
CSS pseudo-classes are keywords added to selectors that specify a state or behavior of
an element. They allow you to style elements based on their state or position in the
document tree.
:hover - applies styles when the element is being hovered over by the mouse.
:active - applies styles when the element is being activated by the user.
:first-child - selects the first child element of its parent.
:nth-child(n) - selects the nth child element of its parent.
35. What is the difference between display: none; and visibility: hidden;?
Both display: none; and visibility: hidden; are CSS properties used to hide elements
on a webpage, but they do so in different ways:
Display: none; - This property completely removes an element from the layout flow,
as if the element is not present in the HTML document at all. Therefore, adjacent
elements will take up the space that the hidden element would have occupied, causing the
layout to reflow.
Visibility: hidden; - This property hides an element while maintaining its space in the
layout. The element is still part of the layout flow but will be invisible. Consequently, the
layout does not reflow as adjacent elements won't take up the hidden element's space.
36. How can you vertically align an element in CSS?
There are multiple ways to vertically align an element in CSS. Here are a few
commonly used techniques:
Using the "display: flex;" property on the parent container and applying "align-items:
center;" to vertically center the child elements.
Applying "position: absolute;" to the element and combining it with "top: 50%;" and
"transform: translatey(-50%);" to center it vertically.
Utilizing CSS Grid and specifying the alignment properties on the grid container.
Em: The em unit represents the size relative to the font-size of the current element. In
other words, if you set the font size to 2em, it means the font size will be 2 times the
computed size of the font of the current element. When applied to elements other than
font-size, em is still calculated based on the font-size of the current element.
Rem: The rem unit represents the size relative to the font-size of the root element ().
This means when you set a font size to 2rem, it is 2 times the font size defined in the
root element, regardless of the current element's parent styles.
Key difference: The em unit depends on the font-size of the current element or its
parent, which can lead to a compounding effect for nested elements. In contrast, the
rem unit depends on the font-size of the root element, making it more consistent
across the entire layout, without the nesting effect
.
38. How do you create a sticky/fixed header in CSS?
To create a sticky/fixed header in CSS, you can use the following approach:
Apply "position: fixed;" to the header element.
Set a suitable "top" or "bottom" value to position it at the desired location.
Specify "width: 100%;" to make it span the entire width of its containing block.
Adjust the z-index property as needed to ensure it appears above other elements.
Use more specific CSS selectors to override the framework's styles. Add class or ID
selectors to target specific elements and apply your desired styles.
Use the "!Important" declaration on your styles to give them higher precedence.
However, it's recommended to use this sparingly as it can lead to maintainability issues.
If possible, customize the framework by modifying its provided variables or using a
custom build to generate a version of the framework with your desired styles.
41. How do you apply CSS styles to only the last child of an element?
To apply CSS styles to only the last child of an element, you can use the ":last-child"
pseudo-class selector. For example:
.parent-element :last-child {
42. What is the purpose of CSS vendor prefixes? Give examples of some common
ones.
CSS vendor prefixes are used to add support for specific CSS features in different
web browsers. They are used before the standard property name and are specific to
certain browser engines. Here are examples of some common CSS vendor prefixes:
-webkit- for webkit-based browsers (Google Chrome, Safari)
-moz- for Mozilla-based browsers (Firefox)
-ms- for Microsoft browsers (Internet Explorer, Microsoft Edge)
-o- for Opera browser.
.element {
-webkit-animation: myanimation 1s;
-moz-animation: myanimation 1s;
Animation: myanimation 1s;
}
CSS inheritance is the process by which certain properties of an element are passed
down from its parent elements. When a parent element has a defined style property,
its child elements will inherit those styles unless overridden. Inheritance applies to
properties like font-size, color, text-align, etc. However, not all properties are
inheritable by default.
45. How do you vertically center an element within its parent container in CSS?
To vertically center an element within its parent container in CSS, you can use one of
the following methods:
Set the parent container's display property to "flex" and use "align-items: center;" to
vertically align the child elements.
Combine "position: absolute;" with "top: 50%;" and "transform: translatey(-50%);" on
the element to center it vertically.
Use CSS Grid and the align-self property to vertically center the element within the
grid container.
46. What is the CSS box-sizing property used for and what are its different values?
The CSS box-sizing property is used to control how the total width and height of an
element are calculated. It has three possible values:
"content-box" (default): The width and height only include the content and exclude
padding, border, and margin.
"border-box": The width and height include the content, padding, and border, but not
the margin. This value makes it easier to work with element sizing.
"padding-box": The width and height include the content and padding but exclude the
border and margin.
47. How do you create a fixed footer that stays at the bottom of the page in CSS?
To create a fixed footer that stays at the bottom of the page in CSS, you can use the
following approach:
50. Explain the concept of CSS grid layout and its advantages over other layout
methods.
CSS grid layout is a powerful two-dimensional layout system that allows you to
create complex grid-based layouts with ease. It provides control over both rows and
columns, making it ideal for responsive designs. Some advantages of CSS grid layout
include:
Simplified grid structure with easy alignment and positioning of elements.
Ability to create complex and flexible layouts without relying on additional
frameworks or libraries.
Support for responsive designs with media queries and auto-placement of grid items.
Efficient use of whitespace and automatic adjustment of grid tracks based on content
and available space.