Docsity

CSS Interview Questions and Core Concepts: A Comprehensive Guide, Lecture notes of Computer Fundamentals

Toggle sidebar
bg1
:
1️CSSBasics(BeginnerLevel)
CoreConcepts
CSS stands for CascadingStyleSheets.
Purpose: Style HTML elements (colors, fonts, layouts).
Ways to apply CSS:
1. InlineCSS: <p style="color:red;">Hello</p>
2. InternalCSS: Using <style> in <head>
3. ExternalCSS: Linking .css file: <link rel="stylesheet"
href="style.css">
Selectors
Element Selector: p { color: blue; }
Class Selector: .className { font-size: 16px; }
ID Selector: #idName { margin: 10px; }
Universal Selector: * { box-sizing: border-box; }
BoxModel
Components: content, padding, border, margin.
Example:
div {
width: 100px;
padding: 10px;
border: 5px solid black;
margin: 20px;
}
InterviewQuestions
1. What is CSS and why do we use it?
2. Explain different types of CSS selectors.
3. What is the CSS Box Model?
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download CSS Interview Questions and Core Concepts: A Comprehensive Guide and more Lecture notes Computer Fundamentals in PDF only on Docsity!

1️ ⃣CSSBasics(BeginnerLevel) CoreConcepts  CSS stands for CascadingStyleSheets.  Purpose: Style HTML elements (colors, fonts, layouts).  Ways to apply CSS:

  1. InlineCSS :

    Hello

  2. InternalCSS : Using
  3. Difference between inline, block, and inline-block.

2️ ⃣CSSIntermediate(Layouts&Styling) Positioning  static (default), relative, absolute, fixed, sticky. .relative-box { position: relative; top: 10px; } Flexbox  One-dimensional layout system. .container { display: flex; justify-content: center; align-items: center; } Grid  Two-dimensional layout system. .grid { display: grid; grid-template-columns: 1fr 2fr 1fr; gap: 10px; } Typography  font-family, font-size, line-height, letter-spacing. Colors  color, background-color, opacity, rgba(), hsla(). InterviewQuestions

  1. Difference between relative and absolute positioning.
  2. Flexbox vs Grid: When to use which?
  3. How does z-index work?
  4. Difference between em, rem, %, px.

body { background-color: lightblue; } } Units  Relative: %, em, rem, vw, vh.  Absolute: px, cm, in. Flex/GridforResponsiveness  Use flex-wrap and auto-fit/auto-fill in grids. InterviewQuestions

  1. How do you make a website mobile-friendly?
  2. Difference between em and rem.
  3. What are breakpoints and how do you use them?

5️ ⃣ExpertCSS(ModernTechniques&Optimization) CSSVariables :root { --main-color: blue; } h1 { color: var(--main-color); } CSSFunctions  calc(), clamp(), min(), max() ModernLayouts  subgrid, aspect-ratio CSSBestPractices  Minimize !important  Use classes over IDs  Organize files modularly (BEM methodology) InterviewQuestions

  1. Explain CSS variables and their scope.
  2. How does clamp() work in responsive design?
  1. What is BEM methodology and why use it?
  2. How to improve CSS performance for large projects?

CSSInterviewQuestions(1️ – 5️ 0) Basics(1️ – 1️ 5️ )

  1. What is CSS? o CSS (Cascading Style Sheets) is used to style HTML elements.
  2. What are the types of CSS? o Inline, Internal, External.
  3. What is the difference between inline, internal, and external CSS? o Inline: within element, Internal: in
  4. What is a media query? o CSS rule to apply styles based on screen size.
  5. Example of a media query:
  6. @media (max-width: 768px) { body { background: lightblue; } }
  7. What are CSS transitions? o Smooth changes between property values.
  8. Example of a transition:
  9. button { transition: background 0.3s ease; }
  10. button:hover { background: red; }
  11. What are CSS animations? o Keyframe-based animation of elements.
  12. Example of an animation:
  13. @keyframes move { 0%{left:0;} 100%{left:100px;} }
  14. div { animation: move 2s infinite; }
  15. What are CSS variables? o Custom properties to reuse values. Example: --main-color: blue;.
  16. What is the difference between absolute and fixed positioning? o Absolute: relative to nearest positioned ancestor, Fixed: relative to viewport.
  17. Difference between inline and inline-block? o Inline-block allows width/height, inline doesn’t.
  18. Difference between position: sticky and position: fixed? o Sticky scrolls until a point, Fixed stays visible always.

