Home > Tags > Cascading Style Sheets
Page 4

Creating First Animations With CSS3 Using Keyframes

One of the main reasons the emergence of CSS3 has been so hotly anticipated is the fact that, in combination with HTML5, it will pose a genuine alternative to technologies such as Flash. With CSS3 and HTML5, you will ultimately be able to create animated, interactive multimedia applications that will be accessible to users regardless of whether Flash is supported in their environment – great news if you’re developing Web apps for iOS or for mobile users in general. In this tutorial we will create a simple first animation using CSS3 keyframes.

Our animation is going to feature a childish cartoon bus moving along the X axis, speeding up in the middle before slowing down again. The bus and wheels will be separate images, with the wheels spinning as the bus moves. These are our image files:

Here is a screenshot from the final animation:

Create a Web Page

Let’s create an HTML5 page as follows:

<!DOCTYPE html> <html> <head> <style type="text/css"> </style> </head> <body> </body> </html>

Add the image elements to the body section of your page, including the bus and two copies of the wheel:

<div> <img class="bus" src="bus.png"/> <img class="b_wheel" src="wheel.png"/> <img class="f_wheel" src="wheel.png"/> </div>

We will use the class attributes to apply styling to the image elements.

Apply Default Styling

Start your CSS3 code by adding style properties for the container “div” as follows:

/*background element*/ div { width:600px; height:220px; background: linear-gradient(top, blue, green); background: -moz-linear-gradient(top, blue, green); background: -webkit-linear-gradient(top, blue, green); }

Here we simply apply a gradient background together with dimensions. Notice that the prefixes we are using only include the default syntax plus versions for Mozilla and Webkit (Safari and Chrome). Internet Explorer and Opera do not yet support the techniques we will be using in this tutorial so we will not be targeting them.

Now add properties for the initial style of your wheel images:

/*initial properties*/ .f_wheel { position:absolute; top:160px; left:190px; } .b_wheel { position:absolute; top:160px; left:50px; }

We use absolute positioning to make sure the wheels and bus are displayed correctly and will move together. Add the initial styling for the bus image:

/*bus properties*/ .bus { position:absolute; top:50px; left:30px; /*animations*/ }

We are going to add more properties in this block, indicating the animations we want to apply to the bus element.

Indicate the Animations

Our bus moving and wheel spinning animations are going to be specified using CSS3 keyframes. We will define the animations in dedicated keyframes sections, but we also need to tell the browser to apply these animations to particular elements. Add the following to your existing “.bus” declarations:

animation: moveBus 5s ease-in-out forwards;...
more →
FeliciaCorrine says: So finally animation is possible using CSS3. It will surely eliminate the extra cost associated with using third party services....

Implementing Drag and Drop Functions with HTML5 and JavaScript

With HTML5 and JavaScript, you can implement native drag and drop functions within the Web browser.

This is one of the emerging HTML5 tools that promises to make websites more interactive without relying on additional technologies such as Flash. In this tutorial we will create a simple page with images the user can drag and drop into designated areas.

Create an HTML5 Web Page

Create an HTML file for your drag and drop function. Use the following basic outline, with sections for JavaScript and CSS in the head area:

<!DOCTYPE HTML> <html> <head> <style type="text/css"> /*styling here*/ </style> <script type="text/javascript"> //functions here </script> </head> <body> <!--page elements--> </body> </html>

Add Drag Targets to the Page

Your page will contain elements that can be dragged and elements in which the dragged items can be dropped. Start with the areas your user will be able to drop draggable elements into, adding the following in your page body section:

<div class="pics"> <div id="place1" ondrop="dropIt(event);" ondragover="event.preventDefault();"> </div> <div id="place2" ondrop="dropIt(event);" ondragover="event.preventDefault();"> </div> <div id="place3" ondrop="dropIt(event);" ondragover="event.preventDefault();"> </div> <div id="place4" ondrop="dropIt(event);" ondragover="event.preventDefault();"> </div> <div id="place5" ondrop="dropIt(event);" ondragover="event.preventDefault();">...
more →
Romain says: I think you should consider keeping the JS stuffs in js part and not in the div / img tags as you did in the second part, using...

