Results 1 to 3 of 3

Thread: Declaring Constants in a CSS file?

  1. #1
    Join Date
    Jun 2005
    Location
    Scotland
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Declaring Constants in a CSS file?

    Is this possible?

    In a css file I have a whole load of elements that have similar settings, colour for example. If I wanted to change the colour during the design phase I don't want to have to go and edit every reference to that colour. In a CSS file can a constant be declared to which a colour is assigned with the elemnets concerned referencing the constant so that all can be changed simply by changing the colour assigned to the constant?

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by AlistairH
    Declaring Constants in a CSS file?

    Is this possible?
    No, but there are alternatives.

    The first is to combine common declarations into a single rule with multiple selectors. Consider the snippet below, which was posted on Usenet recently:

    Code:
    .titlejgloss {
      background : #91eaff;
      border-color : #808080;
      border-style : inset;
      border-width : 1px;
      display : block;
      font-family : Arial,Helvetica,sans-serif;
      font-size : 200%;
      font-weight : bold;
      padding : 5px;
      margin-bottom : 10px;
      margin-top : 0px;
    }
     
    .titlebgloss {
      background : red;
      border-color : #808080;
      border-style : inset;
      border-width : 1px;
      display : block;
      font-family : Arial,Helvetica,sans-serif;
      font-size : 200%;
      font-weight : bold;
      padding : 5px;
      margin-bottom : 10px;
      margin-top : 0px;
    }
    The only thing that is different between them is the background property. It could be replaced by:

    Code:
    .titlejgloss,
    .titlebgloss {
      border: 1px inset #808080;
      display: block;
      font: bold 200% Arial, Helvetica, sans-serif;
      padding: 5px;
      margin: 0 0 10px;
    }
    
    .titlejgloss {
      background: #91eaff;
    }
     
    .titlebgloss {
      background: red;
    }
    Notice the comma that separates the two class selectors. This could be applied to your situation.

    If that isn't adequate, then use a preprocessor of some kind to expand macros within the file. You could use PHP, for instance, on the command line to generate a CSS file from a PHP file, and upload the output.

    Mike

  3. #3
    Join Date
    Jun 2005
    Location
    Scotland
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    That'll do nicely. Thanks Mike.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •