CSS Developer Interview Questions for Freshers
1.
How to align an element both vertically and horizontally at the center of it's parent element
To align an element both vertically and horizontally at the center of its parent element, you can use CSS flexbox,CSS Grid or Custom css with position absolute. Here are examples of both approaches:
1. Using CSS Flexbox:
In this example, the `.parent` class represents the parent element containing the element you want to center. By setting `display: flex` on the parent, its child elements become flex items. The `justify-content: center` property centers the child element horizontally, while `align-items: center` centers it vertically.
2. Using CSS Grid:
Here, the `.parent` class uses CSS Grid by setting `display: grid`. The `place-items: center` property centers the child element both horizontally and vertically within the grid cell.
Remember to apply these styles to the parent element that contains the element you want to center. You can adjust the CSS selectors (`parent`) according to your HTML structure and class names.
3. Custom css with position absolute
In this approach, the parent element should have a non-static position, such as position: relative, to serve as the reference for absolute positioning. The child element is positioned absolutely using position: absolute. By setting top: 50% and left: 50%, the element is moved to the center of its parent.
Finally, transform: translate(-50%, -50%) shifts the element back by 50% of its own width and height, effectively centering it.
You can also use margin-left and margin-top to shift the element by 50 percent, in this case it will be margin-left:-50px; and margin-top:-50px; as height and width of element is 100px
HTML:-
.parent {
display: flex;
justify-content: center; /* Horizontal centering */
align-items: center; /* Vertical centering */
}
.parent {
display: grid;
place-items: center; /* Centers both horizontally and vertically */
}
.parent {
position: relative
}
.container {
position:absolute;
top: 50%;
left: 50%;
width:100px;
height:100px;
margin-left:-50px;
margin-top:-50px;
border: 1px solid #ccc;
background-color: #f3f3f3;
}
or
.parent {
position: relative
}
.container {
position:absolute;
top: 50%;
left: 50%;
width:100px;
height:100px;
transform:translate(-50%,-50%);
border: 1px solid #ccc;
background-color: #f3f3f3;
}
<body class="parent">
<div class="container">
<div class="item item-1">
1.Lorem Ipsum has been the industry's standard dummy text ev
</div>
</div>
</body>
2.
Explain CSS position properties
In CSS, there are several position properties that control the positioning of elements on a web page. Let's explore each of these position properties with examples:
1. position: This property specifies the positioning method for an element. It can take several values:
- `static`: This is the default value, and elements are positioned according to the normal document flow.
- `relative`: Elements are positioned relative to their normal position. You can use the `top`, `bottom`, `left`, and `right` properties to offset the element from its original position.
- `absolute`: Elements are positioned relative to the nearest positioned ancestor or the containing block. If there is no positioned ancestor, it will be positioned relative to the initial containing block (usually the `` element).
- `fixed`: Elements are positioned relative to the viewport and do not move even if the page is scrolled.
- `sticky`: Elements are positioned based on the user's scroll position.
It toggles between 'relative' and 'fixed'. It behaves like `relative` within its container until a specific offset threshold is reached, after which it behaves like `fixed`.
Here are some examples illustrating the usage of CSS position properties:
In Example 1, the `.relative-box` element is positioned relative to its normal position. It is shifted 20 pixels down from its original position and 50 pixels to the right.
In Example 2, the `.absolute-box` element is positioned relative to its closest positioned ancestor or the containing block. It is shifted 50 pixels down from the top edge and 100 pixels to the right from the left edge of its container.
In Example 3, the `.fixed-box` element is positioned relative to the viewport. It remains fixed in its position, regardless of scrolling. It is placed 20 pixels down from the top and 20 pixels from the right edge of the viewport.
In Example 4, the `.sticky-box` element is initially positioned according to the normal flow of the document. Once the user scrolls past the offset of 50 pixels from the top edge of its containing block (in this case, the `.container`), it becomes "sticky" and behaves like `fixed`, remaining fixed at that position while scrolling.
These examples demonstrate how CSS position properties can be used to control the positioning of elements on a web page.
/* Example 1: Relative positioning */
.relative-box {
position: relative;
top: 20px;
left: 50px;
}
/* Example 2: Absolute positioning */
.absolute-box {
position: absolute;
top: 50px;
left: 100px;
}
/* Example 3: Fixed positioning */
.fixed-box {
position: fixed;
top: 20px;
right: 20px;
}
/* Example 4: Sticky positioning */
.sticky-box {
position: sticky;
top: 50px;
}
.container {
height: 2000px; /* To create a scrollable container for sticky positioning */
}
3.
What are pseudo elements and pseudo classes
Pseudo elements:
A CSS pseudo-element is used to style specified parts of an element.
For example, it can be used to:
->Style the first letter, or line of an element
->Insert content before, or after, the content of an element
Pseudo classes
A pseudo-class is used to define a special state of an element.
For example, it can be used to:
->Style an element when a user mouses over it
->Style visited and unvisited links differently
->Style an element when it gets focus
p::first-line {
color: #ff0000;
font-variant: small-caps;
}
* unvisited link */
a:link {
color: red;
}
/* visited link */
a:visited {
color: green;
}
/* mouse over link */
a:hover {
color: hotpink;
}
/* selected link */
a:active {
color: blue;
}
4.
Difference between display inline , display inline block and display block
In CSS, the display property controls how an element is rendered in the document flow. There are three commonly used values for the `display` property: `inline`, `inline-block`, and `block`. Here's a breakdown of the differences between them:
1. `display: inline`: Elements with `display: inline` are rendered as inline elements, meaning they flow within the text content of a line. Inline elements do not start on a new line and only take up as much width as necessary to contain their content. They cannot have a specified width or height, and margins and padding only affect their left and right sides, not top and bottom.
Example:
2. `display: inline-block`: Elements with `display: inline-block` are rendered as inline-level elements but with the ability to have a specific width, height, margins, and padding. They flow within the text content like inline elements, but they can have block-level properties applied to them. Inline-block elements start on the same line as the previous content, but they can have line breaks if necessary.
Example:
3. `display: block`: Elements with `display: block` are rendered as block-level elements. They start on a new line and take up the full available width by default. Block-level elements can have a specific width, height, margins, and padding. They create a "block" that other elements cannot appear within on the same line.
Example:
In summary, the key differences between `display: inline`, `display: inline-block`, and `display: block` are:
- `display: inline` elements flow within the text content, cannot have a specified width or height, and have limited control over margins and padding.
- `display: inline-block` elements flow within the text content but can have a specified width, height, margins, and padding.
- `display: block` elements start on a new line, take up the full available width, and can have a specified width, height, margins, and padding.
It's worth noting that there are additional values for the `display` property, such as `none` (hides the element), `table` (renders as a table), and `flex` (enables flexible box layout), among others. Each value serves a specific purpose and has its own set of behaviors and characteristics.
span {
display: inline;
}
<span>This is an inline element.</span>
div {
display: inline-block;
width: 200px;
height: 100px;
margin: 10px;
padding: 5px;
}
<div>This is an inline-block element.</div>
div {
display: block;
width: 300px;
height: 200px;
margin: 10px;
padding: 5px;
}
<div>This is a block element.</div>
5.
What are media queries
Media query is a CSS technique introduced in CSS3.It is a technique to apply different styles or layouts to a web page based on the characteristics of the user's device or screen size. Media queries allow web developers to create designs that adapt and look well-organized on various devices, such as desktop computers, laptops, tablets, and smartphones.
With media queries, you can set specific CSS rules to trigger only when certain conditions are met, such as the width of the viewport. This enables you to create flexible and responsive designs that provide an optimal viewing experience regardless of the device being used to access the website.
Here's an example of a media query in CSS:
In this example, the `@media` rule specifies a condition for when the screen width is at most 600 pixels. If the screen width matches this condition (i.e., the screen is 600 pixels wide or narrower), the styles inside the curly braces will be applied. In this case, the background color of the `body` element will be changed to light blue.
You can also have multiple conditions within a single media query, allowing you to target different screen sizes with different styles:
In this second example, the first media query targets screens up to 600 pixels wide with a light blue background color, and the second media query targets screens between 601 and 1200 pixels wide with a light green background color.
These examples showcase how media queries allow you to apply different styles based on the characteristics of the screen, creating a responsive design that adapts to various devices.
/* This CSS rule will apply to screens with a maximum width of 600 pixels */
@media (max-width: 600px) {
body {
background-color: lightblue;
}
}
/* Styles for screens up to 600 pixels wide */
@media (max-width: 600px) {
body {
background-color: lightblue;
}
}
/* Styles for screens between 601 and 1200 pixels wide */
@media (min-width: 601px) and (max-width: 1200px) {
body {
background-color: lightgreen;
}
}
6.
What are the new features in CSS3
CSS3 introduced many new features and enhancements to the CSS specification. Below are some of the notable CSS3 features with examples:
1. Border Radius:
Allows you to create rounded corners for elements.
2. Box Shadow:
Creates a shadow effect around elements.
3. Text Shadow:
Adds a shadow effect to the text.
4. Gradients:
Allows you to create gradient backgrounds.
5. Transitions:
Enables smooth transitions between property values.
6. Animations:
Allows you to define complex animations.
7. Flexbox:
A powerful layout system for arranging items within a container.
8. Grid Layout:
A two-dimensional layout system for designing complex web page layouts.
9. Media Queries:
Allows you to apply different styles based on the user's device or screen size.
10. Transforms:
Allows you to perform transformations on elements, such as scaling, rotating, and translating.
Conclusion:
These are just a few examples of the new features introduced in CSS3. CSS3 brought a wide range of capabilities that enable web developers to create more dynamic and visually appealing web pages without relying on complex JavaScript or images for certain effects.
/* CSS3 */
.box {
border-radius: 10px;
}
/* CSS3 */
.box {
box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
}
/* CSS3 */
.text {
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
}
/* CSS3 - Linear Gradient */
.box {
background: linear-gradient(to right, #ff0000, #00ff00);
}
/* CSS3 - Radial Gradient */
.circle {
background: radial-gradient(circle, #ff0000, #00ff00);
}
/* CSS3 */
.box {
transition: background-color 0.3s ease;
}
.box:hover {
background-color: #ffcc00;
}
/* CSS3 */
@keyframes bounce {
0%, 20%, 50%, 80%, 100% {
transform: translateY(0);
}
40% {
transform: translateY(-30px);
}
60% {
transform: translateY(-15px);
}
}
.box {
animation: bounce 2s infinite;
}
/* CSS3 */
.container {
display: flex;
justify-content: center;
align-items: center;
}
/* CSS3 */
.container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 10px;
}
/* CSS3 */
@media screen and (max-width: 768px) {
.box {
font-size: 14px;
}
}
/* CSS3 */
.box {
transform: rotate(45deg);
}
7.
what is root in css
The :root CSS pseudo-class matches the root element of the document. In HTML, :root represents the HTML element and is identical to the selector HTML, except that its specificity is higher.
so, for example if want to change the background of our complete page to black. We can do it by the CSS :
:root is also used for declaring global CSS variables, for example :
:root{
background: black
}
:root {
--main-color: hotpink;
--pane-padding: 5px 42px;
}
8.
What is the meaning of 1. div ~ p 2. div + p 3. Div > p in CSS ?
Please refer this w3schools section for this topic - CSS Combinators
9.
What is the box model in CSS
The box model is a fundamental concept in CSS that describes how elements are structured and how they are displayed on a web page.
In CSS, every element on a web page is essentially a rectangular box, consisting of four parts: margin, border, padding, and content. The content area contains the actual content of the element, while the padding is the space between the content and the border, and the border is the visible line that surrounds the content and padding. The margin is the space between the border and the adjacent elements.
Here's a graphical representation of the box model:
The box model is important because it helps developers understand how elements are laid out on a web page and how they interact with each other. By setting values for margin, border, and padding, developers can control the size and spacing of elements on the page, and create more visually appealing layouts.
Box model and border-box sizing:
The box-sizing property in CSS allows you to control how the total width and height of an element are calculated, taking into account its content, padding, and border. It affects the box model behavior, particularly how the specified width and height of an element are interpreted.
By default, the box-sizing property is set to content-box, which means that the specified width and height of an element only apply to its content area, excluding padding and border. This is the traditional box model behavior.
However, you can change the box-sizing property to border-box, which alters the box model behavior. When box-sizing: border-box is applied to an element, the specified width and height now include the content, padding, and border. In other words, the element's overall width and height will be calculated by adding the padding and border to the specified width and height.
+----------------------------------------------+
| Margin |
| +--------------------------------------+ |
| | Border | |
| | +------------------------------+ | |
| | | Padding | | |
| | | +----------------------+ | | |
| | | | Content | | | |
| | | +----------------------+ | | |
| | +------------------------------+ | |
| +--------------------------------------+ |
| |
+----------------------------------------------+
10.
What are the limitations of CSS?
CSS (Cascading Style Sheets) is a powerful styling language used for describing the presentation of a document written in HTML or XML. While CSS is widely used and versatile, it does have some limitations. Here are some of them:
1. Browser Compatibility:
- Different browsers may interpret CSS rules differently, leading to inconsistencies in the appearance of a webpage. This can require additional effort to ensure cross-browser compatibility.
2. Lack of dynamic behavior
CSS is a declarative language, which means that it describes the desired state of a document. It does not allow developers to write code that executes dynamically in response to user interactions. For this, developers need to use JavaScript or another scripting language.
3. Limited layout control
CSS is primarily designed for styling elements, not for laying them out. While CSS does provide some layout capabilities, such as positioning and floats, it can be challenging to achieve complex layouts with CSS alone. For this, developers need to use CSS frameworks like Flexbox or Grid.
4. Security vulnerabilities
CSS is not inherently secure, and it can be used to introduce security vulnerabilities into web pages. One example is cross-site scripting (XSS), which is a type of attack that allows an attacker to inject malicious code into a web page. Developers need to be careful when using CSS from untrusted sources and should always validate user-supplied input before rendering it on the page.
5. Scalability issues
As the size and complexity of web pages grow, maintaining CSS can become difficult. This is especially true when using a lot of CSS rules or when using CSS frameworks with a lot of nested selectors. To manage CSS complexity, developers can use CSS preprocessors like LESS or Sass, which allow them to write more modular and maintainable CSS code.
6. Accessibility challenges
CSS can be used to create visually appealing web pages, but it is important to consider accessibility when using CSS. Developers should ensure that their CSS code does not interfere with the ability of users with disabilities to access and use web pages. This may involve using CSS to provide alternative text for images, using high-contrast color schemes, and ensuring that the page is navigable without relying on a mouse.
Conclusion:
CSS is a powerful tool for styling web pages, but it is important to be aware of its limitations. By understanding these limitations, developers can avoid problems and create more consistent, maintainable, and accessible websites.