383

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?

4
  • 3
    I created a simple example to demonstrate the difference between the descendent selector and the double-class selector: jsfiddle.net/jyAyX. Also, see developer.mozilla.org/en-US/docs/CSS/Getting_Started/Selectors. Commented Aug 11, 2012 at 23:58
  • Just curious if I do: <p class = "a" class = "b"></p> will both rules be applied or just b or ... I am doing a quick and dirty alpha and was about to cut and paste class statements into lots of elements that did not have classes but a few do. Just trying to work out how this would be handled. Have looked bit with no joy. Commented Sep 12, 2016 at 13:26
  • 1
    Also related (if not even considered as duplicate of): CSS Selector that applies to elements with two classes Commented Jul 15, 2018 at 18:01
  • 1
    @BeNice Only the first class attribute applies; the second one will be ignored. In your example, class = "a" will apply, class = "b" will be ignored. Commented Jan 27, 2021 at 5:35

10 Answers 10

696

If you want two classes on one element, do it this way:

<div class="social first"></div>

Reference it in css like so:

.social.first {}

Example:

https://jsfiddle.net/tybro0103/covbtpaq/

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, wasn't aware I could double up like that :) Good to know for the future!
What would happen if we had just <div class="first"></div> ?
@MarcoCastanho I updated answer with link to example
I just wanted to add the order which you reference the CSS element will not matter so you can also reference it in css like so: .first.social {} and it will be the same as .social.first {}
it is important to note that there is no space in between 2 classes. Spent 5 minutes figuring out what I was doing wrong.
|
44

You can try this:

HTML

<div class="social">
    <div class="socialIcon"><img src="images/facebook.png" alt="Facebook" /></div>
    <div class="socialText">Find me on Facebook</div>
</div>

CSS CODE

.social {
  width:330px;
  height:75px;
  float:right;
  text-align:left;
  padding:10px 0;
  border-bottom:dotted 1px #6d6d6d;
}
.social .socialIcon{
  padding-top:0;
}
.social .socialText{
  border:0;
}

To add multiple class in the same element you can use the following format:

<div class="class1 class2 class3"></div>

DEMO

Comments

34

Remember that you can apply multiple classes to an element by separating each class with a space within its class attribute. For example:

<img class="class1 class2">

Comments

20

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.

1 Comment

For some reason it didn't work for me, although I also believe that it should work the way you've mentioned. Suspecting if my CSS classes being defined in two different stylesheets could be a factor. Need to investigate this.
10

If you only have two items, you can do this:

.social {
    width: 330px;
    height: 75px;
    float: right;
    text-align: left;
    padding: 10px 0;
    border: none;
}

.social:first-child { 
    padding-top:0;
    border-bottom: dotted 1px #6d6d6d;
}

Comments

7

I know this post is getting outdated, but here's what they asked. In your style sheet:

.social {
    width: 330px;
    height: 75px;
    float: right;
    text-align: left;
    padding: 10px 0;
    border-bottom: dotted 1px #6d6d6d;
}
[class~="first"] {
    padding-top:0;
}
[class~="last"] {
    border:0;
}

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.

[class="social first"] {...}

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.

div.myClass1 {font-weight:bold;}
div.myClass2 {font-style:italic;}
...
div.myClassN {text-shadow:silver 1px 1px 1px;}

div.myClass1.red {color:red;}
div.myClass2.red {color:red;}
...
div.myClassN.red {color:red;}

Becomes:

div.myClass1 {font-weight:bold;}
div.myClass2 {font-style:italic;}
...
div.myClassN {text-shadow:silver 1px 1px 1px;}

[class~=red] {color:red;}

Comments

6

If you want to apply styles only to an element which is its parents' first child, is it better to use :first-child pseudo-class

.social:first-child{
    border-bottom: dotted 1px #6d6d6d;
    padding-top: 0;
}
.social{
    border: 0;
    width: 330px;
    height: 75px;
    float: right;
    text-align: left;
    padding: 10px 0;
}

Then, 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-childis 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.

1 Comment

To see if you can use :last-child and :first-child in other browsers check: caniuse.com/#search=first-child
5

Another option is to use Descendant selectors

HTML:

<div class="social">
<p class="first">burrito</p>
<p class="last">chimichanga</p>
</div>

Reference first one in CSS: .social .first { color: blue; }

Reference last one in CSS: .social .last { color: green; }

Jsfiddle: https://jsfiddle.net/covbtpaq/153/

Comments

1

Instead of using multiple CSS classes, to address your underlying problem you can use the :focus pseudo-selector:

input[type="text"] {
   border: 1px solid grey;
   width: 40%;
   height: 30px;
   border-radius: 0;
}
input[type="text"]:focus {
   border: 1px solid #5acdff;
}

Comments

0

If you are using className you can just use string interpolation.

styles.module.css file

.mermaidRows{
  display: flex;
  flex-direction: row;
}

.mermaidColor{
  background-color: purple;
}

Then in the .tsx file html

import styles from './styles.module.css';

<div className={`${styles.mermaidRows} ${styles.mermaidColor}`}>
...
</div>

Both styles will be applied to the div.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.