1

Which is better:

<img src="http://placehold.it/300x200" alt="">

or:

.img {
  content: url(http://placehold.it/300x200); /* or background-image */
}
<div class="img"></div>

Consider loading times and which is better to stop users to be able to highlight the image, (I don't know why I just don't like people highlighting images) which above option is better?

2
  • 2
    You can't prevent someone from downloading an image if you serve it to them. Commented May 16, 2018 at 4:02
  • 1
    If there is a need to prevent some downloads it's possible to create event handler which would interrupt right click so that 90% of users won't be able to download it except for those who go right to the code and get it directly from there. Commented May 16, 2018 at 6:24

4 Answers 4

1

Setting the image through CSS means some browsers will not display the image if the user tries to print your page, and inline CSS styles become hard to manage. You should probably also set the alt, as this helps with accessibility:

<img src="example.png" alt="example image">

There's no sure fire way to prevent someone downloading an image, but if you want to prevent users from right clicking (to save image as), dragging the image etc. you can use some CSS for this:

img {
    pointer-events: none;
}

This rule will be applied to all img elements.

Sign up to request clarification or add additional context in comments.

Comments

0

I think it depends on the scenario.

  • If it is design(background-image) element, using image as css background will be best.
  • For elements like website logos, partner logos or icons etc(generally static images but may be clickable), any method is okay.
  • But if it is part of content, like an image in news article or blog, I think it is better use <img> tag. Since we can use 'alt' and 'title' attributes in <img>, it will help in SEO also.

Comments

0

It all depends on your requirements. <img> tags with alt and title attributes may provide better SEO and is easier for things like maintaining aspect ratio (just providing one dimension - width/height will maintain aspect ratio), while background images allow for easier ways to crop an image or have it dynamically change using CSS.

FYI, you can prevent users from selecting/highlighting your tag with the CSS property user-select.

Comments

0

If the image is part of the content such as a logo or diagram or person then use the <img> tag plus alt attribute.

If you don't want people to highlight the image background-image would be better option.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.