Advanced(3️ 1️ –5️ 0)

  1. How do you make a website responsive? o Media queries + flexible units (%, em, rem, vw).
  1. Difference between em and rem? o em: relative to parent, rem: relative to root.
  2. How do ::before and ::after work? o Create pseudo-elements before or after content.
  3. Difference between relative and static position? o Static is default, relative allows offset positioning.
  4. Difference between absolute and sticky? o Absolute: ignores scrolling, Sticky: scrolls until boundary.
  5. How to create a CSS-only tooltip? o Use ::after and :hover to show hidden content.
  6. How to create a CSS triangle? div { width: 0; height: 0; border-left: 50px solid transparent; border-right: 50px solid transparent; border-bottom: 100px solid red;

3️ 8. Whatisthedifferencebetweenrelative,absolute, fixed,sticky,andstatic? o Relative: offset from itself, Absolute: offset from ancestor, Fixed: viewport, Sticky: scrolls + sticks, Static: default. 3️ 9. Differencebetweenopacityandvisibility? o Opacity changes transparency, Visibility hides but keeps space. 4️ 0. Whatisclamp()inCSS? o Function to restrict a value between min and max.

  1. Whatisminmax()inCSSGrid? o Allows grid track to have min & max size. 4️ 2️. WhatareCSSfunctionsforresponsivelayouts?

An id selector is unique and should be used only once per page, while a class selector can be used on multiple elements.

  1. WhatisinlineCSSandwhenshoulditbeused? Inline CSS is written directly inside an HTML element using the style attribute. It is mainly used for quick styling or testing but not for large projects.
  2. WhatisinternalCSSandwhereisitused? Internal CSS is written inside a style tag in the head section of an HTML page. It is used to style a single page without needing an external CSS file.
  3. WhatisexternalCSSandwhyisitpreferred? External CSS is written in a separate .css file and linked to HTML using the link tag. It is preferred because it can be reused on multiple pages and is easier to maintain.
  4. Whatisthedifferencebetweenrelativeunitslikeem andremandabsoluteunitslikepx? PX is a fixed size. EM is relative to the parent element’s font size. REM is relative to the root element’s font size.
  5. Whatisthedifferencebetweeninline,block,and inline-blockelements? Inline elements do not start on a new line and only take the width of their content. Block elements start on a new line and take the full width. Inline-block elements behave like inline but you can set width and height.
  6. Whatisthedifferencebetweendisplay:noneand visibility:hidden? Display none removes the element completely from the layout. Visibility hidden hides the element but it still occupies space on the page.
  7. Whatisthedifferencebetweenposition:staticand position:relative? Static is the default position and elements follow the normal flow of the page. Relative allows you to move the element relative to its normal position.
  8. Whatisthedifferencebetweenabsoluteandfixed positioning? Absolute positions an element relative to its nearest positioned ancestor. Fixed positions an element relative to the viewport and it does not move when scrolling.
  1. Whatarepseudo-classesinCSS? Pseudo-classes are used to define a special state of an element, like hover, focus, or active. For example, a:hover changes the color when you hover over a link.
  2. Whatarepseudo-elementsinCSS? Pseudo-elements let you style specific parts of an element. For example, p::first-letter makes the first letter of a paragraph larger.
  3. Whatisthedifferencebetweenemand%? Em is relative to the parent element’s font size, while % is relative to the parent element’s dimensions like width or height.
  4. WhatistheCSSBoxModel? The box model describes the spacing around elements, including content, padding, border, and margin.
  5. WhatareCSStransitions? Transitions let you change CSS properties smoothly over time, for example, changing a button color when hovered.
  6. WhatareCSSanimations? Animations let you move or change elements using keyframes, like sliding an image from left to right repeatedly.
  7. WhatisFlexbox? Flexbox is a layout system for arranging items in a row or column and aligning them easily.
  8. WhatisCSSGrid? Grid is a layout system for arranging items in rows and columns, giving more control over complex layouts.
  9. WhatisthedifferencebetweenFlexboxandGrid? Flexbox is for one-dimensional layouts, like a row or column. Grid is for two-dimensional layouts with rows and columns.
  10. Whatisamediaquery? Media queries allow you to apply CSS styles based on screen size, making a website responsive.
  11. WhatareCSSvariables? CSS variables are custom properties you can reuse in your CSS. For example, --main-color: red; can be used throughout the stylesheet.
  12. Whatisthedifferencebetweenrelativeandsticky positioning? Relative always moves an element relative to its normal position.

ancestor. Fixed elements stay in the same position on the screen even when scrolling.

  1. What is the difference between inline-block and float in CSS? Inline-block allows elements to sit next to each other while still respecting width and height. Float moves elements to the left or right and lets text wrap around them.
  2. What is the difference between relative and z-index in CSS? Relative is a positioning type that moves elements from their normal position. Z-index controls the stacking order of positioned elements.
  3. How do you make a website responsive using CSS? By using flexible units like %, em, rem, viewport units, and using media queries to adjust styles at different screen sizes.
  4. What are the common units used in CSS? Pixels (px), percentages (%), em, rem, viewport width (vw), viewport height (vh), and points (pt).
  5. What is the difference between pseudo-class :nth-child() and :nth-of-type()? :nth-child() selects an element based on its position among all children. :nth-of-type() selects an element based on its position among siblings of the same type.
  6. What is the difference between position: relative and transform: translate()? Relative moves the element visually but the original space remains in the layout. Translate moves the element visually without affecting surrounding elements’ space.
  7. How does the CSS property float work? Float moves an element to the left or right and allows other elements to wrap around it.
  8. What is the difference between visibility: hidden and opacity: 0? Visibility hidden hides an element but keeps its space. Opacity 0 makes it invisible but it still responds to events like clicks.
  9. What is the difference between absolute and relative positioning for flex items? Absolute items ignore flex alignment and are positioned relative to the nearest positioned ancestor. Relative items stay within the flex container and move relative to their normal position.
  1. What is the difference between relative and sticky in a scrolling context? Relative moves with its normal flow. Sticky scrolls with the page until it reaches a defined boundary, then sticks in place.
  2. How does CSS specificity work? CSS specificity determines which rule applies if multiple rules match. Inline styles have highest specificity, then id selectors, then class/pseudo-class/attribute selectors, then element selectors.
  3. What is the difference between absolute and fixed in relation to scroll? Absolute elements move relative to their ancestor and scroll with the page. Fixed elements stay in the same position relative to the viewport.
  4. How do you center an element horizontally and vertically using Flexbox? By setting the container to display: flex, justify-content: center, and align-items: center.
  5. How do you create a CSS-only tooltip? By using a combination of a parent element, a hidden span with content, and a pseudo-class like :hover to show the tooltip when hovering.