I have some URLs to which I want to add the same image. I've got the following code:
a.linkArrow
{
background: transparent url('../images/abc.png') no-repeat center right;
padding-right: 60px;
}
And a link example:
<a class="inkArrow" href="#abc">Abc?</a>
The problem is, the image appears on the left of the text of the link. I want that the image appears always on the right side of the text AND that the distance from the start position of the link text to the start position of the image is always the same. So when I have multiple links in a row that the images of the links align. The image should be clickable and lead to the same url as the link (I'm not sure if it is possible to enclose it in the same tag for this approach.
Any ideas?
it is "right center" not "center right" see example: http://jsfiddle.net/bingjie2680/ZtLCA/
it also works when you want them to be same distance between text link and image see example: http://jsfiddle.net/bingjie2680/ZtLCA/1/
You could do this with jQuery too.
Some Link
$('.imageLink').each(function(){
$(this).append(' <img src="YOUR IMAGE HERE">');
});
http://jsfiddle.net/jasongennaro/6Nc3n/
EDIT:
I realized that the OP also said the distance from the start position of the link text to the start position of the image is always the same
So I modified the code
var finalWidth = 0;
var getWidth;
$('.imageLink').each(function(){
getWidth = $(this).width();
if(getWidth > finalWidth){
finalWidth = getWidth;
}
});
finalWidth +=15;
$('.imageLink').each(function(){
var getText = $(this).text();
$(this).html('<span style="display:inline-block; width:' + finalWidth + 'px;">' + getText + '</span>');
$(this).append(' <img src="http://icdn.pro/images/en/l/e/left-arrow-icone-3702-32.png">');
$(this).css('textDecoration','none');
});
What this does is get the width of each link text. If checks if that width is the longest, if not ignores, but if yes, then makes it the finalWidth. This finalWidth then added to a new span created around the text and styled with inlineblock.
Updated Fiddle
http://jsfiddle.net/jasongennaro/6Nc3n/2/
a.linkArrow
{
background: transparent url('../images/abc.png') no-repeat center right;
display:block;
width: /* image width PLUS the distance you want from the text px */;
height: /* image height px */;
}
What you need to do is give them a fixed width and a display: block.
Your css would look like this:
a.linkArrow
{
background: transparent url('../images/abc.png') no-repeat right center;
display: block;
width: 100px;
}
Related
Longshot... I don't think this is possible but I've been shocked before!
I anchor tags, all of which have background images, all 300px wide but their heights all vary. Is there anyway to set these without actually having to type out the height? Sort of setting it to the bg url's dimensions?
Thanks!
I don't think people understand - My fault for rushing the question.
Here's code as an example:
#ex-1 {width: 300px; height: 410px; background: url('/image-1.jpg');}
#ex-2 {width: 300px; height: 420px; background: url('/image-2.jpg');}
#ex-3 {width: 300px; height: 430px; background: url('/image-3.jpg');}
#ex-4 {width: 300px; height: 440px; background: url('/image-3.jpg');}
I'd like to NOT set the height, and it set automatically using CSS only. I don't want to use image tags.
I wasn't sure if this was possible, I assume not.
Thanks
A simple way of doing this is to add an image like this and then make it hidden i used visibility:hidden http://jsfiddle.net/gztpsfkw/1/
i just saw that you don't want to use <img> tags but as for here the image is being hidden and it takes up the space.
<img src="http://placekitten.com/300/301" />aa
And apply the css
a{
display:block;
background-image:url('http://placekitten.com/300/301');
width:100px;
height:auto;
}
img{
visibility:hidden;
}
We can use a visibility: hidden way:
<img src="http://lorempixel.com/100/200/" />
CSS
a {background: url("http://lorempixel.com/100/200/") center center no-repeat; width: 100px;}
a img {visibility: hidden;}
Fiddle: http://jsfiddle.net/praveenscience/vhjxfgtw/
JavaScript Solution
Procedure
To set the height, dynamically, you need to use JavaScript. So, you can get the computed value by adding a <img /> tag and computing the value by setting the src. The pseudo code would have been like this:
Get the computed value of background-image.
Attach it to a new <img /> element in the DOM.
Get the height of the new <img /> element.
Set the height of the fake background <div>.
JavaScript
$(document).ready(function () {
bg = $(".bg").css("background-image");
$("body").append('<img id="faux" src="' + bg.substring(4, bg.length-1) + '" />');
height = $("#faux").outerHeight();
$("#faux").remove();
$(".bg").height(height);
});
Fiddle: http://jsfiddle.net/praveenscience/rcL3xj0x/
If you don't want to use inline CSS, you can use this:
$("style").append('.bg {height: ' + height + 'px}');
If you're looking for a way to make the background images fill all the space available then use background-size: cover
I think you're looking for something like this:
function setBackgroundImage(element, src) {
var img = new Image();
img.onload = function() {
element.style.height = img.height+'px';
element.style.backgroundImage = 'url('+img.src+')';
}
img.src = src;
}
Or, if you need to scale the images for the width:
function setImage(element, src) {
var img = new Image();
img.onload = function() {
var sizeRatio = element.offsetWidth / img.width;
element.style.height = (sizeRatio * img.height)+'px';
element.style.backgroundImage = 'url('+img.src+')';
element.style.backgroundSize = 'cover';
}
img.src = src;
}
Side Note: The <a> tag is not a block level element. In order for the <a> to have a height and a width you need to make it a block level element to show the background image.
You would use: display: block
Now for your question... In order to get the background image, with out having to manual type it in you can use a little jQUery to make your life a lot easier. I have modified your CSS and HTML a little bit to accomodate the jQuery.
CodePen Example
#links { overflow: hidden; }
#links a { display: block; float: left; width: 300px; height: 200px;
/* generic height set in case there is no background image */ }
#ex-1 { background: url('http://www.google.com/images/srpr/logo11w.png');}
#ex-2 { background: url('http://www.bing.com/s/a/hpc12.png');}
#ex-3 { background: url('http://www.google.com/images/srpr/logo11w.png');}
#ex-4 { background: url('http://www.bing.com/s/a/hpc12.png');}
<div id="links">
</div>
Here is the jquery. It will loop through all your images and set the height according to your background image
$(document).ready(function(){
$('#links a').each(function(){
var temp = $(this).css('background-image');
temp = temp.replace('url("', '').replace('")', '');
var newImg = new Image();
newImg.src = temp;
imageHeight = newImg.height;
imageWidth = newImg.width;
$(this).css('height', imageHeight);
});
});
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()
});
});
});
})
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');
});
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
I want to use pretty 3d button images on my website. However, currently the way this works is the text is part of the image.
So, when I want to change the text (or make a new button) it's a 10 minute editing chore instead of a 20 second text change.
I've seen a few websites that have a blank button with text on it.
The real trick is making the entire image clickable. I've been able to make the link inside an image visible but that's a poor UI. Users will expect to click the button anywhere and failure to behave that way will frustrate them.
It seems like they're wrapping a .DIV tag with an image background around a Hyperlink.
<Div (class w/ image>
<a> text
</a>
EXAMPLE:
https://www.box.net/signup/g
Anyone have any insight or explanation of how this works?'
CODE SAMPLE
<a href="#" class="button" style="position: relative;left:-5px;"
onmousedown="return false;"
onclick="document.forms['register_form'].submit(); return false;">
<span>
My text
</span>
</a>
Make the button a background image:
<style>
div.button a {
display: block;
width: /* image width */;
line-height: /* image height */;
text-align: center;
background: url(/* image uri */) no-repeat;
}
</style>
Would setting your anchor to display:block and giving it a height/width equal to your div/background image help you?
perhaps something like
a {
width: something ;
height: something;
display: block;
background: url('hi.png');
}
also,
input { background: url('hi.png'); }
is an alternative
Your example is just placing CSS styles on the a tag...
From there:
The tag:
<a onclick="document.forms['register_form'].submit(); return false;"
onmousedown="return false;" style="position: relative; left: -5px;"
class="button" href="#">
<span>Continue</span>
</a>
Note that they are using JS for some reason, and not using the href, I don't like that.
Then, the button class:
a.button
{
background:transparent url(../img/greenbutton2.gif) no-repeat scroll left top;
font-size:16px;
height:42px;
line-height:42px;
width:155px;
}
This is just how that site you linked to did it.
I found this rather impressing. Using GWT to style hyperlinks.