I have been playing with pseudo selectors and trying to figure stuff out.
This is the general look of the element I am trying to work on:
<div class=simpleGallery>
<a href="...">
<img data-attachment-id="some_number" ........>
</a>
</div>
I am trying to get text to show on a picture with a specific attribute (data-attachment-id for example). I managed to get it by searching for a href that ends in a unique way. Like this...
.simpleGallery a[href$="GP120094-1.jpg"]:before{
content:'Hokus Pokus';
position:absolute;
bottom:25%;
opacity:0;
width:100%;
text-align:center;
color:#fff;
box-sizing:border-box;
-moz-box-sizing:border-box;
-moz-transition: all 1.2s ease;
-webkit-transition: all 1.2s ease;
transition: all 1.2s ease;
}
And then I get the text to show with:
.simpleGallery a:hover:before{
opacity:1;
z-index:1;
-moz-transform: scale(1.1);
-webkit-transform: scale(1.1);
transform: scale(1.1);
}
It all works, but I was wondering why it won't work with something like this:
.simpleGallery a:before img[data-attachment-id="some_number"]
How would it be done by the image data-attachment-id instead of href in the <a> tag?
And why?
Is it because the :before can only go after the last element I am looking for?
.simpleGallery a:before img[data-attachment-id="some_number"]
Is saying:
1.) find an element with the class "simpleGallery"
2.) find a descendant anchor tag
3.) target that element's :before pseudo element
4.) find a descendant img tag of that pseudo element who's data-attachment-id is equal to "some_number"
The problem here is that pseudo elements are not real elements on the dom (hence pseudo) so they don't have children, siblings, decedents, etc. so your selector is invalid.
How would it be done by the image data-attachment-id instead of href
in the tag?
Exactly how you have it: img[data-attachment-id="some_number"]
Related
I have a div that I want to be visible as :target using css. This is working fine so far. My problem is: I want it to be faded in and out.
My code:
<div id="stuff">
Content
</div>
#stuff {
opacity:0;
transition: opacity .5s ease-in-out;
}
#stuff:target {
opacity:1;
}
So let's say on the following url it should fade in (what it does):
example.com/example.htm#stuff
But when I change the url to the following (or anything else), it just becomes invisible without a transition:
example.com/example.htm#
Note that I want to change the url by clicking on a link, not by modifing it via JavaScript.
My question is:
Is it possible to have a transition when changing the target for the previous target without the use of JavaScript? How would you accomplish it?
I do not want to use any JavaScript at all, if possible.
I just changed the value of the opacity property and it's working. As far as I know, opacity only accepts a decimal value between 0 and 1 (apart from inherit, initial and unset).
#stuff {
opacity:0;
transition: opacity .5s ease-in-out;
}
#stuff:target {
opacity:1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="stuff">
Content
</div>
Add hash
Remove hash
i have some child based hovering effect on my project.
My codes are
<ul>
<li>
<a href="">
<i class="fa fa-facebook"></i>
<i class="fa fa-facebook"></i>
</a>
</li>
<ul>
so my css are
a i:first-child{
position: absolute;
top:0px;
}
a i:last-child{
position: absolute;
top:30px;
visibility:hidden;
}
when hovering on first child i want to take it top:-30px and visibility:hidden and last child to vice-versa
I have tried
ul.socials.jump a i:first-child:hover ul.socials.jump a i:last-child{
position: absolute;
visibility: visible;
top: 0%;
transition:all .4s ease;
}
but not working :(
If you want to modify both elements on hover, you will need two separate :hover rules.
One for the first child:
ul.socials.jump a i:first-child:hover {
visibility: hidden;
top: -30px;
}
And one for the last child, but to target the last child on first child hover, you don't need to repeat the first portion of your selector — just use a sibling combinator:
ul.socials.jump a i:first-child:hover + i:last-child {
visibility: visible;
top: 0%;
}
You don't need to redeclare position: absolute, and your transition ought to be declared on a i:first-child, a i:last-child and not on the hover state unless you expect the transition to only apply when the elements leave hover.
Actually, the hover is better served on the anchor tag.
a:hover i:first-child {
position:relative;
top:-30px;
}
a:hover i:last-child {
visibility:visible;
}
It's much more legible, and the hover is more likely to stay active even with the shifting position of the children.
It would also help if you added a class to the link and set its display to inline-block, such as
a.hoverchild { display:inline-block }
That causes the 'blank' space inside the link to be hoverable as well, so that when the children move, the cursor (being inside the link's block, still) is still keeping the :hover active.
If you're trying to do what I think you're trying to do, you might actually be better off using float:left like this:
a.hoverchild {
display:inline-block;
}
a.hoverchild:hover i {
position:relative;
float:left;
clear:left;
visibility:visible;
}
Here's a fiddle to demonstrate.
Hello i think you can use sibling option in css using "~" sign to solve this problem.
Check out following JSfiddle to see your requirement
I'm trying to make a Div change its background color on hover but it's not working.
----------HTML-----------
<div class="home-circles" style="background-color:#92cd00">
<span style="color:#1C263C"><b>About Me</b></span>
</div>
<div class="home-circles" style="background-color:#FFCF79">
<span style="color:#663333"><b>Music</b></span>
</div>
<div class="home-circles" style="background-color:#E5E4D7">
<span style="color:#097054"><b>Hi Morgane</b></span>
</div>
----------CSS------------
.home-circles {
width:250px;
height:250px;
border-radius: 200px;
margin:25px;
float:left;
text-align:center;
vertical-align:middle;
line-height:250px;
font-size:50px;
font-family:rochester;
-webkit-border-radius: 12px;
-moz-border-radius: 12px;
-moz-transition: all .4s ease-in-out;
-o-transition: all .4s ease-in-out;
-webkit-transition: all .4s ease-in-out;
-moz-transition: all .3s ease-in-out;
}
.home-circles:hover
{
cursor:pointer;
background-color:#fff;
}
The problem seems to be the style attribute in the div. The background color specified in the Div seem to supersede the one specified in .home-circles:hover. Is there a way to get around it without changing the CSS for .home-circles?
Since you're using inline style for all your divs with class home-circles such as background-color:#FFCF79, background-color:#E5E4D7.... in your HTML which will override the style you've defined in your external CSS background-color:#fff.
You can use !important property here:
.home-circles:hover
{
cursor:pointer;
background-color:#fff !important;
}
Fiddle Demo
When an !important rule is used on a style declaration, this
declaration overrides any other declaration made in the CSS, wherever
it is in the declaration list. Although, !important has nothing to do
with specificity. Using !important is bad practice because it makes
debugging hard since you break the natural cascading in your
stylesheets
So basically, you can use !important but it's discouraged. I'd suggest you to move your inline styles to external CSS files instead.
just use !important in hover css
.home-circles:hover
{
cursor:pointer;
background-color:#fff !important;
}
DEMO
Note:inline styles must be overrided,since you are giving background-color in style u should use !important to override it.
When Using !important is The Right Choice
Using !important in your CSS usually means you're narcissistic &
selfish or lazy. Respect the devs to come...
You have a small mistake.
First of all you have to know hierarchy of css. In your div tag inline css background which is
first priority that's why your hover action not working. your default background color should come from css class then hover color will work properly
Hope it will help you
Use the !important property! This will override the inline CSS.
<html>
<head>
<style type="text/css">
.mydiv
{
width:100px;
height:100px;
background-color:red;
}
.mydiv:hover
{
background-color:blue;
}
</style>
</head>
<body>
<div class="mydiv">
</div>
</body>
</html>
I've got two divs, div 1, and underneath it is hidden div 2. When I hover over div 1, I want it to hide, and show div 2. Then, once I mouse off of the area (now div 2), div 1 is displayed again.
Here is the code:
<a href="javascript://" class="hoverable">
<div class="normal" style="background:#666;">Hover over me!</div>
<div class="hover" style="background:#888;">Now you see me!</div>
</a>
and here is the css:
<style>
.hoverable {
cursor:default;
color:#000;
text-decoration:none;
}
.hoverable .hover {
display:none;
}
.hoverable:hover .normal {
display:none;
}
.hoverable:hover .hover {
display:block;
}
</style>
My only problem with this is that is is very quick, cut and dry, and not very "fancy". I'd like to add something simple like a fade effect.
I've gotten this working, without the fade effect, here:
http://jsfiddle.net/pBDGW/
If anyone knows how to make those two divs transition with a fade-out, please let me know!
You can use CSS transition with opacity like this:
.hoverable {
cursor:default;
color:#000;
text-decoration:none;
position: relative;
display: block;
}
.hoverable .hover {
opacity:0;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.hoverable .hover,
.hoverable .normal{
transition: opacity .5s;
-o-transition: opacity .5s;
-ms-transition: opacity .5s;
-moz-transition: opacity .5s;
-webkit-transition: opacity .5s;
}
.hoverable:hover .normal {
opacity:0;
}
.hoverable:hover .hover {
opacity:1;
}
You can see the jsfiddle here: http://jsfiddle.net/pBDGW/12/
Some explanation:
The transition applied to both div are the main code that make them fade in & out. You can read more about it here: http://css3.bradshawenterprises.com/transitions/
Since you want the first div to fade out, and the second div to fade in, there will be a moment when both div have to be visible partially, hence position: absolute and some positioning on the second div (to make it overlap with the first div).
You are wrapping an anchor (<a>) around both div, which is actually not encouraged, so I have to give it display: block; . A better approach (HTML-wise) is to wrap both div inside another div (still use the same class hoverable), and use 2 different anchors inside each div.
EDIT: this approach http://jsfiddle.net/pBDGW/14/ works too. Here you only fade out the first div, while the second div is always visible but is hidden under the first div when not hovering. It is shorter css, but I don't recommend this approach though because I sometimes have issues with getting the first div to go on top on different browsers.
You can use jQuery, it has functions fadeIn and fadeOut and also its easy to hide() and show() on events mouseOver and mouseLeave.
You can see fiddle here.
$(document).ready(function(){$(".hover").hide();
$(".normal").mouseover(function(){
$(".normal").fadeOut(0);
$(".hover").fadeIn(1000);
});
$(".hover").mouseout(function(){
$(".normal").fadeIn(1000);
$(".hover").fadeOut(0);
});});
You can use transitions:
ADD THIS TO YOUR :HOVER
-o-transition:.5s;
-ms-transition:.5s;
-moz-transition:.5s;
-webkit-transition:.5s;
transition:.5s;
This adds the effect of fade IN/OUT
Addition: this will only work if you have property set for :hover, for example if you want to change the color, or background or what ever..
The following code shows the image in the div tag.
<div class='item'>
<img class='img' src="image1.png" alt="" />
</div>
I am using the following css to add effects to the html image code:
img{
width:50px;
height:50px;
opacity:0.4;
filter:alpha(opacity=40);
}
img:hover{
opacity:1.0;
filter:alpha(opacity=100);
}
I am using this to have opacity effects in css. With this code, the opacity effect works well when I hover over the image itself. But how do I make it so that the opacity effect on the image occurs when I hover over the div tag instead. I want to be able to hover over any part of the item div which encapsulated the image, to get the change opacity effect on the image. NB effect on just the image not the entire div. Can this be done in css? If so how?
Change this:
img:hover{
opacity:1.0;
filter:alpha(opacity=100);
}
to
.item:hover img{
opacity:1.0;
filter:alpha(opacity=100);
}
This will change the opacity of the img when the div is in hover state.
If you use jquery for this then I think this will be easy
$('.item').hover(function(){ $('.img').css('opacity','1.0');}, function(){ $('.img').css('opacity','0.4');});
I assume you know jquery. You can't use :hover pseudo class of CSS over elements that don't have href attribute according to Sitepoint.