Posts Tagged ‘CSS’
Create a CSS3 Button That Degrades Nicely
|
There was a time where creating a nice looking, scalable button required heavy use of images, the sliding doors technique and even some javascript. As it stands a lot of the modern browsers support the css we would like to use to create a nice looking button. Firefox, Safari and Opera all have support for rounder corners, box shadows and text shadows. What used to take six steps and lots of extra mark-up, images and css, now only takes three steps and some simple CSS3. One of the great perks regarding this technique is that in the event that a user doesn't have a browser that supports the CSS that we will be using, they will still see a nice beveled button – it simply won't look quite as nice. What We are Going To DoTo create a nice button we have a few major components:
In the end we will have a button that looks just like this, only with out using any images behind a few pixel background gradient (and when CSS gradients are supported we can even remove that!).
The HTML
As you can see the HTML is very basic, we essentially just need an anchor tag and text. The paragraph is there just to be semantic. The CSS
The CSS is a bit more complicated, but still fairly simple. We are really just adding a repeating background image, adding some rounded corners, as well as box and text shadows. The borders add a bit more depth / shading and ensures that if the CSS properties are not supported that the element still retains a beveled appearance.
The background image is simple gradient to give the button some depth, in this case I used the following image: That SimpleThat is how easy it is. If you want to play around with the borders you can end up with some pretty nice effects as well. More Examples |
Multiple Class / ID and Class Selectors
|
Can you spot the difference between these two selectors?
They look nearly identical, but the top one has no space between “#header” and “.callout” while the bottom one does. This small difference makes a huge difference in what it does. To some of you, that top selector may seem like a mistake, but it’s actually a quite useful selector. Let’s see the difference, what that top selector means, and exploring more of that style selector.
Here is the “plain English” of “#header .callout”:
Here is the “plain English” of “#header.callout”:
Maybe this graphic will make that more clear:
Combinations of Classes and IDsThe big point here is that you can target elements that have combinations of classes and IDs by stringing those selectors together without spaces. ID and Class SelectorAs we covered above, you can target elements by a combination of ID and class.
Double Class SelectorTarget an element that has all of multiple classes. Shown below with two classes, but not limited to two.
MultiplesWe aren’t limited to only two here, we can combine as many classes and IDs into a single selector as we want.
Although bear in mind that’s getting a little ridiculous =) ExampleSo how useful is all this really? Especially with ID’s, they are supposed to be unique anyway, so why would you need to combine it with a class? I admit the use cases for the ID versions are slimmer, but there are certainly uses. One of those is overriding styles easily.
The second targets the same element, but overrides the color, instead of having to use:
or perhaps prefacing the selector with something even more specific. More useful is multiple classes and using them in the “object oriented” css style that is all the rage lately. Let’s say you had a bunch of divs on a page, and you used multiple various descriptive class names on them:
They all share the class “box”, which perhaps sets a width or a background texture, something that all of them have in common. Then some of them have color names as classes, this would be for controlling the colors used inside the box. Perhaps green means the box has a greenish background and light green text. A few of them have a class name of “border”, presumably these would have a border on them while the rest would not. So let’s set something up:
Cool, we have a good toolbox going here, where we can create new boxes and we have a variety of options, we can pick a color and if it has a border or not just by applying some fairly semantic classes. Having this class name toolbox also allows us to target unique combinations of these classes. For example, maybe that black border isn’t working on the red boxes, let’s fix that:
![]() Border color on red box changed because it had both the red class and border class Based on this demo page. SpecificityAlso important to note here is that the specificity values of selectors like this will carry the same weight as if they were separate. This is what gives these the overriding power like the example above. Browser CompatibilityAll good current browsers support this as well as IE back to version 7. IE 6 is rather weird. It selects based on the first selector in the list. So “.red.border” will select based on just “.red”, which kinda ruins things. But if you are supporting IE 6, you are used to this kind of tomfoolery anyway and can just fix it with conditional styles. |
Tricks
|
CSS has the ability to target HTML elements based on any one of their attributes. You probably already know about classes and IDs. Check out this bit of HTML:
This single element has three attributes: ID, class, and rel. To select the element in CSS, you could use and ID selector (#first-title) or a class selector (.magical). But did you know you can select it based on that rel attribute as well? That is what is known as an attribute selector:
There is a lot more too attribute selectors though, so let’s look closer at all the different options and try to cover some “real world” scenarios on when they might be useful.
Attribute Exactly Equals Certain ValueIn the example we used above, the attribute of the h2 element was “friend”. The CSS selector we wrote targeted that h2 element because it’s rel attribute was exactly “friend”. In other words, that equals sign means just just what you think it does… an exact match. See another basic example:
A great real world example of this is styling a blogroll. Let’s say you had a list of links to friends sites like this:
Then you wanted to style each link slightly differently. The traditional way would probably be to give each link a class name in which to target, but that requires additional markup which is always a nice thing to avoid (semantics and all). Another way might be to use :nth-child, but that requires their order to never change. This is the perfect use for attribute selectors… the links already have a unique attribute in which to target!
I believe the most common use of regular attribute selectors is on inputs. There are text, button, checkbox, file, hidden, image, password, radio, reset, and submit (did I miss any?). All of them are <input>’s, and all of them are very different. So doing something like input { padding: 10px; } is a bad idea most of the time. It’s very common to see things like:
It’s really the only way to get your hands on certain types of inputs without screwing up the others and without adding extra markup. Attribute Contains Certain Value SomewhereThis is where it starts getting more interesting. The equals sign in attribute selectors may be prefaced by other characters which alter the meaning a bit. For example, “*=” means “match the following value anywhere in the attribute value.” Look at this example:
Remember that classes and ID’s are attributes too, and can be used used with attribute selectors. So let’s say you were writing CSS for a site where you couldn’t control the markup and a sloppy developer had three DIVs you need to target:
You could select them all with:
Attribute Begins with Certain Value
A real world example of using this would be, say, that you wanted to style every single link to your friends site different than other links. Doesn’t matter if you are linking to their homepage or any subpage, any links to them you want to style up.
That will match a link to their homepage, but also any other subpages as well. Attribute Ends with Certain ValueWe can select based on how attribute values begin, why not end?
Honestly I struggle a bit to find the perfect real world example of using this, but I do like that it exists. Perhaps you could use it to look for links that end in characters that will likely have no significant effect:
Attribute is within Space Separated ListYou probably already knew that you could apply multiple classes to elements right? Well if you do that, you can still use .class-name in CSS to target any one of them. Attribute selectors aren’t that easy. If your rel attribute has multiple values (e.g. values in a space separated list) you’ll need to use “~=”:
You might be thinking, why would I use this when *= would also match this and be more versatile? Indeed it is more versatile, but it can be too versatile. This selector requires the spaces around the value where as *= would not. So if you had two elements one with rel=home friend-link and one with rel=home friend link you are going to need the space-separated selector to target the second one properly. Attribute is within Dash Separated ListThe dash separated list is very similar to the space separated list above, both in it’s use and how it’s nice to have for stricter enforcement beyond what *= can do.
Multiple Attribute MatchesVital to note is that you can use multiple attribute selectors in the same selector, which requires all of them to match for the selector itself to match.
Browser SupportEvery single example above works in all modern browsers: Safari, Chrome, Firefox, Opera, and IE. Internet Explorer has perfect support for all of these down to version 7, but zero support in 6. To test in your browser, see the test page. If the line/selector style is in red, it works. |
How nth-child Works – CSS-Tricks
|
There is a CSS selector, really a pseduo-selector, called nth-child. Here is an example of using it:
What the above CSS does, is select every third list item inside unordered lists. That is, the 3rd, 6th, 9th, 12th, etc. But how does that work? And what other kinds of things can you do with nth-child? Let’s take a look.
It boils down to what is in between those parentheses. nth-child accepts two keywords in that spot: even and odd. Those should be pretty obvious. “Even” selects even numbered elements, like the 2nd, 4th, 6th, etc. “Odd” selects odd numbered elements, like 1st, 3rd, 5th, etc. As seen in the first example, nth-child also accepts equations in between those parentheses. The simplest possible equation? Just a number. If you put simply a number in the parentheses, it will match only that number element. For example, here is how to select only the 5th element:
Let’s get back to the “3n+3″ from the original example though. How does that work? Why does it select every third element? The trick is understanding the “n” and algebraic equation that represents. Think of “n” as starting at zero and then a set of all positive integers. Then complete the equation. So the 3n is “3xn”, and the whole equation together is “(3xn)+3″. Now substituting in the zero and positive integers, we get: (3 x 0) + 3 = 3 = 3rd Element How about the :nth-child(2n+1)? (2 x 0) + 1 = 1 = 1st Element Hey wait! That’s the same as “odd”, so probably don’t need to use that one very often. But wait now. Haven’t we exposed our original example as being overly complicated? What if instead of “3n+3″, we used “3n+0″, or even simpler “3n”. (3 x 0) = 0 = no match So as you can see, the matches are exactly the same, no need for the “+3″. We can use negative n values, as well as use subtraction in the equations. For example, 4n-1: (4 x 0) – 1 = -1 = no match Using “-n” values seems a little weird, because if the end result is negative there is no match, so you’ll need to add to the equation to get it back positive again. As it turns out, this is a rather clever technique. You can use it to select the “first n elements” with “-n+3″: -0 + 3 = 3 = 3rd Element Sitepoint has a nice reference guide, which includes this handy-dandy table which I’ll shameless republish here:
Browser Compatibilitynth-child is one of those rather unfortunate CSS attributes that is caught between nearly full cross-browser compatibility, except for completely zero support in IE, even IE 8. So when it comes to it’s use, if the end result is “progressive enhancement” in some fashion (e.g. applying a cool color palette to table rows, for example), then by all means, go for it. But you probably shouldn’t use it if you are doing something more important, like relying on it for site structure. For example, removing the right margin from every third box in a three by three grid of boxes, so they will fit properly. One saving grace here is that if you are using jQuery, which supports all CSS selector including :nth-child, the selector will work, even in Internet Explorer. Still not getting it?I’m not a big fan of the phrase “I’m a visual learner”. Of course you are, everybody is. Visual aids are enormously helpful in situations just like this. To help, I put together a little nth-child tester page. There, you can type in equations and see the results of what it selects below. |
HTML Basics
HTML ( Hyper Text Markup Language )
is the code used to create web pages. All tags have brackets (Ex. <u>) and are closed with a / after the first bracket (Ex. </u>). In case you were wondering, <u> stands for underlined text. So, here is an example of a section of HTML code:
<u> HTML basics</u>.
Now let’s go over some basic tags that are used in every page:
<HTML>:This tag represents the beginning of an HTML document. Every HTML document must begin and close with this tag for the page to work.
<HEAD>: This tag contains all of the page header information. This tag can contain attributes such as: TITLE and META. Here is a quick overview of both of them:
<TITLE>: This is the tag used to identify the page name. The text contained in this tag will be the text displayed at the top of the browser as well as the text that will be used to bookmark the page with.
Example: <TITLE>Go 2 Graphics</TITLE>
<META>: META Tags are extremely important because they can determine your search engine ranking. For more information on getting a high search engine ranking, click here. The META tag normally has two attributes; (1) The NAME attribute and (2) The CONTENT attribute. The NAME attribute specifies what the content is in regard to. The CONTENT attribute is what is used by search engines to rank your page/site. Here is an example of what goes in the HEAD Tag:
<HEAD>
<TITLE>Welcome To Go 2 Graphics Inc.</TITLE>
<META name=”keywords” content=”animated gifs, free buttons, HTML Tips”>
<META name=”description” content=”Premium Web Site Design With A Personal Touch”>
</HEAD>
<BODY>: This is the tag that defines the pages attributes. These include: The background color, the background image (if any), the text color, link color, visited link color, & active link color.
The tags for these attributes are, in order: BGCOLOR, BACKGROUND, TEXT, LINK, VLINK, ALINK. Here is an example of the BODY tag, which is placed directly after the HEAD closed tag (</HEAD>):
<BODY bgcolor=”white” background=”br.jpg” text=”black” link=”purple” vlink=”green” alink=”red”>
Now that you have the basics of web page coding down, go on to the next tip on Formatting Text.






Follow Us!