CSS Attribute Selectors

Home / CSS / CSS Attribute Selectors

CSS Attribute Selectors


CSS Attribute Selector:

CSS (Cascading Style Sheets) allows you to control the presentation and styling of HTML elements on a web page. CSS attributes, often referred to as CSS properties, are used to define the visual properties of these elements. Here are some common CSS attributes along with examples:

Color (color):

This property sets the text color of an element.


<style>
    p {
      color: red;
    }
  </style>

Font Size (font-size):

Sets the size of the text within an element.

 
<style>
    h1 {
      font-size: 24px;
    }
  </style>

Background Color (background-color):

Defines the background color of an element.


<style>
    div {
      background-color: lightblue;
    }
  </style>

Margin (margin):

Controls the spacing outside an element's border.

<style>
    img {
      margin: 10px;
    }
  </style>

Padding (padding):

Sets the spacing between an element's content and its border.


<style>
    .box {
      padding: 20px;
    }
  </style>

Border (border):

Defines the border around an element.

 
<style>
    button {
      border: 2px solid black;
    }
  </style>

Text Alignment (text-align):

Adjusts the horizontal alignment of text within an element.

<style>
    p {
      text-align: center;
    }
  </style>

Font Family (font-family):

Specifies the font used for text within an element.

 
<style>
    body {
      font-family: Arial, sans-serif;
    }
  </style>

Text Decoration (text-decoration):

Adds decorative effects to text, like underlines or overlines.

 
<style>
    a {
      text-decoration: underline;
    }
  </style>

Width and Height (width and height):

Sets the dimensions of an element, such as width and height.

 
<style>
    .container {
      width: 300px;
      height: 200px;
    }
  </style>