What am I doing wrong here?
I have a .social
div
, but on the first one I want zero padding on the top, and on the second one I want no bottom border.
I have attempted to create classes for this first and last but I think I've got it wrong somewhere:
.social {
width: 330px;
height: 75px;
float: right;
text-align: left;
padding: 10px 0;
border-bottom: dotted 1px #6d6d6d;
}
.social .first{padding-top:0;}
.social .last{border:0;}
And the HTML
<div class="social" class="first">
<div class="socialIcon"><img src="images/facebook.png" alt="Facebook" /></div>
<div class="socialText">Find me on Facebook</div>
</div>
I'm guessing it's not possible to have two different classes? If so how can I do this?
If you have 2 classes i.e.
.indent
and.font
,class="indent font"
works.You dont have to have a
.indent.font{}
in css.You can have the classes separate in css and still call both just using the
class="class1 class2"
in the html. You just need a space between one or more class names.If you want to apply styles only to an element which is its parents' first child, is it better to use
:first-child
pseudo-classThen, the rule
.social
has both common styles and the last element's styles.And
.social:first-child
overrides them with first element's styles.You could also use
:last-child
selector, but:first-child
is more supported by old browsers: see https://developer.mozilla.org/en-US/docs/CSS/:first-child#Browser_compatibility and https://developer.mozilla.org/es/docs/CSS/:last-child#Browser_compatibility.If you want two classes on one element, do it this way:
Reference it in css like so:
Example:
https://jsfiddle.net/tybro0103/covbtpaq/
I know this post is getting outdated, but here's what they asked. In your style sheet:
But it may be a bad way to use selectors. Also, if you need multiple "first" extension, you'll have to be sure to set different name, or to refine your selector.
I hope this will help someone, it can be pretty handy in some situation.
For exemple, if you have a tiny piece of css that has to be linked to many different components, and you don't want to write a hundred time the same code.
Becomes:
Remember that you can apply multiple classes to an element by separating each class with a space within its class attribute. For example:
If you only have two items, you can do this: