CSS Styling and Selectors Quiz

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which of the following is NOT a location where CSS style rules can be defined?

  • Internal JavaScript File (correct)
  • Embedded Style Sheet
  • External Style Sheet
  • Inline Styles

Inline styles are generally preferred for web development because they promote maintainability and reduce bandwidth usage.

False (B)

What is the primary advantage of using external style sheets for web development?

maintainability

Style rules placed within an HTML element via the ______ attribute are known as inline styles.

<p>style</p> Signup and view all the answers

Which type of CSS is most efficient for styling multiple pages on a website?

<p>External (A)</p> Signup and view all the answers

What is the purpose of CSS selectors?

<p>To specify which HTML elements will be styled. (C)</p> Signup and view all the answers

Embedded styles are ideal for consistently styling multiple HTML documents in a large website.

<p>False (B)</p> Signup and view all the answers

Match the CSS style location with its characteristic:

<p>Inline Styles = Overrides other style definitions and is defined within an HTML element. Embedded Style Sheet = Contained within the <code>&lt;style&gt;</code> element inside the <code>&lt;head&gt;</code> of an HTML document. External Style Sheet = Stored in a separate <code>.css</code> file and provides the best maintainability.</p> Signup and view all the answers

Which of the following statements best describes the use of RGBA in CSS?

<p>RGBA allows adding an alpha value to define the transparency of a color. (B)</p> Signup and view all the answers

Absolute units of measurement are recommended for defining style sheets for printing.

<p>True (A)</p> Signup and view all the answers

Which type of selector is most commonly used by web developers to select elements contained within another element?

<p>Descendant Selector (C)</p> Signup and view all the answers

Explain the difference between relative and absolute units in CSS.

<p>Relative units are based on the value of another property, such as a parent element's font size, whereas absolute units have a fixed, real-world size.</p> Signup and view all the answers

The ___ unit is equal to the computed value of the font-size property of the element on which it is utilized.

<p>em</p> Signup and view all the answers

A child selector in CSS selects all elements that are descendants of a specified element, regardless of the level of nesting.

<p>False (B)</p> Signup and view all the answers

Which of the following CSS units is classified as an absolute unit in CSS3?

<p>px (D)</p> Signup and view all the answers

In CSS, what is the term used to describe selectors that target elements based on their relationship to other elements in the document tree?

<p>Contextual Selectors or Combinators</p> Signup and view all the answers

The ________ selector is used to select an element that is directly after another specified element.

<p>adjacent sibling</p> Signup and view all the answers

What is the primary purpose of the HSL color model in CSS?

<p>To specify colors using hue, saturation, and lightness values. (D)</p> Signup and view all the answers

Which selector targets all paragraph <p> elements that share the same parent as an <h3> element?

<p><code>h3 ~ p</code> (D)</p> Signup and view all the answers

The '%' unit in CSS is always relative to another value.

<p>True (A)</p> Signup and view all the answers

The descendant selector only selects elements that are direct children of the specified element.

<p>False (B)</p> Signup and view all the answers

Match each CSS unit with its correct description:

<p>em = Relative to the font size of the element. % = Relative to another value, varying by property. px (CSS3) = Absolute unit, 1/96th of an inch. RGBA = Color with transparency (alpha) value.</p> Signup and view all the answers

Match the CSS selectors with their descriptions:

<p>Descendant Selector = Selects all elements that are contained within another element. Child Selector = Selects an element that is a direct child of another element. Adjacent Sibling Selector = Selects the first element that comes directly after another element. General Sibling Selector = Selects all elements that share the same parent as another element.</p> Signup and view all the answers

What does the CSS selector div > h2 target?

<p>Only <code>&lt;h2&gt;</code> elements that are direct children of a <code>&lt;div&gt;</code> element. (A)</p> Signup and view all the answers

Which of the following is NOT a reason why conflicts occur in CSS?

<p>Conflicting HTML attributes within the HTML document itself (D)</p> Signup and view all the answers

In CSS, the term 'Cascade' is used to describe what?

<p>How conflicting rules are handled.</p> Signup and view all the answers

