Have you ever heard that world remember just 20% that what they read, but 80% of what they see? if the specific percentages are debated, the simple idea isn’t: It’s easy for civilization to find out and procedure information visually.

You are watching: What is the correct html for inserting a background image

That’s why most websites utilize images, and also why it’s important to include images ~ above your own site. Images help make her content much more informative, engaging, and memorable. In enhancement to boosting the visitant experience, they can also aid boost her organic search traffic.

If you usage a website structure platform likeCMS HuborWordPress, simply click the image symbol in your toolbar, select picture from your record manager, and also insert it. If you don’t use a builder, you deserve to still easily include images to your website. You simply need to recognize someHTML. Stop walk with the process below.

*


How come Insert picture in HTML


To insert picture in HTML, usage the image tag and include a resource and alt attribute. Like any other HTML element, you’ll include images to the body ar of your HTML file.


The syntax looks like this:

*

The HTML image element is one “empty element,” meaning it does not have a closing tag. Unlike aspects like paragraph that consist of an opening and a closeup of the door tag with content in between, picture specifies its contents with characteristics in the opening tag.

Compare the complying with lines of password to see the difference in between a paragraph and also an image:

This is a paragraph.

Notice the two qualities in the image facet above, src and also alt. Let"s discuss both of this important features next.

The img src Attribute

An image aspect must always have a src (source) attribute, which has the image URL or record path. Otherwise, the web browser will not recognize what to render. Allowed image paper types will count on the web browser that lots the page, however all browsers enable you to place common formats prefer .jpeg, .png, and also .gif, and also .svg.

In the code example above, the resource is a complete hyperlink — this is since the picture is gift fetched from another website. If you desire to place an image stored on your server, you deserve to use the image document path there is no the website name or protocol. Because that example, if the image is located in a subfolder stored in the same location as her HTML file, your image aspect can look much more like this:

This is a paragraph.

*

... In this case,"csz" would be a folder situated in the same directory as your HTML file.

The img alt Attribute

While a internet browser can render picture without the alt attribute, it’s considered a finest practice to incorporate this attribute. That’s because this attribute includes image alt text.

Image alt text is vital for a few reasons. First, that will appear in place of an image if the image stops working to load on a user"s screen. Second, it helps screen-reading tools describe images to readers through visual impairments who could have trouble understanding the image without it.

Third, image alt text permits search engines to better crawl and rank your website. Google photos — not Google Search, Google Image find — is a major search engine top top its own, and another way people can uncover your website. Offering images through descriptive alt text can help you rank for your target keywords and also drive traffic to her site. In 2019, sdrta.net did specifically that, leading to a 25% year-over-year expansion in organic web traffic that came from web and also image search.

The img format Attribute

You might likewise see a style attribute within an

*

It’s necessary to note that you can also specify the dimension of photo using interior or outside CSS, over inline CSS. To find out the difference between these three varieties of CSS, watch our overview on exactly how to include CSS come HTML.

The img width and also height Attributes

The width and height of picture can also be mentioned in pixels with different width and also height attributes, prefer so:

This will produce the same an outcome as the image above, wherein the style attribute was used. The main difference between these methods is the using different width and also style characteristics will tell the web browser how much an are to conserve for the image, i m sorry may an outcome in smoother loading (though this will rely on your page layout, ultimately).

How to Insert a Background picture in HTML

If you’d prefer to set an photo as the background of a net page or one HTML element, fairly than just inserting the picture onto the page, you’ll have to use the CSS background-image property. This CSS property replaced the background-image attribute in previous versions that HTML. It"s much much more flexible and also predictable than the HTML attribute — and still simple to use.

To collection the value of background-image, you have to use the complying with syntax: url(" ");

Between the single quotation marks, you’ll put the picture URL or document path.

How to Insert a Background image on a Page

To start, let’s say you want to collection an image as the background of the whole page. In this case, friend would use CSS come the body element. Utilizing a CSS selector, you can specify the background-image residential property in the head section of her HTML paper or in an exterior stylesheet. For this demo, we’ll use the same picture as over and change the text color to white for this reason we can see it.

Here’s the CSS:

human body background-image: url("https://scx1.b-cdn.net/csz/news/800/2017/theoreticala.jpg"); color: #FFFFFF;

Here’s the HTML:

Background Image

The background image is stated in the human body element.

Here’s the result:

*

Looks great! yet what wake up if the picture is smaller sized than the browser? In that case, it will certainly tile chin by default as presented below.

To protect against this indigenous happening, you have the right to use the background-repeat residential or commercial property and collection it come no-repeat.

Here’s the CSS:

human body background-image: url("https://scx1.b-cdn.net/csz/news/800/2017/theoreticala.jpg"); background-repeat: no-repeat; color: #FFFFFF;

The HTML continues to be the same. Here’s how it would certainly look top top the front-end now:

*

You’ve resolved the repeating problem, yet now you have a most extra whitespace below and to the right of the image. To ensure the background image covers the entire body aspect — or, in other words, takes up the entire browser window — you have the right to use the background-size property and set it to cover. Then, to stop the photo from warping that is dimensions, use the background-attachment building and collection it come fixed. The way, the picture will save its initial proportions.

Here’s the CSS:

body background-image: url("https://scx1.b-cdn.net/csz/news/800/2017/theoreticala.jpg"); background-repeat: no-repeat; background-attachment: fixed; background-size: cover; color: #FFFFFF;

The HTML stays the same. Here’s exactly how it would certainly look ~ above the front-end now:

*

How come Insert a Background picture on an HTML Element

You can also set an photo as the lift of an HTML aspect rather than the whole web page.

See more: What Does A Lightning Bolt Mean, Spiritually

For example, you can place HTML facets inside a div, climate target the div through the CSS nature we used above. One difference is that rather of setup the background-size building to cover, we’re walking to collection it to 100% 100%. That means the picture will large horizontally and vertically as essential to right the entire div element, without retaining its original dimensions.

Here’s the CSS:

div background-image: url("https://scx1.b-cdn.net/csz/news/800/2017/theoreticala.jpg"); background-repeat: no-repeat; background-attachment: fixed; background-size: 100% 100%; color: #FFFFFF;