When to use IMG vs. CSS background-image?
Proper uses of IMG
- Use
IMG
if you intend to have
people print your page and you want the image to be included by default.
—JayTee - Use
IMG
(withalt
text) when the image has an important semantic meaning, such as a warning icon. This ensures that the meaning of the image can be communicated in all user-agents, including screen readers.
Pragmatic uses of IMG
- Use
IMG
plus alt attribute if the image
is part of the content such as a logo or diagram or person (real person, not stock photo people).
—sanchothefat - Use
IMG
if you rely on browser scaling to render an image in proportion to text size. - Use
IMG
for multiple overlay images in IE6. UseIMG
with az-index
in order
to stretch a background image to fill its entire window.
Note, this is no longer true with CSS3 background-size; see #6 below.- Using
img
instead ofbackground-image
can dramatically improve performance of animations over a background.
When to use CSS background-image
- Use CSS background images if the
image is not part of the content.
—sanchothefat - Use CSS background images when
doing image-replacement of text eg. paragraphs/headers.
—sanchothefat - Use
background-image
if you intend to have
people print your page and you do not want the image to be included by default.
—JayTee - Use
background-image
if you need to improve download times, as
with CSS sprites. - Use
background-image
if you need for only a portion of the image to be visible, as with CSS sprites. - Use
background-image
withbackground-size:cover
in order to stretch a background image to fill its entire window.
Use the HTML <img> tag as a background image instead of the CSS background-image property?
To make an almost perfectly duplicate of the features used for background-image, we've to consider the features of the img
include:
- It doesn't have any pointer events.
- It's rendered at least one layer below the
div
.
The result would be something like this:
div {
display: inline-block;
overflow: hidden;
position: relative;
width: 100%;
}
img {
pointer-events: none;
position: absolute;
width: 100%;
height: 100%;
z-index: -1;
}
p {
padding: 25px;
}
<div>
<img src="http://via.placeholder.com/720x480/ddd/007" />
<p>
Lorem Ipsum is simply dummy text of the printing and
typesetting industry. Lorem Ipsum has been the industry's
standard dummy text ever since the 1500s, when an unknown
printer took a galley of type and scrambled it to make a type
specimen book
</p>
</div>
html <img> tag instead of css background-image
You can give the img
a z-index
and everything else a higher z-index
.
img { z-index: -1; }
.banner-options: { z-index: 0 }
This will put img
farther in front, but .banner-options
even farther.
How do I set an image background, using the <img> tag rather than CSS?
Just put the img
in the col container, set that element to position: relative;
then use absolute
positioning to put the text over the image.
* {margin:0;}.col { float: left; width: 25%; position: relative;}img { display: block; width: 100%;}.overlay { position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); background: black; color: white; padding: 1em;}
<div class="col"> <img src="http://kenwheeler.github.io/slick/img/fonz1.png"> <div class="overlay"> <h2>text</h2> </div></div><div class="col"> <img src="http://kenwheeler.github.io/slick/img/fonz1.png"> <div class="overlay"> <h2>text</h2> </div></div><div class="col"> <img src="http://kenwheeler.github.io/slick/img/fonz1.png"> <div class="overlay"> <h2>text</h2> </div></div><div class="col"> <img src="http://kenwheeler.github.io/slick/img/fonz1.png"> <div class="overlay"> <h2>text</h2> </div></div>
How to cover a div with an img tag (like background-image does)?
Use object-fit:cover
to not lose ratio.
div {
border: black solid;
width: 400px;
height: 400px;
}
img {
width: 100%;
height: 100%;
object-fit: cover
}
<div>
<img src="//lorempixel.com/100/100" />
</div>
how can I call background-image as <img src>
The style is
background-image
Not background- image
And you refer to a 'https' link which will return no picture at all
(I tried to your link, it gives an error)
EDIT
The https means that the url you access is securised, which means that you can't access its content without login in.
Thus, the picture you wanna access in secured. You can perhaps see it if you login, your visitor wont because they cant access it.
If you want a picture to be background, either link it from your website
background-image:url(images/bg.jpg);
width:100%;
height:100%;
Either take the url from a website WITHOUT https
I recommend you to use either google for pictures, or wallbase.cc which is a good one too
And as a little advice i can give you:
background-image:url(YOUR URL HERE);
background-size:cover;
The background-size:cover
will make your background always the right size if you change your window's width and height, it's perfect for smaller/bigger screen
Related Topics
How to Resolve 500 Internal Server Error
Selecting Siblings Between Two Nodes Using Xpath
Always Show Browser Scrollbar to Prevent Page Jumping
How to Get Rid of X and Up/Down Arrow Elements of a Input Date
New Facebook Like Button HTML Validation
Complete List of Reasons Why a CSS File Might Not Be Working
How to Achieve a Text Loading Animation Over Multiple Lines
Wrapping Flex Items in The Last Row
CSS Changes Are Not Getting Reflected. Why
Background-Position-Y Doesn't Work in Firefox (Via CSS)
How to Select Multiple Elements in The Chrome Developer Tools Elements Panel
Symetric Div with Background Image Without Clip-Path
Box-Sizing: Border-Box => for Ie8
Valid Order for Attributes of Input Type Tag
Schema.Org/Microdata Markup for List of Recent Posts Without Providing "Author"/"Publisher"