CSS Interview Questions
CSS Interview Questions
1. What is the Box model in CSS? Which CSS properties are a part of it?
2. What are the advantages of using CSS?
3. What are the limitations of CSS?
4. How to include CSS in the webpage?
5. What are the different types of Selectors in CSS?
6. What is a CSS Preprocessor? What are Sass, Less, and Stylus? Why do
people use them?
7. What is VH/VW (viewport height/ viewport width) in CSS?
8. Difference between reset vs normalize CSS?. How do they differ?
9. What is the difference between inline, inline-block, and block?
10. How do you test the webpage in different browsers?
11. What is a Pseudo element? What is pseudo-class?
12. How do you specify units in the CSS?. What are the different ways to do it?
13. Does margin-top or margin-bottom have an effect on inline elements?
Content: Actual Content of the box where the text or image placed.
Padding: Area surrounding the content (Space between the border and
content).
Border: Area surrounding the padding.
Browser Compatibility: Some style selectors are supported and some are not.
We have to determine which style is supported or not using the @support
selector).
Cross Browser issue: Some selectors behave differently in a different
browser).
There is no parent selector: Currently, Using CSS, you can’t select a parent
tag.
2 - Embed CSS with a style tag: A set of CSS styles included within your HTML page.
Add your CSS rules between the opening and closing style tags and write your
CSS exactly the same way as you do in stand-alone stylesheet files.
3 - Add inline styles to HTML elements(CSS rules applied directly within
an HTML tag.): Style can be added directly to the HTML element using a
style tag.
4 - Import a stylesheet file (An external file imported into another CSS file):
Another way to add CSS is by using the @import rule. This is to add a new CSS
file within CSS itself.
Class Selector: The class selector also matches all elements on the page
that have their class attribute set to the same value as the class. In the given
example, the provided styles will get applied to all the elements having ID as
the box on the page.
The selector will match all elements that have a class of box and that are
immediate children of the #container element. That means, unlike the
descendant combinator, there can’t be another element wrapping .box it has to
be a direct child element.
General Sibling Combinator: A selector that uses a general sibling
combinator to match elements based on sibling relationships. The selected
elements are beside each other in the HTML.
In this example, all paragraph elements (<p>) will be styled with the
specified rules, but only if they are siblings of <h2> elements. There could
be other elements in between the <h2> and <p> , and the styles would
still apply.
Adjacent Sibling Combinator: A selector that uses the adjacent sibling
combinator uses the plus symbol (+), and is almost the same as the general
sibling selector. The difference is that the targeted element must be an
The above example will apply the specified styles only to paragraph elements
that immediately follow other paragraph elements. This means the first
paragraph element on a page would not receive these styles. Also, if another
element appeared between two paragraphs, the second paragraph of the two
wouldn’t have the styles applied.
Attribute Selector: The attribute selector targets elements based on the
presence and/or value of HTML attributes, and is declared using square
brackets.
6. What is a CSS Preprocessor? What are Sass, Less,
and Stylus? Why do people use them?
A CSS Preprocessor is a tool used to extend the basic functionality of default
vanilla CSS through its own scripting language. It helps us to use complex
logical syntax like – variables, functions, mixins, code nesting, and inheritance
to name a few, supercharging your vanilla CSS.
SASS: Sass is the acronym for “Syntactically Awesome Style Sheets”. SASS can
be written in two different syntaxes using SASS or SCSS
SASS vs SCSS
SASS Syntax
SCSS Syntax
LESS: LESS is an acronym for “Leaner Stylesheets”. LESS is easy to add to any
javascript projects by using NPM or less.js file. It uses the extension .less.
LESS syntax is the same as the SCSS with some exceptions. LESS uses @ to define
the variables.
Stylus: Stylus offers a great deal of flexibility in writing syntax, supports native
CSS as well as allows omission of brackets, colons, and semicolons. It doesn’t
use @ or $ for defining variables.
:link
:visited
:hover
:active
:focus
Example of the pseudo-class, In the below example, the color applies to the
anchor tag when it’s hovered.
::before
::after
::first-letter
::first-line
::selection
In the below example, the color will appear only on the first line of the paragraph.
12. How do you specify units in the CSS?. What are the
different ways to do it?
There are different ways to specify units in CSS like px, em, pt, percentage (%).
px(Pixel) gives fine-grained control and maintains alignment because 1 px or
multiple of 1 px is guaranteed to look sharp. px is not cascade. em maintains
relative size. you can have responsive fonts. Em, will cascade 1em is equal to
the current font-size of the element or the browser default. If u sent font-size to
16px then 1em = 16px. The common practice is to set default body font-size to
62.5% (equal to 10px).
pt(point) are traditionally used in print. 1pt = 1/72 inch and it is a fixed-size unit.
%(percentage) sets font-size relative to the font size of the body. Hence, you
have to set the font-size of the body to a reasonable size.
13. Does margin-top or margin-bottom have an effect
on inline elements?
No, it doesn’t affect the inline elements. Inline elements flow with the contents of
the page.
Content-box: The default width and height values apply to the element's content
only. The padding and border are added to the outside of the box.
Padding-box: Width and height values apply to the element's content
and its padding. The border is added to the outside of the box. Currently,
only Firefox supports the padding-box value.
Border-box: Width and height values apply to the content, padding, and border.
CSS:
Centering with
transform HTML:
CSS:
Centering with flexbox
HTML:
CSS:
CSS:
20. What are the different ways to hide the element using
CSS?
Using display property(display: none). It’s not available for screen
readers. The element will not exist in the DOM if display: none is used.
Using visibility property(visibility: hidden), will take up the space of the
element. It will be available to screen reader users. The element will actually
be present in the DOM, but not shown on the screen.
Using position property (position: absolute). Make it available outside the screen.
No, the browsers will download the CSS in the order of its appearance on the
HTML page.
34. How to determine if the browser supports a certain
feature?
The @support in CSS can be very useful to scan if the current browser has
support for a certain feature.