What Are Some Adult Toys For Children?

 

Have you ever been in a situation where you couldn’t bring yourself to get out of the house and play with your favourite adult toys? You wanted to play and enjoy, but you just couldn’t stop yourself. You wanted to go and see a movie with your friends, but you had the urge to stay at home with your favourite adult toys.

The good news is that adult toys have grown significantly in recent years. There are a ton of choices for you and your kids if you’re going to be purchasing toys for your kids.

There are new games to play, new educational materials to use, and more options to choose from than ever before. If you want to get a jump start on the fun, there are many toys available that will help you get started. Here are some of them.

The biggest problem is that it’s tough to get kids involved with this kind of toy. Kids don’t like to make new things, and they don’t really enjoy learning new skills. This kind of toy can be fun and educational, but it’s not really a good option for kids who want to try something different.

There are toys out there that can actually help your kid’s imagination. This way, you can stimulate their creativity and make them think more rationally.

Toys such as this can teach your kid about colors, shapes, and the world of animals. They’ll learn the difference between a dog and a cat, what an elephant’s trunk looks like, and more. You can even get a pet monkey to help your child learn about all of this stuff. There are so many exciting things to learn about when they have a toy like this around.

When you’re playing with a pet turtle, you’ll be able to communicate a lot more with him than you could with a dog or cat. They have emotions, just like humans do, and they know when you want to pet them or not.

There are plenty of toys out there for both boys and girls. There are all kinds of cute little ones to choose from, whether you want to play with your little princess or something more rugged like a jungle cat or a horse. They’re definitely a great way to entertain your child.

If you’re looking for something that your child might not want to give up entirely, there are still a few options available. You can get a few small bottles of milk or a little hand blender and make milkshakes for your child. These are great because they will keep your kid entertained long enough to get them interested in learning more about cooking.

There are also some toys out there that can allow your child’s imagination to run wild. This means you can teach them everything from construction to sports. math.

You can use all kinds of creative playthings to teach your child the alphabet and numbers. Even if your child is old enough to do it on their own, you can still find ways to teach them how to read. and count.

If you and your child are both interested in toys that your kids can use with other children, you can find tons of toys that will let them bond with other kids. This can be a great way to create a sense of family among the whole family.

The great thing about playing with toys like this is that you can do it any time of the day or night. It doesn’t have to be expensive to have a blast playing with them.

Furniture Stores – Get The Best Selection And Discount

 

Visit New Zealand for a shopping spree and make your purchase at Furniture Stores Auckland. Visit this online furniture store and pick up great bargains on all sorts of unique and contemporary furniture items for your homes and offices.

Meet Cheap and Discount Furniture Stores and Shops in New Zealand. Get a different style of furniture at a discount rate at cheap furniture stores in Auckland, New Zealand. These stores offer various kinds of furniture including contemporary furniture, modern furniture, traditional furniture, children’s furniture, etc, so whatever type of furniture you are looking for, these stores have a great variety.

Visit these furniture stores in New Zealand and pick up unique furniture pieces that will add a new touch to your house. These furniture stores offer different varieties of contemporary furniture, such as coffee tables, chests, bookcases, mirrors, furniture for bedroom, dining room, living rooms, etc. These stores offer different kinds of contemporary furniture. From antique and traditional furniture to modern furniture, you can find them at the local stores.

Furniture stores offer a variety of furniture in different style and price. You will find a different kind of furniture like living room furniture, bedroom furniture, dining room furniture, etc. If you are planning to decorate your home or office with a different kind of furniture, you should try to visit different stores. The more you shop, the more you would get discounts and discount prices. So, visiting the stores will give you a chance to get discounts on the different types of furniture items.

The other great thing about visiting these furniture stores is that you can buy your items online too. This would help you save lots of money. Online stores offer different kinds of discounted and cheap products to attract more people to buy it. The online stores also offer special discounts to their customers if they use their online checkout facility.

The other great thing about shopping online is that you would get to save lots of money. It is true that these online stores offer lower rates than brick and mortar stores but it is worth shopping on the internet because it gives you convenience and comfort. Shopping through the internet saves your time, effort and energy.

There are many other cheap furniture stores in New Zealand. Some of them have a wide variety of different kinds of contemporary and antique furniture as well. If you are looking for traditional furniture, then you can look for the country furniture in New Zealand. The furniture in New Zealand is made from the finest materials and has a touch of culture. You could also shop for unique items and collect some old items for your own collection.

Visit these cheap furniture stores and pick up great deals on all kinds of modern and unique furniture for your homes. It will make you feel satisfied and happy when you are at home or in the office because you would get a comfortable feeling and feel relaxed and contented when you buy furniture at discount and cheap price.

The best part about getting good furniture at discount and cheap price is that you can have it delivered to your home or office. It is easy to do as there are many online furniture shops where you could choose the type of furniture that you want and where you could get it delivered right at your doorstep. With this, you can have your new furniture at your doorstep within no time. so, if you are in Australia and want to have nice furniture for your home, now is the time to order them and get them delivered to you.

There are also discount furniture stores that sell used and new furniture and they provide you with an opportunity to get some of the best quality furniture at the lowest rate. on earth.

When you are in Australia, you will have lots of options to buy furniture for your home, whether it is antique furniture or contemporary furniture. Australian furniture is always in demand. The best thing about the furniture industry is that you can purchase the best quality of furniture from different places across the globe at low rates.

The stores provide you with a lot of space to shop and compare the different types of furniture and you would get better and more value for your money if you shop them from different stores. These stores also provide you with the advantage of knowing the different brands of furniture that are available and the price range and discounts available on different types of furniture.

Finding SEO Services in Brisbane

 

There are many SEO services in Brisbane and their services are as varied as the cities themselves. Some of the areas that provide SEO services are:

Many service providers of SEO services have their head office in other areas like Sydney or Melbourne, but Brisbane is the home of a number of companies providing SEO services. The competition between these companies is stiff, so you need to be sure that you find the company that will work best for your business. However, the most important thing is to find the right company for your business.

Every website has a website address that is associated with the address of the owners of the search engines that crawl the Internet and index the information. This link, called a link, is based on a particular search engine that a site is affiliated with. This link is referred to as a search engine result, and when someone enters this search into a search engine, it results in a page that lists all websites that are associated with that search.

If you are a local business that does not have the time to build and maintain your own website, there are many services that can help you out with the process. SEO services are the backbone of Internet marketing, and they can make your website rank higher in the search engines. The most successful sites have the best SEO tools and techniques in place.

One SEO company in Brisbane that offers their services is World SEO. World SEO specializes in building websites and maintaining them. This includes writing and marketing, website design, and a variety of other areas. It is their service that allows them to get more traffic, which can then translate into increased sales and profits.

The online world has changed quite a bit over the years since search engine optimization was introduced, so there are some differences between webmasters and SEO services today. It is best to consult a professional if you are going to be adding a lot of content to your website. You do not want to miss a moment to add content, as this could be the difference between getting the link back up and not having it at all.

