Welcome to WebmasterWorld Guest from 54.144.126.195

Forum Moderators: not2easy

Message Too Old, No Replies

macros or define

are macros possible

     

jeremy

11:26 am on Apr 26, 2003 (gmt 0)

10+ Year Member



I was wondering if it was possible to define something eg a color with (in C) #define Brown #804040 and then use the term "Brown" eveywhere I wanted to have #804040, in the CSS file. This would make it easier to change colours on a global scale with just one edit, rather than looking for all instances of the colour.

I am new to CSS so excuse the lame questions. Thanks.

aspdaddy

11:51 am on Apr 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I did similar for an intranet in ASP, using a config file and some globals.e.g
G_BGCOLOR = "#f5f5f5"

Couldnt get it into the .css file so the next best thing was inline styles:
<table style="background-color:<%=G_BGCOLOR%>">

Its not a great solution , a macro would be much nicer but I couldnt figure out how.

heini

12:29 pm on Apr 26, 2003 (gmt 0)

WebmasterWorld Senior Member heini is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Jeremy, where do you see the advantage of globally changing "brown" over globally changing "#804040"?

Also, a way to organize your sheets is to make as much central definitons as possible, which also has the effect of cutting down on the size of the sheets.

jeremy

9:29 pm on Apr 26, 2003 (gmt 0)

10+ Year Member



Hello heini,
The advantage is that I would not have to do a global replace. I would only have to change it once - at the (eg #define Brown #804040 to #define Brown #CCCfff) and all instances where "Brown" occurs would change automatically and now be #CCCfff instead of #804040

This method is used in many languages (C pascal etc )
at the top of the file you have stuff like
#define Brown #804040
#define MyEmail billG@microsoft.com

and in the source you have something like.....

Background-color = Brown;
...
...
email = MyEmail;

These may occur many times within the file. The advantage of a macro is that I do not have to find them, just change the definition at the top of the file (or included file) and the preprocessor or compiler does the rest for me. It is neat & tidy.

I am not sure yet how sophisticated this CSS stuff is, I used to be a C,C++ programmer and old habits die hard.

I will probably just move all things that I might want to change from time to time (like aesthetics) to a separate file like color.css and have the definitions there. At least they will be in one place.

grahamstewart

2:39 am on Apr 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No there are no macro or define capabilities in CSS.
I agree it might be handy in some cases, but it would lead to a whole new category of bugs too :)

If you are really keen on coding like this then you could write your CSS in PHP. Like this...

<?php
header( 'Content-Type: text/css' );
define( 'BROWN', '#804040' );
define( 'PINK', '#daa');
?>

.myclass {
font: <?php echo BROWN?>;
background: <?php echo PINK?>;
border: 1px solid <?php echo BROWN?>;
}

and then link to it as an external stylesheet in the normal way..

<link rel="stylesheet" type="text/css" href="styles.php">

To my mind this is a bit too clunky to use effectively so I can't really recommend it - but if you are already using a php stylesheet for other reasons (e.g. browser switching) then it might be useful to you.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month