Comprehensive Guide to CSS Welcome to the comprehensive guide on CSS (Cascading Style Sheets)! CSS is a cornerstone technology of the web, enabling you to create visually appealing and responsive websites. This guide is designed to take you from the basics of CSS to more advanced topics, complete with code examples, detailed explanations, exercises, and multiple-choice questions to test your understanding. Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 1 Comprehensive Guide to CSS 1 1. Introduction to CSS 5 What is CSS? 5 Why Use CSS? 5 Basic Example 5 2. CSS Syntax and Selectors 6 Basic Syntax 6 Basic Selectors 7 Element Selector 7 Class Selector 7 ID Selector 7 Grouping Selectors 7 Combinators 8 Descendant Selector 8 Child Selector 8 Adjacent Sibling Selector 8 General Sibling Selector 8 Pseudo-classes and Pseudo-elements 9 Pseudo-classes 9 Pseudo-elements 9 Attribute Selectors 9 3. CSS Box Model 10 Understanding the Box Model 10 Box Sizing 11 Content-box (Default) 11 Border-box 11 Example: Box Model in Action 12 4. CSS Colors and Backgrounds 13 Color Values 13 Background Properties 14 Background Color 14 Background Image 14 Background Repeat 14 Background Position 14 Background Size 15 Shorthand Background Property 15 Example: Styling a Header with Background 15 5. CSS Typography 16 Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 2 Font Properties 16 font-family 16 font-size 17 font-weight 17 font-style 17 line-height 17 Text Properties 18 color 18 text-align 18 text-decoration 18 text-transform 18 letter-spacing and word-spacing 18 Example: Styling Text 19 6. CSS Layout 20 Display Property 20 Block 20 Inline 21 Inline-block 21 None 21 Positioning 21 Static 21 Relative 21 Absolute 22 Fixed 22 Sticky 22 Flexbox 23 Basic Flexbox Example 23 Common Flexbox Properties 24 CSS Grid 24 Basic Grid Example 25 Advanced Grid Features 26 Example: Creating a Responsive Layout with Flexbox and Grid 27 7. Responsive Design 29 Media Queries 29 Fluid Layouts and Units 30 Relative Units 30 Flexible Grid Systems 31 Mobile-First Approach 31 Example: Responsive Image Gallery 32 8. CSS Transitions and Animations 33 Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 3 Transitions 33 Transition Shorthand 34 Animations 34 Advanced Animation Example 35 Example: Hover Animation 35 9. CSS Variables (Custom Properties) 36 Defining and Using CSS Variables 36 Fallback Values 37 Dynamic Themes with CSS Variables 37 10. Advanced Selectors and Specificity 39 Specificity 39 Advanced Selectors 40 Universal Selector 40 Child Combinator 40 Adjacent Sibling Selector 41 General Sibling Selector 41 Attribute Selectors 41 Pseudo-classes and Pseudo-elements 41 Descendant Selector 42 Combining Selectors 42 11. CSS Best Practices 42 1. Keep CSS Organized 42 2. Avoid Over-Specificity 43 3. Use Comments 43 4. Leverage CSS Variables 43 5. Minimize Use of !important 43 6. Optimize for Performance 44 7. Responsive Design 44 8. Accessibility 44 Example: Organized and Maintainable CSS 44 12. Projects and Exercises 46 Project 1: Personal Portfolio Website 46 Exercise 1: Creating and Styling a Navigation Bar 48 Exercise 2: Building a Responsive Grid Layout 50 Exercise 3: Implementing a Hover Effect with Transition 51 13. Multiple Choice Questions 52 Question 1 52 Question 2 53 Question 3 53 Question 4 54 Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 4 Question 5 54 Question 6 55 Question 7 55 Question 8 56 Question 9 56 Question 10 56 Question 11 57 Question 12 57 Question 13 58 Question 14 58 Question 15 59 14. Conclusion 59 1. Introduction to CSS What is CSS? CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of a document written in HTML or XML. CSS defines how elements should be displayed on screen, on paper, in speech, or on other media. Why Use CSS? ● Separation of Concerns: Separates content (HTML) from presentation (CSS). ● Reusability: Apply the same styles to multiple elements. ● Maintainability: Easier to update and manage styles across large websites. ● Enhanced Design: Create visually appealing and responsive layouts. Basic Example CSS Example

Welcome to CSS!

Explanation: ● The
This is a box.
Explanation: ● The .boxelement has a set width, padding, border, and margin. ● box-sizing: border-box;ensures the total width remains 200px. ● Changing to content-boxwould make the total width 200px + 40px + 10px = 250px. Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 12 4. CSS Colors and Backgrounds Colors and backgrounds are essential for creating visually appealing websites. CSS provides various ways to specify colors and manage backgrounds. Color Values CSS allows you to specify colors using different formats: Named Colors: Predefined color names. p { color: navy; } Hexadecimal Notation: #RRGGBBor shorthand #RGB. h1 { color: #ff5733; } RGB: rgb(red, green, blue)with values from 0 to 255. div { background-color: rgb(255, 0, 0); /* Red */ } RGBA: rgba(red, green, blue, alpha)where alpha is opacity (0 to 1). div { background-color: rgba(0, 0, 255, 0.5); /* Semi-transparent blue */ } HSL: hsl(hue, saturation%, lightness%). span { color: hsl(120, 100%, 50%); /* Green */ } Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 13 HSLA: hsla(hue, saturation%, lightness%, alpha). span { color: hsla(240, 100%, 50%, 0.3); /* Semi-transparent blue */ } Background Properties CSS provides a range of properties to control backgrounds. Background Color Sets the background color of an element. body { background-color: #f0f8ff; /* AliceBlue */ } Background Image Sets a background image for an element. div { background-image: url('images/background.jpg'); } Background Repeat Controls how the background image repeats. div { background-repeat: no-repeat; /* Options: repeat, repeat-x, repeat-y, no-repeat */ } Background Position Sets the initial position of the background image. div { Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 14 background-position: center center; /* Options: top, bottom, left, right, center, or specific coordinates */ } Background Size Specifies the size of the background image. div { background-size: cover; /* Options: auto, cover, contain, or specific dimensions */ } Shorthand Background Property Combines multiple background properties into one. div { background: url('images/bg.png') no-repeat center center / cover #ffffff; } Explanation: ● url('images/bg.png')sets the background image. ● no-repeatprevents repetition. ● center centerpositions the image at the center. ● / coversets the background size to cover the element. ● #ffffffsets the background color to white. Example: Styling a Header with Background Background Example
Welcome to My Website
Explanation: ● The .headerdiv has a background image that covers the entire area. ● Flexbox centers the text both vertically and horizontally. ● Text color is set to white for contrast. 5. CSS Typography Typography plays a crucial role in web design, affecting readability and user experience. CSS provides extensive control over text styling. Font Properties font-family Specifies the typeface to be used for text. body { Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 16 font-family: "Helvetica Neue", Arial, sans-serif; } Explanation: ● Lists fonts in order of preference. ● If the first font isn't available, the browser tries the next one. font-size Sets the size of the font. h1 { font-size: 2em; /* Relative to the parent element's font size */ } p { font-size: 16px; /* Absolute size */ } font-weight Sets the weight (boldness) of the font. strong { font-weight: bold; } .light-text { font-weight: 300; } font-style Sets the style of the font (e.g., italic). em { font-style: italic; } line-height Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 17 Sets the height between lines of text. p { line-height: 1.5; } Text Properties color Sets the color of the text. h2 { color: darkgreen; } text-align Sets the horizontal alignment of text. p { text-align: justify; } text-decoration Adds decorations to text, such as underline. a { text-decoration: none; } text-transform Controls the capitalization of text. .uppercase { text-transform: uppercase; } letter-spacing and word-spacing Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 18 Adjusts spacing between letters and words. h3 { letter-spacing: 2px; } p { word-spacing: 4px; } Example: Styling Text Typography Example

Welcome to My Blog

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Read more about our latest updates and features.

Explanation: ● The body uses a serif font with a comfortable line height. ● The

