Dave
Dave

Reputation: 1852

Can I set a default css class for an html element

If I define a css class, is there anyway to set that class as a default class for an html element? To clarify, I was hoping to factor out the definition of the class so it could be used by one or more css selectors or applied discreetly in the html.

For example:

.myclass {float:right}
h1 {class: myclass}

The above does not work of course, but hopefully you know what I am asking as I have not been able to find how to do this.

Upvotes: 3

Views: 7374

Answers (6)

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201568

From your repeated comment “I was hoping to factor out the definition of the class so it could be used by one or more elements or applied discreetly”, it seems that what you are really up to is how to define a set of CSS declarations so that they apply to elements in a given class and and some elements independently of class. The way to do this is to use a list of selectors, e.g.

.myclass, h1 { float:right; /* and other declarations as needed */ }

This is the kind of “factoring out” that you can achieve in CSS. There is no way to “factor out” “CSS classes”, because there are no CSS classes. There are classes in HTML, and you can specify rules that apply to such classes.

Upvotes: 1

Jonathan
Jonathan

Reputation: 9151

I'm not sure what you want to achieve, but maybe you ask this because you want to have multiple html elements use the same "class"?

If that's the case you can write it like this:

h1, h2, span{
    background: red;
}

Upvotes: 0

Brian Lewis
Brian Lewis

Reputation: 5729

You can do that if you are using a CSS processor like LESS (http://lesscss.org/#-mixins) or SASS (http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#mixins).

Upvotes: 1

Ed Heal
Ed Heal

Reputation: 59997

Just write the following HTML

<h1 class="mylass"> .... </h1>

and write VALID CSS

i.e.

.myclass {float: right}

Upvotes: 0

Matt Ball
Matt Ball

Reputation: 359816

If I define a css class, is there anyway to set that class as a default class for an html element?

No. You can, however, select all elements and apply a rule to them:

* {
    foo: bar;
}

Upvotes: 2

Evan Mulawski
Evan Mulawski

Reputation: 55334

Not with standard CSS, but why not just do:

h1 {
    float: right;
}

Upvotes: 4

Related Questions