Browse Courses

Advanced CSS Properties

In-depth guide to advanced CSS and Flexbox properties.

Cascading Style Sheets (CSS) is a language used to describe the presentation of a document written in HTML. CSS enhances the look and feel of web pages by allowing developers to apply styles to elements. This reading aims to explain various CSS properties and Flexbox properties, including their usage, examples, and values, to help you build visually appealing web pages.


Objective

By the end of this reading, you will:

  • Gain a good understanding of various CSS properties
  • Learn about Flexbox properties
  • Understand how these properties affect HTML elements
  • Learn to use these properties effectively in web designs

CSS Properties

text-decoration

Controls the decoration applied to text, such as underlines, lines above or through text. This property can enhance the visual emphasis of text or indicate a change in text state (like a link being visited).

Example:

1text-decoration: underline;

Applies an underline to text.

Values:

  • none: No decoration
  • underline: Underlines the text
  • overline: Adds a line above the text
  • line-through: Strikes through the text
  • Default value: none

font-weight

Specifies the thickness or boldness of the font characters. This property is used to make text stand out by adjusting its weight, which can enhance readability or create emphasis.

Example:

1font-weight: bold;

This makes the font bold.

Values:

  • normal: Normal weight
  • bold: Bold weight
  • bolder: Bolder than the parent element
  • lighter: Lighter than the parent element
  • 100-900: Numeric values (100 is the thinnest, 900 is the thickest)
  • Default value: normal

margin

Sets the space around an element, outside its border. Margins create space between elements, helping to separate and organize content visually.

Example:

1margin: 20px;

This adds a 20px margin on all sides of an element.

Values:

  • auto: Automatically calculates the margin
  • length: Specifies a fixed margin (e.g., 20px)
  • percentage: Specifies a margin relative to the containing element’s width
  • Default value: 0

padding

Sets the space inside an element, between its content and its border. Padding increases the area within the element, creating spacing between the content and the edges of the element.

Example:

1padding: 10px;

This adds 10px padding inside an element.

Values:

  • length: Specifies a fixed padding (e.g., 10px)
  • percentage: Specifies padding relative to the containing element’s width
  • Default value: 0

float

Specifies whether an element should float to the left or right of its container. Floating elements are taken out of the normal document flow, allowing text and inline elements to wrap around them.

Example:

1float: left;

This makes an element float to the left.

Values:

  • none: The element does not float
  • left: The element floats to the left
  • right: The element floats to the right
  • Default value: none

border-radius

Defines the radius of the corners of an element’s border box, creating rounded corners. This property is used to soften the appearance of boxes, buttons, and other UI elements.

Example:

1border-radius: 10px;

This rounds the corners of an element with a 10px radius.

Values:

  • length: Specifies a fixed radius (e.g., 10px)
  • percentage: Specifies a radius relative to the element’s dimensions
  • Default value: 0

width and height

Sets the width and height of an element. These properties determine the size of the element, affecting how much space it occupies within its container.

Example:

1width: 200px;
2height: 100px;

This sets the width to 200px and height to 100px.

Values:

  • auto: The browser calculates the dimension
  • length: Specifies a fixed dimension (e.g., 200px)
  • percentage: Specifies a dimension relative to the containing element’s dimensions
  • Default value: auto

position

Specifies the type of positioning method used for an element. Positioning determines how an element is placed within its containing block and how it interacts with other elements.

Example:

1position: relative;

This sets the element’s position relative to its normal position.

Values:

  • static: The default positioning
  • relative: Positioned relative to its normal position
  • absolute: Positioned relative to the nearest positioned ancestor
  • fixed: Positioned relative to the browser window
  • sticky: Switches between relative and fixed based on the scroll position
  • Default value: static

Flexbox Properties

What is Flexbox

Flexbox (Flexible Box Layout) is a CSS layout module designed to align and distribute space among items in a container, even when their size is unknown or dynamic. It allows you to create complex layouts with less code and provides greater control over alignment, direction, and order of items within a container.

display: flex

Defines an element as a flex container, enabling flexbox layout for its children. This property makes it possible to use flexbox properties on the child elements to control their alignment, direction, and distribution.

Example:

1display: flex;

This turns the container into a flex container.

Values:

  • flex: Turns the element into a block-level flex container
  • inline-flex: Turns the element into an inline-level flex container
  • Default value: block (for the display property in general)

justify-content

Aligns flex items along the main axis, which is horizontal by default. This property helps control the distribution of space between and around flex items within a container.

Example:

1justify-content: center;

This centers items along the main axis.

Values:

  • flex-start: Aligns items at the start of the main axis
  • flex-end: Aligns items at the end of the main axis
  • center: Centers items along the main axis
  • space-between: Distributes items evenly with the first item at the start and the last item at the end
  • space-around: Distributes items evenly with equal space around them
  • space-evenly: Distributes items evenly with equal space between them
  • Default value: flex-start