is large, centered, and uppercase. ● Paragraphs are slightly larger and justified. ● Links are styled with a distinct color and underline on hover. 6. CSS Layout CSS provides powerful tools to create complex and responsive layouts. This section covers various layout techniques. Display Property The displayproperty defines how an element is rendered on the page. Block Elements occupy the full width available and start on a new line. div { display: block; } Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 20 Inline Elements occupy only the width necessary and do not start on a new line. span { display: inline; } Inline-block Elements behave like inline elements but can have set widths and heights. img { display: inline-block; width: 100px; height: 100px; } None Elements are not displayed on the page. .hidden { display: none; } Positioning The positionproperty controls how an element is positioned in the document. Static Default positioning. Elements follow the normal flow of the page. p { position: static; } Relative Positions the element relative to its normal position. Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 21 .box { position: relative; top: 10px; /* Moves the element 10px down */ left: 20px; /* Moves the element 20px to the right */ } Absolute Positions the element relative to its first positioned ancestor. .container { position: relative; } .box { position: absolute; top: 50px; right: 30px; } Explanation: ● .boxis positioned 50px from the top and 30px from the right of .container. ● If .containerhas position: relative;, .boxis positioned within .container. Fixed Positions the element relative to the viewport, remaining in the same place even when scrolling. .navbar { position: fixed; top: 0; width: 100%; } Sticky Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 22 Positions the element based on the user's scroll position. It toggles between relative and fixed. .header { position: sticky; top: 0; background-color: white; } Explanation: ● .headersticks to the top of the viewport when scrolled to its position. Flexbox Flexbox is a one-dimensional layout method for arranging items in rows or columns. Basic Flexbox Example Flexbox <style> .container Example { display: flex; justify-content: space-between; /* Align items horizontally */ align-items: center; /* Align items vertically */ height: 100vh; padding: 20px; background-color: #f8f8f8; } .box { width: 100px; height: 100px; background-color: #3498db; color: white; Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 23 display: flex; align-items: center; justify-content: center; border-radius: 5px; }
class="box">1
class="box">2
class="box">3 Explanation: ● .containeruses display: flex;to create a flex container. ● justify-content: space-between;distributes space between items. ● align-items: center;centers items vertically. ● .boxelements are flex items, centered both horizontally and vertically. Common Flexbox Properties ● flex-direction: Defines the direction of the flex items (row, column, row-reverse, column-reverse). ● justify-content: Aligns items along the main axis (flex-start, flex-end, center, space-between, space-around). ● align-items: Aligns items along the cross axis (flex-start, flex-end, center, stretch). ● flex-wrap: Controls whether flex items should wrap onto multiple lines (nowrap, wrap, wrap-reverse). CSS Grid Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 24 CSS Grid is a two-dimensional layout system for creating complex and responsive grid-based layouts. Basic Grid Example Grid Example
class="grid-item">1
class="grid-item">2
class="grid-item">3 class="grid-item">4 class="grid-item">5 class="grid-item">6 Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 25 Explanation: ● .grid-containeruses display: grid;to establish a grid layout. ● grid-template-columns: repeat(3, 1fr);creates three equal-width columns. ● grid-gap: 10px;sets the spacing between grid items. ● .grid-itemstyles individual grid cells. Advanced Grid Features Grid Areas: .grid-container { display: grid; grid-template-areas: "header header" "sidebar content" } .header "footer footer"; { grid-area: header; } .sidebar { grid-area: sidebar; } .content { grid-area: content; } .footer { grid-area: footer; } Responsive Grid: @media (max-width: 600px) { .grid-container { grid-template-columns: 1fr; grid-template-areas: Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 26 "header" "content" "sidebar" "footer"; } } Explanation: ● grid-template-areasdefines named grid areas for easier placement of elements. ● Media queries adjust the grid layout for different screen sizes, ensuring responsiveness. Example: Creating a Responsive Layout with Flexbox and Grid Responsive Layout

Main Content

Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 28

Welcome to the main content area.

