Sibling Selector (tilde) Issue - html

I'm trying to modify named elements in my design. I've simplified the situation as you can see in this fiddle:
http://jsfiddle.net/6THpF/
Simply, when I try to modify one named element from the other, it works, but when I try to do the same modification from the opposite direction (other element) it does not work.
Here is the sample code:
body{
background-color:lightgrey;
}
#div_one, #div_two{
background-color:darkred;
display:block;
width:300px;
text-align:center;
padding:30px;
margin:10px;
color:white;
-webkit-transition: 0.3s ease;
-moz-transition: 0.3s ease;
-ms-transition: 0.3s ease;
-o-transition: 0.3s ease;
transition: 0.3s ease;
}
#div_one:hover ~ #div_two{
background-color:red;
}
#div_two:hover ~ #div_one{
background-color:red;
}

From MDN:
The ~ combinator separates two selectors and matches the second
element only if it is preceded by the first, and both share a common
parent.
So since div_one is not preceded by div_two the second rule won't work. In other words, there is no "previous sibling" selector.

Try this.I answered:
HTML:
<div id="div_one">This is div 1</div>
<div id="div_two">This is div 2</div>
CSS:
body{
background-color:lightgrey;
}
#div_one, #div_two{
background-color:darkred;
display:block;
width:300px;
text-align:center;
padding:30px;
margin:10px;
color:white;
-webkit-transition: 0.3s ease;
-moz-transition: 0.3s ease;
-ms-transition: 0.3s ease;
-o-transition: 0.3s ease;
transition: 0.3s ease;
}
JAVASCRIPT:
$(document).ready(function(){
$("#div_one").mouseover(function(){
$("#div_two").css("backgroundColor","blue");
});
$("#div_one").mouseout(function(){
$("#div_two").css("backgroundColor","darkred");
});
$("#div_two").mouseover(function(){
$("#div_one").css("backgroundColor","blue");
});
$("#div_two").mouseout(function(){
$("#div_one").css("backgroundColor","darkred");
});
});
remember that use and embed the jquery.js core to your web page.

I am not solving the sibling selector issue (that I think that can not be solved), but the effect intended.
You need to group both divs in one:
<div>
<div id="div_one">This is div 1</div>
<div id="div_two">This is div 2</div>
</div>
and now target the hover state of the group div:
div:hover #div_two{
background-color:red;
}
div:hover #div_one{
background-color:red;
}
of course the later can be written better, (or shorter) I just wanted to change as little as possible in your example
This has some drawbacks that you may be will need to address.
If you want the divs to have space between them (and this space not responsive to the hover), then I think that you will need to change them to absolute positioning.
see the fiddle
edited
I missed the requirement about not hovering the hovered div. Change css to
div:hover #div_two:not(:hover){
background-color:red;
}
div:hover #div_one:not(:hover){
background-color:red;
}
Now I think it's what you wanted edited fiddle

Related

Center <a> tag in div

Currently having an issue with my button. I want to be able to center my 'a' tag but at the moment it will only stick to the left side. I have tried using "display:block" but this will make my button take up the full width of any div it's been put in.
HTML:
Apply Now
CSS:
.button {
padding:1em;
text-align: center;
display:inline-block;
text-decoration: none !important;
margin:0 auto;
-webkit-transition: all 0.2s ease-in-out;
-moz-transition: all 0.2s ease-in-out;
-ms-transition: all 0.2s ease-in-out;
-o-transition: all 0.2s ease-in-out;
transition: all 0.2s ease-in-out;
}
Use a div to center the link
.button {
padding:1em;
text-align: center;
display:inline-block;
text-decoration: none !important;
margin:0 auto;
-webkit-transition: all 0.2s ease-in-out;
-moz-transition: all 0.2s ease-in-out;
-ms-transition: all 0.2s ease-in-out;
-o-transition: all 0.2s ease-in-out;
transition: all 0.2s ease-in-out;
}
.container{
width: 100%;
text-align: center;
}
<div class="container">
Apply Now
</div>
Use the text-align: center; on a parent element to center align all the child elements inside it. They(child elements) do not have to be block level elements for the same. Your question has the concern that you do not want to take up the full space of the parent div by using display:block on your a tag.
You don't have to, even if you specify display:inline-block on your a tag and wrap it inside a parent with text-align: center;, it will solve your task.
Alternatively, you can use margin-left:25% or so, in case the above answer does not suit your need.
Feel free to drop in the code in case you need more help on this.
Thanks,
Yaman
I agree with Yaman, the easiest way I used is a < p > tag, just put it before and after your link
<p align="center">
Apply Now
</p>
the issue is simple. Just add a paragraph tag before the button and inside the tag, add the alignment. This should work
<p align="center">
Hi
If text-align: center does not work than You can add a style tag in the respective tag and use
justify-content: center;

