Curriculum
Course: CSS
Login

Curriculum

CSS

CSS INTRODUCTION

0/1

CSS Selectors

0/1

CSS Comments

0/1

CSS Padding

0/1

CSS Box Model

0/1

CSS Combinators

0/1

CSS Pseudo-classes

0/1

CSS Pseudo-elements

0/1

CSS Dropdowns

0/1

CSS Image Gallery

0/1

CSS Image Sprites

0/1

CSS Counters

0/1

CSS Website Layout

0/1

CSS Specificity

0/1

CSS Math Functions

0/1
Text lesson

CSS Attribute Selectors

Style HTML Elements With Specific Attributes

You can apply styles to HTML elements that possess particular attributes or attribute values.

CSS [attribute] Selector

The [attribute] selector targets elements with a designated attribute.

In the example below, it selects all <a> elements possessing a target attribute.

Example

a[target] {
  background-color: yellow;
}

CSS [attribute=”value”] Selector

The [attribute=”value”] selector identifies elements with a specific attribute and value.

In the example provided, it selects all <a> elements with a target=”_blank” attribute.

Example

a[target=”_blank”] {
  background-color: yellow;
}

CSS [attribute~=”value”] Selector

The [attribute~=”value”] selector targets elements with an attribute value containing a specified word.

In the following example, it selects all elements with a title attribute containing a list of space-separated words, one of which is “flower”.

Example

[title~=”flower”] {
  border: 5px solid yellow;
}

The provided example will match elements with titles such as “flower”, “summer flower”, and “flower new”, but will not match titles like “my-flower” or “flowers”.

CSS [attribute|=”value”] Selector

The [attribute|=”value”] selector targets elements with the specified attribute, where the value can either exactly match the specified value or be followed by a hyphen (-).

Note: The value must be a complete word, either standalone like class=”top”, or followed by a hyphen (-) as in class=”top-text”.

Example

[class|=”top”] {
  background: yellow;
}

CSS [attribute^=”value”] Selector

The [attribute^=”value”] selector targets elements with the specified attribute, where the value begins with the specified string.

In the given example, it selects all elements with a class attribute value that commences with “top”.

Note: The value does not necessarily have to be a complete word.

Example

[class^=”top”] {
  background: yellow;
}

CSS [attribute$=”value”] Selector

The [attribute$=”value”] selector is employed to choose elements with an attribute value that concludes with a specified string.

In the provided example, it selects all elements with a class attribute value that concludes with “test”.

Note: The value does not need to be a complete word.

Example

[class$=”test”] {
  background: yellow;
}

CSS [attribute*=”value”] Selector

The [attribute*=”value”] selector targets elements with an attribute value that includes a specified string.

In the given example, it selects all elements with a class attribute value that contains “te”.

Note: The value doesn’t necessarily have to be a complete word.

Example

[class*=”te”] {
  background: yellow;
}

Styling Forms

Attribute selectors are beneficial for styling forms lacking specific class or ID attributes.

Example

input[type=”text”] {
  width: 150px;
  display: block;
  margin-bottom: 10px;
  background-color: yellow;
}

input[type=”button”] {
  width: 120px;
  margin-left: 35px;
  display: block;
}