Explanation: ● Navbar and Footer: Use full-width grid areas. ● Container: Uses CSS Grid to layout the sidebar and content. ● Responsive Design: At screen widths below 800px, the layout stacks vertically. 7. Responsive Design Responsive design ensures that web pages look and function well on all devices, from desktops to smartphones. CSS offers several tools to achieve responsiveness. Media Queries Media queries apply CSS rules based on device characteristics like screen width, height, orientation, and resolution. Syntax: @media (condition) { /* CSS rules */ } Example: /* Styles for screens wider than 600px */ @media (min-width: 600px) { .container { display: flex; } Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 29 } /* Styles for screens 600px or narrower */ @media (max-width: 600px) { .container { display: block; } } Common Breakpoints: ● Mobile: max-width: 600px ● Tablet: min-width: 601pxand max-width: 1024px ● Desktop: min-width: 1025px Fluid Layouts and Units Using relative units makes layouts more adaptable to different screen sizes. Relative Units % (Percentage): Relative to the parent element. .box { width: 50%; /* 50% of the parent’s width */ } em: Relative to the font-size of the element. p { font-size: 1.2em; /* 1.2 times the parent’s font-size */ } rem: Relative to the root (html) font-size. h1 { font-size: 2rem; /* 2 times the root font-size */ } vw and vh: Relative to the viewport’s width and height. .banner { Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 30 width: 100vw; /* 100% of the viewport width */ height: 50vh; /* 50% of the viewport height */ } Flexible Grid Systems Using percentages and relative units to create flexible grid layouts that adjust to screen sizes. Example: .container { display: flex; flex-wrap: wrap; } .column { flex: 1 1 300px; /* Grow, shrink, basis */ margin: 10px; } Explanation: ● .columnelements will flexibly adjust their width, wrapping to new lines as needed based on available space. Mobile-First Approach Designing for mobile devices first and then enhancing for larger screens. Example: /* Mobile styles */ .container { display: block; } /* Enhancements for larger screens */ @media (min-width: 600px) { .container { display: flex; Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 31 } } Advantages: ● Prioritizes essential content and performance for mobile users. ● Easier to add enhancements for larger screens. Example: Responsive Image Gallery Responsive Gallery Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 32 Explanation: ● Grid Layout: Uses auto-filland minmaxto create a responsive grid that adjusts the number of columns based on available space. ● Images: Set to width: 100%to fill their grid cells and maintain aspect ratio with height: auto. 8. CSS Transitions and Animations Enhance user experience by adding dynamic visual effects using CSS transitions and animations. Transitions Transitions allow you to change property values smoothly over a specified duration. Syntax: selector { transition: property duration timing-function delay; } Example: .button { background-color: #3498db; transition: background-color 0.3s ease; } .button:hover { background-color: #2980b9; } Explanation: ● The .buttonchanges its background color smoothly over 0.3 seconds when hovered. Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 33 ● easedefines the transition timing function. Transition Shorthand transition: background-color 0.3s ease 0s; Animations Animations allow more complex and multi-step transitions. Syntax: selector { animation: name duration timing-function delay iteration-count direction fill-mode; } @keyframes Rule: Defines the animation sequence. Example: /* Define the animation */ @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } } .box { width: 100px; height: 100px; background-color: #e74c3c; animation: fadeIn 2s ease-in-out; } Explanation: ● @keyframes fadeIndefines an animation that changes opacity from 0to 1. ● .boxapplies the fadeInanimation over 2 seconds with an ease-in-out timing function. Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 34 Advanced Animation Example @keyframes moveRight { 0% { transform: translateX(0); } 50% { transform: translateX(100px); } 100% { transform: translateX(0); } } .moving-box { width: 50px; height: 50px; background-color: #2ecc71; animation: moveRight 3s infinite; } Explanation: ● The .moving-boxmoves 100px to the right and back to the original position repeatedly every 3 seconds. Example: Hover Animation Hover Animation
Hover Me!
Explanation: ● The .cardmoves up by 10px and its shadow becomes more prominent when hovered, creating a lifting effect. 9. CSS Variables (Custom Properties) CSS Variables, also known as Custom Properties, allow you to store reusable values directly in your CSS. They enhance maintainability and make it easier to implement themes. Defining and Using CSS Variables Syntax: :root { --primary-color: #3498db; --secondary-color: #2ecc71; --font-stack: 'Helvetica Neue', Arial, sans-serif; } .button { background-color: var(--primary-color); Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 36 color: white; font-family: var(--font-stack); } .button-secondary { background-color: var(--secondary-color); color: white; font-family: var(--font-stack); } Explanation: ● :rootis a pseudo-class that matches the document's root element (). Variables defined here are globally accessible. ● --primary-color, --secondary-color, and --font-stackare custom properties. ● var(--property-name)is used to retrieve the value of a custom property. Fallback Values Provide fallback values in case the variable is not defined. p { color: var(--text-color, #333); } Explanation: ● If --text-coloris not defined, the color defaults to #333. Dynamic Themes with CSS Variables Changing variables at runtime allows for dynamic theming. Example: CSS Variables Theme Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 37

Welcome!

This is an example of CSS variables.

Learn more

Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 38 Explanation: ● CSS variables define default theme colors. ● The .dark-themeclass overrides these variables. ● Clicking the button toggles the .dark-themeclass, switching between light and dark themes. 10. Advanced Selectors and Specificity Mastering selectors and understanding specificity is key to writing effective and conflict-free CSS. Specificity Specificity determines which CSS rule is applied when multiple rules target the same element. Specificity Hierarchy: 1. Inline styles: Highest specificity (e.g., style="color: red;"). 2. IDs: High specificity (e.g., #header). 3. Classes, attributes, pseudo-classes: Medium specificity (e.g., .button, [type="text"], :hover). 4. Elements and pseudo-elements: Low specificity (e.g., div, p, ::before). Calculating Specificity: ● Inline styles: 1000 ● IDs: 100 per ID ● Classes, attributes, pseudo-classes: 10 per item ● Elements and pseudo-elements: 1 per item Example: /* Specificity score: 10 */ .button { color: blue; } /* Specificity score: 100 */ Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 39 #submit-button { color: green; } /* Specificity score: 11 */ button[type="submit"] { color: red; } /* Specificity score: 1 */ button { color: black; } Explanation: ● An element with id="submit-button"will have its color set to green, overriding other rules. ● If an element has both a class and an element selector, the class selector takes precedence. Advanced Selectors Universal Selector Targets all elements. * { box-sizing: border-box; } Explanation: ● Applies box-sizing: border-box;to all elements, simplifying layout calculations. Child Combinator Targets direct children of an element. ul > li { Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 40 list-style-type: disc; } Adjacent Sibling Selector Targets an element that is immediately preceded by another. h2 + p { margin-top: 0; } General Sibling Selector Targets all siblings after a specified element. h2 ~ p { color: gray; } Attribute Selectors Select elements based on attribute values. input[type="email"] { border: 2px solid blue; } Pseudo-classes and Pseudo-elements Pseudo-classes: Target elements in a specific state. a:hover { color: red; } Pseudo-elements: Target specific parts of an element. p::first-line { font-weight: bold; } Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 41 Descendant Selector Targets elements nested within other elements. div p { margin-bottom: 10px; } Explanation: ● Applies to all

elements inside

elements, regardless of depth. Combining Selectors Combine multiple selectors to target specific elements. /* Targets elements inside
Explanation: ● .grid-containeruses CSS Grid to create three equal columns with gaps. ● .grid-itemelements are styled with background color and padding. ● Media query changes the layout to a single column on screens narrower than 600px. Exercise 3: Implementing a Hover Effect with Transition Task: Create a square div that changes its background color and scales up slightly when hovered, using CSS transitions. Solution: Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 51 Hover Effect
Explanation: ● .squareis a green square with smooth transitions. ● On hover, the background color darkens, and the square scales up by 10%. 13. Multiple Choice Questions Test your understanding of CSS with the following multiple-choice questions. Answers and explanations are provided after each question. Question 1 Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 52 Which of the following is the correct way to apply a class named "active" to a
element in CSS? A) div.active { /* styles */ } B) div .active { /* styles */ } C) div #active { /* styles */ } D) .div.active { /* styles */ } Answer: A) div.active { /* styles */ } Explanation: ● div.activetargets
elements with the class active. ● Option B (div .active) targets elements with class activeinside a
. ● Option C uses ID selector instead of class. ● Option D incorrectly uses a dot before div. Question 2 What does the flex-direction: column;property do in Flexbox? A) Aligns items horizontally. B) Aligns items vertically. C) Reverses the order of items. D) Wraps items onto multiple lines. Answer: B) Aligns items vertically. Explanation: ● flex-direction: column;stacks flex items vertically from top to bottom. Question 3 Which property is used to change the text color of an element? Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 53 A) background-color B) font-color C) color D) text-color Answer: C) color Explanation: ● The colorproperty sets the color of the text. Question 4 How can you make a text bold in CSS? A) font-style: bold; B) text-weight: bold; C) font-weight: bold; D) text-style: bold; Answer: C) font-weight: bold; Explanation: ● The font-weightproperty controls the boldness of the text. Question 5 Which CSS property controls the space between lines of text? A) letter-spacing B) line-height C) text-spacing Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 54 D) word-spacing Answer: B) line-height Explanation: ● The line-heightproperty sets the height between lines of text. Question 6 What does the box-sizing: border-box;property do? A) Includes padding and border in the element's total width and height. B) Excludes padding and border from the element's total width and height. C) Sets the box to have a fixed size. D) Makes the box responsive to content size. Answer: A) Includes padding and border in the element's total width and height. Explanation: ● box-sizing: border-box;ensures that widthand heightinclude content, padding, and border. Question 7 Which of the following is NOT a valid CSS color format? A) rgb(255, 0, 0) B) #FF0000 C) hsl(0, 100%, 50%) D) rgba(255, 0, 0, 256) Answer: D) rgba(255, 0, 0, 256) Explanation: Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 55 ● The alpha value in rgbashould be between 0and 1. 256is invalid. Question 8 Which CSS property is used to create space between the border and the content of an element? A) margin B) padding C) border-spacing D) gap Answer: B) padding Explanation: ● paddingcreates space inside the element between the border and content. Question 9 How do you select all