Coding Vendor Prefixes with JavaScript

Savvy web developers often use vendor prefixes to try out the latest browser styles before the styles have been approved by the W3C and turned into a standard.  By specifying a browser or vendor prefix within your CSS stylesheet or style property within a JavaScript function, you can gain control of how an element will render within a specific browser (Chrome, Firefox, IE, etc.).

This gets you access to these browser’s newest and coolest styles and elements.  This article will illustrate how to use a JavaScript function to identify which vendor prefix is needed for the user’s browser. This technique has been adopted by the Microsoft MSDN team as a best practice for their demos.

Before diving into the tutorial, I want to stress the caveats with using vendor prefixes in both CSS and JavaScript.  While vendor prefixes can open your web pages to all the neat stuff companies have packed into their browsers. They can also render your code worthless if a vendor decides to drop support for the style you are accessing.  This happens when the vendor’s request for a new standard is not adopted by the W3C. The W3C is slow to adopt new standards and sometimes a vendor will withdraw its request in order to pursue other technologies. If you choose to employ vendor prefixes in your pages, you will want to make sure your code or stylesheets will still render beautifully when the vendor prefix is no longer found.

Here is an example of a JavaScript setting the transform style using vendor prefixes. (This could also be done in a CSS stylesheet, but this article is focusing on JavaScript.)

var oElement = document.getElementById("myElement"); oElement.style.msTransform = 'scale(2)'; //IE oElement.style.webkitTransform = 'scale(2)'; //Chrome and Safari oElement.style.MozTransform = 'scale(2)'; //Firefox oElement.style.OTransform = 'scale(2)'; //Opera oElement.style.transform = 'scale(2)'; //Someday this may get adopted and become a standard, so I put it in here.

As you can see, if you plan to use vendor prefixes for several styles in your site, this technique can get clunky and time consuming to code. Instead, a better way to find the supported browser is to loop through each vendor prefix to find the right property or return null if one is not found:

function GetVendorPrefix(arrayOfPrefixes) { var tmp = document.createElement("div"); var result = ""; for (var i = 0; i < arrayOfPrefixes.length; ++i) { if (typeof tmp.style[arrayOfPrefixes[i]] != 'undefined') result = arrayOfPrefixes[i]; } else { result = null; } return result; }

The next step is to initialize a variable for all the properties using vendor prefixes in your page.

var transformPrefix = GetVendorPrefix(["transform", "msTransform", "MozTransform", "WebkitTransform", "OTransform"]); var transitionPrefix...
more →
Dominique Sandoz says: Thanks, just a warning for other people visiting this: Code is buggy and contains logic errors, you may have to correct these...

Developing a Responsive Website Part 4: Finishing The Homepage Portfolio Slider

This week we’re going to finish up the portfolio slider on our homepage that we have already started.

At this point, if you view your index.php file and scroll down to the secondary screen it should look something like this.

We’re close, all we have to do now is plug in our jQuery elements and then add some CSS to make our secondary portfolio slider screen responsive.

Go ahead and download the Java files you’ll need from here, keep the js directory in your root folder and check out what out the image below to see what your finished product will look like.

Let’s begin in our index.php file.  The first thing we’re going to do is add some links inside our heading that point to our java scripts. These are the jQuery and JavaScript’s that power our slider. As you can see I sandwiched mine between the link to my style sheet and the closing of the head tag.