Some SEO services require the person doing the search engine optimization to add the information and then remove it as needed. This can cause a delay as it takes time to edit and maintain your website. When you hire an SEO company, they will know exactly what they are doing and they can get the job done quickly. They will also know how to keep your content fresh and relevant as the need arises.

You will have to pay a company to provide SEO services in Brisbane, but there are some very affordable services that you can choose from. You do not have to go through a search engine optimization firm to get the results you are looking for. There are many other solutions available for a company that needs to optimize their site.

When you are trying to decide which company to use, take the time to do a price comparison. You should also check with the company to see if you are paying for a part-time employee or if the cost of the SEO services is already covered by your current payroll. You do not want to waste your money on SEO services that you may not need later on.

Another reason you may choose to use an SEO company that is not located in Brisbane is that you do not want to spend your money on shipping and handling costs. Search engine optimization is a professional service and the fees should reflect that. This means that if you need the services sooner than you plan, you can move forward without having to worry about a huge bill coming from a company.

There are many different providers of SEO services in Brisbane. You should make sure that you get your SEO work done from the best company you can find. You will also want to make sure that the firm you choose has a track record of quality service.

If you need search engine optimization, Brisbane is the place to go. Make sure that you choose a company that will take care of your business. and give you results that you can be proud of.

CSS Gradients are about to Graduate!

Finally all browser vendors are on the same page when it comes to gradients

The Webkit team introduced CSS gradients on April 14th, 2008. Shortly afterwards they presented it to the W3C for consideration as a recommendation. Sometime later, Mozilla decided to implement CSS gradients using the same notation the Webkit gradients, but with the -moz prefix. After some time, however, they decided to replace that notation with a simpler one. Ever since, to do CSS gradients on the Web you had to write gradients the Webkit way for Safari and Chrome, and the Mozilla way for Firefox. Well, now all browser vendors have agreed to implement the same spec. At this time you can write a single notation that is identical for Safari, Chrome, Firefox, Opera and IE10.

Since version 5.1, Safari has supported the newer notation. Opera beta running Presto 2.10.299 fully supports the linear and radial notation, although the developer site only mentions support for the linear notation. Firefox, of course, already supports this notation. And the latest beta of IE10 does as well. This means that at some point all browsers will support this notation. Well, sort of.

Recently somebody at the W3C has been thinking a little too much. As of January 2012 the document CSS Image Values and Replaced Content Module Level 3 now has a slightly different notation. Since this just showed up, there is no support for it in any browser. And, since it is a working draft, it could change at any time. So, at this time, I’m going to ignore what the document says and just cover what the latest browsers have agreed to support.

Because CSS gradients are still a draft proposal, all browser vendors use their respective prefixes with the notation. This is a good thing since the spec is in flux. They shouldn’t change their present implementation until the spec reaches recommendation status, in which case they can implement the final version of CSS gradients without their vendor prefixes.

Webkit: -webkit-
Mozilla: -moz-
Opera: -o-
Microsoft: -ms-

CSS gradients are rendered as a background image. That means that all of the properties of the CSS background module apply to CSS gradients. In other words, you can have multiple gradients on the same element and control their size and position as well, or use them as image masks. Also, whereas in the past you might have used a background color in conjunction with a background image, you can now use a background gradients as the backdrop for a background image, or you could have a transparent gradient layered over a background image. The ability to stack up gradients while controlling their size, position and repetition means you can create complex repeating patterns that would normally require a background image. The ability to define multiple background images results in a layered affect not unlike Adobe Photoshop’s layers. The new CSS filters module will increase the comparison with Photoshop further my providing many color filters, such as reverse, multiply, dodge, burn, darken, lighten, grayscale, etc. With JavaScript you could also manipulate the properties of your CSS gradients to produce a background pattern that morphs in real time.

Linear Gradients

The notation for linear gradients is quite simple and straightforward. First thing, you’ll be defining it as a background image on an element. CSS uses functions that create the gradient according to the arguments you provide. For linear gradients, the values you’ll need are as follows:

1

[<point (one value, two values or degrees)>]?, <color [stop]?>, <color [stop]?>

From the above definition we can see that all values are optional, except the start and end colors.

The first value (point) is optional. If no value is provided, it will default to drawing the gradient from top to bottom. You can use keywords for the position. These can be a single keyword or a combination of two. You can also use any valid degree values. The purpose of these point definitions is to define which direction the gradient flow towards. You also need to provide at least two colors. The first color will be where the gradient starts from, and the last color will be where the point indicates. So a gradient with yellow and blue and a point value of top will start at the bottom with yellow and end at the top with blue. Here re possible point values:

– top
– right
– bottom
– left
– top left
– top right
– bottom left
– bottom right
– 0deg
– 11deg
– 67deg
– 182deg
– -45deg
– -90deg

Colors can have optional stop values. If no stops are provided, the colors will blend equally from the first to the last.You can provide as many colors as you want to create the gradient, and you can vary their widths by providing stops. The value of the stops can use any valid CSS length identifier (px, pt, em, %, etc.). The position value is placed after the color. You can use any valid CSS colors, such as keywords (red, yellow, blue), or hex, hsl, rgb or rgba values. Regardless of what the stop position is for the last color, that color will continue filling the element the rest of the way.

1
2
3

background-image: linear-gradient(top, yellow 10px, red 80px);
background-image: linear-gradient(top right, orange, green 30%, yellow 70%);
background-image: linear-gradient(60deg, red, white, blue 50%);
Repeating Linear Gradients

Repeat linear gradients are easy to do. Actually, the same effect can be achieved with background size and background repeat properties, but it is nice to have it I suppose. It works the same way as regular linear gradients, except that you use repeating-linear-gradient as the function:

1
2
3

background-image: repeating-linear-gradient(top, yellow 10px, red 80px);
background-image: repeating-linear-gradient(top right, orange, green 30%, yellow 70%);
background-image: repeating-linear-gradient(60deg, red, white, blue 50%);
Radial Gradients

The notation for radial gradients is similar to linear gradients, except that you can specific a shape argument to control how the radial is produced. Here’s the possible values:

1

[<position (two separate values)>]?[[<shape (literal value or length value)>] || [<size length value>]>]? <color [stop]?> <color [stop]?>

From the above definition we can see that all values are optional, except the start and end colors, just like linear gradients.

If no value for position is supplied, the radial gradient will be centered in the element. Position is based on the center of the radial gradient. It accepts the same values as background position. Those values include percentages, lengths and keywords. Just like background positioning, you can use negative values to move the center of the radial gradient off the screen. Percentages and lengths require two values, the first defining the x axis and the second defining the y axis.

1
2

percentage: 0% 0%, 20% 60%, 50% 50%
length: 0px 0px, 24px 45px, 3em 5em

The keywords are: top, right, bottom, left and center. You could combine these in various ways:

1

top left, bottom right, center center, center right, top center

Shape can be either circle or ellipse. If no shape is supplied, if the length value is single, it defaults to a circle with a radius defined by that length. Otherwise, if no shape is supplied and two lengths are passed, it uses those to draw an ellipse. The first value determines the width, the second the height:

1
2
3
4