flex-direction

Defines the direction in which flex items are placed within the flex container. This property determines whether the items are displayed in rows or columns, and can also reverse the order.

Example:

1flex-direction: column;

This arranges items in a column.

Values:

  • row: Default direction, items are placed in a row
  • row-reverse: Items are placed in a row in reverse order
  • column: Items are placed in a column
  • column-reverse: Items are placed in a column in reverse order
  • Default value: row

flex-wrap

Specifies whether flex items should wrap onto multiple lines when they overflow the container. This property helps manage the layout when there are too many items to fit in one line.

Example:

1flex-wrap: wrap;

This allows items to wrap onto multiple lines if necessary.

Values:

  • nowrap: Items do not wrap
  • wrap: Items wrap onto multiple lines
  • wrap-reverse: Items wrap onto multiple lines in reverse order
  • Default value: nowrap

align-items

Aligns flex items along the cross-axis, which is vertical by default. This property is used to control the alignment of items within the flex container’s cross-direction.

Example:

1align-items: stretch;

This stretches items to fill the container’s cross-axis.

Values:

  • flex-start: Aligns items at the start of the cross-axis
  • flex-end: Aligns items at the end of the cross-axis
  • center: Centers items along the cross-axis
  • baseline: Aligns items’ baselines
  • stretch: Stretches items to fill the container
  • Default value: stretch

align-self

Aligns a single flex item along the cross-axis within its flex container. This property allows individual items to override the alignment set by align-items.

Example:

1align-self: center;

This centers the item within its flex container along the cross-axis.

Values:

  • auto: Inherits the alignment from the parent container
  • flex-start: Aligns the item at the start of the cross-axis
  • flex-end: Aligns the item at the end of the cross-axis
  • center: Centers the item along the cross-axis
  • baseline: Aligns the item’s baseline with the parent’s baseline
  • stretch: Stretches the item to fill the container
  • Default value: auto

flex-flow

Shorthand property for flex-direction and flex-wrap. This property combines both the direction and wrapping behavior into a single declaration.

Example:

1flex-flow: row wrap;

This arranges items in a row and allows them to wrap onto multiple lines.

Values:

  • Combines values for flex-direction and flex-wrap
  • Default value: row nowrap

justify-self

Aligns an individual flex item along the main axis. This property affects the alignment of a single item relative to its container’s main axis.

Example:

1justify-self: center;

This centers a single item within its flex container along the main axis.

Values:

  • auto: Inherits the alignment from the parent container
  • flex-start: Aligns the item at the start of the main axis
  • flex-end: Aligns the item at the end of the main axis
  • center: Centers the item along the main axis
  • baseline: Aligns the item’s baseline with the parent’s baseline
  • stretch: Stretches the item to fill the container
  • Default value: auto

Conclusion

CSS properties play a crucial role in web design, allowing developers to style and layout web pages effectively. Understanding advanced CSS properties and Flexbox properties is essential for creating visually appealing and responsive designs. By mastering these properties, you can build modern, user-friendly websites that adapt to different screen sizes and devices.


FAQ

The text-decoration property enhances text styling by allowing developers to add underlines, overlines, or strikethroughs to text, improving visual emphasis or indicating state changes like visited links.

The font-weight property is important because it adjusts the thickness of text, helping to create emphasis, improve readability, and establish a visual hierarchy in web design.

Margin is better for creating space between elements, while padding is used to create space within an element, between its content and border.

Yes, the border-radius property can create circular elements by setting its value to 50% on a square element.

The position property affects element placement by determining how an element is positioned relative to its normal flow, its containing block, or the viewport, using values like static, relative, absolute, fixed, and sticky.

If a flex container has too many items to fit in one line, the flex-wrap property can be used to allow items to wrap onto multiple lines, ensuring a responsive layout.

  • justify-content aligns items along the main axis (horizontal by default).
  • align-items aligns items along the cross-axis (vertical by default).

The flex-direction property should be used when you need to control the direction of flex items, such as arranging them in rows, columns, or reversing their order.

The align-self property is most effective when you need to override the cross-axis alignment for a single flex item, without affecting the alignment of other items in the container.

Yes, the flex-flow property is a shorthand for the flex-direction and flex-wrap properties, allowing you to set both in a single declaration.

The justify-content property distributes space in a flex container by aligning items along the main axis, using options like flex-start, center, space-between, and space-around.

The display: flex property is essential because it defines an element as a flex container, enabling the use of Flexbox properties to control the layout and alignment of its child elements.

The align-items property controls the alignment of flex items along the cross-axis, ensuring consistent vertical alignment within the flex container.

The flex-wrap property allows flex items to wrap onto multiple lines, making it easier to create responsive designs that adapt to different screen sizes and prevent content overflow.

The border-radius property improves UI design by softening the appearance of elements, creating rounded corners, and enhancing the overall aesthetic of buttons, cards, and containers.