CSS interview questions and Answers

Questions

35

Last updated

Feb 7, 2024

Cascading Style Sheets (CSS) is a style sheet language or a mechanism used for enhancing its style by adding colors, fonts, and spacing to your web documents. We have a collection of CSS interview questions that helps in crack your interviews. It is designed to enable the separation of content for improving content accessibility and providing more flexibility in the specification of presentation and reducing complexity in the structural content. The specifications of CSS are maintained and managed by the W3C.

Most Frequently Asked CSS interview questions

Here in this article, we will be listing frequently asked CSS interview questions and Answers with the belief that they will be helpful for you to gain higher marks. Also, to let you know that this article has been written under the guidance of industry professionals and covered all the current competencies.

Q1. What is CSS?
Answer

Cascading Style Sheets is a style sheet language which is used for describing the presentation of a document written in a markup language like HTML. It is a cornerstone technology of the WWW, alongside HTML and JavaScript. It handles the look and feel part of a webpage.

Q2. How many types of CSS?
Answer

There are three Types of CSS used to develop a web page.

  • External css : These types of style sheets are having their separate files.
  • Internal css : These types of styles are placed at the top of each web page document, in between head tag.
  • Inline css : These types of styles are placed directly with HTML tags.
Q3. What are the latest versions of CSS?
Answer

CSS3 is the latest version of CSS.

Q4. What are advantages of CSS?
Answer
  • It is Easy to maintain and update.
  • It has Great consistency in design.
  • It has more formatting options.
  • It is Lightweight code.
  • Faster downlad times.
  • It has search engine optimization benefits.
Q5. What are the difference between Inline CSS & Internal CSS?
Answer

CSS can be applied to our website's HTML files in various ways. We can use an external css, an internal css, or an inline css.

Inline CSS : It can be applied on directly on html tags. Priority of inline css is greater than inline css.

Internal CSS : It can be applied in web page in top of the page between heading tag. It can be start with

Q6. What are the different font attributes?
Answer

There are 3 font attributes which is used to write CSS.

  • color
  • face
  • size
Q7. What is CSS selector?
Answer

A CSS selectors is used to select HTML elements based on their name,id,class and attribute. It is the part of a CSS rule that actually selects the content we want to style.

Type of CSS selectors

  • Universal Selector
    For Example
    * {
    color: green;
    }
  • Element Type Selector
    For Example
    ul {
    list-style: none;
    }
  • ID Selector
    #container {
    width: 960px;
    }
  • Class Selector
    For Example
    .box {
    padding: 20px;
    }
  • Attribute Selector
    For Example
    input[type="text"] {
    background-color: #444;
    }
Q8. List some CSS frameworks?
Answer
  • Bootstrap
  • Semantic
  • Materialize
  • Bulma
  • Foundation
  • Pure CSS etc
Q9. What is universal css selector?
Answer

It is used to select any type of elements in an HTML page. An asterisk ( * ) is used to denote a universal CSS selector. This is useful when we want to set a style for of all the elements of an HTML page.

Syntax : * { CSS-Property: value; ................. }

Q10. How to write media query for mobiles?
Answer

In between @media screen and (max-width: 600px) { and } we can write our CSS. It can directly effect for mobile device.

Q11. What is float in CSS?
Answer

The float property in CSS places an element on the right or left side of its container, allowing text and inline elements to wrap around it.

.sidebar {
float: right;
width:30%;
}

Q12. What is hover effect in css? How we can apply on tags?
Answer

It select and style a link when you mouse over it. An :hover is used to select elements when you mouse over them.

a:hover{
background-color: yellow;
}

Q13. What is CSS Box Model and also explain its elements?
Answer

CSS Box Model is used when talking about design and layout. It is essentially a box that wraps around every HTML element. It consists of margins, borders, padding, and the actual content.

Elements of CSS Box Model

  • Content
  • Padding
  • Border
  • Margin
Q14. What is the use of z-index in CSS?
Answer

It specifies the stack order of an element. An element with greater stack order is always in front of an element with a lower stack order. z-index only works on positioned elements like position:absolute, position:relative, or position:fixed.

Q15. What is the use of @import?
Answer

@media can be used in media query to apply different styles for different media types or devices. It can be used to check many things, such as width and height of the view port.

Q16. What are the difference between "display:none" and "visibility:hidden" in CSS?
Answer

visibility:hidden : Ithides an element, but it will still take up the same space as before. The element will be hidden, but still, affect the layout.

display:none : It hides an element as well as doesn't preserve the space.

Q17. How to make scroll bars with the help of CSS?
Answer

The CSS overflow property specifies whether to clip content or to add scrollbars when the content of an element is too big to fit in a specified area.

Property of overflow is given below :-

  • visible
  • hidden
  • auto
  • scroll