// A vertical ellipse:
background-image: radial-gradient(center, 100px 400px, red, white);
// An horizontal ellipse:
background-image: radial-gradient(center, 400px 100px, red, white);

If you use one of the shape keywords, you can also use size keywords to control how the radial gradient is draw in relation to its container. The size keywords are:

– closest-side
– farthest-side
– closest-corner
– farthest-corner

First of all, these keywords only work when you use a shape keyword. Defining the shape of the radial gradient with lengths and then trying to use these size keywords will not work. So, this is how these size keywords work. If the container is narrower than it is tall, the size of circle or ellipse will be restrained by the left and right of the container. With farthest-side, it would be restrained by the top and bottom of the container, probably resulting in the left and right extending beyond those sides. Be aware that these work in relation to the position of the radial gradient. That means that if it is positioned near the top left, it will be rendered differently than if it were centered. Closest-corner and farthest-corner work just like closes-side and farthest-side. The best way to get the hang of these keywords is to play around with a radial gradient to see how they affect it. Although these keywords are nice conveniences, you’ll see that they are far from exact. If you need precise placement and size, you’re better off using lengths to define the size of your radial gradient.

Color and stops work similar to linear gradients. The first color defined the color drawn from the center of the radial gradient. The last color will define the outermost color of the gradient. Stops allow you to define more precise positioning of those colors in the radial gradient.

Repeating Radial Gradients

Like repeating linear gradients, it is possible to create the same effect as a repeating radial gradient. You’d just need to define it tediously repeating the colors and providing appropriate stops. In contrast, using the repeating-radial-gradient function makes this easy.

Practical Examples

Enough with theory. Let’s look at what we can do with linear and radial gradients. By layering them and controlling their size and positioning, we can create complex patterns that we can use as Web assets. Please note that in all of the following examples I’m presenting the gradients without vendor prefixes. To have them render in a browser that currently supports CSS gradients, you would need to provide the appropriate vendor prefix for that browser. When the current spec reaches recommendation status, browser vendors will drop their prefix.

In most of these examples I’ve used a transparent gray. This was so I could just add an background color to show through the transparency. Having the color controlled by the background color makes it easy to change the feel of the pattern.

Here’s a vertical stripe with the transparent gray stripe is separated by a 1 pixel transparent stripe:

1
2
3
4
5

background-image:
linear-gradient(0deg,
rgba(0,0,0,0.1) 75%,
transparent 75%);
background-size: 5px 100%;

 

Here’s the same stripe horizontal:

1
2
3
4
5

background-image:
linear-gradient(90deg,
rgba(0,0,0,0.1) 75%,
transparent 75%);
background-size: 100% 5px;

 

And here it is slanted to the left:

1
2
3
4
5
6
7
8
9

background-image:
linear-gradient(45deg,
transparent 15%,
rgba(0,0,0,0.1) 15%,
rgba(0,0,0,0.1) 50%,
transparent 50%,
transparent 65%,
rgba(0,0,0,0.1) 69%);
background-size: 6px 6px;

 

And then to the right:

1
2
3
4
5
6
7
8
9

background-image:
linear-gradient(-45deg,
transparent 15%,
rgba(0,0,0,0.1) 15%,
rgba(0,0,0,0.1) 50%,
transparent 50%,
transparent 65%,
rgba(0,0,0,0.1) 65%);
background-size: 6px 6px;

 

Now lets have some fun with repeating linear gradients. Here’s vertical repeating stripes:

1
2
3
4
5
6

background-image:
repeating-linear-gradient(0deg,
rgba(0,0,0,0.1) 0px,
rgba(0,0,0,0.1) 5px,
transparent 5px,
transparent 10px);

 

Horizontal repeating stripes:

1
2
3
4
5
6

background-image:
repeating-linear-gradient(90deg,
rgba(0,0,0,0.1) 0px,
rgba(0,0,0,0.1) 5px,
transparent 5px,
transparent 10px);

 

Left slanted repeating stripes:

1
2
3
4
5
6

background-image:
repeating-linear-gradient(45deg,
rgba(0,0,0,0.1) 0px,
rgba(0,0,0,0.1) 5px,
transparent 5px,
transparent 10px);

 

Right slanted repeating stripes:

1
2
3
4
5
6

background-image:
repeating-linear-gradient(-45deg,
transparent,
transparent 5px,
rgba(0, 0, 0, 0.1) 5px,
rgba(0, 0, 0, 0.1) 10px);

 

Now lets cross them:

1
2
3
4
5
6
7
8
9
10
11

background-image:
repeating-linear-gradient(0deg,
transparent 0,
transparent 5px,
rgba(0, 0, 0, .1) 5px,
rgba(0, 0, 0, .1) 10px),
repeating-linear-gradient(90deg,
transparent 0,
transparent 5px,
rgba(0, 0, 0, .1) 5px,
rgba(0, 0, 0, .1) 10px);

 

And now criss-cross them:

1
2
3
4
5
6
7
8
9
10
11

background-image:
repeating-linear-gradient(-45deg,
transparent,
transparent 5px,
rgba(0, 0, 0, 0.1) 5px,
rgba(0, 0, 0, 0.1) 10px),
repeating-linear-gradient(45deg,
transparent,
transparent 5px,
rgba(0, 0, 0, 0.1) 5px,
rgba(0, 0, 0, 0.1) 10px);

 

Enough with stripes, let’s get square:

1
2
3
4
5
6
7
8

background-image:
linear-gradient(180deg,
rgba(0,0,0,0.05) 75%,
transparent 75%),
linear-gradient(90deg,
rgba(0,0,0,0.05) 75%,
transparent 75%);
background-size: 5px 100%, 100% 5px;

 

And a nice checkerboard pattern:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

background-image:
linear-gradient(45deg,
rgba(0,0,0,0.1) 4.5px,
transparent 5px,
transparent 16px,
rgba(0,0,0,0.1) 16px,
rgba(0,0,0,0.1)),
linear-gradient(45deg,
rgba(0,0,0,0.1) 4.5px,
transparent 5px,
transparent 16px,
rgba(0,0,0,0.1) 16px,
rgba(0,0,0,0.1));
background-position: 0 0, 8px 8px;

 

And let’s make those diagonal:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

background-image:
linear-gradient(45deg,
rgba(0,0,0,0.1) 25%,
transparent 25%),
linear-gradient(-45deg,
rgba(0,0,0,0.1) 25%,
transparent 25%),
linear-gradient(45deg,
transparent 75%,
rgba(0,0,0,0.1) 75%),
linear-gradient(-45deg,
transparent 75%,
rgba(0,0,0,0.1) 75%);
background-size: 10px 10px;

 

Now that we have them diagonal, lets spice them up and turn them into an argyle pattern:

1
2
3
4
5
6
7
8
9
10
11
12

background-image:
linear-gradient(right top,
rgba(0,0,0,0.1),
rgba(0,0,0,0.1) 50%,
transparent 50%,
transparent),
linear-gradient(left top,
rgba(0,0,0,0.1),
rgba(0,0,0,0.1) 50%,
transparent 50%,
transparent);
background-size: 12px 12px;

 

