banner



Can I Use A Url For Background-image

Accept you lot always heard that people remember but 20% of what they read, just 80% of what they meet? While the exact percentages are debated, the basic idea isn't: It'southward easy for people to learn and process information visually.

Site owner sitting on a couch with a laptop inserting an image in HTML

That'due south why most websites employ images, and why it'due south important to include images on your own site. Images aid make your content more informative, engaging, and memorable. In addition to improving the visitor experience, they can besides help boost your organic search traffic.

If you utilize a website building platform similarCMS HuborWordPress, merely click the prototype icon in your toolbar, select an image from your file manager, and insert it. If you lot don't use a builder, y'all tin however easily add images to your website. You merely need to know someHTML. Let's walk through the process beneath.

Download Now: Free HTML & CSS Hacks

The syntax looks like this: <img src="URL" alt="descriptive text">

The HTML epitome element is an "empty element," meaning it does not have a closing tag. Unlike elements like paragraph that consist of an opening and a closing tag with content in between, an epitome specifies its content with attributes in the opening tag.

Compare the following lines of code to encounter the difference between a paragraph and an image:

                              

<p>This is a paragraph.</p>

<img src="https://scx1.b-cdn.net/csz/news/800/2017/theoreticala.jpg" alt="an artist's rendition of a blackness hole in infinite">

Find the 2 attributes in the image element higher up, src and alt. Let's discuss both of these important attributes next. Just first, this video dives in deeper into the steps we merely explained.

The img src Aspect

An paradigm element must always have a src (source) attribute, which contains the image URL or file path. Otherwise, the browser will not know what to render. Permitted image file types will depend on the browser that loads the folio, but all browsers allow yous to place common formats similar .jpeg, .png, and .gif, likewise as .svg.

In the code example in a higher place, the source is a full hyperlink — this is because the prototype is being fetched from some other website. If you want to place an image stored on your server, you tin can use the image file path without the website proper noun or protocol. For example, if the epitome is located in a subfolder stored in the aforementioned place as your HTML file, your paradigm chemical element can look more than like this:

                              

<p>This is a paragraph.</p>

<img src="/csz/news/800/2017/theoreticala.jpg" alt="an artist's rendition of a black hole in infinite">

... in this case,"csz" would exist a folder located in the aforementioned directory equally your HTML file.

The img alt Attribute

While a browser can render an image without the alt attribute, information technology's considered a all-time practice to include this attribute. That'south because this attribute contains image alt text.

Image alt text is of import for a few reasons. Outset, it will appear in place of an epitome if the paradigm fails to load on a user'due south screen. Second, it helps screen-reading tools describe images to readers with visual impairments who might have trouble understanding the image without it.

Third, image alt text allows search engines to better crawl and rank your website. Google Images — not Google Search, Google Image Search — is a major search engine on its own, and another way people can detect your website. Providing images with descriptive alt text tin help you rank for your target keywords and bulldoze traffic to your site. In 2019, HubSpot did exactly that, leading to a 25% year-over-yr growth in organic traffic that came from web and prototype search.

The img style Aspect

You might also see a style attribute inside an <img> tag containing the width and height of the image. Specifying the width and summit can help prevent the web page from flickering while the prototype loads. Here's how the code might expect with these additional attributes:

                              
<img src="https://scx1.b-cdn.net/csz/news/800/2017/theoreticala.jpg" alt="an artist'south rendition of a black hole in space" style="width:400px;height:200px;">

Image inserted below heading and paragraph in HTML file

Information technology'due south important to note that you can also specify the size of an image using internal or external CSS, over inline CSS. To acquire the difference betwixt these three types of CSS, see our guide on how to add CSS to HTML.

The img width and height Attributes

The width and height of an prototype can also be specified in pixels with separate width and height attributes, like so:

                              

<img src="https://scx1.b-cdn.net/csz/news/800/2017/theoreticala.jpg" alt="an artist's rendition of a black hole in space" width="400" superlative="200">

This volition produce the same issue every bit the image above, where the way attribute was used. The primary difference betwixt these methods is that using separate width and way attributes will tell the browser how much space to save for the image, which may result in smoother loading (though this volition depend on your page layout, ultimately).

How to Insert a Background Paradigm in HTML