<link href="main.css" rel="stylesheet" type="text/css" /> <script src="js/jquery-1.2.1.pack.js" type="text/javascript"></script> <script src="js/jquery-easing.1.2.pack.js" type="text/javascript"></script> <script src="js/jquery-easing-compatibility.1.2.pack.js" type="text/javascript"></script> <script src="js/coda-slider.1.1.1.pack.js" type="text/javascript"></script> <script type="text/javascript"> jQuery(window).bind("load", function() { jQuery("div#slider1").codaSlider() // jQuery("div#slider2").codaSlider() // etc, etc. Beware of cross-linking difficulties if using multiple sliders on one page. }); </script> </head>

At this point you should have a fully functioning slider, but it’s not quite responsive yet.  To make our slider responsive we just have to add a few more things in our media queries and we’ll be done.  First up will be our media query for landscape view tablets with a max width of 1024 px, all we need to do is add our #mainBG2 tag under our #mainBG tag.

#mainBG2 { border-top:10px solid #EDEAE5; background:...
more →
Jeff_DD says: Here is the one that kicked off the series. I will post links to each one of these for the readers....

Developing a Responsive Website: Secondary Page Part 2

We’re going to wrap up our tutorial on how to develop a responsive website this week by making our secondary page, well, responsive.

We created our large layout for the page in our last tutorial, but now we want to make it fluid so that it will display nicely across various platforms, ranging from tablets and mobile devices to PC’s. 

Let’s take a quick peak at what we’re working towards.

Notice how once we hit the skinnier, mobile version of the site we go to a more vertical layout.  This makes it easier to read the text as it allows someone browsing the page from their touch-screen mobile device to navigate the page with one hand, as they can hold the device in their palm while vertically scrolling with their thumb.

The first order of business is to set some parameters for our main “services” id section under the landscape tablet media query.  We want to use our medium sized image to cut down on load time.  So, under the Tablet Layout: 768px  @media only screen and (max-width: 1024px) and (orientation:landscape), I have added the following bit of CSS.

#services { background: url(images/servicesMed.jpg) no-repeat fixed; background-position:center; background-size: cover; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; }

Once we have shrunk our background image down a bit we’re going to want to do the same with our services bubbles, and all of the content within the bubbles.  You’ll notice that I also changed the margins up a bit to accommodate the smaller sized circles.

.servCats { width:100px; height:100px; margin-left:12%; } .servCats p { width:100px; text-align:center; margin-top:30px; } .servCats a { font-size:1.2em; }

That does it for the landscape tablet view, so now let’s scroll down a little further in our main.css file and tweak the portrait view.  This will be the exact same code as above.

#services { background: url(images/servicesMed.jpg) no-repeat fixed; background-position:center; background-size: cover; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; } .servCats { width:100px; height:100px; margin-left:12%; } .servCats p { width:100px; text-align:center; margin-top:30px; } .servCats a { font-size:1.2em; }

Last up on the docket is our mobile media query.  What we’re going to do here is take our two “servColumn” elements and display them one on top of the other, instead of side-by-side like they are in the larger formats of our design.

Displaying these elements vertically like this will make the content much easier to read on mobile device.  To maintain the same appearance as we did in our larger design we would have had to shrink the text so small that it was unreadable, or if we maintained a reasonable size font we would have averaged a word or two per line.

First things first, go ahead and define the small image for the mobile query.

#services { background: url(images/servicesSmall.jpg)...
more →
Bikebone says: Thank you very much for the well-written tutorial. How about a link/links to the other articles like here:...

Skewing Web Page Elements Using The CSS3 Skew Transform

With CSS3, you can transform the appearance of Web page elements. The skew transform allows you to skew a particular element or group of elements, by supplying a number of degrees to skew along the X and Y axes.

Your code needs to be tailored to the specific browsers supporting CSS3 and you do, of course, need to remember that not all browsers support these properties yet.

The CSS3 skew transform can execute as the user interacts with your pages, and you can optionally include a transition within the effect.

Build an HTML Page

You can use an existing page if you have one you’re working with, but if not use the following HTML5 code outline:

<!DOCTYPE html> <html> <head> <style type="text/css"> </style> </head> <body> <img src="picture.jpg" alt="picture" class="skewer" /> </body> </html>

We are going to apply the skew effect to an image element, so alter the markup to reflect the name and location of your own image file, which can be any image you have but will be easier to work with if it is relatively small. Include your image name in the “img” tag “src” attribute.

Style the Element

To see the skew transform working, you first need to give your image element some initial style properties. Add the following declarations inside the CSS area within your page head:

img.skewer { left:100px; top:150px; position:absolute; }

This gives us a default position for the image element within the page.

Make the Element Interactive

Rather than simply applying the skew effect straight away, lets make it happen on user interaction. Add the following event attributes to your image element:

onmouseover="this.className='skewer skewed'" onmouseout="this.className='skewer'"

Your image element should now look as follows, with your own image indicated in the “src” attribute:

<img src="picture.jpg" alt="picture" class="skewer" onmouseover="this.className='skewer skewed'" onmouseout="this.className='skewer'" />

When the user moves their mouse onto the image, the page gives it an extra class name, then removes this name when the user moves their mouse away from the image. This additional class name will allow us to specify the skew transform in a separate CSS section.

Skew the Element

Add a new section in your CSS area as follows:

img.skewed { /*General*/ transform: skew(30deg, 10deg); /*Firefox*/ -moz-transform: skew(30deg, 10deg); /*Microsoft Internet Explorer*/ -ms-transform: skew(30deg, 10deg); /*Chrome, Safari*/ -webkit-transform: skew(30deg, 10deg); /*Opera*/ -o-transform: skew(30deg, 10deg);...
more →
says: Inline JS? That's a bad practice. How about putting the skewed css in the :hover state? skewer:hover { transform: skew(30deg,...

Developing a Responsive Website: Secondary Page

We’re going to begin to wrap up our tutorial on creating a responsive website this week by creating a secondary services page to feature the necessary, and important, content of our site.  Keeping a sites layout visually appealing is a crucial aspect of keeping a visitor on your site.

Having a simple box filled with verbiage running from left to right is not very inviting, nor aesthetically pleasing.  When displayed like that, the sheer amount of text could be enough to cause your visitor to quickly back up and off your site.  That’s why we’re going to break our text up and display it a little more creatively on our larger format.

Let’s first take a look at what we’re going to create and download the work up to this point, if need be.

When developing a site I typically use my homepage as a template by utilizing the “Save As” feature and renaming it what ever my new page will be.  Feel free to start from scratch or follow along on my code deleting rampage.

Open up your index.php file and save it as services.php.  Once it’s saved as services.php we’re going to need to delete some of the content.  We’ll start in the head of our document by deleting most of the Java scripts and links, with the exception of the two lines following the link to our CSS file.  These two lines call the jQuery and link the the JavaScript necessary for the Twitter feed in our footer to work.  The rest of the Java links can go, they were only necessary for features on our homepage and are no longer relevant.

My <head> section now looks as follows.

<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width; initial-scale=1; maximum-scale=1"> <title>Responsive Web Design</title> <link href="main.css" rel="stylesheet" type="text/css" /> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script> <script src="js/twitter.js"></script> </head>

We’re going to rename our “mainBG” div to “services”.  Go ahead and make that switch and change class “homeContent” to “servContent”.  You’ll also swap the note that closes our services div from “mainBG” to “services” to prevent any future confusion.

Next we’re going to want to ditch our “blurb” class, and the final thing we’re going to want to nix is the entire “mainBG2” section, which contains all of the information for the portfolio slider that’s on our homepage.

At this point, weather you gave your delete key a work out or exercised your words per minute, your page should look as follows.

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width; initial-scale=1; maximum-scale=1"> <title>Responsive Web Design</title> <link href="main.css" rel="stylesheet" type="text/css" /> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"...
more →
says: Layout is the key element in your website to keep visitors attracting & coming again & again to view your website. Good...