views:

181

answers:

6

Is there a way to point to the default CSS class of an object?

For example, depending if a user is logged in I want to specify a different CSS class to control the style of a header.

$css_class = "";
if($logged_in)
    $css_class = "success"

echo "[h1 class=".$css_class."] My Title [/h1]"

If the user is logged in, the css class is set to "success"" otherwise, it's set to "".

I know it's improper w3c to have a blank class, but is there a way that I can just point to the default H1 property instead of creating a separate "not logged in" css class?

+1  A: 

<h1 class="">My Title</h1> will work fine (not sure if it'll pass W3C validation, but it'll work in all major browsers).

ceejayoz
An empty CSS class will not validate.
Developer Art
If an empty CSS class will not validate, then you can just use a dummy CSS value, i.e. one that doesn't have an actual style defined.
Matt Huggins
Yes it displays fine, but it doesn't validate. I'm just curious if there's a way to reference it's base class. like class="self" or something along those lines. something that WILL validate :)
justinl
It will only not validate under XHTML 1.1, XHTML 1.0, HTML 4.01 and HTML5 all validate fine, and arguably you should be moving towards HTML5 anyway.
roryf
@Matt Huggins. You mean like .dummy{} . and then refer to class="dummy"?
justinl
That should read that it's only invalid in XHTML 1.1, the rest validate fine
roryf
@justinl No need to even declare `.dummy {}` in the CSS - `class="dummy"` will work fine w/o. @Matt Huggins' method would seem to be the way to go.
ceejayoz
+2  A: 

Why not just omit the class attribute if it's empty?

$css_class = "";
if($logged_in)
    $css_class = " class=""success"""

echo "[h1.$css_class.] My Title [/h1]"

(Not sure about the escaping of quotation marks in a string... I have no idea what language you are using...)

Guffa
He's using PHP, which escapes with a backslash, i.e.: $css_class = " class=\"success\"";
Matt Huggins
Hi Guffa. Thanks. I'm doing that in a few other places, but in some more copmlex cases, i'm using more than 1 class to define an object's css so having the class="" inside the var doesn't always work. However this is a great alternative for most applications of the solution.
justinl
A: 
$css = $logged_in ? 'success' : 'dummy-undefined-css-value';
echo "<h1 class=\"$css\">My Title</h1>";
Matt Huggins
Thanks Matt. It does validate. Is it bad practice to be using a dummy undefined class like that? Or is that acceptable practice? (sorry I'm a beginner).
justinl
+2  A: 

It's not invalid to to have an empty string for a className. The class attribute is a cdata-list, so pretty much anything can go in there and it will still validate.

However, you will need to use quotes around your attribute values, otherwise you can't make an empty attribute parse. It's the right thing to do to always include the quotes anyway.

echo '<h1 class="'.htmlspecialchars($css_class).'"> My Title </h1>'
bobince
+3  A: 

An empty class attribute is only invalid under XHTML 1.1. Using a DOCTYPE of XHTML 1.0, HTML 4.01 and HTML 5 is will validate fine.

I wouldn't get too hung up on validation, it's very useful but isn't the be-all and end-all of web development. The only instance where I absolutely make sure my HTML 100% validates is during the very initial HTML and CSS build, since at that stage invalid markup can cause havock with CSS. Once I start adding server-side and Javascript interactions I'm not overly concerned with it.

Of course, you shouldn't just blatently ignore it, but as long as you know what the validation errors are, understand them, and have made a concsious decision not to fix them, I think that's okay.

roryf
Thanks Rory. It's good to hear that, while validation is a great tool, it's not the "be-all end-all" as you say. And it's okay if things aren't 100%
justinl
Yeah. Validation can be very handy in diagnosing display issues, but there are times where you can look at a validation error and say "I know that doesn't cause problems, I can ignore it". This would be one of them.
ceejayoz
A: 

If you're going to have a conditional like in Matt Huggins' answer anyway, and considering your comment on Guffa's answer, why don't you move the conditional into the echo? That'll give you the cleanest HTML output. For example:

$css_class = array();
if($logged_in)
{
    $css_class[] = "success";
}

if($something_else)
{
    $css_class[] = "some-class";
}

echo '<h1' .
     empty($css_class)
         ? ''
         : ' class="' . implode(' ', $css_class) . '"'
     . '> My Title </h1>';
mercator