If you'd like to set an prototype as the background of a web page or an HTML element, rather than simply inserting the paradigm onto the page, you'll need to use the CSS background-image property. This CSS belongings replaced the groundwork-image aspect in previous versions of HTML. It's much more flexible and predictable than the HTML attribute — and still like shooting fish in a barrel to use.

To set the value of groundwork-image, you take to use the following syntax: url(' ');

Between the unmarried quotation marks, y'all'll put the image URL or file path.

How to Insert a Groundwork Image on a Page

To beginning, let's say y'all desire to set up an image as the background of the entire folio. In this case, you would employ CSS to the body element. Using a CSS selector, you can define the background-prototype holding in the head section of your HTML file or in an external stylesheet. For this demo, we'll use the same prototype as in a higher place and modify the text color to white so nosotros tin see it.

Here's the CSS:

                              
body {
    background-image: url('https://scx1.b-cdn.internet/csz/news/800/2017/theoreticala.jpg');
    colour: #FFFFFF;
}

Here'southward the HTML:

                              
<h2>Groundwork Epitome</h2>
<p>The background image is specified in the body element.</p>

Here's the issue:

Setting background image of the entire web page in HTML

Looks nifty! Just what happens if the epitome is smaller than the browser? In that case, it volition tile itself by default as shown beneath.

Setting background image of the body element in HTML means it will repeat by default

To preclude this from happening, you can use the groundwork-echo property and ready information technology to no-repeat.

Here'south the CSS:

                              
body {
    background-image: url('https://scx1.b-cdn.net/csz/news/800/2017/theoreticala.jpg');
    background-echo: no-repeat;
    colour: #FFFFFF;
}

The HTML stays the same. Hither's how it would await on the front-end at present:

Setting background image of body element in HTML to not repeat

You've solved the repeating problem, but now you have a lot of extra whitespace below and to the right of the image. To ensure the background image covers the entire torso element — or, in other words, takes up the entire browser window — yous can use the groundwork-size property and prepare it to comprehend. And so, to preclude the image from warping its dimensions, utilize the groundwork-zipper property and fix information technology to fixed. That manner, the image will continue its original proportions.

Here'due south the CSS:

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

The HTML stays the same.  Here's how it would await on the front-end now:

a background image in html expanding to take up the entire browser window

How to Insert a Background Prototype on an HTML Element

You tin can also set an paradigm equally the background of an HTML chemical element rather than the entire web folio.

For example, you could place HTML elements inside a div, and so target the div with the CSS properties we used in a higher place. One deviation is that instead of setting the background-size property to cover, nosotros're going to set it to 100% 100%. That means the image will stretch horizontally and vertically as needed to fit the entire div element, without retaining its original dimensions.

Here's the CSS:

                              
div {
    background-epitome: url('https://scx1.b-cdn.net/csz/news/800/2017/theoreticala.jpg');
    background-repeat: no-repeat;
    groundwork-zipper: fixed;
    background-size: 100% 100%;
    color: #FFFFFF;
}

Here's the HTML:

                              
<div>
    <h2>Background Image</h2>
    <p>In this example, the background epitome is specified for the div chemical element.</p>
    <p>But you tin can specify the background prototype for any HTML chemical element.</p>
    <p>Try information technology for a paragraph, heading, and more than.</p>
</div>

<p>This paragraph is not contained in the div. Therefore it does not take an image background.</p>

Here'south the result:

Setting background image of a div element in HTML

How to Brand an Image Link in HTML

Images are also effective links — you tin can make a link from an icon or a high-res prototype. Either style, the process is the same: Enclose your <img> element in an <a> (anchor) tag, like and then:

                              
<a href="URL">
    <img src="URL" alt="descriptive text"/>
</a>

Here'south an interactive example — click the the HubSpot logo to be taken to the HubSpot homepage.

See the Pen Create an Image Link by Christina Perricone (@hubspot) on CodePen.

Making Your Website Visual

Adding images on your website is important to both your visitor experience and to search engines. It'southward easy whether you're building your website with a content direction arrangement or from scratch. You just demand to know some HTML and CSS.

Editor'southward note: This postal service was originally published in September 2020 and has been updated for comprehensiveness.

New Call-to-action

Can I Use A Url For Background-image,

Source: https://blog.hubspot.com/website/insert-image-in-html

Posted by: reynoldsfoure1965.blogspot.com

0 Response to "Can I Use A Url For Background-image"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel