I would like to have all my images in CSS (the only way I know how is to put them in as background images).
But the problem in this solution is you can never let the div take the size of the image.
So my question is: what is the best way to have the equivalent of
<div><img src="..." /></div>
in CSS?
With Javascript/Jquery:
img
div
and set width, height and backgroundremove the original img
$(document).ready(function() {
var image = $("<img>");
var div = $("<div>")
image.load(function() {
div.css({
"width": this.width,
"height": this.height,
"background-image": "url(" + this.src + ")"
});
$("#container").append(div);
});
image.attr("src", "test0.png");
});
Take this as a sample code. Replace imageheight and image width with your image dimensions.
<div style="background:yourimage.jpg no-repeat;height:imageheight px;width:imagewidth px">
</div>
you can do this:
<div class="picture1"> </div>
and put this into your css file:
div.picture1 {
width:100px; /*width of your image*/
height:100px; /*height of your image*/
background-image:url('yourimage.file');
margin:0; /* If you want no margin */
padding:0; /*if your want to padding */
}
otherwise, just use them as plain
Source: Stackoverflow.com