The visual metaphor of a mountain stream progressing downstream over rocks represents how HTML elements are rendered in a browser.

<p>False (B)</p> Signup and view all the answers

Which of the following is NOT one of the cascade principles that CSS uses to resolve conflicts?

<p>Complexity (D)</p> Signup and view all the answers

In CSS, many properties, such as font, color, list, and text properties, affect not only themselves but also their ______.

<p>descendants</p> Signup and view all the answers

Which of the following categories of CSS properties are generally NOT inheritable?

<p>Layout properties (C)</p> Signup and view all the answers

All CSS properties are inheritable by default.

<p>False (B)</p> Signup and view all the answers

Match each CSS property type with its inheritance behavior:

<p>Font = Inheritable Color = Inheritable Layout = Not Inheritable Border = Not Inheritable</p> Signup and view all the answers

Which CSS property is not typically inherited by default?

<p>border (D)</p> Signup and view all the answers

It is impossible to force inheritance of CSS properties that are normally not inheritable.

<p>False (B)</p> Signup and view all the answers

What CSS keyword can be used to make a non-inherited property become inherited?

<p>inherit</p> Signup and view all the answers

Using CSS, to force an element to inherit a normally non-inheritable property from its parent, you can set the property's value to __________.

<p>inherit</p> Signup and view all the answers

Given the following CSS:

`div { font-weight: bold; margin: 50px; border: 1pt solid green; }

p { border: inherit; margin: inherit; } `

If a <p> element is nested inside a <div> element, what will be the margin and border of the <p> element?

<p>margin: 50px; border: 1pt solid green; (D)</p> Signup and view all the answers

Which CSS property is inheritable by default?

<p>font-weight (C)</p> Signup and view all the answers

Specificity determines which style rule takes precedence when multiple rules could apply to the same element.

<p>True (A)</p> Signup and view all the answers

How does a browser determine which style rule takes precedence when multiple rules apply to the same element?

<p>The browser assigns a weight to each style rule, and the rule with the greatest weight takes precedence.</p> Signup and view all the answers

The more ______ the selector, the more it takes precedence.

<p>specific</p> Signup and view all the answers

Given the following CSS:

body { color: red; } div { color: magenta; } p { color: green; }

What color will text within a <p> element inside a <div> element be?

<p>green (B)</p> Signup and view all the answers

Which of the properties is non- inheritable in CSS by default?

<p>margin (B)</p> Signup and view all the answers

A style rule defined for the body element will always override any conflicting styles defined for child elements.

<p>False (B)</p> Signup and view all the answers

Match the CSS concepts with their descriptions:

<p>Inheritance = The ability of certain CSS properties to be passed down from parent to child elements. Specificity = A set of rules to resolve conflicts when multiple CSS declarations apply to an element. Cascade = The algorithm defines how user agents combine style sheets originating from various sources.</p> Signup and view all the answers

Flashcards

Pseudo Selectors

Selects elements based on their position in the document tree, like first-child or last-child.

Contextual Selectors

Selects elements based on their relationship to other elements (ancestors, descendants, or siblings).

Descendant

An element that is contained within another element, no matter how deeply nested.

Child

An element that is a direct child of another element.

Signup and view all the flashcards

Adjacent Sibling

An element that is the very next sibling of another element in the HTML structure.

Signup and view all the flashcards

General Sibling

All elements that share the same parent as the specified element.

Signup and view all the flashcards

Descendant Selector

Matches all elements contained within another element, regardless of nesting level.

Signup and view all the flashcards

Descendant Selector Example: div p

Targets a specified element that is anywhere inside another specified element.

Signup and view all the flashcards

RGBa

Adds transparency to colors, allowing backgrounds to show through.

Signup and view all the flashcards

HSL

Specifies colors using Hue, Saturation, and Lightness values.

Signup and view all the flashcards

Relative Units

Units based on the value of another element, like a parent.

Signup and view all the flashcards

Absolute Units

Have a fixed, real-world size (though generally discouraged).

Signup and view all the flashcards

em Unit

A relative unit equal to the font size of the current element.

Signup and view all the flashcards

