<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alfredo C.</title>
    <description>The latest articles on DEV Community by Alfredo C. (@newincome).</description>
    <link>https://dev.to/newincome</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F402664%2F3607bd1a-6587-4657-bf19-e05483923f72.jpeg</url>
      <title>DEV Community: Alfredo C.</title>
      <link>https://dev.to/newincome</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/newincome"/>
    <language>en</language>
    <item>
      <title>How to add some CSS image magic to your backgrounds</title>
      <dc:creator>Alfredo C.</dc:creator>
      <pubDate>Fri, 05 Jun 2020 19:01:22 +0000</pubDate>
      <link>https://dev.to/newincome/how-to-add-some-css-image-magic-to-your-backgrounds-3ji6</link>
      <guid>https://dev.to/newincome/how-to-add-some-css-image-magic-to-your-backgrounds-3ji6</guid>
      <description>&lt;p&gt;Have you ever struggled with image positioning and backgrounds in HTML and CSS? As a beginner Web Developer one of the things I struggled with a lot was image positioning; whether or not to use any image for a background or make my unique style. Most of the time it depends a lot if you only want to show the image of an article, have a nice background for a section of your page or a text area. While working in different projects and after a lot of practice I found out how powerful the CSS background property can be and how much we can do with it.&lt;/p&gt;

&lt;p&gt;We know that backgrounds can have basic values like a simple color, the URL of an image or a linear/radial-gradient, etc.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fd08dasr891c6x00we7o1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fd08dasr891c6x00we7o1.png" alt="Simple Background"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    div {
    background: #81d4be; /* url(https://image.jpg) *//* linear-gradient(45deg, red, black) */
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But it can do much more than that.&lt;/p&gt;

