Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing a CSS class only within a specific element

Tags:

css

matching

I am trying to find the CSS syntax for a specific problem. I really searched but couldn't find answer. Here is the problem. My HTML markup is as follow:

<div>
 <header>
  <div class="container">
   <div class="row">
   my content that I want red
   </div>
  </div>
 </header>
 <div class="container">
  <div class="row">
  my content that I want blue
  </div>
 </div>
</header>

I would like to display "my content that I want red" in red, and "my content that I want blue" in blue, and I only can use CSS for that.

Does anyone knows the trick that will make it? sub classes cant work here unfortunately.

Thanks in advance,

like image 315
user1084028 Avatar asked Jul 25 '13 17:07

user1084028


People also ask

How do I apply a CSS to a specific element?

The CSS id Selector The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.

Can you use the same CSS class on multiple elements?

When you group CSS selectors, you apply the same styles to several different elements without repeating the styles in your stylesheet. Instead of having two, three, or more CSS rules that do the same thing (set the color of something to red, for example), you use a single CSS rule that accomplishes the same thing.

Can you make a class selector particular to an element type?

You can create a selector that will target specific elements with the class applied.

How do you apply a class style rule to an element?

If you want to use a class, use a full stop (.) followed by the class name in a style block. Next, use a bracket called a declaration block that contains the property to stylize the element, such as text color or text size. CSS Classes will help you stylize HTML elements quickly.


1 Answers

There's lots of options. It all really depends on your circumstances. Here's a couple without modifying the HTML:

Using parent element

.row { color: blue }
header .row { color: red }

Using :nth-of-type

.column:nth-of-type(1) .row { color: red }
.column:nth-of-type(2) .row { color: blue }

If you're willing to add additional markup, you could always add additional classes.

like image 146
ponysmith Avatar answered Oct 13 '22 08:10

ponysmith