1. Home
  2. Articles
  3. Web Design
  4. CSS Inheritance Override Examples
Read about CSS Inheritance Overrides

CSS Inheritance Override Examples

Published:

Share

Description

Two problems were presented to me for input. The first was creating a solution apply a specific style an image without using a class on the image element. The caveat to this was that it had to work with a content management system that utilizes dynamically generated container IDs. The second request was to provide a solution to a problem with inline styling in the same content management system. While neither of these solutions are grand, they may be useful to those wanting to learn more about inheritance.

Article

CSS Inheritance Overide by Selector and Child Element Reference

The following examples illustrate the ability of CSS to cascade a style to an unidentified child element through the use of a parent container using inheritance.

EXAMPLE: Selection using Class Attribute on Child Element

Template Editable Region

This area represents the editable region in the template. The image below contains a class attribute with the value set to ".image-class".

HTML

<img class="image-class" src="https://files.aati-us.com/images/agilent/hero-cell-metabolism-2018-1.jpg">

CSS

.image-class { width: 100%; height: auto; }

EXAMPLE: Selection using Class Attribute on Container Element

Container for Template Editable Region

This area represents a container that surrounds the editable region in the template. The container is outside of the editable region so that it is not available to users implementing the template.

Template Editable Region

This area represent the editable region in the template. The image below doesn't include a class attribute.

HTML

<img src="https://files.aati-us.com/images/agilent/hero-cell-metabolism-2018-1.jpg">

CSS

.ref-class img { width: 100%; height: auto; }

CSS Inheritance Inline Style Overide by usage of !important

The following examples illustrate the ability of CSS to cascade a style to an unidentified child element through the use of a parent container using inheritance.

EXAMPLE: Inline Style Attribute on Anchor Element

This is an example showing an anchor element containing an inline style attribute. The inline style attribute has a value set to the color of the link to red (#ff000).

Inline Style Link Example

HTML

<a href="#" style="color: #ff0000;">Inline Style Link Example</a>

EXAMPLE: Inline Style Attribute on Anchor Element with !important Override

This is an example showing an anchor element containing an inline sytle attribute. The inline style attribute value has been overridden with a selector in the header <style> using the !important declaration. The anchor element has been forced to Agilent Blue (#00a9e0) and the underline has been removed.

HTML

<a href="#" style="color: #ff0000;">Inline Style Link Example</a>

CSS

Note: The .link-ref is only implemented to allow the previous example to function correctly on the same web page.

.link-ref a { color: #00a9e0 !important; text-decoration: none; }

Author

Profile Picture of Bryan Myers - The Web Guy

Bryan Myers - "The Web Guy"

Advanced Digital Channel Engineer

Tags

  1. Articles discussing web related topics and technologies.Articles
  2. Articles about Web Design.Web Design

Related Articles

Go Back