&lt;p&gt;For example you can give an image a shading effect with a linear-gradient:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fygufwitnxbxpvb7cd53o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fygufwitnxbxpvb7cd53o.png" alt="Img bckgnd + gradient"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div {
background: linear-gradient(#ddd5, #222b), url(“https://responsive-design.com/images/picture.svg") 50% 50% no-repeat;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Or you can add a corner shading effect for a corner image and a shading effect for the grater background image, like they do in the page &lt;a href="https://thenextweb.com/" rel="noopener noreferrer"&gt;The Next Web&lt;/a&gt;. By adding some simple code like so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fynp6mf9q578cbm3qn99f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fynp6mf9q578cbm3qn99f.png" alt="Image with 3 shades"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div {
background: url(https://cdn0.tnwcdn.com/wp-content/themes/cyberdelia/assets/img/tnw.svg) 0% 0% no-repeat, linear-gradient(165deg, #f42a, #c096, #9050 55%) 100% 0% no-repeat, linear-gradient(#ddd5, #222b), url(“https://learnsometing.github.io/responsive-design/images/picture.svg") 50% 50% no-repeat;
background-size: 50%, 100% 60%, auto, 120%;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here I mention the attributes of the property, if you need more information about it I will provide some links at the end.&lt;/p&gt;

&lt;p&gt;For proper work with the “background” property with more than one value (URL or color), you need to separate each value by a comma. If you want a plain color it needs to be with a gradient. And since gradients need 2 values it will work by repeating the color.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Here is a list of the background possible attributes:&lt;/em&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;&lt;a href="https://www.w3schools.com/cssref/pr_background-image.asp" rel="noopener noreferrer"&gt;background-image&lt;/a&gt;: url(/image.png) or linear-gradient(#333, #ccc)&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;&lt;a href="https://www.w3schools.com/cssref/pr_background-position.asp" rel="noopener noreferrer"&gt;background-position&lt;/a&gt;: 0% 0% [in % or px]&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;&lt;a href="https://www.w3schools.com/cssref/css3_pr_background-size.asp" rel="noopener noreferrer"&gt;background-size&lt;/a&gt;: auto auto [can be % or px for x and y or just x]&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;&lt;a href="https://www.w3schools.com/cssref/pr_background-repeat.asp" rel="noopener noreferrer"&gt;background-repeat&lt;/a&gt;: repeat&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;&lt;a href="https://www.w3schools.com/cssref/css3_pr_background-origin.asp" rel="noopener noreferrer"&gt;background-origin&lt;/a&gt;: padding-box&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;&lt;a href="https://www.w3schools.com/cssref/css3_pr_background-clip.asp" rel="noopener noreferrer"&gt;background-clip&lt;/a&gt;: border-box&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;&lt;a href="https://www.w3schools.com/cssref/pr_background-attachment.asp" rel="noopener noreferrer"&gt;background-attachment&lt;/a&gt;: scroll&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;&lt;a href="https://www.w3schools.com/cssref/pr_background-color.asp" rel="noopener noreferrer"&gt;background-color&lt;/a&gt;: transparent&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;The background property is easier with it’s short hand property, which defines the gradient or attachment with it’s parameters in one line.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The order is as follows:

&lt;ol&gt;
&lt;li&gt;&lt;em&gt;background-color&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;background-image&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;background-repeat&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;background-attachment&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;background-position&lt;/em&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It’s very important to know that the order is crucial and the first value set in the background is the first layer on screen, the rest stack to the back. Here are two different examples of this usage of the property:&lt;/p&gt;

&lt;p&gt;A simple visual layered box:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ff60i8oa7eq9n24smge9w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ff60i8oa7eq9n24smge9w.png" alt="Layered N"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div {
background: linear-gradient(#f00,#f00), linear-gradient(#fff,#fff), linear-gradient(#f00,#f00);
background-position: 0% 0%, 50% 50%, 100% 100%;
background-repeat: no-repeat;
background-size: 90% 90%, 85% 85%, 85% 85%;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the above example the background property has 3 linear gradients values with a same full color, red(#f00), white (#fff) and red respectively. The second parameter is the position, defined as x and y value.&lt;/p&gt;

&lt;p&gt;It’s important to mention the direct relation of the size of the element on display outcome of all the layers. For this reason on the above example, the div had to have a width and height defined at 35px to show a nice small layered square.&lt;/p&gt;

&lt;p&gt;I’ll show a last more complex example for this, a square with more layers that gives a stacked like view of a square:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpzsyv526ioju2w9dgvwg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpzsyv526ioju2w9dgvwg.png" alt="Multi-layer"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div {
background: linear-gradient(#eee,#eee), linear-gradient(#e00,#e00), linear-gradient(#eee,#eee), linear-gradient(#e00,#e00), linear-gradient(#eee,#eee), linear-gradient(#e00,#e00), linear-gradient(#eee,#eee);
background-position: 76% 75%, 80% 80%, 67% 65%, 59% 51%, 49% 40%, 42% 26%, 100% 100%;
background-repeat: no-repeat;
background-size: 54% 52%, 60% 60%, 60% 61%, 60% 60%, 60% 62%, 60% 60%, 100% 100%;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here we have 7 values in the background property to have view of a front frame and 2 red shades to the back. To achieve this the colors in the shades are in the following order: gray(1st-Layer), red, gray, red, gray, red, gray(Background-Layer).&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;“The possibilities are endless and the limit is your imagination”
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;I leave you here with an amazing work of art that inspired me to extract as much as possible from the tools at our hand, it’s made with CSS and one HTML element. You can find it on the &lt;a href="https://codepen.io/" rel="noopener noreferrer"&gt;Codepen&lt;/a&gt; webpage, do check it out. The name is &lt;a href="https://codepen.io/pbmasigla/pen/aMRwXV" rel="noopener noreferrer"&gt;Pure CSS Single Div Up House!&lt;/a&gt;, author: &lt;a href="https://codepen.io/pbmasigla" rel="noopener noreferrer"&gt;Patricia Masaglia&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you need more info on the background and more CSS properties the &lt;a href="https://css-tricks.com/" rel="noopener noreferrer"&gt;CSS-tricks&lt;/a&gt; site is a great source. Code on!&lt;/p&gt;

</description>
      <category>css</category>
      <category>html</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