OK, now lets take another look at stripes. But this time we want to shake things up about. We want to get out to the monotony of strictly repeating patterns, but still have a pattern. What I’m talking about is sometimes called the Cicada principle. It produces a pattern that doesn’t repeat itself exactly. It does this my layering different repeating patterns, except that these are different proportions, similar to the progression of prime numbers. Using this principle, I’ve created a series of overlapping lines that creates a textile like pattern that retains it’s randomness. Here’s the CSS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

background-image:
linear-gradient(left,
rgba(0,0,0,0.1),
rgba(0,0,0,0.1) 25%,
transparent 25%,
transparent),
linear-gradient(left,
rgba(0,0,0,0.1),
rgba(0,0,0,0.1) 15%,
transparent 15%,
transparent),
linear-gradient(left,
rgba(0,0,0,0.1),
rgba(0,0,0,0.1) 15%,
transparent 15%,
transparent),
linear-gradient(top,
rgba(0,0,0,0.1),
rgba(0,0,0,0.1) 17%,
transparent 17%,
transparent),
linear-gradient(top,
rgba(0,0,0,0.1),
rgba(0,0,0,0.1) 15%,
transparent 15%,
transparent),
linear-gradient(top,
rgba(0,0,0,0.1),
rgba(0,0,0,0.1) 13%,
transparent 13%,
transparent);
background-size: 10px 100%, 17px 100%, 21px 100%, 100% 10px, 100% 17px, 100% 21px;
backgroud-position: 0 0, 6px 0, 14px 0, 0 0, 0 6px, 0 16px;

 

Now lets play with radial gradients. We’ll start with basic patterns and build up to more complex combinations.

1
2
3
4
5
6
7

background-image:
radial-gradient(
rgba(0,0,0,0.1),
rgba(0,0,0,0.1) 45%,
rgba(0,0,0,0) 50%,
rgba(0,0,0,0) 95%);
background-size: 8px 8px;

 

Now we’ll take the above pattern and turn the dots inside out so that the pattern looks like someone cut holes in the color.

1
2
3
4
5
6
7

background-image:
radial-gradient(
rgba(0,0,0,0),
rgba(0,0,0,0) 50%,
rgba(0,0,0,0.1) 55%,
rgba(0,0,0,0.1) 95%);
background-size: 10px 10px;

 

Now we’ll just play with the size of circles a bit.

1
2
3
4
5
6
7

background-image:
radial-gradient(
rgba(0,0,0,0.1),
rgba(0,0,0,0.1) 9px,
rgba(0,0,0,0) 9px,
rgba(0,0,0,0) 17px);
background-size: 18px 18px;

 

OK, I now the above patterns are not revolutionary or anthing. Now we’re going to get creative. For the next ones we’re going to user colors, yeah! The next pattern has a gold background with orange and greenish polka dots:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

background-image:
radial-gradient(
rgba(255,0,0,0.2),
rgba(255,0,0,0.2) 4px,
rgba(255,255,255,.10) 5px,
rgba(255,255,255,.10) 7px),
radial-gradient(
rgba(0,0,0,0.2),
rgba(0,0,0,0.2) 4px,
rgba(255,255,255,.25) 5px,
rgba(255,255,255,.25) 7px);
background-size: 18px 18px;
background-color: #ffd239;
background-position: 10px 10px, 0% 0%;

 

The next one I call puff balls because of their look:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

background-image:
radial-gradient(
rgba(255,255,255,0) 90%,
rgba(255,255,255,.5) 90%,
rgba(255,255,255,.5)
),
radial-gradient(
rgba(255,255,255,0.01),
rgba(255,255,255,0.01) 10%,
rgba(255,255,255,255.1) 70%,
rgba(0,0,0,0) 79%,
rgba(0,0,0,0) 96%);
background-size: 20px 20px;
background-color: #ff77a1;

 

Here’s another gold pattern with four-pointed stars:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

background-image:
radial-gradient(
rgba(255,0,0,0.2),
rgba(255,0,0,0.2) 8.5px,
rgba(255,255,255,.5) 9.5px,
rgba(255,255,255,.5) 17.5px),
radial-gradient(
rgba(0,0,0,0.2),
rgba(0,0,0,0.2) 8.5px,
rgba(255,255,255,.15) 9.5px,
rgba(255,255,255,.15) 17.5px);
background-size: 18px 18px;
background-color: gold;
background-position: 9px 9px, 0% 0%;

 

And here we change the background to green:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

background-image:
radial-gradient(
rgba(255,210,57,0.42),
rgba(255,210,57,0.42) 9px,
rgba(0,0,0,0) 11px,
rgba(0,0,0,0) 15px),
radial-gradient(
rgba(0,0,0,0),
rgba(0,0,0,0) 9px,
rgba(255,210,57,1) 10px,
rgba(255,210,57,1) 15px);
background-size: 17px 17px;
background-color: #1e8e04;
background-position: 8.5px 8.5px, 0% 0%;

 

And finally, we do some interesting overlap with the radial gradients:

1
2
3
4
5
6
7
8
9
10
11
12
13

background-image:
radial-gradient(
rgba(255,255,255,0),
rgba(255,255,255,0.5) 14px,
rgba(255,255,255,0) 14px,
rgba(255,255,255,0) 26px),
radial-gradient(
rgba(0,0,255,0.31),
rgba(0,0,255,0.31) 14px,
rgba(255,255,255,0) 16px);
background-size: 28px 28px, 28px 28px;
background-position: 14px 18px, 0% 18px, 0% 0%;
background-color: rgba(200,250,190,0.5);

 

You can see these gradients in action here. You can same the page to your desktop, since it is a self-contained document.

Proposed Changes to Spec

So, as I mentioned, since January 2012 the draft for CSS gradients at the W3C has some notation changes. Really their minor, but in my opinion, not necessary. Actually, I’m irritated by them. The two owners of the document: Elika J. Etemad and Tab Atkins Jr., from Mozilla and Google respectively, have decided to make the notation more English-like by requiring prepositions for the position keywords for linear and radial gradients. So if you had a yellow to red linear gradient that ended in the bottom right, you’d need to write the following:

1

linear-gradient(to bottom right, yellow, red);

For a radial gradient, it would be

1

radial-gradient(circle at closest-side, yellow, red);

OK, so if the point is to make it more English-like, shouldn’t it be:

1

linear-gradient(to the bottom right, yellow, red)

Or better yet:

 

Or:

1

radial-gradient(a circle positioned at closest-side, yellow, red);

And, last time I check, a big chuck of the world doesn’t know what the English word “to” means and so it adds no clarity to what the keywords are doing. I’m totally against requiring unnecessary English grammatical constructs in to CSS. We don’t do that with HTML or JavaScript. What strikes me as really odd, one of the authors is from Mozilla. They were the guys that complained that the original Webkit notation used “to” and “from” to designate the start and end colors. Now they want to make the simpler notation more complicated. Adding prepositions to keywords for gradients will not add any new functionality nor prevent any rendering errors. They just make the notation more wordy. Fail! Making CSS more English-like is a rabbit hole down which you do not want to go.

