Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to put a css class inside of a css class?

Tags:

css

stylesheet

.mac{margin-left:auto; margin-right:auto;}

.table{.mac; color:red;}

I want to be able to do that, it doesn't make sense to me why I would need to do:

.table{margin-left:auto; margin-right:auto; color:red;}

I know for the table I could do:

<table class="table mac">

or just type the .table{margin-left:auto; margin-right; color;} like above but that seems redundant.

Is there some way to do what I'm trying to accomplish in my first statement, is there some special syntax that I'm not aware of to make this work?

like image 863
payling Avatar asked Dec 21 '09 16:12

payling


People also ask

Can you put a class inside a class CSS?

Nested rules are defined as a set of CSS properties that allow the properties of one class to be used for another class and contain the class name as its property. In LESS, you can use class or ID selectors to declare mixin in the same way as CSS styles.

How do you add a class inside CSS?

Creating a CSS Class Using a Class Selector After adding the code snippet to your styles. css file, save the file. In this code snippet you have added text using the HTML <p> tag. But you have also specified the red-text class by adding the highlighted class attribute class="red-text" inside the opening HTML tag.

Can you add a class through CSS?

With CSS Hero you can easily add classes to elements without needing to touch any of your theme files or adding weird code to your contents. Plus you can control all your classes right from the CSS Hero interface.

How can I use one class in another class in CSS?

You can just add a comma-separated list of classes in . radius , like . radius, . another, .


3 Answers

You can combine multiple CSS selectors on the same line with a comma:

.mac, .table
{
    margin-left:auto;
    margin-right:auto;
}

.table
{
    color:red;
}
like image 187
David Avatar answered Oct 08 '22 12:10

David


The short answer is no, you can't "include" other CSS classes or "inherit" them so to speak.

There are tools you can use to abstract that kind of stuff for you, though. LESS comes to mind. I think the "mixin" is what you're looking for.

Here is a code sample from the LESS front page:


.rounded_corners (@radius: 5px) {
  -moz-border-radius: @radius;
  -webkit-border-radius: @radius;
  border-radius: @radius;
}

#header {
  .rounded_corners;
}

#footer {
  .rounded_corners(10px);
}
like image 41
mrvisser Avatar answered Oct 08 '22 12:10

mrvisser


.table, .mac {margin-left:auto; margin-right:auto;}
.table {color:red;}
like image 29
Jake McGraw Avatar answered Oct 08 '22 11:10

Jake McGraw