5
.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?

4 Answers 4

8

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

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

.table
{
    color:red;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Yes... I gave an example of that above when I wrote <table class ="mc table"> This is not what I'm trying to acomplish.
What you're trying to accomplish doesn't work. This answer is correct and makes way more sense than trying to nest CSS classes.
That answer is NOT correct for what I'm trying to do. David simply posted a solution to another problem. Nesting CSS does make sense. For instance, maybe I don't want to have to write margin-left:auto; margin-right:auto for every page specific class instead just nest the .mac class and I don't want to have a global style sheet with 100s of classes refering to margin-left:auto; margin-right:auto; on a single line. You could techinically do it that way but to me it's messy and possibly only a few of the classes specified would be global classes. I hope this helps explain my reasoning.
I am with Tegeril and David here - in essence the answer IS nesting them - by referencing them. SO you either reference them in the html as you indicate or in the .css as the answer indicates depending on how you wish to construct your .css and html - if you have multiple tables, or if you other design considerations. The comma list is the "nesting" you desire really.
One other side note: table.table {} form also works for ONLY tables with .table class
|
7

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);
}

1 Comment

This is exactly what I was looking for, finally! Thanks BranTheMan.
2
.table, .mac {margin-left:auto; margin-right:auto;}
.table {color:red;}

3 Comments

why did somebody, mark this answer down? it's exactly the same as the first answer, just more compact ... there is nothing wrong with having your code on one line.
Yes, for production this is great, for development, I prefer the other form, but either works.
oh man, I was hoping for my Peer Pressure badge :-P, I submitted this answer about 3 seconds after David, maybe they thought I just copied it?
1

i depends what do you wanna do. in your case: - if you wanna use the CLASS table you can use .table - for CLASS mac you can use .mac -and yes You can combine them with a comma : .table, .mac - this is usually used if you put the same style for both of the classes.

BUT - if you wanna put style on the CLASS .mac which is in a table then this will help You:

table .mac{} - notice that I didn't use comma, just a space...this way You define the CLASS .mac inside the table..but not the CLASS .mac which is outside of the table

good luck

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.