In this detailed but easy-to-follow article, we've discussed the most common CSS interview questions and provided answers suitable for both beginners and experienced individuals.
CSS is like the fashion designer of websites. It's all about making web pages look nice by changing colors, shapes, and how things are arranged on the screen. It's what makes websites look good and work well.
CSS stands for Cascading Style Sheet. It is a popular styling language used in conjunction with HTML to create web pages. It is also compatible with any XML document, including plain XML, SVG, and XUL.
The CSS versions are as follows:
CSS1 was founded in 1996, which was the first version. The latest CSS version is CSS3, which is the most advanced version with numerous functionalities and features.
Three ways to include CSS in web pages.
Some of the primary CSS benefits are:
CSS helps in getting more output with less code. It reduces the load on the web page, which results in increased page speed.
The primary role of CSS is to make the pages look appealing and well-designed. Eventually, it improves the experience of the users coming to the site.
A single string of CSS code can be used for as many pages as you like. It means you don’t have to write the same styling code for all pages. As a result, the time required to design web pages is lower.
Responsive web design is not only crucial for user experience across multiple screen sizes but also important for SEO. CSS helps in designing web pages that are compatible across all devices.
CSS frameworks are pre-created libraries that make web page style more accessible and standard compliant. CSS frameworks that are commonly used are:
This is because of two reasons:
A CSS style specification is an embedded style sheet approach used in conjunction with HTML. Using the STYLE element, you can embed the whole stylesheet in an HTML document.
<style>
body {
background-color: linen;
}
h1 {
color: red;
margin-left: 80px;
}
</style>
It is an important part of CSS that allows developers to select the HTML elements that are supposed to be styled with CSS. The following are the main CSS selectors:
These are style sheets created by the author of the web page. They are what most people think of when they think of CSS-style sheets.
Margin and padding can be confusing to a fresher developer. After all, in some ways, they seem like the same thing: white space around an image or object.
Padding is the space inside the border and between the border and the actual image or cell contents. Note that padding goes completely around the contents. It can be on the top, bottom, right and left sides.
Margins are the spaces outside the border, between the border and the other elements next to this object.
CSS is written with US English spellings. So if you are writing colour:red; in your style sheets, none of your text will change color.
Make sure that when you write CSS color styles, you use the US English spelling for them:
Some CSS Style components are as follows:
It is used to specify the transparency of an element. In layman’s terms, it indicates the clarity of the image. Opacity is described as the amount of light that can pass through an item in technical terms.
As an example:
<style>
img.trans {
opacity: 0.4;
filter: alpha(opacity=40); /* For IE8 and earlier */
}
</style>
Instead of choosing elements of a particular type, the universal selector matches the name of any element type.
<style>
* {
color: green;
font-size: 20px;
}
</style>
The background-repeat feature causes the background picture to be repeated horizontally and vertically. Specific images are only repeated horizontally or vertically.
<style>
body {
background-image: url("paper1.gif");
margin-left:100px;
}
</style>
The background-position parameter specifies the position of the backdrop picture at the start. The background image is usually placed in the top-left corner of the webpage.
You can configure the following positions:
The background-attachment attribute specifies whether the background picture should be fixed or scroll with the rest of the page in the browser window. If you set the background picture selected, it will not move when you scroll through the browser.
Let’s look at the static background image as an example:
background: white url('bbb.gif');
background-repeat: no-repeat;
background-attachment: fixed;
Class selectors are given an overall block, whereas id selectors accept only one element that differs from other components.
CSS Class Selector
<style>
.center {
text-align: center;
color: blue;
}
</style>
CSS Id Selector
<style>
#para1 {
text-align: center;
color: blue;
}
</style>
The external style sheets in CSS provide the following benefits:
These are used to style a single line of code.
syntax
<htmltag style="cssproperty1:value; cssproperty2:value;"> </htmltag>
Style sheets that are embedded are placed between the head>…/head> tags.
syntax
<style>
body {
background-color: linen;
}
h1 {
color: red;
margin-left: 80px;
}
</style>
By updating only one style sheet, you may apply the style to all of your website’s pages
syntax
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
RWD is an abbreviation for Responsive Web Design. This strategy is used to correctly show the specified page across all screen sizes and devices, such as a smartphone, tablet, desktop computer, or laptop. It helps you to avoid the need to build a separate page for each device.
A web page containing many photos will take longer to load because each image sends out an HTTP request separately. CSS sprites minimize the loading time of a web page by combining multiple small pictures into a single image. The number of HTTP requests is reduced, as is the loading time.
CSS box models specify the design and layout of CSS elements.
The components are as follows:
It is used to move the image to the right or left, as well as the text wrapped around it. It does not affect the properties of the elements used before it.
There is no simple way to restore default values to whatever browser is being used. The closest option is the ‘initial’ property value, which restores the default CSS values rather than the browser’s default styles.
The z-index assists in specifying the stack order of positioned items that may overlap. The z-index has a default value of zero and can be either positive or negative.
A higher z-index element is always stacked above a lower index.
The following are the possible values for Z-Index:
Visibility: hidden hides the element, but it takes up space and interferes with the layout of the content.
<!DOCTYPE html>
<html>
<head>
<style>
h1.vis {
visibility: visible;
}
h1.hid {
visibility: hidden;
}
</style>
</head>
<body>
<h1 class="vis">It is visible</h1>
<h1 class="hid">It is hidden</h1>
<p>Note - Second heading is hidden, but it still occupies space.</p>
</body>
</html>
Display: none also hides the element but does not occupy space. It will not affect the layout of the document.
<!DOCTYPE html>
<html>
<head>
<style>
h1.vis {
display: block;
}
h1.hid {
display: none;
}
</style>
</head>
<body>
<h1 class="vis">It is visible</h1>
<h1 class="hid">It is hidden</h1>
<p>Note - Second heading is hidden and not occupy space.</p>
</body>
</html>
W3C is an abbreviation for World Wide Web Consortium. Its objective is to give World Wide Web information. It also creates Web rules and guidelines.
The primary difference between CSS2 and CSS3 is that CSS3 is separated into portions known as modules. CSS3 modules, unlike CSS2, are supported by a wide range of browsers.
CSS3 also includes new General Sibling Combinators, responsible for matching sibling elements with supplied elements.
CSS uses RGB to represent colors. Red, Green, and Blue are the three streams. Color intensity is expressed by values ranging from 0 to 256. CSS can now have a wide range of visible colors.
CSS was created to specify how web pages look visually. It enables developers to segregate the structure and content of a website, which was previously impossible.
Class is a method of customizing HTML components. They are not unique and contain several elements. ID, on the other hand, is unique and can only be assigned to a single element.
CSS can be integrated into HTML in three ways: utilizing style tags in the head section, inline styling, writing CSS in a separate file, and linking to the HTML page using the link tag.
The CSS box model specifies how CSS elements are laid out and designed. For example, content (such as text and images), padding (the area around content), border (the area around padding), and margin are the elements (the area around the border).
A universal selector matches the name of any element type rather than picking items of a specific type.
Example:
<style>
* {
color: blue;
font-size: 10px;
}
</style>
Flexbox is an abbreviation for the flexible box. It was introduced in CSS in 2017 to provide an efficient approach to handling layouts, aligning elements within them, and distributing gaps amongst the items in dynamic/responsive situations.
In addition, it improves the ability to change the proportions of things and optimally use the available space in the container. CSS3 gives various attributes to help with this.
Flexbox has the following properties:
This parameter specifies how the container should stack the flex-targeted objects. This property has the following values:
This property determines whether or not flex elements should be wrapped. Possible values include:
This property sets both the flex-direction and the flex-wrap attributes in a single sentence.
It’s use is for flex item alignment.
Align the flex lines.
No, it does not affect the inline parts. Inline items flow with the page’s content.
There are certain CSS limitations you must know: