Thanks all for your responses. However, I haven’t found any good answer to my question. So let me be more clear.
PURPOSE
As I’m working on SEO, I need to include images as html elements and mimic all the css attributes.
What I currently have
I added images within the css in order to expand/stretch and always center them in the middle of a div.
.page-electricite-generale .one {
background: url('assets/images/elec/XXXX.jpg') 50% 50% no-repeat;
background-size: cover;
}
As you can see, the center of the image is always centered, without any crop/distortion inside the div.
What I need
I want to integrate images as html element and mimic the previous jsfidle.
Here is the jsfidle with an html image
Thanks
5
Answers
Try
jsfiddle http://jsfiddle.net/guest271314/th2LefL1/
Its not perfect, but I think you may be able to do something like this:
Working Example
You can do this using an HTML
<img>
tag, and pure CSS to resize the image. This way you have the image as an HTML element for SEO purposes, but you don’t have to use JavaScript to responsively change the size.HTML
CSS
Assuming the image is at least as large as it’s container, you can just use (assuming the style applies to an IMG element)
You have several possible solutions:
1. Combine tag and background image
This one is easiest. You put image in your markup like you would normally but hide it and use background-image on parent element. All modern browsers will download your image only once and cache it so you don’t need to worry about performance. Just mind your image has correct path depending on file.
Example:
2. CSS hacks
Check out this article: https://css-tricks.com/perfect-full-page-background-image/
There are several techniques listed there. This one worked for me:
3. Object-fit property
This is new property that is basically
background-size
version for images in markup. Unfortunately, browser support is not there yet.Resources:
https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
http://caniuse.com/#feat=object-fit