Use CSS to put img attributes - html

Can I use CSS programmatic to put the attributes of my img tag?
<span><img class="img-dollar"></img></span>
<span><img class="img-royalty"></img></span>
I want to put src to get the image and put height and width to scale it down. How can I achieve?

The answer is No. You can't manipulate the html tags with the help of css. Use javascript for that.
CSS is only used for manipulate the style attributes.
To change the height and width property using css you can do something like this
.img-dollar
{
height:100px;
width: 100px
}

You can set the size of an image using css e.g.
img{
width: 200px;
height: 200px;
}
If you have div wrapper you can make the image take up the size of that div e.g.
.wrapper{
width: 200px;
height: 200px;
}
.wrapper img{
width: 100%;
height: auto;
}
You can fake the src using an image as a background e.g.
.img{
width: 200px;
height: 200px;
background: /images/image.gif
background-size: 200px 200px /* CSS3 */
}
You can find out more about background image size here http://www.css3.info/preview/background-size/

You can't alter attributes in CSS, only create rules based on attributes.
In your case, you can use CSS content property to set URL to image or inline Base64-encoded images as content of certain elements.
More information here: https://developer.mozilla.org/en-US/docs/Web/CSS/content and here: http://www.w3schools.com/cssref/pr_gen_content.asp
For example:
HTML:
<span class="img-dollar"></span>
<span class="img-royalty"></span>
CSS:
span.img-dollar:before {
content: url(images/dollar.png);
}
span.img-royalty:before {
content: url(images/royalty.png);
}
This will put image into your <span>.

You can't set the src but use the background to achieve a similar effect
img-dollar{
width:5px;
height:5px;
background:url(dollar.png);
}

Yes and No.
You can't add a src attribute using css. You could however use
Javascript for that.
a quick example:
$("img.imgNav").hover(function() {
var src = $(this).attr("src").match(/[^\.]+/) + "over.png";
$(this).attr("src", src);
},
function() {
var src = $(this).attr("src").replace("over", "");
$(this).attr("src", src);
});
You can style the background-color and width/height with css.
img
{
background-color: #222;
width: 200px;
height: 100px;
}
for example.

You could give it a set with or height then use background:url();
Or, using JQuery, you could use $('img-dollar').attr('src', 'image.jpg');
Or, using pure javascript, you could use:
document.getElementById('img-dollar').setAttribute("src", "image.png");

To change any attribute of html element you need to use javascript or jQuery .
you can change image source in jQuery as
$(document).ready(function(){
$('.img-dollar').attr('src','imgpath/imagename.png');
});
and similar code to change other attributes

Here im setting the content and size of an image through straight css:
http://jsfiddle.net/nQxje/
.img-dollar{
content: url('http://woodgears.ca/box_joint/tiny_box_scale.jpg');
height: 100px;
width: 100px;
}
hope this works for you.

Related

Can the same image in HTML have different sources depending on whether the mouse is on it?

So here's the problem - i need an image to slightly change when the cursor is hovering on it. However, simply writing something like this in CSS styles:
img {src="";} img:hover {src="";}
seems to do nothing. Is there a solution to this problem using only HTML and CSS?
Thank you for your time!
You can use background-image property and change the url on hover
img {
height: 200px;
width: 200px;
background-image: url("https://pixy.org/src/477/4774988.jpg");
background-size: 200px 200px;
;
}
img:hover {
background-image: url("https://pixy.org/src/19/193722.jpg");
}
<img />

Change image height, using only its URL

Say we have the following HTML:
<img src="IMAGE-URL" />
And I need to change the image height in CSS, without adding classes or id.
How do I write this in CSS, something like IMAGE-URL height: 100px ?
You can use an attribute selector:
img[src="IMAGE-URL"] {
height: 100px;
}
It's only possible if you attach style to the img itself in html like:
<img src="IMAGE-URL" style="height: 100px;"/>
Or in CSS to the img globally like:
img {
height 100%;
}

Making background-image clickable

I would like to create a link out of the following so that the image is clickable.
What would you do to get the background-image clickable?
HTML: (Displays an Image which I would like clickable)
<div class="some_image">
</div>
CSS:
.some_image{
width: 200px;
height:100px;
background-image: url(../images/image.png);
}
You can't make a background image clickable with HTML alone. It's a property of an element and not an element itself. Simply wrap the div in an anchor. This is permissible in HTML5 spec.
More info
Of course, you could simply style the anchor:
.some_image {
display: block;
width: 200px;
height:100px;
background-image: url(../images/image.png);
}
<a class="some_image" href=""></a>
You can't do this traditionally however you may achieve the effect with hacky techniques:
#heatSpot {
position: absolute;
left: 20px; // over area of bg image
top: 10px; // over area of bg image
cursor: pointer;
}
then just fire the link or whatever your trying to do onClick with either wrapping above element in <a> tag -- or using jQuery / JavaScript.

Replicate background-style:contain on img?

Notice that I need to declare the img source from the html (this will be dynamic), so i dont use background here.
HTML
<div class='some-form'>
<form>
<button>...<button>
<img id="some-img" src="something"/>
<input id="some-input"/>
</form>
</div>
CSS
.some-form {
display: block;
position: relative;
}
.some-form #some-input {
background-color: rgba(255,255,255,0);
border: 1px solid #2F2F2F;
width: 300px;
color: #000;
opacity: 1;
}
.some-form #some-img {
position:absolute;
background-color: #FFF;
z-index: -1;
//background-size: contain; //this does not work
//background-position: center right 50px; //so this will not work
}
How can I get the image to act like contain so that I can align it the way i want?
Keep your code as-is, but change #some-img from an img to a div (and specify width and height as needed based on the image dimensions). It's not possible (at least not in a simple way) to make an img element behave as if it was using background-size and background-position properties since img elements are not backgrounds. So in order to do so, you instead make the image a div with a background-image.
Since you are dynamically populating the image src, you can instead use inline styles to define a background-image on the div, as this lets you call a PHP or other server-side function to echo the image url (which you can't do in a CSS file).
So for example, keep the CSS you have now (but add height/width or other styles to the #some-img div as needed) but replace <img id="some-img" src="something"/> with something like this:
<div style="background-image: url(<?php theDynamicImageURL(); ?>);"></div>
or equivalent in whatever language or method you are using to populate the image dynamically.
There are better ways to do this as inline CSS is generally something that should be avoided, but the use in this case is not too dangerous but it'll work in a pinch and most other methods would either be equally sloppy or a lot more work.
If you include jquery, you can write a script to cheat this:
<script type="text/javascript">
height = $('#some-img').height();
width = $('#some-img').width();
src = $('#some-img').attr('src');
$('#sime-img').wrap('<div id="contain"></div>');
$('#contain').height(height).width(width);
$('#contain').css('background',"url('" + src + "')");
$('#contain').css('background-sizing','contain');
$('#some-img').css('opacity','0');
</script>
It isn't nice. You can do the same thing w/o JQuery, I just used it for convenience.
If I understand correctly, you're looking to constrain an image to the size of its containing element and center it vertically and horizontally.
This will get you pretty close, but the image will only scale up to its actual size, no bigger.
HTML
<div class='some-form'>
<form>
<button></button>
<img id="some-img" src="http://lorempixel.com/300/200/sports"/>
<input id="some-input" />
</form>
</div>
CSS
.some-form {
display: block;
position: relative;
width:400px;
height:180px;
background: rgba(255,255,0,.1); /* for checking that it fits*/
}
.some-form #some-input {
background-color: rgba(255,255,255,0);
border: 1px solid #2F2F2F;
width: 300px;
color: #000;
opacity: 1;
}
.some-form #some-img {
position:absolute;
background-color: #FFF;
top:0;
bottom:0;
left:0;
right:0;
margin:auto auto;
max-width:100%;
max-height:100%;
z-index: -1;
}
fiddle: http://jsfiddle.net/XNR38/
Good luck!

css - how to change image source by its id?

Does anyone know how can I control the image source from the CSS?
I need to be able to change the image src from the CSS. I have loop printing < img id=.. > tags, and for every id it different image. I want to be able to set the source by its id from the style css area.
Does anyone know how to do this?
This is not possible: The image's source is part of the markup, not CSS.
The only workaround would be having div elements with background-image properties instead. Those you could set from within the style sheet:
<div id="image1"></div>
#image1 { width: 100px; height: 50px; background-image: url(image.gif); }
However, with this method you lose all the img tag's advantages like
The ability to set an alt text
Resizing
Printing (most browsers don't print background images)
Search engine indexing (probably)
the only other alternative is by using JavaScript, but that obviously won't work if JavaScript is disabled, which makes it a no-no in my view.
This is now possible with CSS3 using the Content style.
I use this to swap images within a slider based on window size through media queries.
Edit: When I originally posted this, I was unaware that it only worked in Webkit at the moment. But I doubt it will take long before it gains more functionality across browsers.
HTML
<img class="img1" src="image.jpg">
CSS
#media (min-width: 768px) {
.img1 {
content: url(image.jpg);
}
}
#media (max-width: 767px){
.img1 {
content: url(new-image.jpg);
}
}
That is not possible with CSS.
However, this is very easy with Javascript:
document.getElementById("IdOfImage").src = "SourceOfImage";
You cannot really do that, however, if you do need to do that using CSS, you can do it for two images with the same size like this:
<style>
img {
width:0;
height:0;
display:block;
background: url('2.png') no-repeat bottom left;
padding-left:196px;
padding-bottom:187px;
}
</style>
<img src="1.png">
Only tested it in FF3.6 though.
I found this article that might be useful. It actually changes background of an image
here is the example in case website goes missing:
HTML
<html>
<body>
<div class="header">
<img class="banner" src="http://notrealdomain1.com/banner.png">
</div>
</body>
</html>
CSS
/* All in one selector */
.banner {
display: block;
-moz-box-sizing: border-box;
box-sizing: border-box;
background: url(http://notrealdomain2.com/newbanner.png) no-repeat;
width: 180px; /* Width of new image */
height: 236px; /* Height of new image */
padding-left: 180px; /* Equal to width of new image */
}
If you don't want to use backgrounds nor use javascript, you layer 2 images with different src on top of each other (using absolute positioning) and use CSS to hide one or another. Visually it will be the same then changing the src.