Master of the universe

CSS Properties Complete Guide: Text, Font, Background, List, And Table

Introduction

Cascading Style Sheets (CSS) play a crucial role in defining the appearance of webpages by controlling various aspects such as text, fonts, backgrounds, lists, and tables. Mastering CSS properties allows you to create visually appealing designs and ensure a smooth user experience.

In this comprehensive guide, we'll explore a wide range of CSS properties that can be used to style text, font, background, list, and table elements. By the end, you'll have a solid understanding of each property and be able to effectively apply them to your web designs.

Text Properties

Text properties in CSS enable you to modify the appearance and layout of text within a webpage. They allow you to control color, alignment, decoration, transformation, and indentation. Let's dive into each of these properties.

Color

The color property is used to set the color of text for an element. Colors can be specified using different formats, such as keywords, hexadecimal, RGB, and HSL. Here's an example of setting a paragraph's text color using the color property:

p {
  color: #ff0000; /* red */
}

Text Alignment

The text-align property determines the horizontal alignment of text within an element. It accepts four different values: left, right, center, and justify. By default, text is aligned to the left. Here's an example of center-aligning text within a heading:

h1 {
  text-align: center;
}

Text Decoration

The text-decoration property is used to apply different stylistic effects to text content, such as underlining, overlining, or striking through. It accepts four values: none, underline, overline, and line-through. The default value is none. Here's an example of underlining a hyperlink:

a {
  text-decoration: underline;
}

Text Transformation

The text-transform property lets you control how the text is capitalized. It accepts four values: none, uppercase, lowercase, and capitalize. By default, text is displayed as it is written in the HTML document. Here's an example of setting the text in a heading to uppercase:

h2 {
  text-transform: uppercase;
}

Text Indentation

The text-indent property is used to apply indentation to the first line of a text block. This can be useful for formatting purposes, such as creating a hanging indent or a paragraph indent. The property accepts various units, such as pixels, em, rem, and percentage. Here's an example of indenting the first line of a paragraph:

p {
  text-indent: 2em;
}

Font Properties

Font properties in CSS provide control over the appearance and style of fonts used in a webpage. They enable you to define font families, sizes, weights, styles, and line heights. Let's explore each of these properties in detail.

Font Family

The font-family property is used to specify a stack of fonts applied to an element. The browser will use the first available font in the defined stack. It's essential to provide fallback fonts in case the preferred font is not available on the user's machine. Here's an example of setting a paragraph's font family with fallback options:

p {
  font-family: 'Roboto', Arial, sans-serif;
}

Font Size

The font-size property enables you to control the size of text for a specified element. This property can be defined using various units, such as pixels (px), em, rem, percentage (%), and viewport units (vw, vh). Here's an example of setting a paragraph's font size using the font-size property:

p {
  font-size: 16px;
}

Font Weight

The font-weight property determines how bold or light the text should appear. You can control the boldness using numerical values (100-900) or keywords such as normal and bold. Here's an example of setting a heading's font weight:

h1 {
  font-weight: 700; /* Bold */
}

Font Style

The font-style property is used to apply an italic or oblique effect to text. It accepts three values: normal, italic, and oblique. By default, the text has a normal style. Here's an example of setting a paragraph's font style to italic:

p {
  font-style: italic;
}

Line Height

The line-height property determines the spacing between lines of text. You can define line height using unitless values, pixels (px), em, rem, and percentages (%). Unitless values are recommended for better scalability and inheritance. Here's an example of setting line height for a paragraph:

p {
  line-height: 1.5;
}

Background Properties

Background properties in CSS enable you to control the background appearance of an element. You can set background colors, images, repetition, position, and size. Let's examine each of these properties more closely.

https://www.youtube.com/watch?v=cLyzBfXI0I0

Background Color

The background-color property is used to set the background color of an element. Similar to the color property, background colors can be specified using keywords, hexadecimal, RGB, and HSL. Here's an example of setting a div's background color:

div {
  background-color: rgba(0, 0, 255, 0.5); /* Semi-transparent blue */
}

Background Image

The background-image property allows you to set an image as the background of an element. The image URL should be specified within the url() function. Here's an example of setting a div's background image:

div {
  background-image: url('path/to/image.jpg');
}

Background Repeat

The background-repeat property is used to control how a background image is repeated within an element. It accepts four values: repeat, repeat-x, repeat-y, and no-repeat. By default, background images repeat both horizontally and vertically. Here's an example of setting a div's background image to repeat only horizontally:

div {
  background-repeat: repeat-x;
}

Background Position

The background-position property allows you to control the position of a background image within an element. You can use units like pixels (px), percentages (%), or keywords such as top, right, bottom, and left. Here's an example of setting a div's background image position:

div {
  background-position: right bottom;
}

Background Size

The background-size property enables you to control the size of a background image. It accepts four values: auto, contain, cover, and explicit dimensions. Here's an example of setting a div's background image to cover the entire element:

div {
  background-size: cover;
}

List Properties

List properties in CSS provide control over the appearance and styling of ordered and unordered lists. They enable you to customize list item markers, their position, and even use custom images as markers. Let's dive into each of these properties.

