I've come so far (but you wouldn't know it). Struggling through my art website.
This page
http://rollinleonard.com/elements/
This page has a little overlay div that hovers over these img. The first img is in the background of a div. I want that img to also have this blue highlighter effect.
How do I get it so that its a normal img and then on another layer over that img there is my nav (text with white background)?
Thanks!
I thick the easyest way to do this is to mak your first div like other image a > img and put your nav in absolute position.
HTML :
<nav>...</nav>
<a href="#">
<img src="home.gif" alt="Home BG" />
</a>
CSS :
nav {
position: absolute;
}
# rollin , there no need to use js check this
http://jsfiddle.net/sandeep/f2662/
wrap your navwrap-div in another div of the same dimension with the first image as a background
set the background of your navwrap div to transparent
allocate z-index values: 0 for the new wrapper, 1 for original wrapper, 2 for the nav-element
adjust the selector of your mouseenter-handler to 'img, #navbg'. you may also have to guard the dynamic setting of your click handler on the overlay div (pointless if vent target is the new div).
your code looks like this:
<div id="navbg" style="width: 300px; height: 300px; z-index: 0; background-image: url('home.gif');">
<div style="z-index: 1; background-color: transparent;" id="navwrap">
<nav style="z-index: 2;" >
...
</nav>
</div>
</div>
(use of inline definitions for styles for demonstration purposes only)
best regards, carsten
add this in your js file inside the $(window).load() event
$('#navwrap').bind('mouseenter', function () {
var $this = $(this);
if ($this.not('.over')) {
$this.addClass('over');
$overlay.css({
width : $this.css('width'),
height : $this.css('height'),
top : $this.offset().top + 'px',
left : $this.offset().left + 'px'
}).show();
}
}).bind('mouseout', function () {
$(this).removeClass('over');
});
Related
I have a div that is supposed to follow the pointer, but it stays very far from the pointer right now, though it follows it.
I've tried some different codes, both written by me, suggested on here, or found online, but nothing helped.
Right now the one that works best is this..
var currentMousePos = { x: -1, y: -1 };
$(document).mousemove(function(event) {
currentMousePos.x = event.pageX;
currentMousePos.y = event.pageY;
$(".hidden-img").css('top', currentMousePos.y);
$(".hidden-img").css('left', currentMousePos.x);
});
I've also tried this http://jsfiddle.net/BfLAh/1/ but it doesn't work as in the fiddle
It follows the pointer but it's very far from the top left of the pointer.
I have managed to go with this code and make it work, at least the positioning
$(document).ready(function() {
$(".list-item").mousemove(function(e) {
var offset = $(".list-item").offset();
$("#hoverDisplay").animate({
left: e.pageX - offset.left,
top: e.pageY - offset.top
}, 1);
});
});
Now the only problem that stays is, I have 20+ "li" items that have to get hovered and show their own hidden div.
Here is the code of just 2 "li" elements.
<li class="list-item" style="background-color: #03C0A4;">
<div class="small-title" style="color:#E07354">nevaly</div>
<a href="a-project-called/nevaly/index.html" style="color:#E07354" class="project-title" onmouseenter=toggleHiddenDisplay("hoverDisplay") onmouseout=toggleHiddenDisplay("hoverDisplay")>Connecting brands and influencers - no limits.</a>
<div class="hidden-img" id="hoverDisplay" style="display:none;">
<img src='a-project-called/nevaly/thumb.gif'>
</div>
</li>
<li class="list-item" style="background-color: #f8e975;">
<div class="small-title" style="color:#e32e1d">Kulturhavn</div>
<a href="a-project-called/kulturhavn/index.html" style="color:#e32e1d" class="project-title" onmouseenter=toggleHiddenDisplay("hoverDisplay") onmouseout=toggleHiddenDisplay("hoverDisplay")>Ahoy! From the cultural harbour of Copenaghen.</a>
<div class="hidden-img" id="hoverDisplay" style="display:none;">
<img src='a-project-called/kulturhavn/thumb.gif'>
</div>
</li>
Now when I hover just the first hidden div content gets shown.
How can I take the hoverDisplay of the hovered "li" element using the JS that I wrote (thanks to you also guys!)?!
Thanks
If you want to match the position of image's top left corner and mouse pointer, then jsut set negative margin-top, and margin-left for you image like below:
$(document).ready(function () {
$("body").mousemove(function (e) {
$("#image").animate({
left: e.pageX,
top: e.pageY
}, 1);
});
});
body {
overflow:hidden;
position:absolute;
height:100%;
width:100%;
background:#efefef;
}
#image {
height:50px;
width:50px;
margin-top:-10px;
margin-left:-10px;
position:absolute;
}
<div id="container">
<img id="image"></img>
</div>
Here is its link to check it online in fiddle.
UPDATE
If your only reason for detecting mouse position is to show and hide the content of list items, then you don't need to detect mouse position manually, you can do it using jQuery with ease. First add this selector to your CSS file:
li.list-item div {
display: none;
}
And then you can show child of each .small-item using mouseover and children like below:
$(document).ready(function() {
$('.list-item').mouseover(function() {
$(this).children('.small-title').show();
}).mouseout(function() {
$(this).children('.small-title').hide();
});
})
We can get rid of this because as you mentioned in your comment you just want to fix the child state.
.mouseout(function() {
$(this).children('.small-title').hide();
})
I have a scenario in which I have a team page with pictures and some blurb. Under each picture I have social media links much like the following:
These are images that sit within a horizontal list underneath each item using the below base markup.
<ul>
<li>
<a><img src=""/></a>
</li>
<li>
<a><img src=""/></a>
</li>
</ul>
At the moment these are images but I would very much like if when hovered the grey inards of these images turned blue.
I was thinking just have a span with a background image like this:
<a><span class="linkedin"></span></a>
.linkedin{
height:28px;
width:auto;
background-image:url(link/to/the/linkedin/picture)
}
.linkedin:hover{
height:28px;
width:auto;
background-image:url(link/to/the/linkedin/picture-blue-version)
}
However, when I attempted this the space was empty instead of taking the size of the image.
If I enter as content I get a small part of the background image, furthermore giving the class an absolute position takes it out of document flos
Is this the ideal approach?
The problem is if you use a <span> element you need to set it to display: inline-block and you need to set a width for the image to show up. Then it works, here is a demo:
.linkedin {
display: inline-block;
width: 140px;
height:100px;
background-image:url(http://ipsumimage.appspot.com/140x100,ff7700)
}
.linkedin:hover {
background-image:url(http://ipsumimage.appspot.com/140x100,0000FF)
}
<span class="linkedin"></span>
As you see on the first :hover it flickers. Cause it will not load the image bevore you :hover the first time. This is why you should consider another solution. Like mentioned in some comments you could use http://fontawesome.io/icons/ and then just change the color.
To prevent flickering you could do the same with using <img> tags then the source will be loaded and ready to be shown on :hover. But it works best with also setting positions, demo like so:
a.special {
position: relative;
}
a.special img {
visibility: hidden;
position: absolute;
top: 0;
left: 0;
}
a.special img:first-child {
visibility: visible;
}
a.special:hover img:first-child {
visibility: hidden;
}
a.special:hover img:last-child {
visibility: visible;
}
<a class="special" href="#">
<img src="http://ipsumimage.appspot.com/140x100,ff7700">
<img src="http://ipsumimage.appspot.com/140x100,0000FF">
</a>
Best approach for this is to use SVG's and change the fill of the SVG on hover.
Your approach should work however, it might be that you've not got the correct size image? try 'background-size: cover;' Or that the element has no width. Try setting a width on the span too. (don't forget to give it 'display: inline-block;' too.
Ed: checkout https://css-tricks.com/lodge/svg/
Font-Awesome is a great idea for what you're trying to achieve. Takes less data to load the page too if you can get away with using text in place of images.
By the way, when using the :hover property there is no need to redefine the width and height of the image... Just redefine the changes you'd like to make.
I'm working with four images every image has a link and when clicked it take us to the another page. Now my problem is that I want to make the black overlay on hovering the image and when the image is clicked and take us to another page it remain active with that overlay.
i have four images on front page and same images on inside pages.
Here is the HTML:
<ul class="thumbnails">
<li class="span3">
<a href="#">
<div id="ImageHover">
<img src="http://209.236.116.120/~sciallin/it/backend/wp-content/uploads/2014/07/model23.png" alt="" />
</div>
</a>
</li>
</ul>
Here is the CSS:
<style>
#ImageHover img:hover
{
background-color:rgba(0,0,0,0.6);
}
</style>
its working for me but the overlay is not coming over the image.and i also want that the image remain active with overlay when we goes inside the page which is linked with this image.
the first problem is solved and when inside the page to make the image active with overlay i have done this ->
Here is the HTML:
<div id="ImageHover" class="active">
<img alt="" src="http://209.236.116.120/~sciallin/it/backend/wp-content/uploads/2014/07/model23.png">
</div>
Here is the CSS:
#ImageHover.active {
background : rgba( 0, 0, 0, 0.5 );
bottom : 0;
content : ' ';
left : 0;
position : absolute;
right : 0;
top : 0;
}
but it make the image dissappear.and when i block the postion it comes back but back side of image. what should i do now.
This (or something similar) should give you the effect you are after, at least in modern browsers:
#imageHover {
position : relative;
}
#imageHover:hover:after {
background : rgba( 0, 0, 0, 0.6 );
bottom : 0;
content : ' ';
left : 0;
position : absolute;
right : 0;
top : 0;
}
To get the effect to carry over to indicate that the user is on a given page you'll need a class on the relevant #imageHover on the page. You can then add #imageHover.active as an extra rule after the hover style.
As a side note, you'll probably want to use a class instead of an ID, or you'll need to add a CSS rule for each and every imageHover element.
EDIT : the selector for the .active class will also need the :after pseudo-element. The CSS rule will be something like:
#imageHover:hover:after,
#imageHover.active:after {
/* ...css... */
}
Apologies for not making that clear.
Further reading on pseudo elements (those things rock!)
Replace your current CSS with the following and apply the .active class to your box.
#ImageHover {
position: relative;
}
#ImageHover:hover:after,
#ImageHover.active:after {
background : rgba( 0, 0, 0, 0.5 );
position: absolute;
top: 0;
left: 28px;
bottom: 15px;
right: 28px;
content: '';
}
I modified the position so that it outlines your box perfectly, but you can change it back if you wish.
To get the image to be allready active when the next page is loaded, you should load the page with a hash attribute.
Let's say you have 4 pictures, each with a different id, on a page called pictures.html :
<img id="image1" class="imageOverlay" src="" />
<img id="image2" class="imageOverlay" src="" />
<img id="image3" class="imageOverlay" src="" />
<img id="image4" class="imageOverlay" src="" />
To load the page with the 3rd image active, you call pictures.html#image3
Then, in your CSS:
.imageOverlay:active {
/* CSS for image overlay */
}
That CSS will apply to the picture having an id that corresponds to the hash in the URL.
The problem you could face is that the browser will scroll to that image on page load. Which is something you may not want.
I don't think there's a way in CSS to avoid that behaviour.
Is it possible with CSS/HTML to resize some box to match exactly it's background image size? Without using javascript.
For instance let's say I have a simplest div:
<div class="image">TEST</div>
.image {
background-image: url(http://placehold.it/350x150);
width: 350px;
height: 150px;
}
And I would like to resize it to those 350x150 dimensions without hardcoding those values. Also I cannot put any content inside this div.
http://jsfiddle.net/5Dane/
EDIT: I see a lot of answers I already was aware of, thank you for them, but that's not the solution here unfortunately. Below I'm explaining why I need such functionality.
What I'm trying to do is a form with steps (buttons previous and next). In session I hold all the values the user has input but there are some buttons which will add more functionality for the user (like multiple dynamically added rows for data). I'm doing it with jQuery of course, but I want the form to be able to work when there is no java script enabled.
Now to the point - I was trying to find out how to tell the difference which button the user has clicked. The case is all my submit buttons need to be images and the simplest solution <input type="image"/> doesn't send info about the button clicked with POST data. That's why I came to this solution:
<input class="submit_img" type="submit" style="background-image:url(http://placehold.it/108x23); width:108px; height: 23px;" value=" " name="some" />
/* Submit button with image */
input.submit_img {
font-size: 1em;
border-radius: 0px;
box-shadow: rgba(0, 0, 0, 0.15) 0 1px 1px;
border: solid 0px #000000;
cursor: pointer;
}
http://jsfiddle.net/XRvqV/
This way my form will submit all the data AND I will know which button the user clicked. Also the button looks fine, like it should look. I was wondering though if it was possible to make it a little more portable - my buttons all have different widths for different functions. Can someone suggest another approach here?
No, you can't. CSS is not aware of the the image size. You can do it easily with JQuery.
JQuery exmaple
$(function(){
var bg = $("div.image").css('background-image');
bg = bg.replace('url(','').replace(')','');
var newImg = new Image();
newImg.src = bg;
$("div.image").css("width",newImg.width);
$("div.image").css("height",newImg.height);
});
This is a hack and doesn't use background-image (uses an img tag instead), but is the only way I can think of without using JS.
HTML
<div class="container">
<div class="image">
<img src="http://www.pandafix.com/pandafix/images/untitled_1.jpg"/>
</div>
<div class="content">
some text
<br/>
some more text
<br/><br/><br/><br/>
text text text
</div>
</div>
CSS
.container {
position: relative;
}
.content {
position: absolute;
height: 100%;
width: 100%;
top: 0;
left: 0;
color: red;
}
Basically, you allow an img tag to determine the height and width of a container. Then, overlay whatever content you want on top of the image (I'm assuming you want to put something on top).
jsFiddle
i would suggest you a alternative way to solve your problem. if you use bootstrap you can involve a div to make resizable image.
<div class="img-responsive">
<img src="test.jpg" width='xxx' height='yyy' alt='test'>
</div>
You can't do that using just HTML. But you can do this using HTML!
You should try this:
background-size: values;
This way, you will resize the background-image to the size of the container!
You can't do it directly.
The only solution it would be fetching the BG of the DIV element and attach new DOM img element to the DOM node, afterwards you could use the info of the image to add the proper with and height..
if you are willing to use jquery you can do this.
$(function(){
$('.image').each(function(index,element){
var _t = $(this);
_t.data("LinkedImg","LinkedImage"+index);
$('body').append(
$('<img />',{
id:"LinkedImage"+index,
src:_t.css('background-image')
}).hide());
});
$('document').on('load',function(){
$('.image').each(function(index,element){
var _t = $(this);
var _tmp_img = $('#'+ _t.data("LinkedImg"));
_t.css({
width:_tmp_img.width(),
height: _tmp_img.height()
});
});
});
})
How do I convert and img tag to css so I don't have to have a million img tags. Or whats the best way todo images with css
<img src="hg-rx.gif" name="foo" width="18" height="18">
I tried background:url in css and it needs text for it to display properly, id hilight and the image would disappear
.hg-text-rx {
background:url(hg-rx.gif);
background-repeat: no-repeat;
position: relative;
}
You can do this with just css by using a div or other block element of fixed width and height and make the image the background of that. But to do this, you must still put the div (for the image) in the HTML so you aren't really cleaning anything up, unless you are just trying to make the site easier to skin completely using CSS. However, this does make rollover states a breeze.
div#hg-rx {
display:block;
width:18px;
height:18px;
background: url(hg-rx.gif) 0 0 no-repeat transparent;
}
<div id="hg-rx"></div>
If you are doing borders, rounded corners or buttons you might want to look into sprites.
http://css-tricks.com/css-sprites/
you can add a image as background in css, but you must set the width and height of image to be visible.
css
.hg-text-rx {background:url("http://dummyimage.com/200x200/000/545");width:200px;height:200px};
Demo: http://jsfiddle.net/2XX8A/
Actually, while this cannot be done strictly in CSS, if you have IMG tags and want to convert them to divs, you can do so using jQuery (a javascript wrapper) on the fly pretty easily.
LIVE DEMO http://jsfiddle.net/Epgvc/4/
HTML
<img src='http://dummyimage.com/200x200/000/fff&text=image1' />
<img src='http://dummyimage.com/100x100/f00/ff0&text=image2' />
<img src='http://dummyimage.com/250x50/0ff/fff&text=image3' />
JS
$('img').each(function(){
var html="<div style='border:1px solid #ff0;width:" + $(this).width() + "px;height:" + $(this).height() + "px;background:url(" + $(this).attr('src')+ ");color:#fff;'>This is a Div!</div>"
$(html).insertBefore($(this));
$(this).remove(); //Comment out this line if you want to leave the original image
});
If you intent on having the image as a background to a text field you could alway use text-indent
.hg-text-rx {
background:url(hg-rx.gif);
background-repeat: no-repeat;
position: relative;
**text-indent:-10000px;**
}
<p>this text wont show, but the image will</p>
However there is conflicting arguments about this technique from a seo point of view