{"id":2592,"date":"2020-10-26T04:05:23","date_gmt":"2020-10-26T09:05:23","guid":{"rendered":"https:\/\/cssnewbie.com\/?p=2592"},"modified":"2020-10-26T04:05:23","modified_gmt":"2020-10-26T09:05:23","slug":"how-to-add-a-background-image-in-css","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/how-to-add-a-background-image-in-css\/","title":{"rendered":"How To Add A Background Image In CSS"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-2594\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2020\/09\/464915-PGTBJ5-627-1024x819.jpg\" alt=\"Background Image In CSS\" width=\"1024\" height=\"819\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Images are a vital part of modern webpages and adding a background image gives a perfect touch to the look and feel of a webpage. <a href=\"https:\/\/www.designhill.com\/design-blog\/importance-of-large-beautiful-background-images-on-a-website\/#:~:text=Visually%20Appealing%20Website&amp;text=A%20background%20image%20is%20always,explore%20your%20other%20site%20pages.\">DesignHill<\/a> talks about how the background image is attention-grabbing and the most visually appealing element of any webpage.<\/p>\n<p>We will learn all about it by starting with a code to add an image on the entire page or having different images in sections. And later on, we will explore some properties of a background image like center, repeat, etc. I would suggest to follow the code closely and try it in <a href=\"https:\/\/codepen.io\/\">CodePen<\/a>.<\/p>\n<p>The first code will be used to add an image in the background of the body.<\/p>\n<p>Here\u2019s the code:<\/p>\n<pre>&lt;html&gt;\n&lt;head&gt;\n  &lt;title&gt; Back ground image in body &lt;\/title&gt;\n&lt;\/head&gt;\n\n&lt;link rel=\u201dstylesheet\u201d href=\u201dbackground.css\u201d&gt;\n\n&lt;body&gt;\n&lt;h1&gt; Hello World! &lt;\/h1&gt;\n&lt;p&gt; This text is not easy to read on this background image. &lt;\/p&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>CSS File: Background.css<\/p>\n<p>&nbsp;<\/p>\n<pre>body {\n\n\u00a0 background-image: url(\"https:\/\/cdn.pixabay.com\/photo\/2017\/03\/02\/08\/58\/background-texture-2110724_960_720.jpg\");\n\n}<\/pre>\n<p>Output:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2595\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2020\/09\/BackgroundImage.jpg\" alt=\"Background Image In CSS\" width=\"951\" height=\"566\" \/><\/p>\n<p>So, we have used the property <em>background-image <\/em>with the value as a URL of the website from where we want to import an image. The property has been used for the content inside the body tag and therefore we will see the background image on the entire page. The text inside the body tag will overlay on the background image.<\/p>\n<h3>Adding Image Only In A Part Of A Page.<\/h3>\n<p>Well, we may want to write over the image only in a part of the webpage which could be for the aesthetics or to show importance or to help the reader visualize. We use the property <em>background-image <\/em>but only for the content in &lt;div&gt; tag. Let\u2019s see a code to understand it better.<\/p>\n<p>Code:<\/p>\n<pre>&lt;html&gt;\n\n&lt;head&gt;\n\n\u00a0 &lt;title&gt; Back ground image in body &lt;\/title&gt;\n\n&lt;\/head&gt;\n\n&lt;link rel= \u201cstylesheet\u201d href= \u201cDivisionImage.css\u201d&gt;\n\n&lt;body&gt;\n\n&lt;div&gt;\n\n&lt;h1&gt; Hello World! &lt;\/h1&gt;\n\n&lt;p&gt; This text is not easy to read on this background image. &lt;\/p&gt;\n\n&lt;\/div&gt;\n\n&lt;\/body&gt;\n\n&lt;\/html&gt;<\/pre>\n<p>CSS Filename: DivisionImage.css<\/p>\n<pre>div {\n\n\u00a0 background-image: url(\"https:\/\/cdn.pixabay.com\/photo\/2017\/03\/02\/08\/58\/background-texture-2110724_960_720.jpg\");\n\n}\n\nOutput:<\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2596\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2020\/09\/DivImage.jpg\" alt=\"Background Image In CSS\" width=\"951\" height=\"564\" \/><\/p>\n<h3>How To Add Multiple Background Images<\/h3>\n<p>In the above code, we saw how to add an image in a division. Well, we can expand the usage by adding multiple &lt;div&gt; tags in the HTML code with unique ids assigned to every tag. This id will be further utilized in the CSS to add different background images to every section.<\/p>\n<p>I\u2019m sure this short text would have helped to map how to do it. For clarity have a look at the following code.<\/p>\n<p>Code:<\/p>\n<pre>&lt;html&gt;\n&lt;head&gt;\n  &lt;title&gt;Back ground image in body&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;div class=\"c1\"&gt;\n&lt;h1&gt;Hello World!&lt;\/h1&gt;\n&lt;p&gt; This text is not easy to read on this background image. &lt;\/p&gt;\n&lt;\/div&gt;\n  &lt;div class=\"c2\"&gt;\n&lt;h1&gt;Hello World!&lt;\/h1&gt;\n&lt;p&gt; This text is not easy to read on this background image. &lt;\/p&gt;\n&lt;\/div&gt;\n  &lt;div class=\"c3\"&gt;\n&lt;h1&gt;Hello World!&lt;\/h1&gt;\n&lt;p&gt;This text is not easy to read on this background image.&lt;\/p&gt;\n&lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<p>CSS File: MultipleDiv.css<\/p>\n<pre>.c1 {\n  background-image: url(\"https:\/\/cdn.pixabay.com\/photo\/2017\/03\/02\/08\/58\/background-texture-2110724_960_720.jpg\");\n}\n.c2 {\n  background-image: url(\"https:\/\/cdn.pixabay.com\/photo\/2017\/08\/12\/10\/13\/background-2633962__340.jpg\");\n}\n.c3 {\n  background-image: url(\"https:\/\/cdn.pixabay.com\/photo\/2017\/07\/27\/23\/00\/background-2547097__340.jpg\");\n}\n\n<\/pre>\n<p>Output:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2597\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2020\/09\/MultipleBImages.jpg\" alt=\"Background Image In CSS\" width=\"952\" height=\"565\" \/><\/p>\n<p>We also have properties to be used along with the background-image such as background-position, background-repeat, background-size. To understand all properties it is better to use all inside a single code. Thus, by following the code and changing the values you can understand the usage.<\/p>\n<p>Check the code below and relate the use of different properties in there.<\/p>\n<pre>&lt;html&gt;\n&lt;head&gt;\n&lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;div class=\"c1\"&gt;\n  &lt;div class=\"c2\"&gt;\n \u00a0\u00a0 &lt;h1 style=\"font-size:50px\"&gt; Hi multiple properties have been used around &lt;\/h1&gt;\n \u00a0\u00a0 &lt;h3&gt; Feel free to change and check. &lt;\/h3&gt;\n \u00a0\u00a0 &lt;button&gt; Click me to get relief &lt;\/button&gt;\n  &lt;\/div&gt;\n&lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>CSS File:<\/p>\n<pre>body {\nmargin: 0;\n}\n.c1 {\nbackground-image: url(\"https:\/\/cdn.pixabay.com\/photo\/2020\/09\/05\/12\/38\/landscape-5546444_960_720.jpg\");\nheight: 250px;\nbackground-position: center;\nbackground-repeat: no-repeat;\nbackground-size: cover;\nposition: fix;\n}\n.c2 {\ntext-align: center;\nposition: absolute;\ntop: 50%;\nleft: 50%;\ntransform: translate(-50%, -50%);\ncolor: white;\n}<\/pre>\n<p>Output:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-2598\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2020\/09\/MultipleProperties-1024x363.png\" alt=\"Background Image In CSS\" width=\"1024\" height=\"363\" \/><\/p>\n<h3>Conclusion:<\/h3>\n<p>We have seen codes to add background image in different ways and later used multiple properties inside the code. In all, with this article, you should be equipped with the required properties to play around background images.<\/p>\n<p>To learn how to center an image in CSS, visit our previous article. To learn more about CSS, subscribe to our <a href=\"http:\/\/www.cssnewbie.com\">blog<\/a>!<\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<\/p>\n<p>&nbsp;<\/p>\n<p>Images are a vital part of modern webpages and adding a background image gives a perfect touch to the look and feel of a webpage. DesignHill talks about how the background image is attention-grabbing and the most visually appealing element [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/how-to-add-a-background-image-in-css\/\" title=\"Click to read 'How To Add A Background Image In CSS'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[183],"tags":[],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/2592"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=2592"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/2592\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=2592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=2592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=2592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}