Basic CSS Properties

Introduction to CSS Properties

  • In Cascading Style Sheets (CSS), a property is defined as the element that determines the visual appearance of an HTML element.

  • Syntax for assigning values: selector { property: value; }.

  • Standard syntax example: p { color: blue; } where p is the selector, color is the property, and blue is the assigned value.

Text Color: color

  • Definition: The color property is used specifically to set the color of text content within an element.

  • Acceptable Values:

    • Color names: Examples include red, blue, and green.

    • HEX codes: Hexadecimal color representations such as #ff0000.

    • RGB: Red-Green-Blue functional notation such as rgb(255,0,0).

  • CSS Example: css h1 { color: red; }     

  • HTML Application: <h1>This is a red heading</h1>.

Background Color: background-color

  • Definition: This property sets the background color for a specific element.

  • CSS Example: css body { background-color: lightgray; } &nbsp;&nbsp;&nbsp;&nbsp;

  • HTML Application: <body>This page has a light gray background</body>.

Background Image: background-image

  • Definition: Applies a specific image to serve as the background of an element.

  • CSS Example: css body { background-image: url("background.jpg"); } &nbsp;&nbsp;&nbsp;&nbsp;

  • Additional Behavior and Notes:

    • Default Behavior: By default, the specified background image will repeat to fill the element's space.

    • Repetition Control: To prevent the image from repeating, the background-repeat: no-repeat; property must be utilized.

Text Alignment: text-align

  • Definition: Controls the horizontal alignment of the text within an element.

  • Available Values:

    • left

    • right

    • center

    • justify

  • CSS Example: css p { text-align: center; } &nbsp;&nbsp;&nbsp;&nbsp;

  • HTML Application: <p>This text is centered</p>.

Font Family: font-family

  • Definition: Specifies the specific typeface (font) used for the text of an element.

  • CSS Example: css body { font-family: Arial, sans-serif; } &nbsp;&nbsp;&nbsp;&nbsp;

  • Best Practices: Always include a fallback font (such as sans-serif) in case the primary font is unavailable.

Font Size: font-size

  • Definition: This property is used to control the height/size of the text.

  • Measurement Units:

    • pxpx (pixels): Absolute units.

    • emem (relative): Units relative to the font-size of the element.

    • % (percentage): Relative units based on the parent element's font-size.

  • CSS Example: css p { font-size: 18\,px; } &nbsp;&nbsp;&nbsp;&nbsp;

Font Weight: font-weight

  • Definition: Controls the thickness or boldness of the text characters.

  • Available Values:

    • normal

    • bold

    • Numeric range: From 100100 up to 900900.

  • CSS Example: css h1 { font-weight: bold; } &nbsp;&nbsp;&nbsp;&nbsp;

Font Style: font-style

  • Definition: Defines the specific style or slant of the text.

  • Available Values:

    • normal

    • italic

    • oblique

  • CSS Example: css p { font-style: italic; } &nbsp;&nbsp;&nbsp;&nbsp;

Combined Practice Example

  • The following code demonstrates a practical implementation of all properties combined:

  • General Body Settings: css body { background-color: #f0f0f0; background-image: url("bg.jpg"); font-family: Arial, sans-serif; } &nbsp;&nbsp;&nbsp;&nbsp;

  • Heading 1 (h1) Settings: css h1 { color: blue; text-align: center; font-size: 32\,px; font-weight: bold; } &nbsp;&nbsp;&nbsp;&nbsp;

  • Paragraph (p) Settings: css p { color: black; text-align: justify; font-size: 16\,px; font-style: italic; } &nbsp;&nbsp;&nbsp;&nbsp;

Summary of Essential Properties

  • color: Used for setting the text color.

  • background-color: Used for setting the background color of an element.

  • background-image: Used for adding a background image to an element.

  • text-align: Used to align text horizontally (left, right, center, or justify).

  • font-family: Used to set the font type or typeface.

  • font-size: Used to control the size of the text (using pxpx, emem, or %).

  • font-weight: Used to control the boldness or thickness of text (e.g., bold or numeric values 100−900100-900).

  • font-style: Used to control the style of text, such as italic.