% (Percentage) Unit

Relative unit representing percentage relative to another value.

Signup and view all the flashcards

ex Unit

A relative unit based on the x-height of a font; rarely used.

Signup and view all the flashcards

ch Unit

A niche relative unit based on the width of the '0' character in a font.

Signup and view all the flashcards

CSS Style Locations

CSS rules can be located in three different locations: Inline, Embedded and External.

Signup and view all the flashcards

Inline Styles

Style rules placed directly within an HTML element using the style attribute.

Signup and view all the flashcards

Embedded Style Sheet

Style rules placed within the <style> element inside the <head> of an HTML document.

Signup and view all the flashcards

External Style Sheet

Style rules placed in an external .css file, linked to HTML documents.

Signup and view all the flashcards

Why use External Style Sheets?

The most common way to locate style rules, promoting maintainability and caching.

Signup and view all the flashcards

CSS Selectors

Used to indicate which HTML elements a CSS rule should apply to.

Signup and view all the flashcards

CSS Selectors selection types.

CSS selectors allow you to select individual elements, multiple HTML elements, elements that belong together or elements that are positioned in specific ways.

Signup and view all the flashcards

CSS Rules

Used to apply style rules for the HTML document.

Signup and view all the flashcards

Why CSS Conflict?

CSS needs a system to solve display issues when style rules conflict.

Signup and view all the flashcards

Cascade in CSS

The process by which CSS handles conflicting rules to determine which styles to apply to HTML elements.

Signup and view all the flashcards

Cascade Principles

Inheritance, specificity and location.

Signup and view all the flashcards

Inheritance

Some CSS properties that automatically apply not only to the specified element but also to all its children (descendants).

Signup and view all the flashcards

Inheritable Properties

Font, color, list, and text properties.

Signup and view all the flashcards

Non-Inheritable Properties

Layout, sizing, border, background, and spacing properties.

Signup and view all the flashcards

CSS Inheritance

The passing down of CSS property values from parent elements to their children.

Signup and view all the flashcards

Inherited vs. Non-inherited Properties

Properties like font and color are inherited, while properties like border and margin are not.

Signup and view all the flashcards

Forcing Inheritance

Setting a CSS property to inherit forces an element to inherit the property from its parent, even if it's normally non-inheritable.

Signup and view all the flashcards

Border Property

The border property controls the border of the element.

Signup and view all the flashcards

Margin Property

The margin property sets the space around the element.

Signup and view all the flashcards

Inheritable vs Non-Inheritable properties

Properties like font-weight and color are inheritable, while properties like margin and border are not.

Signup and view all the flashcards

CSS Specificity

Specificity determines which CSS rule takes precedence when multiple rules apply to the same element.

Signup and view all the flashcards

Specificity Weight

The browser assigns weights to CSS rules; the rule with the highest weight wins.

Signup and view all the flashcards

Selector Specificity Examples

Selectors like div and p are more specific than the body selector.

Signup and view all the flashcards

Overriding Styles

A style defined for a div or p element overrides the same style defined for the body element.

Signup and view all the flashcards

Most specific selector styling

When multiple styles apply, the most specific selector's style is applied.

Signup and view all the flashcards

Color example

If the color is set red in the body, magenta in div and green in p, then text in p will be green.

Signup and view all the flashcards

Study Notes

CSS Introduction

  • CSS is a W3C standard to describe the presentation of HTML elements
  • CSS allows to assign font properties, colours, sizes, borders, background images, and the position of elements
  • CSS is a language and can be added directly to any HTML element (via the style attribute) within the element, or in a separate CSS file
  • Using CSS offers the ability to have degree of formatting control better than HTML, more accessible sites, faster download times, page adoption for different output mediums, and web sites that are significantly more maintainable

CSS Syntax

  • A CSS document has style rules made of selectors, properties, and values
  • A rule consists of a selector that identifies the HTML element or elements that will be affected, followed by property and value pairs called a declaration
  • A series of declarations is also named the declaration block
  • A declaration block can be together on a single line, or spread across multiple lines
  • White space is ignored
  • Each declaration is terminated with semi-colon
  • Selectors tell the browser which elements in the HTML document will be affected
  • Every CSS rule begins with a selector
  • Thinking of selectors is that they are a pattern to select the HTML elements
  • Each CSS declaration need a property
  • CSS2.1 Recommendation has over a hundred property names