overflow: scroll
div {
overflow: scroll;
}

overflow-x and overflow-y
div {
overflow-x: hidden; /* Hide horizontal scrollbar */
overflow-y: scroll; /* Add vertical scrollbar */
}

Q18. Explain the difference between CSS2 & CSS3?
Answer

CSS2 : It was released in 1998. In CSS2 designers could only use web safe fonts for being 100% sure to use fonts that would always display the same on every system. CSS2 had "simple selectors". In CSS2, the developers or designers had difficulty because the standard was not equipped with automatically breaking of the text so that it fits within a box.

CSS3 : It was released in 1999. In CSS3 designers can use special fonts like those available in Google Fonts and Typecast. CSS3 calls the components as "a sequence of simple selectors". In CSS3 has the capability to split text sections into multiple columns so that it can be read as a newspaper.

Q19. What is the limitations of CSS?
Answer

CSS has various limitations as a programming language thats are as follows:

  • CSS cannot perform any logical operations like if/else or for/while or +/-.
  • We can not read any files using CSS.
  • It can not interact with databases.
  • CSS can not request a web page.
Q20. What are the difference between "position:absolute" & "position:relative" in CSS?
Answer

Both Postions(absolute & relative) are having different features. Once we set Position then you can able to use top, right, bottom, left attributes.

An absolute position element is positioned relative to the first parent element that has a position other than static.

Absolute : In case of position absolute, an element searches for the nearest available coordinate axes among its parent elements. The element is then positioned by specifying offsets from this co-ordinates axis. It removed from document normal flow.

Relative : In case of position relative, an element creates its own co-ordinates axes, at a location offset from the view port co-ordinate axis. It is Part of document flow but shifted.

Q21. What are the difference between Internal & External CSS?
Answer

Internal CSS are the ones that we can write within the same file i.e the HTML code and CSS code are placed in the same file.

External CSS are that we can write in a separate file than the html code i.e the HTML file is separate like(index.html) and CSS file is separate like(style.css).

Q22. How to write css for 3rd child of every <ul> tags?
Answer

nth-child(n) : It matches every element that is the nth child, regardless of type, of its parent.

ul:nth-child(3) {
background: red;
}

 

Q23. What is responsive?
Answer

It is about making a website look good on all devices like desktops, tablets, and phones etc. In responsive HTML and CSS is automatically resize a website.

Q24. What is attributes in CSS and how they used?
Answer

It is used to select elements with a specified attribute. It is possible to add css that have specific attributes or attribute values.

It is used to select elements with an attribute value containing a specified word.

a[href="#"] {
background-color: yellow;
}

Q25. What is the used of !important? How we can use it?
Answer

The !important css rule is used for overriding the previously assigned CSS declarations.

#sidebar {
   font-size: 12px !important;
}

Q26. What are the difference between margin & padding?
Answer

Margin is a outer space of an element but padding is the inner space of an element. In other words we can say margin is the space outside of an element's border but padding is the space inside of its border.

Q27. What are Advantages of CSS3 over CSS?
Answer

CSS3 is a new version of CSS that have various benefits from technical features and properties. From better maintenance, loading speed, and layout design properties CSS3 is much more versatile than CSS. Designers can use all these property in a simpler manner.

Q28. What is Tweening?
Answer

Tweening is a familiar term for those used to animating in Flash. With CSS animation we can use the latter, pose to pose.

 

p {
animation-duration: 2s;
animation-name: slidein;
}

@keyframes slidein {
from {
margin-left: 95%;
width: 200%;
}
to {
margin-left: 0%;
width: 100%;
}
}

 

Q29. What is the use of sprites in CSS?
Answer

It is a collection of images put into a single image. A page with many images can take a long time to load and generates multiple server requests. Using sprites will reduce the number of server requests and save bandwidth.

Q30. What is the purpose of pseudo elements?
Answer

It is an element which is used to style specified parts of an element. It can be used to either "Style the first letter, or line, of an element" or "Insert content before, or after, the content of an element".

Syntax :
selector::pseudo-element {
property:value;
}

Q31. Explain the following common CSS units of length: cm, em, in, mm, pc, pt, and px.
Answer
Q32. How to write CSS for all anchor tag which have href="#" link?
Answer

a[href="#"] {
   color:red;
}

 

Q33. What are difference between class selector and ID selector?
Answer

In CSS, a Class selector is a name preceded by a full stop (".") and an ID selector is a name preceded by a hash("#"). Difference between an ID and a class is that an ID can be used to identify one element, whereas a class can be used to identify more than one.

Q34. What is the meaning of parent-child selector?
Answer
Q35. How we can override CSS?
Answer

With the help of !important we can override CSS properties.

.sidebar{
width:30%;
}

.sidebar{
width:40% !important;
}