CSS

CSS Attribute Selectors

CSS Attribute selectors are used to select specific attributes in HTML. For example,

<body>
     <h2 class="title">Welcome!</h2>
     <p class="foo" rel="newText">Some text here</p>
</body>

We have two class attributes and on rel attribute labeled, “newText”. We could use the attribute selector like the following:

.title {font-size:2em;}
.foo {color:#ccc;}
p[rel=newText] { /*attribute selector*/
     font-size:1.15em;
     font-style:italic;
}

Using this rel attribute and stating what its value is, we can add styles to that exact element even though it has other inherited styles.

Breakdown

Attribute selectors can be somewhat strange at first. But fret not! They’re quite easy to learn and can be useful in tricky situations.

[attrib=value]

This means the attribute will equals a certain value. For example:

<div id="form">
     <form action="process.php" method="post">
          <input class="button" type="button" name="button"
value="Click Me" />
     </form>
</div>

Since this button already has a class attribute, it’s already being styled independently. For the sake of argument, let us assume this button is used multiple times on a website and it would be a pain for us to add a new class value. We could simply use an attribute selector to style the button with the value of “Click Me!”.

input.button {
     padding:3px;
     background:#3b3b3b url(images/button.png) repeat-x;
     width:50px;
     height:25px;
     color:#ccc;
}
input.button [value=Click Me] {
     background:#4c4c4c url(images/new-button.png) repeat-x;
     color:#999;
}

We have our class called “button” which is styled a certain way. Assuming it was a pain for us to create a new class, we can have the attribute selector get the exact value of “Click Me” and then style it differently.

[attrib*=value]

This one is a little bit more strange. The “*” in this situation kind of acts as a wildcard. This “*=” will search for the the following value anywhere in the attribute value. For example:

<h3 rel="foobarfoo">Text here</h3>

Using this attribute selector we could do the following:

h3 [rel*=bar] {
     text-shadow:1px 1px 1px #000;
     color:#fff1d8;
}

This will search the attribute for the word bar and add the styles you want to it. We can also look at this from a different angle:

<div class="item1">Content here</div>
<div class="item_2">Content here</div>
<div class="item-3">Content here</div>

This would be considered poor coding on any level. But let us assume there was a situation like this and you wanted to create a style that would apply to all three of these class selectors. Instead of editing each one individually, we could do the following:

div[class*=post] {font-family:monospace;}

[attrib^=value]

This allows us to choose an attribute based on how it starts. If we have several attributes that start with the same string of text with multiple id and class styles added to the elements already, this can make our life easier.

<div class="foo bar" rel="hello-world">Content Here</div>
<div id="world" class="foo">
     <ul class="bar" rel="hello-world">
          <li>Item</li>
          <li>Item</li>
          <li>Item</li>
          <li rel="hello-world">Item</li>
     <ul>

With a little CSS…

*[rel^=hello-world] {font-style:italic;}

Note: The * in this instance is a wildcard which will automatically put whatever element the attribute and value applies to.

Here is an alternative example:

<a href="http://www.atomicpages.net/blog/2010/05/04/css3-opacity/">CSS3 Opacity</a>
<a href="http://www.atomicpages.net/blog/2010/04/09/mmf2-beginning-video-tutorial-series/">MMF2 Tutorial Series</a>
<a href="http://www.w3.org/standards/xml/core">W3C XML Info</a>
<a href="http://www.whatwg.org/">WHATWG</a>
a[href^=http://www.aomicpages.net/] {color:#ff9300;}

[attrib$=value]

This will search for the value but at the end of the string.

<div class="foo bar">
<!--content here-->
</div>
<div class="bar"></div>
<div class="foo"></div>
div[class$=bar] {text-decoration:underline;}

[attrib~=value]

A typical HTML element that has multiple classes styling it would look like the following:

<div class="foo bar"></div>

Notice that the values are separated by a space. What if we had a different attribute that had multiple values that were separated by a space? Could we use *=? Probably, but the issue with *= is that it can be too picky since this does not require spaces. For example,

<div class="red" rel="foo bar"></div>
div.red {color:red;}
div[rel~=foo] {color:#470b0b;}

Using ~= would require the attribute to be separated by spaces if there were multiple values.

[attrib|=value]

This attribute selector allows us to styles attribute values separated by a dash. This is an alternative to using *=. See the following code:

<div class="blue" rel="bar-foo-bar"></div>
div.blue {background-color:blue;}
div[rel|=bar] {background-color:rgba(50, 50, 50, 0.6);}

Multiple Attribute Selectors

We can use multiple attribute selectors if we want. Consider the following code:

<div title="AtomicPages Blog" rel="example-link" class="blog img">&nbsp;</div>
div [title=AtomicPages Blog][rel|=link] {color:#ccc;}

Is my browser supported?

The only browser that isn’t supported it Internet Explorer 6.

Basics of CSS – Part #10

Today we’re going to talk about tables and how they can look significantly better when we add a little bit of CSS to them. If you need a quick refresher on tables, please click here.

Styling Tables

As mentioned above, styling tables is very useful and can instantly make a webpage come to life. The HTML default table is like the following:

Item 1 Item 2
Item 1 Item 2
Item 1 Item 2

Pretty boring if you ask me. Notice how the table has a background of transparent so it will take the color of any background element. The table has that border by default and it definitely could use some tweaking to make it look better.

We’re going to use some key properties:

  • border-collapse
  • border

The border property allows us to style all four corners of a border at once, or each side individually. There are no arbitrary shapes with borders. All elements have a clearly defined left, right, bottom, and top side to the element.

border property

Notice how the square has an irregular shape and doesn’t appear to be a conventional square. We would call this a diamond shape or something along those lines. It’s actually a perfect square that was rotated 45° on a canvas. Notice how the diamond has a background since it is an image and that background has four sides: top, right, bottom, and left.

To see the border tutorial click here

Now that we have a firm understanding and we have refreshed our minds on the border property, we can fully style our tables!

If we add a little bit of CSS and tweak the table to our liking then it could very well look like this:

Item 1 Item 2
Item 1 Item 2
Item 1 Item 2

Although this dives into the world of pseudo-classes, which is discussed in the advanced css tutorial series, we know what our tables can potentially look like with a little bit of work.

Styling

Consider the following code:

table, td {border:1px solid #ff9300;}

This code would add a 1px border that is solid with the color #ff9300 (AtomicPages Orange) around the table and td (table cell) elements. The reason why we chose these elements is because the tr (table row) element does not have a border around it be default. (Usually applying a border color to a table would only mean applying it to the table element only and not the td element.)

This code will outline the table and td elements with a border that is 1px in width, that is solid and the color #ff9300. This can be useful if you want to quickly style a table quickly and easily.

Item 1 Item 2
Item 1 Item 2
Item 1 Item 2

border-collapse

The border-collapse property is a very useful property that collapses the default double border into a single border. For example:

table {
     border-collapse:collapse;
     border:1px solid #ff9300;
}
Item 1 Item 2
Item 1 Item 2
Item 1 Item 2

Notice how the border collapse property instantly gives the table a sharper and less “busy” look. The default border around the table and td elements ceases to exist and we are given a sharper look.

With a little padding (for spacing) for the td elements we can easily space out the table like the following:

th, td {padding:3px;}
/*use as an alternative for cellpadding*/
Item 1 Item 2
Item 1 Item 2
Item 1 Item 2

Using that same principle, we can apply background images and colors to the table, tr, th, and/or the td elements.

For example:

table {
     border-collapse:collapse;
     border:1px solid #ccc;
     background-color:#dbdbdb;
}
Item 1 Item 2
Item 1 Item 2
Item 1 Item 2

We’ve seen how tables can look and we know what they look like by default — boring. Styling tables can be a great addition to any website whether it is made from tables itself or from a tableless markup using CSS.

Styling Tables
Styling Tables
Size: 8.36 kB

Real Hosting

Real hosting is something much more than what meets the eye. It is more than a place to merely store files and have a space on the Internet. It’s more than just unlimited this and unlimited that; it involves real space, real deals, real support, and real guarantees.

What you REALLY pay for

Ever heard of the Myth of Unlimited Hosting? Aside from being technically impossible, it is highly unlikely that a web hosting company will charge you $4.95 a month if you agree to stay for 60 months. This includes unlimited disk space, a free domain name, unlimited bandwidth, unlimited databases, unlimited FTP accounts, unlimited emails, and unlimited everything else.

If you happen to break the 60 month agreement you are slapped with a termination fee and other hidden fees. This can quickly become a major hassle, especially when you receive poor support and service when you just want out of a bad hosting situation.

Often, in these situations, the hosting companies will begin to nag at you when too much space is taken up on a shared server, you’re taking up too much CPU, or taking up too much RAM on the server. This renders the promise of unlimited features as invalid and not a real guarantee and not real, honest hosting.

These types of hosting companies will go into your account and begin changing things if you have too many files on the server, if the files are taking up too much space, or too many “unrelated files”. They will disable the website and then it becomes a hassle to get your website back online. While they’re breaking promises, you’re losing traffic, clients, and/or money. Whether it is a business website or a personal blog, it is embarrassing when you have to explain to clients and friends why your website is down in the first place.

While you’re banging your head against a wall wondering why your website was suspended, they will be helping other people and not replying to your support tickets, emails, or phone calls. Some hosts will treat you like a second-class citizen if your account is suspended.

What you get with real hosting from AtomicPages

AtomicPages Real Hosting is different; we do not make promises we cannot keep. We will never claim to have unlimited features and go back on our word later on. The amount of features you pay for is the amount of features you will actually receive and nothing less; no questions asked.

This is what defines real hosting from impostors. We are honest, backup our guarantees, and have real morals. You will never hear that we are suspending your account due to too much space, taking up too many resources on the server, or any other made-up excuse. We will not impose upon your website unless it is breaking the law. AtomicPages real hosting will never leave you in the dark about your website.

Helping people with websites and delivering good quality hosting while keeping a promise is not all smoke and mirrors. There is no mystery to keeping a promise.

CSS3 Opacity

The Opacity Property is slightly different then RGBA and HSLA which was discussed in CSS3 Colors.

Opacity v. RGBA & HSLA

Indeed RGBA has the addition of the “alpha” or opacity value like so:

background-color:rgba(127, 60, 87, 0.4);
background-color:hsla(68, 100%, 50%, 0.8);

Either of these examples will have a color that is opaque in some fashion. Consider the following:

If you’re using IE see below:

background-color:rgba(0, 0, 255, 1);
background-color:rgba(0, 0, 255, 0.8);
background-color:rgba(0, 0, 255, 0.6);
background-color:rgba(0, 0, 255, 0.4);
background-color:rgba(0, 0, 255, 0.2);

This code using rgba allows us to create our desired effect.

Allows us to use these opaque colors, however, we can reach the same effect using the standard rgb color space or by using hexadecimal values.

Consider the following example:

We have the same effect using slightly different code in fact. We used the opacity property to create the fading squares.

background-color:rgb(0, 0, 255); opacity:1.0;
background-color:rgb(0, 0, 255); opacity:0.8;
background-color:rgb(0, 0, 255); opacity:0.6;
background-color:rgb(0, 0, 255); opacity:0.4;
background-color:rgb(0, 0, 255); opacity:0.2;

Main difference

The main difference between rgba + hsla and opacity is that we can apply opacity to any element we want instead of just backgrounds. This includes, but is not limited to, images and text.

This is some more text

As we can see, the text is fading with word. The first word is at 100% opacity, the second at 80%, third at 60%, etc… We denote these values in terms of decimals so 1 = 100%, 0.8 = 80%, etc…

Opacity

One really great thing about the opacity property is that you don’t need to create an image sprite if you want an image to be slightly translucent before roll over and fully opaque during roll over. Here is an example of opacity:

Chrome

.chrome-opacity {
  background:url (images/chrome.png) center center no-repeat;
  height:64px;
  width:64px;
  opacity:0.8;
}
.chrome-opacity:hover {
  background:url (images/chrome.png) center center no-repeat;
  height:64px;
  width:64px;
  opacity:1.0;
}

Using method above allows us to easily create semi-opaque images or text and allow them to become fully opaque on roll over or vice versa.

Note: IE 6 only supports the :hover pseudo-class on <a> tags only. Also, IE does not natively support this property where all of the other major browsers do.

To use this property with IE we’ll need to create a conditional comment for all versions of IE like the following:

<!--[if IE]>
   <style type="text/css">
        .chrome {filter:alpha(opacity=80);}
	.chrome:hover {filter:alpha(opacity=100);}
   </style>
<![endif]-->

Note: that IE 6 and 7 do NOT extend support to the pseudo-class :hover on anything but <a> tags and therefore the hover will not work.

If you want to see working examples of the opacity property then please download the file!

opacity.zip
opacity.zip
Size: 170.97 KB

CSS3 Colors

You might be wondering what is so special about CSS3 colors and why they’re even different considering that the color and background-color property and their values have not changed since CSS1 specification were released.

As set by CSS1 we can use the following for the color and background-color properties:

Color Background-color
  • color:[colorname];
  • color:rgb([value 1],[value 2],[value 3])
  • color:#[hex value]
  • background-color:[colorname];
  • background-color:rgb([value 1],[value 2],[value 3])
  • background-color:#[hex value];
  • color:orange;
  • color:rgb(255, 255, 125);
  • color:#ff9300;
  • background-color:black;
  • background-color:rgb(124, 56, 87);
  • background-color:#470b0b;

Aside from these standard mundane implementations of colors, CSS3 added a whole new color space: HSL.

HSL

HSL stands for Hue Satuation and Lightness. The break down is as follows:

Hue Satuation Lightness
Hue is the degree of a color wheel that goes from 0 to 360. Here as some general colors:

  • 360 is red
  • 120 is green
  • 240 is blue
Satuation is the amount of color, or how “full” the color is. Here are some values:

  • 100% represents full color
  • 75% represents 3/4 color
  • 50% represents 1/2 color
Lightness is also a percentage but deals with how much white and black is added to the color. Here are some examples:

  • 100% means 100% white is added which means the color will be white
  • 50% means 50% of white and 50% of black is added so they basically balance each other out
  • 0% means no white is added or 100% black which means the color is dwarfed by pure black.

Here is an example of HSL:

Here is an example with RGB:

Here is an example with HEX:

The corresponding CSS for HSL colors is the following:

background-color:hsl ([0-360], [x]%,[x]%);
background-color:hsl (360, 100% 50%);
background-color:hsl (133,85%, 61%);

RGBA

There is nothing new about the RGB color space. We learned about the RGB color space in Basics of CSS Part 2 and we have seen many colors in the RGB color space and in hex format Color Reference.

Common examples of RGB would be the following:

background-color:rgb(255, 0, 0); /*red*/
background-color:rgb(0, 255, 0); /*green*/
background-color:rgb(0, 0, 255); /*blue*/

So what does the “a” mean in RGBA? The A stands for alpha which deals with transparency or the opacity of colors. The syntax for the alpha value will span from 0-1 in decimal form. For example:

background-color:rgba(0, 0, 255, 0.4);
color:rgba(0, 0, 255, 0.8);

The first example will set the opacity of pure blue to 40% and the second example will set the opacity of pure blue to 80%.

Alternatively, the RGB colors will be the following:

RGBA RGB
background-color:rgba([color 1], [color 2], [color 3], [alpha]);

background-color:rgba(0, 0, 255, 1);
background-color:rgba(0, 0, 255, 0.8);
background-color:rgba(0, 0, 255, 0.6);
background-color:rgba(0, 0, 255, 0.4);
background-color:rgba(0, 0, 255, 0.2);
background-color:rgb([color 1], [color 2], [color 3]);

background-color:rgb(0, 0, 255);
background-color:rgb(50, 50, 254);
background-color:rgb(101, 101, 254);
background-color:rgb(152, 152, 254);
background-color:rgb(203, 203, 254);
These colors are not contingent on the color of the background. Since these colors have alpha transparency, they will show any colors or background images behind the elements. These colors are contingent on the color of the background. If you wish for the element to be a certain color then you need to specify the color you wish to have. Will not show any color behind the element and will not show any background images behind the element because no alpha transparency exists.

For example

Notice how above they were the same colors and now since we have a different color background the colors with rgba will allow the black background to show through where as the rgb colors will display their respective color values.

Which Browsers Support RGBA?

  • Firefox 3.x+
  • Safari 3+
  • Safari 4+
  • Google Chrome
  • Opera 10.10+
  • Flock 2.02
  • Mobile Safari
  • Blackberry Browser

IE Hack

Mission someone? Of course we are! We’re missing lovely Internet Explorer and all of its versions. Luckly, there is a hack for IE.

<!--[if IE]>
   <style type="text/css">
   selector {
       background:transparent;
       filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#990000FF,endColorstr=#990000FF);
    }
    </style>
<![endif]-->

We can use this hack as inline css or use in an internal or external style sheet. If you want a specific class selector or element to have an translucent color then use the hack for that element or class like the example above. If you want to use inline css then place the hack in the style attribute.

You might be wondering what #800000FF is. Think of this as #AARRGGBB where AA is the alpha value in two integers, RR are the red hexadecimals, GG are the green hexadecimals, and BB are the blue hexadecimals.

In order to covert our initial RGBA value to Hex we need to think interms of Hex to Decimal values.

Hex to Decimal

In order to convert Hex to Decimal we need to think mathematically. We’ll start with an easy example:

Hex Decimal
#ffffff rgb(255, 255, 255)
#0000ff rgb(0, 0, 255)
#ff00ff rgb(255, 0, 255)

How did we get these values of the top of our head? The solution is simple. Follow these steps from Jackson Hines’s post on RGB to HEX and vice versa:

HEX to RGB (Ex: #a84fff)

  1. Take the first digit of the red part of the hex color. (ex: a)
  2. If this digit is a letter, replace with the letter of the alphabet and add 9. If it is not a letter, leave it. (ex: a = 1; 1 + 9 = 10)
  3. Take the second digit of the red part of the hex color. (ex: 8 )
  4. If this digit is a letter, replace with the letter of the alphabet and add 9. If it is not a letter, leave it. (ex: 8 )
  5. Divide this number by 16. (ex: 8 ÷ 16 = 0.5)
  6. Add the number you got in step two and the number you got in step 5. (ex: 10 + 0.5 = 10.5)
  7. Multiply this number by 16 to get the red value. (ex: 10.5 × 16 = 168)
  8. Repeat steps 1 through 8 with green and blue values. (ex: 168, 79, 255))

Alpha (#AA) & A

So what about alpha? Knowing the information we have obtained by Jackson’s helpful article, we can convert our rgb value to hex and vice versa. If you wish not to do so much math and want to get results quickly, I recommend using Colorpicker.com.

#AARRGGBB to RGBA

Let us assume #660000FF is our #AARRGGBB color of choice. Convert #0000FF to RGB which will be rgb(0, 0, 255) and now time to convert #AA.

Following HEX to RGB conversion of #66:

  1. The first number is 6
  2. The second number is 6 ÷ 16 = 0.375
  3. The third number is the (first number + the second number) × 16 = 102
  4. 102 ÷ 255 = 0.4 for RGBA

If you don’t want to that much math then you can use this conversion tool to convert any TWO hex characters to decimal or any series of Decimal characters. Once you have your decimal number, divide by 255 and you will have your RGBA number.

HSLA

Using the same principles as RGBA and HSL, just add the A to the end of the HSL values. For example

background-color:hsla ([0-360], [x]%, [x]%, [0-1]);
background-color:hsla (133, 85%, 61%, 0.6);

For more information an examples on the IE Hack for RGBA and HSLA, please download css3-colors.zip and open in IE.

css3-colors.zip
css3-colors.zip
Size: 1.71 KB