css background etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
css background etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

CSS Background

CSS Background

The background of your website is very important, so please spend some time with this tutorial. If you are aiming for a professional website, a good rule of thumb is to use a light background with dark text. However, if you're just making a website for pleasure, then any kind of color combination is acceptable.

With CSS, you are able to set the background color or image of any CSS element. In addition, you have control over how the background image is displayed. You may choose to have it repeat horizontally, vertically, or in neither direction. You may also choose to have the background remain in a fixed position, or have it scroll as it does normally. The following examples will show you how to implement all of these options.

CSS Background Color

As you have seen throughout Tizag Tutorials, many different background colors are present. These varying backgrounds were obtained without using tables! Below are a couple examples of CSS backgrounds.

CSS Code:

h4 { background-color: white; }

p { background-color: #1078E1; }
ul { background-color: rgb( 149, 206, 145); }

Display:

This is a

with a white background

This is a

with a background using the hexadecimal value of #1078E1

  • This is an unordered list
  • with an RGB value of 149, 206, 145

In the above example we used three different formats for defining a color: a color name, hexadecimal values, and RGB. Check out the the list of supported color names. Hexadecimal form is a pound sign (#) followed by, at most, 6 hex values (0-F). RGB defines the individual values for Red, Green, and Blue. Example form: rgb(Red, Green, Blue); with the range of 0-255 for each value.

CSS Background Image

Need an image to repeat left-to-right, like the gradient background that appears at the top of Tizag.com? Or maybe you would like to have an image that remains fixed when the user scrolls down your page. This can be done quite easily with CSS and more, including:

  • choosing if a background will repeat and which directions to repeat in.
  • precision positioning
  • scrolling/static images

Let's begin with a default CSS background image.

CSS Code:

p { background-image: url(smallPic.jpg); }

h4{ background-image: url(http://www.tizag.com/pics/cssT/smallPic.jpg); }

Display:

This

has a background image using a local path to the picture.

This

has a background image using the complete url path.

Background Image Repeat

You can have a background image repeat vertically (y-axis), horizontally (x-axis), in both directions, or in neither direction.

CSS Code:

p {
background-image: url(smallPic.jpg);
background-repeat: repeat; }
h4 {
background-image: url(smallPic.jpg);
background-repeat: repeat-y;}
ol {
background-image: url(smallPic.jpg);
background-repeat: repeat-x;}
ul {
background-image: url(smallPic.jpg);
background-repeat: no-repeat;}

Display:

This

has a background image repeating in both directions (default repeat). If you use this option, make sure that your image was designed to be repeated.

This

has a background image repeating vertically (y). You could this to add a style to the side of your element.

  1. This is an ordered list
  2. With a background that repeats
  3. Horizontally (x)
  • This is an unordered list
  • With a background that repeats
  • in neither direction.

CSS Fixed Background Image

You may choose to have your background scroll naturally, or to have it in a fixed position. Note: This feature does not work properly in most browsers when placed within a textarea, except Internet Explorer 6.0, which displays it correctly.

CSS Code:

textarea.noScroll {
background-image: url(smallPic.jpg);
background-attachment: fixed;
}
textarea {
background-image: url(smallPic.jpg);
background-attachment: scroll;}

Display:

CSS Background Image Positioning

If you would like to define where exactly an image appears within an HTML element, you may use CSS's background-position. Please take note that there are three different ways of defining position: length, percentages, and keywords. We recommending using lengths -- specifically, pixels.

CSS Code:

p {
background-image: url(smallPic.jpg);
background-position: 20px 10px;
}
h4 {
background-image: url(smallPic.jpg);
background-position: 30% 30%;
}
ol {
background-image: url(smallPic.jpg);
background-position: top center;
}

Display:

This

has a background image positioned with pixel values.

This

has a background image positioned with percentages.

  1. This is an ordered list
  2. With a background that was positioned
  3. using keywords.

Note: When using pixels, the location of the image will be (A)px from the left of the screen and (B)px from the top of the screen, where A and B are integers. Note: When using percentages, the location of the image will be (A)% from the left of the screen and (B)% from the top of the screen, where A and B are integers. Note: Available positioning keywords are: top, right, bottom, left, and center.

CSS Gradient Background

If you would like to create a gradient background like the one that appears at the top of Tizag.com, you must first create an image inside a painting program (Photoshop, Draw, etc) like the one you see below.

Necessary Image:

Notice that the image is very slim. We are going to be tiling the image horizontally, so you can make the image skinny as possible. As long as the image is 1 pixel or wider, you will be fine.

Using the repeat attribute, we set the value to repeat-x which causes the image to span left to right across the specified element. This example adds a gradient background to the paragraph element.

CSS Code:

p {

background-image: url(http://www.example.com/gradient.gif);
background-repeat: repeat-x;

}

Display:

This paragraph has a gradient background. The gradient background was first made in a painting program and then repeated horizontally across the paragraph element. It makes for a neat effect that also loads quickly! Because the image is very skinny, the file size is also very small. You could also create a gradient that changes color left to right and repeat it in the vertical direction to have a gradient on the side of your web page.


Found Something Wrong in this Lesson?

Try it out!




Read More!

CSS Tutorial - The background-position Property

CSS Tutorial - The background-position Property

Everybody who has done any major website coding loves the CSS background-Image property. Besides doing plain old background images quite well, it has been used and abused in dozens of ways to get nice flicker free rollover images But while we will be using the background-Image property today, it will not be our main focus - instead we will be taking a look at the background-position property. Read on to learn about what this property is, and how it can be extremely useful when writing a website (and we show you yet another way to do rollover images).


So what does this background-position property do? Well, when used in conjunction with background-Image, you can actually offset the background image in both the x and the y direction. So say you have a 10x10 pixel div with a 20x20 pixel background image. By default, the upper left of that 20x20 image would be displayed (the 10x10 square on the upper left). But maybe you actually wanted to display the content of the lower right of the image. Well all you would have to do is do the following in your style tag:

background-position: -10px -10px;

What that property will do is shift your image 10 to the left and 10 to the top. Below are some examples of what I am talking about:

So the large square above is the full image that we are using. It is just a 100x100 pixel image where each quadrant is a different color. The two rows of squares below that image are each separate divs with the full image set as the background, but with different background-position values set. On the first row of 4, the green square has the value "0px 0px", which is the default value for that property. The next square, the red one, has the value "-50px 0px". This tells the browser to shift the image left by 50 pixels. In this particular case, you could also use the value "50px 0px" and get the same effect - this would shift the image right by 50 pixels - and since by default the browser will wrap the image, you would end up with the 'right' of the image on the left. I tend to like working with the negative position values better, that way you don't have to worry quite as much about the image wrapping.

The next square, the blue one, the image is shifted up by 50px (background-position:0px -50px). And finally on that row we have the yellow square, which is both shifted up by 50px and shifted left by 50px (background-position:-50px -50px).

The last row of squares shows more of what happens with the image wrapping. With the first square, the image is shifted down and to the right by 25 pixels ("25px 25px" or "-75px -75px"). With the wrapping this causes the right of the image to show up on the left, and the bottom of the image to show up on top. Then we have a square showing "-25px -75px, which ends up just flipping the top and bottom, while shifting the image left. The third square flips the left and right while shifting the image up, with the position of "-75px -25px". The final square shows the dead center of the image, using the coordinates "-25px -25px".

Ok, hopefully that didn't hurt your head too much. You might what to get an image of your own, and play around with the property, so you get a better sense for how image wrapping affects all of this. Of course, you can turn off image wrapping using the css property background-repeat and its associated options, but where would the fun be in that?

So why is this useful at all? Well, there are two main uses that I have found for manipulating background image position. The first is actually pretty widely used already - and that is to make the perfect rollover button. This property, combined with the css "hover" pseudo-element, makes setting up rollover images very easy. Below is an extremely simple example:


As you can see, when you mouse over that green square, it turns to yellow. The code to do this is quite short - short enough, in fact, that here it all is:

a.myLink
{
background-Image:url('my_best_image.jpg');
display:block;
width: 50px;
height: 50px;
}
a.myLink:hover
{
background-position: -50px -50px;
}

<-a class="myLink"-><-/a->

All we do here is create a style class for links, where we give it a background image and a size. We also need to set the display property to block to make the link actually take the given size (instead of sizing to the contained content). One thing to remember - since we don't have the background-position set, it is set to "0px 0px". The magic happens in the a.myLink:hover section - because, now when the mouse is over a link which has the "myLink" style, the background-position gets changed. And when the mouse leaves, it gets changed back.

There are a couple benefits to using this technique. For one, there is no javascript needed. And more importantly, there is never any image flicker as the browser tries to load a second image. Ever. There is only one image, and so if it is displayed at all, it means the image data is there for both the hover and non-hover cases.

Ok, so that is all well and good - but the technique I just covered is actually pretty well known. But there is a second, less talked about, reason that the background-position property is so useful. It can help you cut down on the number of images on a website. Think about it - a lot of websites have a least a dozen (and often many more) tiny images. Need a rounded corner? Thats another image. A nice looking tab? Another image. And so on and so forth.

Each additional image means another round trip from client to server. It means more bandwidth wasted on image headers (which can be a significant percentage of the total size of a small image). It means another blank spot on your web page as a user waits for it to load. But by using the background-position property, you can cut down on all that! You can put all your icons, all your corners, all your tabs, etc.. into a single image. For instance, take a look at the following image:


This image is what we use on GamingTextures as our main background and icon image file. Almost every single icon and background image on the site is in this one file - 34 in total. The combined image is only 6.3KB (and only takes one request from the client to get). When we were using separate images for everything, the total came to almost 20KB, and there were 34 image requests that the client had to send. The only annoying thing about a scheme like this is that you need to have pixel positions for each individual image. For instance, if I want to display the closed folder icon, I need to know that it is at position "-84px -30px":


style=
"background-Image:url('
/files/Tutorials/CSS/Background_Position/combined_100x283.gif'
);background-position:-84px -30px;width:16px;height:14px"
>

Other than having to know those values (or open up an image editing program and finding them) using an image like this works wonderfully. And besides cutting down on bandwidth and load time, I find it also helps with the appearance of a site. This is because if all the images and icons are in one image, then they are either all being displayed or none of them are. When the image loads, every single element that references it on the page shows up instantaneously, all at the same time.

One thing that you do have to watch out for is repeating images. You can't put a repeater in an image like the one above - because when the image repeats, the whole image will wrap. One thing you can do is put all your vertical repeaters in one image, and all your horizontal repeaters in another. If you have something that needs to repeat both vertically and and horizontally, it sadly needs to be in its own image file. Below we have an example of sticking a couple vertical repeaters in a single image:






And there you go! Have fun combining all your images together, and if you have any questions or comments about advantages/disadvantages to this technique, please leave your thoughts below.
Read More!