CSS for styling website
Css interview questions
- 1. What is the Box model in CSS? Which CSS properties are a part of it?A rectangle box is wrapped around every HTML element. The box model is used to determine the height and width of the rectangular box. The CSS Box consists of Width and height (or in the absence of that, default values and the content inside), padding, borders, margin. 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. Margin: Area surrounding the border.
- 2. What are the advantages of using CSS?The main advantages of CSS are given below: Separation of content from presentation - CSS provides a way to present the same content in multiple presentation formats in mobile or desktop or laptop. Easy to maintain - CSS, built effectively can be used to change the look and feel complete by making small changes. To make a global change, simply change the style, and all elements in all the web pages will be updated automatically. Bandwidth - Used effectively, the style sheets will be stored in the browser cache and they can be used on multiple pages, without having to download again.
- 3. What are the limitations of CSS?Disadvantages of CSS are given below: 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.
- 4. How to include CSS in the webpage?1 - External Style Sheet: An external file linked to your HTML document: Using link tag, we can link the style sheet to the HTML page. 2 - Embed CSS with a style tag: A set of CSS styles included within your HTML page. /*Add style rules here*/ 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.
Inline Style
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. @import "path/to/style.css"; - 5. What are the different types of Selectors in CSS?A CSS selector is the part of a CSS ruleset that actually selects the content you want to style. Different types of selectors are listed below. Universal Selector: The universal selector works like a wildcard character, selecting all elements on a page. In the given example, the provided styles will get applied to all the elements on the page. * { color: "green"; font-size: 20px; line-height: 25px; } Element Type Selector: This selector matches one or more HTML elements of the same name. In the given example, the provided styles will get applied to all the ul elements on the page. ul { line-style: none; border: solid 1px #ccc; } ID Selector: This selector matches any HTML element that has an ID attribute with the same value as that of the selector. In the given example, the provided styles will get applied to all the elements having ID as a container on the page. #container { width: 960px; margin: 0 auto; } 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. .box { padding: 10px; margin: 10px; width: 240px; } Descendant Combinator: The descendant selector or, more accurately, the descendant combinator lets you combine two or more selectors so you can be more specific in your selection method. #container .box { float: left; padding-bottom: 15px; }
Title
Paragraph example.
Paragraph example.
Paragraph example.
Paragraph example.
) will be styled with the specified rules, but only if they are siblings of
elements. There could be other elements in between the
and
, 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 immediate sibling, not just a general sibling. p + p { text-indent: 1.Sem; margin-bottom: 0; }
Title
Paragraph example.
Paragraph example.
Paragraph example.
Paragraph example.
Paragraph example.
- 7. What is VH/VW (viewport height/ viewport width) in CSS?It’s a CSS unit used to measure the height and width in percentage with respect to the viewport. It is used mainly in responsive design techniques. The measure VH is equal to 1/100 of the height of the viewport. If the height of the browser is 1000px, 1vh is equal to 10px. Similarly, if the width is 1000px, then 1 vw is equal to 10px.
- 8. Difference between reset vs normalize CSS?. How do they differ?Reset CSS: CSS resets aim to remove all built-in browser styling. For example margins, paddings, font-sizes of all elements are reset to be the same. Normalize CSS: Normalize CSS aims to make built-in browser styling consistent across browsers. It also corrects bugs for common browser dependencies.
- 9. What is the difference between inline, inline-block, and block?Block Element: The block elements always start on a new line. They will also make space for an entire row or width. List of block elements are,
. Inline Elements: Inline elements don't start on a new line, they appear on the same line as the content and tags beside them. Some examples of inline elements are , , , and tags. Inline Block Elements: Inline-block elements are similar to inline elements, except they can have padding and margins added on all four sides.
10. How do you test the webpage in different browsers?It’s most important to test a website in different browsers when you’re first designing it, or when making major changes. However, it’s also important to repeat these tests periodically, since browsers go through a lot of updates and changes.11. What is a Pseudo element? What is pseudo-class?Pseudo-classes select regular elements but under certain conditions like when the user is hovering over the link. :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. /* mouse over link */ a:hover { color: #FFOOFF; } A pseudo-element however allows us to create items that do not normally exist in the document tree, for example ::after. ::before ::after ::first-letter ::first-line ::selection In the below example, the color will appear only on the first line of the paragraph. p: :first-line { color: #ffOOOO; font-variant: small-caps; }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.14. Different Box Sizing Property?The box-sizing CSS property sets how the total width and height of an element are calculated. 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.15. Can you name the four types of @media properties?The four types of @media properties are: All → It’s the default property. Used for all media-type devices. Screen → Used for computer screen, mobile screen. Print → Used for printers. Speech → Used for screen readers.16. What is the grid system?CSS Grid Layout is the most powerful layout system available in CSS. It is said to be a 2-dimensional system, meaning it can handle both columns and rows, unlike flexbox which is largely a 1-dimensional system.17. 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.18. What does the :root pseudo-class refer to?The :root selector allows you to target the highest-level “parent” element in the DOM, or document tree. It is defined in the CSS Selectors Level 3 specification.19. What does Accessibility (a11y) mean?Accessibility refers to how software or hardware combinations are designed to make a system accessible to persons with disabilities, such as visual impairment, hearing loss, or limited dexterity. For example, a website developed with accessibility in mind might have text-to-speech capabilities. In the USA public websites have to have accessible compliance. It’s defined in 508 compliance. It gives the guidelines and best practices for all website users that should be met with key areas of accessibility.20. How do I restore the default value of a property?The keyword initial can be used to reset it to its default value.