Jump to content

[SOLVED] Apply a class to another class


jaymc

Recommended Posts

Is their a way to do this

 

.mytext {

color:#ff0000;

}

 

.someclass {

text-align:center;

background-color:#000000;

color: .mytext

}

 

I know thats not the correct syntax, but in my head thats how i'd like it to work. This means that rather than constantly defined the text colour in each class i make, I can just do it once and import it into each class

 

This is just a dummy example, so dont say put it in the body {} etc, it needs to work like above

 

Can this be done?

 

If so, how

 

Thanks

Link to comment
Share on other sites

you have a couple of possibilities.

 

in css....

.mytext,
.someclass
{
color:#ff0000;
}

.someclass
{
text-align:center;
background-color:#000000;
}

or kind of using your original code... (note the color: .mytext WILL fail so don't use it)

.mytext
{
color:#ff0000;
}

.someclass
{
text-align:center;
background-color:#000000;
}

 

<p class="someclass mytext">

  ...

</p>

Link to comment
Share on other sites

Is their a way to do this

 

.mytext {

color:#ff0000;

}

 

.someclass {

text-align:center;

background-color:#000000;

color: .mytext

}

 

I know thats not the correct syntax, but in my head thats how i'd like it to work. This means that rather than constantly defined the text colour in each class i make, I can just do it once and import it into each class

 

This is just a dummy example, so dont say put it in the body {} etc, it needs to work like above

 

Can this be done?

 

If so, how

 

Thanks

 

The code you posted will not work. You can not define classes within other classes. Instead, css cascades from the parent div to the child divs - this is called inheritance and in many ways, is similar to your idea.

Link to comment
Share on other sites

The only real way to do this is to make your stylesheets php documents, then add the color using php. You will want to look at how to cache php documents if you decide to use this method however. It's not easy.

 

Using the find and replace function in whatever text editor you are using would probably be easier and faster in the long run. If you want to change from #123456 to #654321, then you just use ctrl+f and enter in the first one. Every time it finds it, change it.

Link to comment
Share on other sites

The only real way to do this is to make your stylesheets php documents, then add the color using php...

 

No it isn't.  You should have NO need for php to generate a css file ever - the beauty of cs files is that they are cached and hence speed up the rendering of your site...

The ONLY situation where I use php to control anything to do with my presentational layer would be to generate inline styles - and then I'll only use it to put background-image: url(/path/to/image); in something like a product list...

 

Use css well and you don't end up with reams of css to manage - the technique I gave earlier in this thread is very much the way you should build your presentational layer.

Link to comment
Share on other sites

His point was that he wanted to be able to change the color once, and have it changed for all items that used the same color. I at the time couldn't think of a way to do it other than PHP, which is why I suggested it. As for caching, php files can be cached, though it is a bit of a pain in the ass to do it.

 

BUT that all being said, anything that requires the color could be put into one selector so that the color would only have to be changed once. For example:

 

a.this_class, #some_id, div span a.link, pre.another_class // add all other areas that use the color in question
{
    color: red;
}

 

It would make that one statement very bulky, but it would solve the problem of only having to change the color in one spot, and having that be reflected anywhere that color is used.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.