March 23, 2012

Since writing this post there has been some ongoing discussion at CSS Working Group about the reasoning behind the use of prefixes in the new syntax. The gist of it is that prepositions actually remove the ambiguity of direction when dealing with gradients. Here’s a quote from the discussions:

The “to” keyword was added to linear gradients because there was
significant confusion about whether “top” meant “start from the top
(put the 0% color on the top)” or “point toward the top (put the 100%
color on the top)”.

Radial gradients gained keywords in an attempt to make the syntax more
flexible and more readable – previously, for example, you could have a
declaration like “radial-gradient(20% 20%, 30% 30%, black, white)”.
It’s very unclear what that means – even if you know that one of them
is a position and the other is a size, how can you tell which is
which? Due to this ambiguity, the syntax also disallowed specifying
just a size without a position.

The new syntax, while not ideal in some ways, solves this problem –
you’d write the previous example as “radial-gradient(30% 30% at 20%
20%, black, white)” which gives you some clues as to which is the size
and which is the position. As well, it’s now possible to specify
either a size or a position alone, and have it parsed unambiguously –
“radial-gradient(30% 30%, black, white)” has an explicit size and
default position, while “radial-gradient(at 20% 20%, black, white)”
has an explicit position and default size.

In light of the above I’ve been won over to support prepositions in the gradient syntax. It solves the problems described above and will make creating gradients easier for everyone.

Practical Examples of the Flexible Box Model

If there’s one thing in CSS3 that really gives me warm fuzzies, it’s the flexible box model. What it does is provides a way to create many layout affects that otherwise would require workarounds with float or positioning or both. And there are many types of layouts that without out it would require JavaScript to create the same effect. Sadly, at present the flexible box model is only implemented in Firefox, Safari and Chrome. As it turns out, Microsoft’s IE team approached the W3C standards teams with a proposal for a grid-based layout module. It appears to be based on experience implementing the Silverlight grid layout system, which is a good thing. Silverlight grids are a powerful layout tool and is the most important feature for creating interfaces with Silverlight. Because this grid proposal is very solid, the W3C has dropped all further work on the flexible box model and instead are moving ahead in finalizing the draft for the grid layout module. If you read that speck, you’ll realize how much we need a CSS grid system. For now, however, we do have the flexible box model working in Firefox, Chrome and Safari, as well as on mobile Webkit, which includes iOS, Android, WebOS, and Blackberry. This means that if you’re doing mobile development you can use the flexible box model today to help you implement your layout needs. Once you start using the flexible box model, you’ll hate having to go back to situations where it’s not available.

The flexible box model got a lot of attention from a post by Alex Russell back in August 2009. The good thing is that in his post he showed some of the practical things that the flexible box model addresses and provided a stylesheet with a small subset of the flexible box model properties that people could use right away. His post has been replicated and linked to all over the Web. Unfortunately a lot of people who are not familiar with the spec for the flexible box model are under the impression that what’s in his stylesheet is it. So, with that in mind I’m loosing another stylesheet on the public that allows a greater utilization of the flexible box model. I’ll also give you a layout where a lot of these are in use so that you can see how they work.

Before I get going let me say this. The flexible box model is not perfect. It isn’t supported by IE9 or Opera. And now it looks like the W3C is bypassing it for a more robust grid system. Personally, I’d like to see something like the Silverlight grid system combined with aspects of the flexible box model. That would be the best of both approaches. If you’re targeting the mobile Web, in other words you intend on target Webkit browsers, you can expect robust support for the flexible box model. I doubt it’s going to go away just because the W3C doesn’t include it in any recommendation. I expect it to remain in Webkit for the foreseeable future because it’s already been in use for a number of years.

The flexible box model is about the way a collection of child nodes gets laid out in relation to each other and their parent node. To achieve its layout goals, the flexible box model offers properties for the parent node as well as for the child nodes that determine the layout characteristics of the child nodes.

To work with the flexible box model you need to define its display property to box:

1
2
3
4
#myDiv {
    display: -webkit-box;
    display: -moz-box;
}

Elements with display set to box can order their child nodes in two orientations: vertical or horizontal. If no orientation is provided, the box defaults to vertical:

1
2
3
4
5
6
#myDiv {
    display: -webkit-box;
   -webkit-box-orient: horizontal;
    display: -moz-box;
    -moz-box-orient: horizontal;
}

Besides defining the orientation, you can also designate what direction the child elements stack, either normal or reverse. A box direction of reverse means the elements display in the opposite order in which they physically appear in the document, so that the first would be last and the last first. By default the box direction is normal stacking order.

Then there is the packing order. This allows you to define how the child elements are packed inside the parent. By default they are packed in from the beginning of the parent. For vertical orientation this is the top and for horizontal orientation this is the left. Other packing orders are end, center and justify. If the packing order is end, for a vertical orientation the elements will stack up from the bottom. For a box with horizontal alignment, the boxes will stack from the right. A packing order of center means that for vertical orientation the child elements will be centered vertical with any left over space displayed equally at the top and bottom. For horizontal orientation a packing order of center means the elements are centered horizontally with left over space equally divided on the left and right. A packing order of justified means that any available space is spread equally between the child elements, for vertical orientation this is vertical spacing, and for horizontal orientation this is horizontal spacing.

Box-align-start

A box with alignment set to “start”

box-align-end

A box with alignment set to “end”

box-align-center

A box with alignment set to “center”

And elements can have their box alignment defined. The default is stretch. This stretches the dimensions of the child elements to fill the parent box. If the alignment is start, for horizontal orientation the elements are aligned to the top of the parent. If the orientation is vertical, they are aligned to the left of the parent. An alignment of end will align elements with horizontal alignment to the bottom of the parent. With vertical orientation they will be aligned to the right of the parent. An alignment of center will align horizontal elements along the horizontal center of the parent and for vertical orientation it will center them along the vertical center of the parent. There is also an alignment of baseline, but this is only for horizontal orientation. It aligns the elements along their horizontal baseline.

Then there is a set of properties that you can define on the child elements. These are flex and ordinal group. The flex property tells the browser how to deal with the dimensions of the child element. If you have three child elements and they don’t fill their parent, giving one of the a value of flex:1 will cause that element to take up all the left over space. If you gave one element flex: 1 and another one flex: 2, the available space would be divided up such that the element with flex: 1 would get one third of the available space and the element with flex: 2 would get two thirds of the available space. Of course the element with no flex value would default to whatever its width is. The ordinal group property allows you to designate groups of elements so that they appear in a different order than their document order. I’m not so sure about the practical use for this property. I’ve thought about it for months and have not been able to come up with a use case where I would need it. But it’s there.

I’ve created a flexible box model stylesheet with classes for all the various box properties. This means you can add the classes to an element to build out the definition you need for an element. I’ve also put together a interactive test case where you can dynamically toggle a number of the box properties and see their effect in real time. You can try it out online if you’re using Firefox, Chrome or Safari, or you can download the working example to dissect and learn.

