Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Affecting child elements by modifying the parent by CSS. is it possible?

Tags:

css

hover

Maybe it's a silly question, but I'm sure it will be useful for many of beginners. I have constructed a code. What I'm trying to do is to display the text (in <p> tags) which is currently hidden, only when the actual <a> is hovered. Is this possible without js? Can it be done by operating on css / html only? the html code is here:

                <div class="promo2">
                    <a href="speakers-hire.html">
                        <div class="promo2a">
                            <p>SPEAKERS</p>
                        </div>
                    </a>

                    <a href="decks-hire.html">
                         <div class="promo2a">
                            <p>CD PLAYERS</p>
                        </div>
                    </a>

                </div> 

and the css which correspond to the above html:

.promo2a:hover{
background:url(../_images/generic/glass5.png);
display:inline-block;

}

.promo2a p{
display:none;

}

So basically I want the text in <p> to be displayed when the whole div promo2a is hovered.

Any help is kindly appreciated. Thank you in advance.

like image 685
Piotr Ciszewski Avatar asked Jul 28 '12 12:07

Piotr Ciszewski


People also ask

How do I apply CSS to child element based on parent?

It's easy to apply style to a child element, but if you want to apply style to a parent class that already has child elements, you can use the CSS selector child combinator ( > ), which is placed between two CSS selectors. For example, div > p selects all <p> elements where the parent is a <div> element.

Is there a concept for CSS parent selector?

Let's be clear here, just in case someone is finding this from a search engine: there are no parent selectors in CSS, not even in CSS3. It is an interesting topic to talk about though, and some fresh talk has surfaced.


2 Answers

Try

.promo2a:hover > p {display:block;}

or whatever suits your needs

like image 106
amon Avatar answered Oct 31 '22 08:10

amon


You get pretty close, i just have to use :hover to display , ex:

.promo2a:hover p { display: block; }
like image 39
Marcelo De Zen Avatar answered Oct 31 '22 07:10

Marcelo De Zen