How can I make this so it becomes visible when hovered over

I'm just trying to make a paragraph that becomes visible when I hover over it. In HTML I just have a paragraph inside the body, and I've also tried it in a div in the body.My code in CSS is just
p{
position:absolute;
visibility:hidden;
}
p:hover p{
visibility:visible;
}
First of all you cannot put <p> elements inside another <p>, that might be the problem. You can use a <div> or any of these as the container.
<div><p>paragraph</p></div>
div p {}
div:hover p {}
You can do this pretty easily with the opacity element in css. Do note that there are vendor specific usages with opacity and transition.
.hover-vis {
filter: alpha(opacity=0);
opacity: 0;
-moz-transition: opacity 0.3s ease-in-out;
-o-transition: opacity 0.3s ease-in-out;
-webkit-transition: opacity 0.3s ease-in-out;
transition: opacity 0.3s ease-in-out
}
.hover-vis:hover {
opacity: 1
}
<p class="hover-vis">I show up when hovered on</p>
<p>I'm normal</p>

Smooth transition using background images

I am transitioning a couple of background images. However, I am noticing that when if the user acts before the transition has fully taken affect, the transition is instant.
For example, I hover over the div and the image takes 1 second to completely transition. However, I remove the cursor .5s into the transition. So it transitions back to the original image but instead of smoothly transitioning, it changes instantly. How do I make it smooth at all times?
.class {
width:500px;
height:500px;
background-image:url("http://activite-paranormale.net/extended/photo/news/nature-harmony-girl.jpg");
-webkit-transition: all 1s ease 0;
-moz-transition: all 1s ease 0;
-o-transition: all 1s ease 0;
transition: all 1s ease 0;
}
.class:hover {
background-image:url("http://images4.fanpop.com/image/photos/22700000/Spring-beautiful-nature-22727146-500-500.jpg");
}
<div class="class"></div>
http://codepen.io/john84/pen/vEdPEW
fiddle
I changed ease to ease-in-out
edit: Firefox doesn't support this, but there are many workarounds, example:
fiddle
HTML:
<div class="class"></div>
<div class="class2"></div>
CSS:
.class {
position:absolute;
width:500px;
transition:all 500ms;
height:500px;
background-image:url("http://activite-paranormale.net/extended/photo/news/nature-harmony-girl.jpg");
}
.class2{
width:500px;
height:500px;
background-image:url("http://images4.fanpop.com/image/photos/22700000/Spring-beautiful-nature-22727146-500-500.jpg");
}
.class:hover {
opacity:0;
}

How to use a non-fixed height with transitions on an accordion

My work so far is here - http://jsfiddle.net/WDz6R/.
I'm trying to make each section of the accordion have an automatic height, but when i use 'height:auto' or a percentage instead of a pixel based height the sections are the correct height, but the transitions break.
Does a fixed height have to be used in order to use css transitions or is there another workaround?
This is the relevant part of the css:
#accordion section {
overflow:hidden;
height:220px;
}
#accordion section, #accordion .pointer, #accordion h1, #accordion p {
-webkit-transition: all 0.5s ease-in-out;
-moz-transition: all 0.5s ease-in-out;
-ms-transition: all 0.5s ease-in-out;
transition: all 0.5s ease-in-out;
}
Short answer, to animate it you need fixed heights. There's a sloppy max-height workaround shown here:
http://jsfiddle.net/WDz6R/3/
#accordion section.ac_hidden {
max-height:44px;
}
#accordion section {
overflow:hidden;
height:auto;
max-height: 400px;
}
Or use jQuery's slideUp() / slideDown()

targeting different div styles with target method

Hello I am using the target method to manipulate different div styles, for the first "link_one" everything is working, while I have only one link, the question is how to make it work for "link_two" ? So link_two will do the second part of the css ? What is more important here is that each link is maniluplating 2 different classes in which link one and two one of the class is the same.
link_one
<div id="sections">
<div id="link_one">info</div>
<div id="link_two">info</div>
</div>
/* link one code */
#sections:target #link_one{
height:90px;
background:#333;
transition:all 1s ease;
}
#sections:target .rslides {
height:0px;
transition:all 1s ease;
}
/* link two code */
#sections:target #link_two{
height:90px;
background:#333;
transition:all 1s ease;
}
#sections:target .rslides {
height:0px;
transition:all 1s ease;
}
One way to apply the target selector would be:
for this HTML
link_one
<br>
link_two
<div id="sections1"></div>
<div id="sections2"></div>
<div id="link_one" class="link">info</div>
<div id="link_two" class="link">info</div>
Set this CSS
.link {
height: 20px;
transition:all 1s ease;
}
#sections1:target ~ #link_one{
height:90px;
background:#333;
}
#sections2:target ~ #link_two{
height:90px;
background:#333;
}
fiddle