List Style Type

The list-style-type property is used to control the appearance of list item markers. For unordered lists, it accepts values such as disc, circle, and square. For ordered lists, it can take values such as decimal, lower-alpha, and upper-alpha. To hide the list item markers, you can use the none value. Here's an example of setting an unordered list's style type:

ul {
  list-style-type: square;
}

List Style Position

The list-style-position property controls the position of list item markers relative to the list items' content. It accepts two values: inside and outside. The default value is outside. Here's an example of setting the list item markers' position to inside for an ordered list:

ol {
  list-style-position: inside;
}

List Style Image

The list-style-image property allows you to use a custom image as a list item marker instead of the default marker styles. The image URL should be specified within the url() function. Here's an example of setting a custom list item marker for an unordered list:

ul {
  list-style-image: url('path/to/custom_marker.png');
}

Table Properties

Table properties in CSS help you control the layout, border behavior, spacing, and cell content alignment of HTML tables. Let's explore each of these properties in detail.

https://www.youtube.com/watch?v=IGBRYTpgyg4

Table Layout

The table-layout property is used to determine the algorithm used to lay out a table. It accepts two values: auto and fixed. The auto value (default) allows the browser to automatically calculate the table's dimensions based on the content. The fixed value enables you to set the table and column widths explicitly. Here's an example of setting a table's layout to fixed:

table {
  table-layout: fixed;
  width: 100%; /* Specify a width for the table */
}

Border Collapse

The border-collapse property is used to control the behavior of table borders. It accepts two values: collapse and separate. The collapse value combines adjacent table cell borders into a single border, while the separate value (default) maintains individual borders for each cell. Here's an example of collapsing table borders:

table {
  border-collapse: collapse;
}

Border Spacing

The border-spacing property lets you control the spacing between table cells when the border-collapse property is set to separate. It can take one or two length values, defining the horizontal and vertical spacing, respectively. Here's an example of setting border spacing:

table {
  border-collapse: separate;
  border-spacing: 10px 5px;
}

Vertical Alignment

The vertical-align property is used to control the vertical alignment of table cell content. It accepts values such as top, middle, bottom, baseline, and inherit. The default value is baseline. Here's an example of setting the vertical alignment of table cell content to the middle:

td {
  vertical-align: middle;
}

Conclusion

In this comprehensive guide, we've covered a wide array of CSS properties that you can use to style various aspects of your webpages, specifically text, font, background, list, and table elements. By mastering these properties, you can create visually appealing designs, ensure a smooth user experience, and effectively convey information.

To recap, we've explored the following properties:

  • Text: color, text-align, text-decoration, text-transform, text-indent
  • Font: font-family, font-size, font-weight, font-style, line-height
  • Background: background-color, background-image, background-repeat, background-position, background-size
  • List: list-style-type, list-style-position, list-style-image
  • Table: table-layout, border-collapse, border-spacing, vertical-align

Understanding and utilizing these properties in your web designs will empower you to create sophisticated layouts and reinforce your expertise as a web developer.

Frequently Asked Questions

Here are some common questions related to CSS properties for text, font, background, list, and table elements, along with their answers.

What is the difference between em and rem units?

The em unit is a relative unit that represents the current font size of its closest parent element. For example, if the parent element's font size is 16px, 1em equals 16px. On the other hand, the rem unit is also a relative unit, but it represents the root element's (i.e., <html>) font size. Therefore, 1rem is equal to the font size of the <html> element, which by default is 16px.

Can I use multiple background images for a single element?

Yes, you can apply multiple background images to a single element using the background-image property. To do this, provide a comma-separated list of image URLs within the url() function. The first image specified will be the topmost layer, and the following images will be layered beneath it in the order specified.

div {
  background-image: url('path/to/image1.png'), url('path/to/image2.jpg');
}

How can I style the numbers or bullets in a list separately from the list content?

You can achieve this by using pseudo-elements, such as ::before or ::marker. For example, you can use the ::marker pseudo-element to target the marker and apply specific CSS properties, such as color.

li::marker {
  color: red;
}

How can I apply a fixed background image that doesn't scroll with the page content?

You can create a fixed background image that remains static while the user scrolls through the content by using the background-attachment property with a value of fixed.

body {
  background-image: url('path/to/image.jpg');
  background-attachment: fixed;
}

How can I create a responsive table that adjusts its layout on small devices?

You can create responsive tables by applying CSS media queries and adjusting the table's layout, such as changing the display type or adding overflow properties, on smaller devices.

/* Default table styles */
table {
  width: 100%;
  border-collapse: collapse;
}

/* Responsive table on small devices */
@media (max-width: 768px) {
  table {
    display: block;
    overflow-x: auto;
    white-space: nowrap;
  }
}

Sign up for the Artisan Beta

Help us reimagine WordPress.

Whether you’re a smaller site seeking to optimize performance, or mid-market/enterprise buildinging out a secure WordPress architecture – we’ve got you covered. 

We care about the protection of your data. Read our Privacy Policy.