elements that are direct children of a

? A) div p { /* styles */ } B) div > p { /* styles */ } C) div + p { /* styles */ } D) div ~ p { /* styles */ } Answer: B) div > p { /* styles */ } Explanation: ● The >combinator targets direct children only. Question 10 Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 56 Which property is used to make an element stay fixed in place even when the page is scrolled? A) position: B) position: relative; absolute; C) position: D) position: fixed; sticky; Answer: C) position: fixed; Explanation: ● position: fixed;fixes the element relative to the viewport, maintaining its position during scroll. Question 11 Which CSS property allows you to control the opacity of an element? A) visibility B) opacity C) display D) z-index Answer: B) opacity Explanation: ● The opacityproperty sets the transparency level of an element. Question 12 What is the default value of the displayproperty for elements? A) block Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 57 B) inline C) inline-block D) flex Answer: B) inline Explanation: ● elements are inline by default. Question 13 Which CSS property is used to change the font of an element? A) font-style B) font-weight C) font-family D) font-size Answer: C) font-family Explanation: ● font-familyspecifies the typeface for text. Question 14 How can you center a block-level element horizontally within its container? A) text-align: center; B) margin: auto; C) display: flex; justify-content: center; D) All of the above Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 58 Answer: D) All of the above Explanation: ● Option A: text-align: center;centers inline and inline-block elements. ● Option B: margin: auto;centers block-level elements with a defined width. ● Option C: Using Flexbox to center elements. Question 15 Which pseudo-class would you use to style an element when it is being hovered over by the mouse? A) :active B) :focus C) :hover D) :visited Answer: C) :hover Explanation: ● :hoverapplies styles when the user hovers over an element. 14. Conclusion Congratulations! You've completed the comprehensive guide to CSS. This guide has covered everything from the basics of CSS syntax and selectors to advanced topics like Flexbox, Grid, responsive design, transitions, animations, and best practices. By working through the code examples, exercises, and multiple-choice questions, you've built a solid foundation in CSS that will empower you to create stunning and responsive web designs. Learn more HTML, CSS, JavaScript Web Development at https://basescripts.com/ Laurence Svekis 59