Multiple Background Images & Animation

Multiple background images allow one to build up complex layered visual effects using one element. In the past such effects would require each background image rendered inside its own element, these being either nested or stacked on top of each other with positioning. When I was working on creating the progress bar example, I ran to a problem where I couldn’t seem to get a specific layer to animate. I could figure out what was going on. Recently, after looking at the code and fiddling with it, I found out what the problem was. Animation of background images works a little differently from other CSS properties. Because multiple backgrounds consists of multiple definitions, it works more like other properties with multiple property values: margins, borders, padding. For example, you can animate all borders on an element, otherwise you can animate a single border. Unfortunately, with multiple backgrounds there is no easy way to single out an individual background image for animation as you can with a border, margin or padding property.

Now the thing to bare in mind when dealing with multiple background images is the stacking order. The first image defined is the topmost image, and the last image defined is the bottommost. Same thing with animations for multiple background images. So, in the case of the animated progress bar, I wanted to animate the bottommost gradient image which consisted of white slanted bars. The rest of the progress bar was fine stable. Well, except that I used a single animation which would instead affect the topmost layer, making it impossible for me to get the stacking layer they way I wanted. Here is the gradient:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
background-color: rgb(56,138,213);
background-image:
    -webkit-gradient(linear, 18 0, 0 10,
        color-stop(0.23, rgba(255,255,255,0)),
        color-stop(0.3, rgba(255,255,255,0.8)),
        color-stop(0.3, rgba(255,255,255,1)),
        color-stop(0.7, rgba(255,255,255,1)),
        color-stop(0.7, rgba(255,255,255,0.8)),
        color-stop(0.77, rgba(255,255,255,0))),
    -webkit-gradient(linear, 0 0, 0 100%,
        color-stop(0, rgba(255,255,255,.8)),
        color-stop(0.45, rgba(255,255,255,.05)),
        color-stop(0.55, rgba(0,0,0,.05)),
        color-stop(0.85, rgba(0,0,0,.2)),
        color-stop(0.98, rgba(0,0,0,.5))),
    -webkit-gradient(linear, 0 0, 0 100%,
        color-stop(0.20, transparent),
        color-stop(0.20, rgba(255,255,255,.5)),
        color-stop(0.32, rgba(255,255,255,.5)),
        color-stop(0.32, transparent));

To animate this, I was using a key-frame animation that moved the x axis of the background gradient across the element from 0 to 100% in a loop:

1
2
3
4
@-webkit-keyframes progressBarAnim {
    0% { background-position-x:  0%; }
    100% { background-position-x: 100%; }
}

If you ran my previous example of the progress bar, it animates as you would expect, except one problem. The topmost gradient layer is the part that should be on the bottom. Yet every time I positioned it to where I wanted it, it wouldn’t animate. In my mind I was thinking that the key frame animation defined above would be animating all the background images. Actually, it doesn’t. It only animates the topmost image background, which is why I had to have the part I wanted to animate on top. Unfortunately I didn’t figure this out until recently. After a lot of fiddling and digging into what was going on with animation of multiple background images, I finally sorted it all out. When defining animations you have to start from the top and work you way down. You don’t have to include an animation for every background image, only down to the layer you need to. But you do have to include all the layers above the animated one up to the topmost one. So here’s my new stacking order for the CSS the way I actually wanted it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
background-color: rgb(56,138,213);
background-image:
    -webkit-gradient(linear, 0 0, 0 100%,
        color-stop(0, rgba(255,255,255,.8)),
        color-stop(0.45, rgba(255,255,255,.05)),
        color-stop(0.55, rgba(0,0,0,.05)),
        color-stop(0.85, rgba(0,0,0,.2)),
        color-stop(0.98, rgba(0,0,0,.5))),
    -webkit-gradient(linear, 0 0, 0 100%,
        color-stop(0.20, transparent),
        color-stop(0.20, rgba(255,255,255,.5)),
        color-stop(0.32, rgba(255,255,255,.5)),
        color-stop(0.32, transparent)),
    -webkit-gradient(linear, 18 0, 0 10,
        color-stop(0.23, rgba(255,255,255,0)),
        color-stop(0.3, rgba(255,255,255,0.8)),
        color-stop(0.3, rgba(255,255,255,1)),
        color-stop(0.7, rgba(255,255,255,1)),
        color-stop(0.7, rgba(255,255,255,0.8)),
        color-stop(0.77, rgba(255,255,255,0)));

And here’s the key frame animation, notice the extra animations for the upper layers:

1
2
3
4
@-webkit-keyframes progressBarAnim {
    0% { background-position-x:  0%, 0%, 0%; }
    100% { background-position-x: 0%, 0%, 100%; }
}

So remember, in the above key frame animation the first value if for the topmost layer and the last value is for the bottom most layer.

To show the difference in the stacking order, I changed the white stripe that goes horizontally across the progress bar to black. In the wrong stacking order version you can see that its actually behind the white stripes. The corrected version with multiple background animations has it correctly on top:

 

progress bar with incorrect stacking order

progress bar with incorrect stacking order

 

progress bar with correct stacking order

progress bar with correct stacking order

 

Of course, the final product would not be a black streak, it would be white. This was just to show how the stacking order was previously wrong.

Advertisements

Subpixel Rendering

You know the problem. No matter what you do you can’t get two elements to line up properly. The connecting points are always off by one pixel.

Everyone doing Web development at some point or other comes across a layout problem where no matter what you do, you can’t get two elements to align perfectly. One or the other is always off by one pixel. I was pulling my hair out try to get the pointers on the back and next buttons to align perfectly. They just didn’t look perfect. Worse still, when I used the browser’s “Zoom In” command from the view menu, I could clearly see that the lines did not connect properly.

After hours of fiddling with element sizes and positioning, I was on the verge of giving up. It was then that I remembered similar layout problems that I dealt with when doing Silverlight development. Silverlight is Microsoft’s vector-based, Flash killer/non-killer plugin for creating RIAs. For whatever reason, versiond before 4.0 had terrible problems with exact positioning of elements, causing frequent one pixel disconnects when rendered to screen. The only way to resolve this was to use subpixel rendering. This was accomplished by positioning an element by using partial pixel values, such as 1.5 or 1.25. This would force Silverlight to output the element with subpixel rendering, eliminating the visual disconnect.

OK, so what the heck is subpixel rendering? You experience it everyday with the browser’s font smoothing. You know it as anti-aliasing. The browser looks at the bézier curves of the font and when it sees that a line passes though a pixel, it looks at how much of the pixel is intersected. Depending on the percentage, the browser outputs a percentage of the font’s color. Less means the pixel gets less of the font’s color. For the human eye this creates the illusion of smoother curves.

You can use this same technique to trigger subpixel rendering on an element by giving it percentage-based position, or percentage-based dimensions. Here are some examples:

1
2
3
4
5
6
7
.button {
    position: absolute;
    left: 0px;
    top: 2.5px;
    height: 23.5px;
    width: 23.5px;
}

