Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two classes next to each other in CSS

Tags:

html

jquery

css

HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="navigation_desktop">

    <div class="button">1.0 Main Menu
        <div class="FadeItem">
            <ul>
                <li>1.1 Sub Menu </li>
                <li class="button">1.2 Sub Menu
                    <div class="FadeItem">
                        <ul>
                            <li>1.2.1 Sub Menu</li>
                            <li>1.2.2 Sub Menu</li>
                        </ul> 
                    </div>
                </li> 
            </ul>
        </div>
    </div>
</div>

CSS:

 .button {
 float: left;
 position: relative;
 padding-left: 1%;
 padding-right: 1%;
 cursor: pointer;
 }

.FadeItem {
display: none
}

.FadeItem .FadeItem {
position: absolute;
left: 100%;
top: 0;
width: 130px;
height: 100%;
}

jQuery:

$(document).ready(function() {
  $(".button").mouseenter(function() {
    $(this).children(".FadeItem").fadeIn(500);
  });
  $(".button").mouseleave(function() {
    $(this).children(".FadeItem").fadeOut(500);
  });
});

With the code above I let FadeIn/Out some items once I hover over a button. The code works perfectly but I have a general question regarding the coding of the CSS in the example above.

In the CSS there is this part:

.FadeItem .FadeItem {
}

When do you create a CSS code like this? What does it trigger?

I am a newbie to the CSS programming and so far I only used one class or two classes divided by a comma. I want to improve my coding knowledge so it would be cool if you could give me an explanation about the code above.

The code you can also find here: https://jsfiddle.net/gge42bob/3/

like image 570
Michi Avatar asked Aug 12 '17 07:08

Michi


People also ask

How do I get two items next to each other in CSS?

The most common way to place two divs side by side is by using inline-block css property. The inline-block property on the parent placed the two divs side by side and as this is inline-block the text-align feature worked here just like an inline element does.

Can you have two classes in CSS?

To specify multiple classes, separate the class names with a space, e.g. <span class="left important">. This allows you to combine several CSS classes for one HTML element.

Can 1 div have 2 classes?

Absolutely, divs can have more than one class and with some Bootstrap components you'll often need to have multiple classes for them to function as you want them to. Applying multiple classes of course is possible outside of bootstrap as well. All you have to do is separate each class with a space.

How do I align boxes next to each other in CSS?

Instead of using float:left, you can use: "display:inline-block", this will put the div next to each other.


2 Answers

It targets a .FadeItem within another .FadeItem, so in this case it's useful because the styles only apply to the inner .FadeItem element, not the outer.

Your markup is similar to

<div class="FadeItem">
    <div class="FadeItem"></div>
</div>

So only

.FadeItem {styles}

will target both of them, but

.FadeItem .FadeItem {styles}

targets only the one that's inside the other

like image 190
adeneo Avatar answered Sep 20 '22 13:09

adeneo


This code:

.FadeItem .FadeItem { }

Selects all elements with class FadeItem that is inside another element with class FadeItem.

For example :

.FadeItem .FadeItem {
    color: red;
}
<div class="FadeItem">First Fade Item
    <div class="FadeItem">
       Second Fade Item 
    </div>
</div>

This code:

.FadeItem { }

Selects all elements with class FadeItem.

For example :

.FadeItem {
    color: red;
}
<div class="FadeItem">First Fade Item
    <div class="FadeItem">
       Second Fade Item 
    </div>
</div>
like image 41
Ehsan Avatar answered Sep 19 '22 13:09

Ehsan