10

What does the following CSS do and is it valid?

h4 {
    width: 83%;
    #width: 75%;
}
1
  • That's not an acceptable hack to use. There are other (similar) hacks that have been tested and are more widely known.
    – thirtydot
    Aug 23, 2011 at 13:21
6

It is not valid. #width: 75%; is a syntax error, since # isn't used in CSS property names (although it is used in CSS selectors, to select elements with specific ids). Most browsers will ignore it (hopefully) and only the first rule will be applied.

It might have been someone's attempt to write a CSS comment. This is the valid way: /*This is a comment*/

Edit

I would suggest using a CSS reset file to account for browser differences.

4
  • 1
    # is used in CSS selectors, by the way, but I'm sure you know that. Aug 23, 2011 at 13:05
  • 3
    @Delan: But the OP is not referring to a selector.
    – BoltClock
    Aug 23, 2011 at 13:05
  • 2
    No, I have someone writing CSS like this for lots of things to fix IE issues and I need to know if it is acceptable, I am not a CSS expert. Aug 23, 2011 at 13:09
  • Well, hacks usually work but I wouldn't recommend them. To account for browser differences, it's usually much better to use a CSS reset file: yuilibrary.com/yui/docs/cssreset Aug 23, 2011 at 13:11
5

Apparently there's a hash hack which looks exactly like the one you have, but I have no idea what specific browsers the author is trying to target or filter since there aren't any reliable results as to what browsers apply the rule and what don't (that looooooong list of user agent strings isn't what I'd call reliable; I'd call it inconsistent).

In any case, a hash is not a valid character for property names. I'm sure anyone that isn't IE will squarely discard it on sight.

2
  • 1
    And before anyone gets started on safe CSS hacks, just look at that list of user agent strings in that link and tell me if you'll consider it safe. Then again, I quite like how the article states at the top, "Please ignore: this isn't a hack, because the CSS it specifies is invalid." - most hacks are invalid anyway but are still hacks.
    – BoltClock
    Aug 23, 2011 at 13:10
  • I don't think anyone was going to say # is a safe hack.
    – thirtydot
    Aug 23, 2011 at 13:22
3

using # before a property is applying different css style for ie 7. Is a css hack like *. To make it valid you can use conditional comments for ie.

2

From what I've read on http://developer.expressionz.in/blogs/2007/09/08/for-your-ies-only/ the hash-hack is intended to make a rule only visible to IE browsers. Since it is - as already mentioned by others - not a valid property, other browsers will ignore it.

2

BTW if the second width was not preceded by #, it would take width = 75% and not 83%. The last value always overrides all the preceding ones. As others pointed out, it could be a hack, which I don't know but most likely a syntax error.

2

To basically answer both your questions.

  • The # before the property targets IE7 & IE6 (and lower)
  • No, it's not valid.

I asked the same question, there's more info there that may be helpful to others:

Post: " CSS - "#" sign before property "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.