Here’s a image of my next button with the browser zoomed in. As you can see the pointer doesn’t line up perfectly with the rest of the button. This caused a slightly noticeable disconnect at normal size as well.

next button with its pointer misaligned

Now here’s the same button with the pointer using position set to top: 2.5px;:

next button using subpixel positioning

Subpixel rendering solved the connect problem I had at all zoom levels, including at normal size. Depending on your problem, subpixel positioning may be enough, or subpixel dimensions may be enough, or you may need to do both. Using subpixel values can help resolve problems when your layouts are not coming out pixel perfect.

You can try this out online or download the source code.

Today’s new technology terms:

subpixel:
   A pixel rendered with a shade of an adjacent element’s color to make it appear as if the element occupies part of that pixel’s space.
subpixelate:
   To force the browser to render an element with subpixel values.
subpixelation:
   The act of forcing an element to render with subpixel values or the condition of being rendered with subpixel values.
Advertisements

ChocolateChip-UI 1.0

For those of you who visit my blog looking for how to put together different widgets with HTML and CSS, I’m proud to announce the launch of ChocolateChip-UI 1.0. This is a major update with many refinements, bug fixes and new features, such as titled lists, alphabetical lists, and iPad style popovers. If you like the examples in these blog post and would like to use them, you can download an entire framework with everything in this blog and more, all ready to use. And, if you’re good with CSS, it’s fairly easy to customize the look and feel of the framework.

Visit ChocolateChip-UI.com.

Here’s a list of the types of widgets, controls and layout options that ChocolateChip-UI offers:

  • Toolbars
  • Navbars
  • Buttons
  • Icons
  • Navigation Lists
  • Table Views
  • Selection Lists
  • Switch Controls
  • Popups
  • Popovers
  • Expanders
  • Progress Bars
  • Activity Indicators
  • Actionsheets
  • Tab Bars
  • Segmented Controls
  • Deletion Lists
  • Sliders
  • Carouses
  • Scroll Panels
  • Paging Controls
  • Split Layout

Here’s what some of those layouts and controls look like:

ChocolateChip-UI for jQuery

ChocolateChip-UI has been ported to jQuery. This means if your preferred JavaScript library is jQuery, you can now use this version of ChocolateChip-UI for creating mobile apps. It uses the latest version of jQuery for DOM traversing, manipulation and Ajax calls. It also includes jquery.tmpl.js to accommodate your tempting needs.

Because ChocolateChip.js was designed to work similar to jQuery, in most cases the port was trivial. However, jQuery’s wrapping of nodes in its object instead of returning an actual node introduced complications that I did run into in the regular version of ChocolateChip.

The end result of the port is that ChocolateChip-UI’s controls work the same as they always have. There are only some slight difference in how callbacks are handled in a couple of places. If you’ve already been using the regular version of ChocolateChip and want to switch to the jQuery version, open up and examine the provided examples to see if you need to change anything. In most cases you won’t.

ChocolateChip-UI for jQuery:
On Github: rbiggs/chocolatechip-ui-jq

To learn more about how to use ChocolateChip-UI, visit ChocolateChip-UI.com.

iOS 5 Style Switch Control

Recreating the iOS 5 Switch Control with HTML5, CSS3 and a Bit of ECMAScript 5

The final result of this post will run in iOS 5, Safari 5.1, as well as the latest versions of Chrome, Firefox and Opera.

Previously I had created a version of the switch control in iOS. With the launch of iOS5 Apple complete updated the look of the switch control. They went with a rounded style, which they also did with most controls in their desktop operating system, Lion.

After playing around with the early betas of iOS 5, I came up with the following reproduction of the new switch control look using just HTML5, CSS3 and some JavaScript for the interactive part. Functionally the switch control is nothing more than a fancier way of presenting a checkbox. So, for our purposes we are going to use a checkbox. Except that we need a couple of tags to wrap the checkbox so we can make it look like the switch control. Fortunately the amount of wrapper is really minimal. If you examine the picture below, you will notice that the switch control really has only two parts: the oblong base and the circular thumb. In our case we need a third part: a checkbox input.

Switch Control

We’re going to make a minor tweak to this default look. You’ll notice that the version above is in English. Actually, only the English version has labels for “On” and “Off”, everyone else uses the international symbols instead. They look like this:

International version of Switch Control

If we ignore the “On/Off” parts and just look at the colored areas we can see that we’re only really dealing with a simple vertical gradient on the thumb and some inset box shadows on the switch control base. This makes our styling really easy. For the “On/Off” parts we don’t need extra markup. You’ll notice that they exist in relation to the switch control’s thumb. We can use CSS pseudo elements on the thumb to create them.

To recreate the iOS5 switch control all we need is the following markup:

1
2
3
4
<div class="switch">
    <span class="thumb"></span>
    <input type="checkbox" />
</div>

Without styling, this gives us a very normal checkbox:

Unstyled switch control

We know what the dimensions need to be by measuring the screenshots, so we can give the switch control base some styling:

1
2
3
4
5
6
7
.switch {
    height: 28px;
    width: 77px;
    border: 1px solid #979797;
    border-radius: 20px;
    overflow: hidden;
}

This will give us the following:

Switch control with rounded border

It looks kind of funny with the checkboxes. We don’t need to see them. We will be setting their checked state with JavaScript later on anyway. So for now we can hide them:

1
2
3
.switch input[type=checkbox] {
    display: none;
}

Now let’s add some color. How to re-create that gray shadow area? We’ll use a series of inset box shadows. Like gradients, you can define multiple box shadows on an element. These stack up like layers, the last one being the bottom-most and the first being the top-most. We need to create a sizable gray choke inside the switch base, so we’ll use a box shadow with four values instead of three to create that effect:

1
box-shadow: inset 0 12px 3px 2px rgba(232, 232, 232, 0.5);

To this we’ll add a second inset box shadow to create a darker shadow along the top inside of the switch control:

1
box-shadow: inset 0 1px 3px #BABABA, inset 0 12px 3px 2px rgba(232, 232, 232, 0.5);

Switch control with gray box shadow

Here’s the complete CSS definition for the switch control:

1
2
3
4
5
6
7
8
9
10
.switch {
    height: 28px;
    width: 77px;
    border: 1px solid #979797;
    border-radius: 20px;
    margin-top: -5px;
    box-shadow: inset 0 1px 3px #BABABA, inset 0 12px 3px 2px rgba(232, 232, 232, 0.5);
    cursor: pointer;
    overflow: hidden;
}

Now for a tricky part. This gray inset box shadow is for the off state. How do we implement the bluish on state? Well, first of all we need to decide how to represent the states in markup. We’ll do this by added a class of “on” to the switch control base. That means that the base will have a class of “switch on” for when it’s flipped on and just “switch” when it’s off. We can use a pseudo element on the switch base to create the blue state and position it in view or out of view based on the presence of the “on” class. Of course we’re going to need a little JavaScript to set and remove the “on” class when the user clicks. So, here’s the CSS for the on state. We create an empty text node and give it the height we need to match the base. We don’t give it a width just yet since that will get set when the switch has the “on” class. We give it a bluish background color and inset box shadow. The absolute positioning is so that when it’s show, it doesn’t push the thumb out of the switch but instead sits independently inside the switch.

