HOME | DD
Published: 2013-08-03 22:08:19 +0000 UTC; Views: 10374; Favourites: 198; Downloads: 0
Redirect to original
Description
body div#devskin10299383 hr { }
Introduction
Custom shapes and images are an integral part of any journal skin. They can include buttons, icons, header backgrounds, dividers, textures - sometimes the whole design is one big, sliced image artwork with a text area so one can rightly call it a journal skin. Other times the custom image usage is low or non-existent, and you wouldn't believe how coders make their skins rock anyway (or because). In this Let's code! I'm going to address the different background properties and their values.
Index
1. The Background Property: Structure
1.1 background-color
1.2 background-image
1.3 background-repeat
1.4 background-position
+ a nifty tip
1.5 background-attachment
1.6 Shorthand Form
1. The Background Property: Structure
There are five properties associated with "background":
background-color
background-image
background-repeat
background-position
background-attachment
1.1 background-color
As the name suggests, this property defines the color of an element's background. You can also use it to make the background transparent. Now you may want to ask, "Why make the background transparent?" When you are creating your own journal style you will be presented with the default deviantART skin with its own properties and values. Usually, you'll want to get rid of that stuff nice and easy. Putting
* {background: transparent;}
into your code will ensure that each and every background-color property returns as transparent due to the use of the universal selector *. I like to call it the "God Selector" or "Ultimate Parent", because whatever you put in there, it will match every element inside your code. Of course you can "overwrite" the universal selector's transparent value by manually setting the various element's background-colors where you need and want them.
A short discourse into parent and child relationships. CSS coding is a lot about structure, and just like in HTML and pretty much any other scripting language this structure consists of containers that contain containers that contain containers that contain content, if you catch my drift. If you specify Verdana as font-family for the "upper-most" container, this property and value will automatically be inherited by its sub-containers, unless you dictate otherwise. It is a dynamic that you should keep in mind.
background-color: red;
Yes, you can put in the name of the color you want to use, but I'd strongly advise against it. You relinquish virtually all control over the resulting color, except for the basic red, green, blue, black, white, and so on. Hexadecimal color values
I daresay that this is the most commonly used value variation. A hex code looks like this:
#FF0000
It consists of six digits ranging from 0 to FF, with 0 being the lowest and FF being the highest. The first pair of digits defines the color RED, the second pair of digits defines the color GREEN, and the third pair of digits defines the color BLUE. In above example you'd get a bright red, because the first two digits controlling red are set to FF, the highest value, while green and blue are set to 0, the lowest value.
There is a shorthand form that can be used in CSS. If you have a hex color value like #000000, which is black, you can shorten it to #000. Same goes for #FFFFFF (#FFF ), which is white, and also #AA6622 (#A62 ), which is some creepy orange-brown if you must know. You cannot use it on a hex value like #A26BDF , a quirky purple.
RGB and RGBA
RGB stands for the color channels Red Green Blue. The intensity of the colors is being defined by values ranging from 0 to 255, or percentages ranging from 0% to 100%. Example, both putting out the same bright red:
background-color: rgb(255, 0, 0);
background-color: rgb(100%, 0%, 0%);
RGBA adds to RGB with a fourth channel, the alpha channel. This is not a color channel; the value defines the opacity of your chosen color, 0.0 translating as full transparency and 1.0 translating as full visibility. So, 0.5 would be 50% visbility.
background-color: rgba(255, 0, 0, 1.0);
There are also HSL (Hue Saturation Lightness) and HSLA (Hue Saturation Lightness Alpha), but they are not supported (yet?), so I won't delve into it.
1.2 background-image
The art to put a custom image into your skin. Now, mind you, "background" is a bit misleading as a term here. You're not using this property solely to code in actual backgrounds, but also all the rest of your custom images you want properly defined or customized and not in the journal body via HTML code. That said, getting an image into your skin is as easy as being too lazy to come up with a funny analogy. It's the formatting that may or may not make you bite into your keyboard. But more on formatting later. First off, example code minus formatting:
.image {background-image: url('IMGURL');}
1.3 background-repeat
You may have an image that needs repeating, like a tiled texture. Or an image that doesn't need repeating. Either way, as you will have gleaned from the property name, this is the way to do it. There are five possible repeat values:
no-repeat: The image won't be repeated.
repeat-x: The image will be repeated along the x-axis, aka horizontally, like: left to right.
repeat-y: The image will be repeated along the y-axis, aka vertically, like: top to bottom.
repeat: The image will be repeated on both axes.
inherit: The image will repeat or not depending on the settings of the parent element.
Example:
.image {
background-image: url('IMGURL');
background-repeat: no-repeat;}
1.4 background-position
One very subjective view of mine I want to share with you holds that this property is one of the most useful and effective properties in all the image formatting options (on dA, at least). Background-position is awesome for several reasons I will tell you about in a bit, but let's start with what it actually does. It specifies the position of your image. Yeah, you go ahead and roll your eyes at me, but you may want to stop for a moment to have a look at the values:
right top
left top
center top
right center
left center
right bottom
left bottom
center bottom
center center
Should you omit either of the two values it will always return center.
percentage
Two values, the first specifying the percental position on the x-axis, the second on the y-axis. 0% 0% by default. Should you omit either of the two values it will always return 50% (in other words: center). Keep in mind that these values are the percental equivalent of the list above. The default 0% 0% is top left, while 100% 100% is right bottom. Likewise, 50% 50% would be center center. The difference is that you have more control in positioning the image.
positions
A lot like percentage. Actually, exactly like it as there is no CSS unit specified for use with these values. px, %, cm, mm, in, pc... You can also mix positions and percentages. As is usually the case, the first value is X, the second value is Y, X being horizontal, Y being vertical.
inherit
The position will depend on the settings of the parent element.
Example for further reference:
.image {
background-image: url('IMGURL');
background-repeat: no-repeat;
background-position: 100% 20px;}
So, you see, background-position helps you a great deal in moving an image around. But the usefullness does not end here, in fact, it only just started. Moving an image inside its own boundaries
Let's say you - stop thinking why the heck you'd ever want to do that - you have an image, and you want that image to change to another image on mouse over. Logic dictates that you'll need two different images: the regular image, and the on-hover image. The code would look something like this:
.regularimage {
background-image: url('regularimage.jpg');
background-repeat: no-repeat;
width: image-width-in-px;
height: image-height-in-px;}
.regularimage:hover {
background-image: url('hoverimage.jpg');
background-repeat: no-repeat;
width: image-width-in-px;
height: image-height-in-px;}
And yes, that would very much work. Although on loading the page only the regular image would be loaded, not the hover image, which would only then be loaded when the visitor hovers his cursor over it. A small file size will act to your advantage; you may be lucky. But there is the possibility that the visitor hovers his cursor over the regular image, and the image vanishes to reveal background while the hover image loads and then pops up. The loading process can take up the smallest amount of time, but it will be seen, and it will be ugly.
What to do about it?
Simple. Two images, one file. By placing the hover image into the same file as the regular image, they will be loaded simoultaneously. No loading time inbetween mouse over and hover image appearance. To better illustrate my point I've made two simple buttons and saved them in one file:
Now to the CSS code. The image width will be 173px - because that's the width of the buttons - and the height will be enough to show one of the buttons. One button is 30px, so the height is going to be 30px. The code for the regular button will look like this:
.button {
background-image: url('IMGURL');
background-repeat: no-repeat;
background-position: 0 0;
width: 173px;
height: 30px;}
Making for this:
As you can see I didn't change the default background-position values. 0 0, after all, is top left. The CSS code for the hover button will look somewhat like this:
.button:hover {
background-image: url('IMGURL');
background-repeat: no-repeat;
background-position: center bottom;
width: 173px;
height: 30px;}
Now hover over this:
Same file, different image. Might come in handy. Stacking up images in one file - for optimization purposes, for positioning purposes, for in-boundary positioning purposes, for all of those purposes, and any other purpose you can think of.
1.5 background-attachment
This one has three values, and since the most common variant also happens to be the default, background-attachment is not often used. The three values are:
scroll
This is the default value. The image scrolls with the element, meaning that it has a place inside your journal skin (or design of whatever kind) and it will stay put when you scroll down the page.
local
The image does move on scroll in regards to the element's contents.
fixed
The image is fixed and moves with your scrolling; not with a specific element or content, but the viewpoint. As soon as the image hits the upper part of the page, it will follow you down, and when you scroll up, it will follow you up again. This comes in handy when you have a dynamic design and an intricate background. Despite the fact that the design can be prolonged as far as one wishes, the background image will always follow.
.image {
background-image: url('IMGURL');
background-attachment: fixed;}
1.6 Shorthand Form
What is a shorthand form, you ask? Well, a shorthand strives to shorten the properties by compressing all of them into one. In the background's case, an image taking advantage of every property would look like this:
.image { background: transparent1 url('IMGURL')2 no-repeat3 scroll4 center top5;}
1 background-color
2 background-image
3 background-repeat
4 background-attachment
5 background-position
In that order. It does not matter if one is missing, as long as the order is kept.
Stay strong! Stay CSS.
Celvas
Related content
Comments: 39
ttobserve [2016-02-04 02:22:06 +0000 UTC]
Thank you for sharing your expertise!
Question:
For a journal on the topic of feminine fingers, I have several illustrations in the form of images;
but, at the end, I want to show an animated image.
A .gif animation qualifies as “image”, but a .mp4 does not.
However, the .gif takes up over 30MB, whereas the equivalent .mp4 is under 700kB;
therefore, it would be to everyone’s advantage (the viewer, the DA, the author’s storage)
to scrap the .gif and use the .mp4, right?
Problem is, DA software rejects my
neporo [2015-07-22 23:03:42 +0000 UTC]
Hmmm is there a way to make my background image stretch the box itself? Like...I want to literally use only my background image on the custom box rather that type stuff in it. Surely I can align it on My actual resolution, but is there a way to fix the image to work in every (or most) resolutions?
👍: 0 ⏩: 0
FluffyXai [2014-10-06 22:45:24 +0000 UTC]
If I wanted to use this to put a background colour on my deviations on my page, how would I use the code? i'm not very familiar with CSS
👍: 0 ⏩: 1
Celvas In reply to FluffyXai [2014-10-06 23:08:28 +0000 UTC]
You cannot use CSS outside of journal skins, I'm afraid. If you want to know how to put a consistent solid color background on your widgets, reading the Custom-box Background Tutorial should answer your question.
👍: 0 ⏩: 1
Celvas In reply to MariaPereira [2013-12-26 02:10:21 +0000 UTC]
You're most welcome. I'm happy that so many people find it useful. Mission complete.
👍: 0 ⏩: 1
Deltapotamus [2013-12-21 16:16:59 +0000 UTC]
You have great tutorials.
And the single image for a background that changes on hovering is exactly
what I need to add to the skin I'm working on right now. I knew about it,
just forgot - I'm glad I skimmed through your tutorials.
👍: 0 ⏩: 1
Celvas In reply to Deltapotamus [2013-12-21 16:35:10 +0000 UTC]
I am happy that this here tutorial managed to be of some use to you, fellow journal skinner. Thank you for the flowers.
👍: 0 ⏩: 0
Celvas In reply to theNekk [2013-11-03 20:47:46 +0000 UTC]
My pleasure. Glad you find it useful.
👍: 0 ⏩: 0
zomgmad [2013-10-26 12:59:51 +0000 UTC]
Great tutorial! I didn't know the stuff about background-position so thanks for teaching me something new.
👍: 0 ⏩: 1
Celvas In reply to zomgmad [2013-10-26 14:01:46 +0000 UTC]
My pleasure. I want to mention that background-position comes in handy not only with hover effects. You can also stack up other elements in one file to decrease the file size (and loading time) and use background-position to separate them.
👍: 0 ⏩: 1
zomgmad In reply to Celvas [2013-10-26 18:06:15 +0000 UTC]
Nifty! I'll try that sometime. Thanks.
👍: 0 ⏩: 1
Celvas In reply to zomgmad [2013-10-26 18:21:06 +0000 UTC]
To show you an example, this here uses two image files. Pretty much everything is in here .
👍: 0 ⏩: 1
zomgmad In reply to Celvas [2013-10-26 18:30:35 +0000 UTC]
That's so cool! I would never have thought of putting everything in one image like that.
👍: 0 ⏩: 0
MissLunaRose [2013-10-25 20:24:06 +0000 UTC]
I am keeping this forever in my favorites for reference. Thank you!
👍: 0 ⏩: 1
Celvas In reply to MissLunaRose [2013-10-25 20:59:20 +0000 UTC]
I live to reference, ma'am.
👍: 0 ⏩: 0
TehAngelsCry [2013-10-22 19:16:48 +0000 UTC]
Hey there!
Your awesome tutorial has been featured in my weekly journal: Tutorial Tuesday #22!
I'd love if you could take a look and support your fellow tutorial writers through comments on their deviations
👍: 0 ⏩: 1
Celvas In reply to TehAngelsCry [2013-10-22 20:06:29 +0000 UTC]
Thanks. I will when I have something to say. ^^
👍: 0 ⏩: 0
PizzaPotatoNBacon [2013-09-03 03:56:24 +0000 UTC]
I had no idea you can use one file for two images.
Very wonderful article!
👍: 0 ⏩: 1
Celvas In reply to PizzaPotatoNBacon [2013-09-03 04:09:56 +0000 UTC]
Thankies, fluf. You can put as many images in one file as you like, not only two. Pretty cool stuff, I agree.
👍: 0 ⏩: 0
Celvas [2013-08-04 22:48:27 +0000 UTC]
Thanks for the feedback, as well as . People ask you CSS questions all the time? I wonder *cough* how that could be *cough*...
I plan on writing some more of these, so if you have any suggestions on the most popular questions, I suppose that would be a good place to start.
👍: 0 ⏩: 1
gillianivyart In reply to Celvas [2013-08-05 04:46:52 +0000 UTC]
;D haha. I do not mind, of course, which is why I have the handy reference folder. I am a CSS enthusiast and happy to see new people join our community, so help out. You might want to join , we are compiling many such articles and tips and tricks type of things. You can join as a collaborator if you like.
👍: 0 ⏩: 2
TehAngelsCry In reply to gillianivyart [2013-10-18 15:21:57 +0000 UTC]
I'm totally joining this too Gillian <3
👍: 0 ⏩: 1
gillianivyart In reply to TehAngelsCry [2013-10-19 08:03:57 +0000 UTC]
Awesomesauce. Especially since everyone slowed down their contributions (myself included there).
👍: 0 ⏩: 1
TehAngelsCry In reply to gillianivyart [2013-10-20 18:16:46 +0000 UTC]
Me too recently... since I've started working
👍: 0 ⏩: 1
gillianivyart In reply to TehAngelsCry [2013-10-20 19:17:10 +0000 UTC]
Awww, but good to have a job, right? ;D
👍: 0 ⏩: 1
TehAngelsCry In reply to gillianivyart [2013-10-20 19:21:16 +0000 UTC]
Indeed, and I am loving it
👍: 0 ⏩: 1
gillianivyart In reply to TehAngelsCry [2013-10-21 06:18:52 +0000 UTC]
Then congratulations on it ;D
👍: 0 ⏩: 1
Celvas In reply to gillianivyart [2013-08-05 05:18:36 +0000 UTC]
I have to ask: I tried joining as a collaborator, but it will only let me choose Member and Writer. So which one would be the collab?
👍: 0 ⏩: 1
gillianivyart In reply to Celvas [2013-08-05 06:24:27 +0000 UTC]
I guess writer. Ask ^bradleysays he is the founder, so he would have all the rules for it. But I believe Writer is right.
👍: 0 ⏩: 0
gillianivyart [2013-08-04 22:03:09 +0000 UTC]
Great breakdown and explanations. Putting this in my faves, I have people asking me CSS questions all the time, it is good to have something to refer them to.
👍: 0 ⏩: 1
Celvas In reply to gillianivyart [2013-08-04 22:49:15 +0000 UTC]
I, kind of, wrote myself a comment. Up there, that's for you.
👍: 0 ⏩: 0