Suppose we have HTML:
<div id="wrapper">
I want this to change color when button hovered
<div class="button">Close</div>
</div>
We can change the element's style when the wrapper is hovered:
#wrapper:hover .button{
color:red
}
Now want the opposite thing:
button:hover #wrapper{
background:yellow
}
#dan; you can do this with css also like this:
#wrapper{position:relative;}
#wrapper:hover .button{
color:red;
}
.button:hover:after{
content:"";
background:yellow;
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
z-index:-1
}
check this example http://jsfiddle.net/sandeep/b6apC/ .It's a fake but can achieve that effect with css.
While, as noted, this question cannot be answered with CSS, it can be achieved with JavaScript (and without need of a library, such as jquery, mootools etc):
var b = document.getElementsByClassName('button');
for (i = 0; i < b.length; i++) {
b[i].onmouseover = function() {
this.parentNode.style.backgroundColor = '#f90';
};
b[i].onmouseout = function() {
this.parentNode.style.backgroundColor = '#fff';
};
}
JS Fiddle demo.
Edited
You can also, with CSS3, apply fading to the backgroundColor changes:
div[id^=wrapper] {
/* other stuff */
-webkit-transition: background-color 0.5s linear;
-moz-transition: background-color 0.5s linear;
-o-transition: background-color 0.5s linear;
}
JS Fiddle demo
If you don't mind an extra element for the text (which is most common anyway) then use the hackless and pure CSS1 solution from this answer which leads to this fiddle:
Markup:
<div id="wrapper">
<span>I want this to change color when button hovered</span>
<div class="button">Close</div>
</div>
Style sheet:
#wrapper {position: relative}
#wrapper:hover {background: yellow}
#wrapper span:hover {background: white}
#wrapper span {display: block; padding-bottom: 1em}
#wrapper .button {position: absolute; bottom: 0}
This cant be done via CSS im afraid, however via Jquery... Here is a solution! (Untested but theory is there)
<script type="text/javascript">
$(document).ready(function()
{
$(".button").hover(function()
{
$(this).closest("#wrapper").stop().animate({"color": "#fff"}, "medium");
},
function()
{
$(this).closest("#wrapper").stop().animate({"color": "#000"}, "fast");
}
);
});
</script>
Hope this helps you. The key is the "closest" bit, This searchs up the structure to find the first instance of "wrapper" and then does its jquery magic to it.
In general, CSS styles can only affect things further down the tree - so you cannot affect the wrapper (essentially ever).
However, you may be able to fake it: you probably don't care about whether the wrapping element changes color, but rather whether the visual circumscribing block changes color. This outer block does not necessarily need to be a wrapping element - indeed, there are several possible alternatives if you're willing to control the layout in more detail.
You could make the "wrapper" and "button" siblings, and then use #button:hover + #wrapper
You could have an invisible element the size of the button and include the wrapper and button within it - then declare the hover style on it.
If you only care about a background color, make the wrapper's background transparent. Then, when on button hover unhide or generate a large colored background box with a lower z-index. (You can position this new background box either handily using top, left etc. or, in case that's impossible due to other positioning, simply make it huge and with negative margins and hide overflow in the wrapper).
This last approach is particularly attractive since it doesn't require manually positioning the button. However, it's also more limited in that in doesn't really affect the surrounding box; so you can only change the background-color and not for instance the text color. I've implemented an example here: http://jsfiddle.net/emn13/xExvC/
Related
Given a basic HTML template and CSS style, I'm seeing two different elements react completely different.
setTimeout(function() {
document.body.id = 'animate';
}, 100);
#animate input[type="checkbox"]{
width: 100px;
height: 100px;
transition: 2s all;
-moz-appearance: none;
}
#animate div{
width: 100px;
height: 100px;
background:blue;
transition: 2s all;
}
<input type="checkbox"/>
<div></div>
If you open this in a browser, you see that, on load, the div already has its 100px height/width, yet the checkbox grows from 0px to 100px height/width over 2s.
Why does the input behave differently than the div? Is it because the input has default -webkit-appearance giving it something to transition between?
The div's default width/height is auto and as such it won't animate.
The input has a default width/height and as such will animate.
As a side note, the transition does work on the div, though only animate its color, as it is possible to animate a color from transparent to blue
You should also consider to not use all with transition, as it can give unpredictalbe result because of this fact that browsers does set some values on elements to a default value, where some can be animated, some can't.
So, in your case, if your intention is to animate width/height, set it up like this: transiton: width 2s ease, height 2s ease;
The answer is simple. The input's style has a pre-loaded values in the DOM, that's why just right after appearing in the document, smoothly changes his shape.
Quite contrary with the div element. The div hasn't any pre-loaded, default values in the DOM before setting them by the user. That's why it appears in the document with already set size.
Important note
If you want the transition to work, it has to have a set, starting, default value and ending value. The animation will occur between these two values. The input has already set the default value of the size, that's why the animation will occur.
You may ask, so why the background transition is working? It works, since the default value of background is transparent.
setTimeout(function() {
$('.xx').addClass('x');
}, 500);
* {
margin: 0;
padding: 0;
border: 0;
}
input[type="checkbox"] {} div {} .x {
width: 100px;
height: 100px;
transition: all 2s ease;
background: blue;
}
.container {
display: flex;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='container'>
<input type='checkbox'>
<div class='xx'></div>
<input class='xx' type="checkbox">
</div>
Short answer without being to technical. CSS transition's will do it's job when there is a previous state to start animate from.
A default div doesn't have any styling by default.
An input element is always pre-styled in the browser itself.
Here is a fiddle that can be used to re-create the behaviour that the OP mentioned. It simulate external loading with a simply JS delay. https://jsfiddle.net/xvt359ju/1/
Nevermind, 2 other answers was faster than me.
Browsers have their own basic CSS styling of elements, the checkbox too have it. When you inspect the element you can see width and height to the checkbox applied by the browser, that will be overridden when your external stylesheets loads. And animating it as you have given transition to it.
I am trying to make a toggle sidebar which animates.
When I try to hide the sidebar with CSS3 Transition property by adding a hidebar class, it works perfectly. But It's a toggle, and when I show it again, there is no transition. The menu just snaps out.
#page-content.hidebar {
transition: margin 0.3s ease;
margin-left: 0;
}
Can anyone suggest how can I have the transition property when I toggle the sidebar to visibility as well?
I am attaching a fiddle as an example.
http://jsfiddle.net/dxYCm/1/
You needed to do several things:
since all rules have been applied using id selectors in css, your class selector had no effect, as in css specificity it had low points to override previous rules specified under id. So you need to add !important. http://htmldog.com/guides/css/intermediate/specificity/ Learn more there...
You needed to put white-space:nowrap; as text/content of first div would curl up as div would get small.
Check it Out>>>
http://jsfiddle.net/techsin/dxYCm/5/
You don't need a hide class at all, jQuery has awesome built in features that do the same thing like .toggle() and .slideToggle!
Here's an example of .toggle
$("a#menu-trigger").click(function () {
$("#page-sidebar").toggle("fast");
$("#page-content").toggleClass("hidebar");
});
Also, you want to apply the transition to #page-content, not #page-content.hidebar so it transitions both expanding and contracting
If you do still want to do it with using a .hide class not changing the jQuery or the HTML, you can do it this way, by toggling the width and height
Relevant CSS for that:
.hide {height:0px; width:0px; color:transparent;}
#page-sidebar {width: 230px; float:left; transition: all 0.3s ease;}
Example:
<div id="one">
(content)
</div>
<div id="two">
<ul>............</ul>
</div>
I want to create an effect that appears that #two is comming down from #one, I tried using transitions so when I :hover over #one so #two would appear to be coming down from #one but the content stayed there while only the background changed in size, I want the whole list to appear to be coming down from #one like in this website: http://merryweddings.com/
If you know the size of the div that will pop up, you can do a simple transition on the 'height' property, like this:
http://jsfiddle.net/BeDQr/
You also could use the transition on the 'max-height' property and set it to a very large value.
#two {
max-height: 0;
overflow: hidden;
transition-property: max-height;
transition: all 1s ease-in-out;
}
.wrapper:hover #two {
max-height: 500px;
}
But in this case, the end of the animation might be a bit abrupt.
You might want to use JQuery for this instead of pure CSS.
Check out this example : http://labs.abeautifulsite.net/jquery-dropdown/
This link also shows a lot of possible JQuery solutions.
See this link here: How can I transition height: 0; to height: auto; using CSS?
Also see the link in the first answer of the above link which is here: Can you use CSS3 to transition from height:0 to the variable height of content?
This, unfortunately is the only solution you have for a pure CSS method. The second link shows a sort of workaround or a hack. The first gives some further details.
I have a div in which there is an a tag.
I gave opacity:0.5 to the div then the text inside opacity is also 0.5
I don't want to use background image, then how can I have a text with opacity:1 inside my div with opacity:0.5 ??
Set the background color of the parent using rgba (includes alpha transparency). Example:
.Container {
background-color:rgb(0,0,0); /* fallback for IE 8 and below */
background-color:rgba(0,0,0,0.5);
}
.Text {
color:rgb(255,255,255);
}
This sets the opacity of the background of the container when using colors, however it does not set the opacity of the children. If you need to do that, set the opacity of the children to whatever you'd like with another class:
.OtherChildItem {
opacity:0.5;
filter:alpha(opacity=50); /* IE 8 and below */
}
If you want to use a background-image, just set the opacity on the image itself (use a PNG).
You can't. The real child opacity can't be greater than its parent's opacity in the HTML rendering model.
From the documentation (emphasis mine) :
Opacity can be thought of as a postprocessing operation. Conceptually,
after the element (including its descendants) is rendered into an RGBA
offscreen image, the opacity setting specifies how to blend the
offscreen rendering into the current composite rendering.
You must put your child div outside the parent div. This is usually achieved using a different kind of positioning than the static one.
Use a totally different <div> for the text.
<div id="parentDiv">
<div id="mainDiv">
</div>
<div id="childDiv">
Hello
</div>
</div>
CSS
#parentDiv
{
position:relative;
}
#childDiv
{
position:absolute;
top:45px;
left:45px;
opacity:1;
}
#mainDiv
{
width:100px;
height:100px;
opacity:0.5;
}
Check it out : http://jsfiddle.net/AliBassam/aH9HC/ I added background colors so you can notice the result.
Since I'm forcing you to use absolute, I don't want you to have a problem with positioning the text, so make some mathematical calculations to get the best position:
top = ( Height of Div Opacity(0.5) - Height of Div Opacity(1) ) / 2
left = ( Width of Div Opacity(0.5) - Width of Div Opacity(1) ) / 2
The a tag takes opacity from parent div. You can use the rgba CSS property on div rgba(0, 0, 0, 0.5) and again on a tag rgba(255, 0, 0, 1.0).
Like the answer above states, you'd need a separate div for the text, absolutely positioned to fit over the opaque div. You might want to set the z-index to something high as well.
Warning: this solution will work only if you want outer element to be completely transparent.
Instead of opacity: 0 and opacity: 1 use visibility: hidden and visibility: visible
Worked in my case (may not work in yours but it's worth the shot) :)
I would like to have a background image, from a URL (So I can't edit its opacity manually)
with an low opacity and write content on top of it:
but the content should be with the normal 1 opacity
http://jsfiddle.net/ca11111/BAJN5/
edit: http://jsfiddle.net/ca11111/BAJN5/1/ slightly better
The opacity property is inherited. If you set opacity < 1 for an element, all its child elements will also have that < 1 value for opacity and you cannot change this.
The easiest way to achieve what you want is to use multiple backgrounds and have a semi-transparent background on top of your image. Of course, this raises some browser compatibility problems (see multiple backgrounds support and gradient support).
Example here: http://dabblet.com/gist/2818293 (should work in IE10, Opera 11+ and every non-dinosaur version of other desktop browsers)
There is also the option of not setting the background on the parent, but on a child with no children of its own (or on a pseudo-element), that is absolutely positioned and has a z-index < than that of the parent (which has an rgba background).
You mean something like this? http://jsfiddle.net/BAJN5/2/
Try setting it this way:
<div style="position:relative"> <!-- wrapper div (relative) -->
<div style="background:url(an-image-url) no-repeat center center; opacity:0.5; height:220px"></div> <!-- half opacity background -->
<span style="opacity:1; position:absolute; z-index:1"><!-- full opacity (absolute) -->
some text
......
</span>
</div>
see i have made through pseudo-elements :before & :after
HTML
<div class="addFav">
<div>asdfasfasfafs</div>
</div>
CSS
.addFav:before {
background:url(http://lorempixel.com/400/200/sports) no-repeat;
border:1px solid red;
height:200px;
width:400px;
color:#000;
padding:15px;
position:absolute;
content:"";
opacity:0.1;
}
.addFav {
height:200px;
width:400px;
color:red;
padding:15px;
position:relative
}
see the demo:- http://jsfiddle.net/8LFLd/66/
Updated Demo here http://jsfiddle.net/8LFLd/68/
This page shows a technique for emulating background image opacity using a psuedo element
Hey i think you should want this
Define your Main Div rgba properties
div {
background: rgba(200, 54, 54, 0.5);
}
more info http://css-tricks.com/rgba-browser-support/