1
2
3
4
5
6
7
8
9
10
.switch::before {
    content: "";
    display: block;
    height: 28px;
    width: 0px;
    position: absolute;
    border-radius: 20px;
    box-shadow: inset 0 1px 2px #0063B7, inset 0 12px 3px 2px rgba(0, 127, 234, 0.5);
    background-color: #64B1F2;
}

To show the “on” state we just need to give the blue pseudo element the same width as the base:

1
2
3
.switch.on::before {
    width: 77px;
}

If we add the “on” class to one of our switches, we can see how the on state looks:

1
2
3
4
<div class="switch on">
    <span class="thumb"></span>
    
</div>

Switch control with 'on' state

That’s all we need for the switch control’s base. Now let’s tackle the thumb. We’ll make the span a block element with dimensions, set its positioning to relative so we can give it a higher z-index than the other elements in the switch control, specifically, the blue on state pseudo element. Next up: border, box shadow and gradient, very straightforward. And finally, because we want to have the thumb slide back and forth when the switch is clicked, we need to enable a CSS transition and give it a default translate value. Note: you will need to add an appropriate vendor prefix for the gradient, transition and transform.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
.switch > .thumb {
    display: block;
    width: 26px;
    height: 26px;
    position: relative;
    top: 0;
    z-index: 3;
    border: solid 1px #919191;
    border-radius: 28px;
    box-shadow: inset 0 2px 1px white, inset 0 -2px 1px white;
    background-color: #CECECE;
    background-image: linear-gradient(top, #CECECE, #FBFBFB);
    transition: all 0.125s ease-in-out;
    transform: translate3d(0,0,0);
}

This gives us the following:

Switch control with thumb

As you can see, all thumbs are in the same place. We need to define a translate value for their “On” state:

1
2
3
4
5
.switch.on > .thumb {
    -webkit-transform: translate3d(49px,0,0);
    -o-transform: translateX(49px);
    -moz-transform: translateX(49px);
}

Which gives us:

Switch control thumb in 'on' state

Now the only thing left is to create the “on/off” indicators. We’ll start with the “on” one. It’s really quite simple. a vertical stripe with a border around it. We’ll create a pseudo element that has an empty text node, style it and position it beside the thumb. Here’s the CSS:

1
2
3
4
5
6
7
8
9
10
11
12
.switch > .thumb::before {
    content: "";
    display: block;
    height: 14px;
    width: 2px;
    background-color: white;
    box-shadow: 0px -1px 1px #666;
    border: none;
    position: absolute;
    top: 6px;
    left: -24px;
}

Switch control with 'on' state indicator

And for the “off” indicator, we create a pseudo element with an empty text node styles as a circle positioned to the right of the thumb:

1
2
3
4
5
6
7
8
9
10
11
.switch > .thumb::after {
    content: "";
    display: block;
    height: 10px;
    width: 10px;
    border-radius: 10px;
    border: solid 2px #777;
    position: absolute;
    right: -32px;
    top: 6px;
}

Switch control with 'off' state indicator

Now we have a fully styled switch control with minimal markup. We just need to add some interactivity. For that we’ll have to write some JavaScript. Since this is a self-contained example, I’m going to use the very latest version of ECMAScript 5. This gives me an easy way to get DOM elements and toggle classes on elements. If you want to reuse this you’ll need to switch those parts out for whatever methods your chosen JavaScript library provides.

So, first up I’m going to wrap everything up in an anonymous function:

1
2
3
(function() {
})();

Next I need a convenience method to get a collection of nodes and turn it into an array so I can iterate over it. I use call slice method of the Array object and pass in the results of querySelectorAll. That will convert the node collection into an array:

1
2
3
4
5
(function() {
    var $$ = function(selector) {
        return Array.prototype.slice.call(document.querySelectorAll(selector));
    }
})();

Now I want to define an event that executes when the DOM is fully loaded:

1
2
3
4
5
6
7
8
(function() {
    var $$ = function(selector) {
        return Array.prototype.slice.call(document.querySelectorAll(selector));
    }
    document.addEventListener("DOMContentLoaded", function() {
    
    }, false);
})();

After getting an array of all switch controls in the document, we iterate through them with the **forEach** method and bind a click event listener. The listener will execute a function that toggles the class “on”. ECMAScript 5 introduces a new token collection for classes called classList. This has several useful functions: add, remove, contains and toggle. To accomplish these methods with straight JavaScript you would need to use regular expressions. Instead I can just use **Element.classList.toggle(“on”)** to add and remove the class when the user clicks:

1
2
3
4
5
6
7
8
9
10
11
12
(function() {
    var $$ = function(selector) {
        return Array.prototype.slice.call(document.querySelectorAll(selector));
    }
    document.addEventListener("DOMContentLoaded", function() {
        $$(".switch").forEach(function(switchControl) {
            switchControl.addEventListener("click", function toggleSwitch() {
                switchControl.classList.toggle("on");
            }, false);
        });
    }, false);
})();

With the above JavaScript in our document, when the user clicks a switch control, the class “on” will be added to or removed from the switch, causing its thumb to slide to the left or right accordingly. This handily takes care of our visual requirements for the functionality of the switch control. However, we do need to manage the checked state of the checkbox. The first thing we’ll do is make sure any switch controls that had the class “on” during page load have their checkboxes set to chekced. Since the checkbox is the last element in the switch control div, we can reference it that way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
(function() {
    var $$ = function(selector) {
        return Array.prototype.slice.call(document.querySelectorAll(selector));
    }
    document.addEventListener("DOMContentLoaded", function() {
        if (switchControl.classList.contains("on")) {
            switchControl.lastElementChild.checked = true;
        }      
        $$(".switch").forEach(function(switchControl) {
            switchControl.addEventListener("click", function toggleSwitch() {
                switchControl.classList.toggle("on");
            }, false);
        });
    }, false);
})();

Next we need to update a switch controls checkbox when the switch control itself is clicked. We just need to again get a reference to the checkbox and set its clicked state to the opposite of what it was when the user clicked:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(function() {
    var $$ = function(selector) {
        return Array.prototype.slice.call(document.querySelectorAll(selector));
    }
    document.addEventListener("DOMContentLoaded", function() {
        var checkbox;
        if (switchControl.classList.contains("on")) {
            switchControl.lastElementChild.checked = true;
        }      
        $$(".switch").forEach(function(switchControl) {
            switchControl.addEventListener("click", function toggleSwitch() {
                checkbox = switchControl.lastElementChild;
                checkbox.checked = !checkbox.checked;
                switchControl.classList.toggle("on");
            }, false);
        });
    }, false);
})();

And that’s all you need to make the switch controls work. The final example has some extra JavaScript to output some text when the user flips a switch on to show them working. For Safari, Chrome and Opera, I use innerText to set the text value, but Firefox uses textContent. So the code has to deal with those differences.

You can try the working example. If you want the code, just save that page to your desktop. Everything is self-contained in the page.

Advertisements