Commonly Used Properties

  • Property types include:
    • Fonts: Includes font, font-family, font-size, font-style, font-weight, and @font-face
    • Text: Includes letter-spacing, line-height, text-align, text-decoration, text-indent
    • Colour and Background: Includes background, background-color, background-image, background-position, background-repeat, and colour
    • Borders: Includes border, border-color, border-width, border-style, border-top, border-top-color, border-top-width, etc.
    • Spacing: Includes padding, padding-bottom, padding-left, padding-right, padding-top, margin, margin-bottom, margin-left, margin-right, and margin-top
    • Sizing: Includes height, max-height, max-width, min-height, min-width, and width -Layout: Includes bottom, left, right, top, clear, display, float, overflow, position, visibility, and z-index
    • Lists: Includes list-style, list-style-image, and list-style-type

Values

  • Each CSS declaration contains a value for a property
  • The unit of any given value is dependent upon the property
  • Some values are from a predefined list of keywords
  • Other values are length measurements, percentages, numbers without units, colour values and URLs
  • CSS supports different ways of describing colours:
    • Name: Using one of 17 standard colour names or one of 140 standard names in CSS3
    • RGB: Using numbers between 0 and 255 to describe the Red, Green, and Blue values for the colour
    • Hexadecimal: Using a six-digit hexadecimal number to describe the colour; hexadecimal number is preceded by a hash symbol (#)
    • RGBa: To add an alpha or degree of transparency value; transparency is a value between 0.0 (fully) and 1.0 (opaque)
    • HSL: To specify a colour using Hue Saturation and Light values
  • Relative units are based on other elements whereas absolute units have a real-world size
  • It is recommended not to use absolute units unless defining a style sheet for printing
  • Pixel unit is a popular exception
  • Types of relative units:
    • px (pixel): Relative in CSS2, absolute in CSS3 at 1/96 of an inch
    • em: Computed value of font-size; used in font sizes, it is in relation to the font size of the parent
    • %: Measure relative to another value
    • ex: Rarely used relative measure that is with x-height
    • ch: Seldom used relative element related to the width of zero
    • rem: Stands for root em, the font size of the root element,Unlike em, may be different for each element, rem is constant
    • vw, vh: Stands for viewport width and viewport height; percentage values between 0 and 100 of browser that allows item to resize
  • Absolute units of measure
    • in (inches)
    • cm (centimeters)
    • mm (millimeters)
    • pt (points which equals 1/72 of an inch)
    • pc (pica equals 1/6 of an inch)
  • It is helpful to add comments to style sheets which takes the form: "/* comment goes here */"
  • To indicate that related style rules are together, and ways to hide a of rules, to with debugging

Location of Styles

  • There are 3 types: author-created , user style sheet (custom), and browser style sheet (default)
  • Author CSS style rules can be located in three different locations: Inline, Embedded and External
  • Style rules can be combined and put in all 3 locations!
  • Inline styles increase bandwidth and maintainability by placing them within the style attribute of HTML elements
  • Embedded style sheets by and large discouraged and are style rules placed within the "" element inside the "" element
  • External styles are located most commonly, as it provides the best maintainability, with style rules in a CSS extension. This format can update used documents with automated updates and performance through browser level caching of style sheets

Selectors

  • The selector tells the browser which elements will have their style changed
  • Allow the element to select individuals, HTML groups, elements that belong, or positioned
  • Universal elements use HTML name
    • is the universal element selector for selecting all elements.
  • Select a group by separating element names with commas. This sensible way to decrease complexity by identical CSS rules into a single rule.
  • Reset quickly remove browser defaults that often reduce browser level margin inconsistencies, line heights and font sizes, with rulesets that can be placed in their own CSS file
  • Class selectors simultaneously aim different HTML elements through a series of labelled HTML attributes
  • The targeting is by using class selector, a period (.) followed by name
  • An ID selector targets by attribute to style using pound/hash (#) by the name
  • Only use an ID once per page
  • ID selectors should reference a single document and element in the same HTML element
  • Class selectors are when referencing related elements
  • An attribute selector is the presence of a value for either the element, attribute or its value
  • Attribute selector are helpful in the use of in styling of hyperlinks and images
  • A pseudo-element selects where there is existing HTML but that still needs identifiable structure
  • A pseudo element applies either to HTML element state or to their relation
  • The most common use is for targeting link states
  • Contextual selectors CSS3 known as combinators, select the elements based on their relation tree
  • Descendant selector matches element contained in another. The space character idicates this selection.

The Cascade, how styles interact

  • Because there are three different types of style sheets (author-created, user-defined, and the default browser style sheet), author-created style sheets can define multiple rules for the same HTML element and can lead to conflicting style applications
  • The Cascade refers to how the browser deals with conflict determination
  • the cascade metaphor: of water moving down a mountain downstream as its the given property of a parents affects its children elements
  • Cascade Principles help the browser with defaults and conflicts: inheritance specificity and location
  • Inheritance is the ability for CSS to modify descendants with font colour list and text properties with sizes, border background and spacing property changes
  • To control what can be inherited, there is the ability also an inherit keyword to force the inheritances of a tag

Specificity the 2cd Casacade principle

  • How browser determines which style rule to precedence
  • The more 'specific' the selecter the more the takes precedence
  • Specificity assigns weight to rule
  • Class selectors are are specific then and override an element selector in the HTML
  • ID selecter are more specific and take precedence than class selector
  • Algorithm that counts
  • 1 if the declaration is from a style attribute in HTML 0 otherwise
  • The count to the number of ID attributes
  • And then, count the number of attributes of pseudo class selector
  • Count the number of elements. Or pseudo elements
  • Finally then concatenate the four numbers and calculate the difference
  • Start the value at zero ad 100 for ID value at 10 for each class value at one selector for psedo attribute
  • Principle of location says, latest will get more weight

The Box Model

  • The box model describes all HTML elements within element boxes
  • Background colour or images and border of elements will be in the code
  • Commonly use CSS to display presentation images than elements
  • Background property allows setting many of different style property options the shorthand
  • Background attachment specifies image will scroll
  • Repeating repeats an image
  • Background size allows changing the size of image
  • Borders creates visual element separation, with multiple ways to style
  • There is the combine short hand

TRBL border usage notation

  • Border, margin, padding properties, have both long and short form usage notation
  • This allows for top, right, bottom and left properties assignment
  • Margins and padding are model, the style rules affect spacing between element and other elements within the document
  • If the spacing between paragraphs from, one to two is, in the fact that adjoining the vertical margins also have code impacts

Margin impact

  • Vertical elements collapse into a single margin
  • If they do not collapse margin of first and of would be 180
  • Collapsing is due to vertical margins often largest taking the value
  • The height and The width properties specfic size of elements content area
  • only can be specified Block level elements such as images

Potential sizing

  • The text has font size and font face impacts are of actual
  • Text size is determined via the pixel measurements of dimension

Sizing considerations

  • Default is total size that includes padding borders
  • box-sizing: content-box, uses style dimensions
  • box-sizing: border-box the opposite
  • The properties allows what to di with overflow of dimensions if you need to change visibility, have it hidden, add scroll
  • Sizing is the percentage of sizing
  • Ems also
  • Percentages are relative to parent elements and its size
  • Ems or relative for the text or with its sizing of that text

Text Styling

  • There are two forms that the text displays font's and paragraphs
  • Fonts change different web fonts often in a web stack
  • Selector allows you to use on fonts, though it may not come to your web
  • Percents are relative scaling

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

More Like This

Advanced CSS Selectors and Styling Quiz
6 questions
HTML and CSS Styling Quiz
18 questions
HTML Lists and CSS Styling
35 questions

HTML Lists and CSS Styling

RapturousPipeOrgan avatar
RapturousPipeOrgan
Use Quizgecko on...
Browser
Browser