Archive for the ‘CSS’ Category
|Fixed Elements and Scrolling Divs in iOS 5
Friday, November 11th, 2011
I’ve been playing around with creating a web app for iOS and testing some of the new features in mobile safari that were released with iOS 5. Fixed divs and the ability to scroll a div are common in iOS native apps but were difficult to implement in a web app. These have both been added in the new safari but they still require some work-arounds to make them function like you probably want.
Fixed Elements
The first versions of iOS didn’t support fixed elements. They would fix correctly to the screen, but then they would scroll along with the rest of your content. In iOS 5, fixed elements (e.g. position: fixed; bottom: 0;) work as expected.
Keep in mind you’ll want to use the iOS meta tags if you’re designing specifically for the device. The first line below prevents the default scaling for sites that aren’t built for to adapt to the screen size and the second allows you to save the site and run it without the browser bars.
<meta name="viewport" content="initial-scale = 1.0, user-scalable = no">
<meta name="apple-mobile-web-app-capable" content="yes" />
The first issue with the fixed elements is that your site will still have the ‘bounce’ effect when scrolling past the top/bottom of your content that reveals the browser chrome (background beneath your page as it bounces). If your site doesn’t need to scroll this is a very simple fix, just add a line to prevent the default action when moving your finger:
<script type="text/javascript">
document.addEventListener('touchmove', function(event) {
event.preventDefault();
}, false);
</script>
If you want scrolling areas, but don’t want the browser chrome to show then continue on!
Scrolling Divs
Another new feature in iOS 5+ is divs can finally scroll. To make a div scroll you’ll just need to add the following (this example has a fixed header but adjust the positioning to fit your app):
.scrollable {
position: absolute;
top: 50px;
left: 0;
right: 0;
bottom: 0;
overflow: scroll;
-webkit-overflow-scrolling: touch;
}
The trick here is adding the -webkit-overflow-scrolling to anywhere you’d normally set your overflow. The downside of Apple’s implementation is that the div doesn’t bounce as you’d hope when you are already at the absolute top or bottom. Instead of bouncing the div being scrolled, it bounces the whole page and reveals the browser chrome. This is where you’ll need a javascript fix.
Fixing the Scrolling Element
Thanks to the development community there are several great options out there that you can implement. These include Scrollability and iScroll. My issue with iScroll was that it doesn’t play well with form elements if you have them in your app. At the time of this article, Scrollability’s author says it isn’t ready for release usage, but it seems to be a great solution if you want to test it. If you want your app to work well with old implementations of iOS you’ll need something similar to this (plus you’ll also need to code around the fixed elements issue), but for now we’re only addressing iOS 5+ and we’ll will save fallbacks for a different article.
The solution I like the best for iOS5 is also the simplest. Joe Lambert’s ScrollFix increases the page slightly when at the top or bottom to make your div bounce instead of the whole browser. It’s very simple to add to an element that you already have scrolling:
<script type="text/javascript" src="scrollfix.js"></script>
<script type="text/javascript">
var scrollingContent = document.getElementById("myScrollingDiv");
new ScrollFix(scrollingContent);
</script>
If you have multiple scrolling divs just apply this again to those divs. For my app I’ve got several divs which either shown or hidden and each has this script applied. We’re almost there, but two possible issues still exist. When scrolling on our fixed elements and our scrolling elements that don’t have enough content to overflow the page, the browser chrome is still revealed.
Preventing Fixed Headers from Showing Browser Chrome
This is a pretty simple fix, similar to what was described above. In my fixed header I’ve got multiple elements, so I made sure that each element or it’s parent had the class “noBounce” and then applied the following script:
document.addEventListener('touchmove', function(event) {
if(event.target.parentNode.className.indexOf('noBounce') != -1
|| event.target.className.indexOf('noBounce') != -1 ) {
event.preventDefault(); }
}, false);
This prevents the default bounce when dragging those classed elements, but doesn’t break the div that you want to scroll.
Adapting for Elements That Don’t Overflow
When creating my code some of the divs for my main content overflowed the browser window which triggers the scrollbar and bounce fix, but others didn’t. There are several ways to address this, but the easiest solution I came up with just extends all divs so they have at least a bit of scroll. To do that I added this jQuery script (you could do something similar in straight JS as well) to all my content divs:
<div class="scrollable">
<div class="contentWrapper">
Your content
</div>
</div>
<script type="text/javascript">
$('.contentWrapper').css('min-height', $(window).height() - 55 + 'px');
</script>
You’ll need to adjust the “- 55″ portion based on your application. I have it in there to prevent it from having too much scroll because of my fixed header.
Here’s a really basic demo if you want to test it out in your mobile browser: kylejlarson.com/files/iosdemo/
Posted in CSS, HTML, Javascript, mobile | 4 Comments »
An Event Apart Minneapolis
Saturday, August 20th, 2011
Last week I had the opportunity to attend An Event Apart (AEA) when it stopped by Minneapolis. AEA is a conference “for people who make websites” taught by some of the best known names in web design. I had seen Jeffery Zeldman, Jason Santa Maria, Jeremy Keith, Jared Spool, Kristina Halvorson, and Luke Wroblewski speak at various trips to Austin for South by Southwest (SXSW), but all these talks were fresh content.
Every speaker did a great job and had tons of good information, but a few of the talks stood out and got me really excited about what I was learning. The talks that were the most inspirational for me were Eric Meyer’s talk about using flexible boxes, Nicole Sullivan on optimizing CSS, and Ethan Marcotte on responsive web design. The future of designing sites for multiple devices looks very promising with the combination of the flexible boxes and responsive design (media queries). The great part is that the boxes allow content to be reordered for different purposes where it has previously been difficult to do using floats (without absolute positioning). If you don’t know about media queries check out the original responsive design article.
If you’ve got a chance to attend An Event Apart I’d highly recommend it. While it didn’t have the variety, scale, or full immersion in a sea of web geeks like SXSW, it had many of its own benefits. At AEA there weren’t giant lines, missed panels that were at capacity, or most importantly – panels that didn’t have any valuable content. There wasn’t a session here that didn’t deliver! Overall it was a much more focused and professionally delivered effort (food was also great and I got to sit next to Zeldman at lunch!).
I put together a PDF of my AEA notes if you’d like to get an idea of what was covered.
Posted in CSS, HTML | No Comments »
How to Create Pie Charts with CSS3
Thursday, January 27th, 2011
Last week I decided to update my online resume to make it a little more graphical, because well… I’m a designer. The first step was coming up with some good ways to represent data, and pie charts seemed like a good fit to show skill development. Pie charts are pretty simple to create using a spreadsheet program or a drawing program, but I wanted something out of the ordinary that didn’t require creating new graphics every time I update them. It seemed like it should be possible with the new capabilities of CSS3 so I started working on a solution.
The two most promising things I found were Google’s Chart Wizard, (which uses javascript) and a CSS3 Pie Chart Article from Atomic Noggin. I wanted to do something purely with CSS & HTML so I took the ideas from that article and tweaked them a bit to get the result that I wanted.
There were a few things that I wanted to do differently from the original article:
- In the article the pie pieces are pulled out from the sphere and I wanted a solid circle.
- Once aligned as a solid sphere there were some rendering issues (in some browsers) aligning clipped shapes next to each other that left gaps between the shapes.
- I wanted to be able to add styles to the full sphere such as a border or drop-shadow.
Creating a Drop-Shadowed Circle
My first step was creating a circle that will be the bottom layer of the chart. This bottom circle will also hold any border or drop-shadow style. Additionally, I’ve put a div with a set height around the circle so it stays inline with the content (as the circles will be absolutely positioned to stay on top of each other). To create a circle, simply add a border-radius that is half the pixel value of the width of a square div. You’ll notice below I’ve used browser specific css tags (e.g. -moz-border-radius) for the CSS3 elements. Currently CSS3 support is varied between browsers. Eventually, as full support is included in browsers, you won’t need them any more and this will be much simpler to manage.
<style>
.pieContainer {
height: 100px;
}
.pieBackground {
background-color: grey;
position: absolute;
width: 100px;
height: 100px;
-moz-border-radius: 50px;
-webkit-border-radius: 50px;
-o-border-radius: 50px;
border-radius: 50px;
-moz-box-shadow: -1px 1px 3px #000;
-webkit-box-shadow: -1px 1px 3px #000;
-o-box-shadow: -1px 1px 3px #000;
box-shadow: -1px 1px 3px #000;
}
</style>
<div id="pieContainer">
<div class="pieBackground"></div>
</div>
Adding a Slice of the Pie
Next you’ll want to create a half circle by using clipping to hide the 2nd half. Unless you want exactly 50% you’ll need to change the size of that circle by dropping it inside of a div that controls the rotation and use the inner div to adjust the size. For the first slice we’ll start the outer div at 0 degrees, which is the default, so we’ll only need to edit that for additional slices. (* Note that I’m adding to the code above and only displaying the new CSS.)
<style>
.pie {
position: absolute;
width: 100px;
height: 100px;
-moz-border-radius: 50px;
-webkit-border-radius: 50px;
-o-border-radius: 50px;
border-radius: 50px;
clip: rect(0px, 50px, 100px, 0px);
}
.hold {
position: absolute;
width: 100px;
height: 100px;
-moz-border-radius: 50px;
-webkit-border-radius: 50px;
-o-border-radius: 50px;
border-radius: 50px;
clip: rect(0px, 100px, 100px, 50px);
}
#pieSlice1 .pie {
background-color: #1b458b;
-webkit-transform:rotate(50deg);
-moz-transform:rotate(50deg);
-o-transform:rotate(50deg);
transform:rotate(50deg);
}
</style>
<div class="pieContainer">
<div class="pieBackground"></div>
<div id="pieSlice1" class="hold"><div class="pie"></div></div>
</div>
Large & Multiple Slices
If you’d like to make a slice take up more than 50% of the pie you’ll just need to add another pie div of the same color with a hold div wrapped around it. To add a slice in a new color just change the background color. Below is an example of making a slice over 50% and a second slice of a new color.
<style>
#pieSliceBlue .pie {
background-color: #1b458b;
-webkit-transform:rotate(180deg);
-moz-transform:rotate(180deg);
-o-transform:rotate(180deg);
transform:rotate(180deg);
}
#pieSliceBlue2 {
-webkit-transform:rotate(180deg);
-moz-transform:rotate(180deg);
-o-transform:rotate(180deg);
transform:rotate(180deg);
}
#pieSliceBlue2 .pie {
background-color: #1b458b;
-webkit-transform:rotate(40deg);
-moz-transform:rotate(40deg);
-o-transform:rotate(40deg);
transform:rotate(40deg);
}
#pieSliceRed {
-webkit-transform:rotate(220deg);
-moz-transform:rotate(220deg);
-o-transform:rotate(220deg);
transform:rotate(220deg);
}
#pieSliceRed .pie {
background-color: #cc0000;
-webkit-transform:rotate(140deg);
-moz-transform:rotate(140deg);
-o-transform:rotate(140deg);
transform:rotate(140deg);
}
</style>
<div class="pieContainer">
<div class="pieBackground"></div>
<div id="pieSliceBlue" class="hold"><div class="pie"></div></div>
<div id="pieSliceBlue2" class="hold"><div class="pie"></div></div>
<div id="pieSliceRed" class="hold"><div class="pie"></div></div>
</div>
IE Fallback
As expected, this doesn’t work in any Internet Explorer browsers below version 9. If you’re concerned about IE users, a simple way to fix this is adding a conditional comment for IE with a jpg of whatever charts you’re trying to display.
Posted in CSS, HTML | 3 Comments »
Building a Dock with CSS Animation
Monday, July 12th, 2010
After seeing some interesting uses of CSS animation, I decided to make a fisheye dock using only html, css and some images.
Currently not all browsers will support the functionality, but it will function correctly in Webkit browsers (Chrome / Safari) and Opera. The latest full release of Firefox (as of the writing of this article) doesn’t support css transitions so it won’t have the nice fade effects.
Dock Setup
The first step is to create some transparent PNGs to use as images for the dock. Then add the images to a list and put the text for the image in a paragraph tag. I’m also adding a div tag that we’ll use later in setting up a text bubble.
<ul id="dock-container">
<li>
<a href="#"><img src="images/catalog.png" alt="catalog" />
<p>Catalog</p><div></div></a>
</li>
<li>
<a href="#"><img src="images/passkey.png" alt="passkey" />
<p>Login</p><div></div></a>
</li>
<li>
<a href="#"><img src="images/certification.png" alt="certification" />
<p>Certification</p><div></div></a>
</li>
<li>
<a href="#"><img src="images/mountbuilder.png" alt="mountbuilder" />
<p>MountBuilder</p><div></div></a>
</li>
<li>
<a href="#"><img src="images/rackbuilder.png" alt="rackbuilder" />
<p>RackBuilder</p><div></div></a></li>
<li>
<a href="#"><img src="images/videos.png" alt="videos" />
<p>Video</p><div></div></a></li>
<li>
<a href="#"><img src="images/calculator.png" alt="calculator" />
<p>Calculator</p><div></div></a>
</li>
<li>
<a href="#"><img src="images/mobileSite.png" alt="mobile site" />
<p>Mobile</p><div></div></a>
</li>
<li>
<a href="#"><img src="images/savedProducts.png" alt="saved products" />
<p>Saved</p><div></div></a>
</li>
</ul>
Next add some CSS to float the images in a horizontal line and lower their size in the default state so they don’t become pixelated when they are zoomed.
#dock-container {
font: Helvetica, Arial, sans-serif;
font-size: 12px;
}
#dock-container li {
float: left;
list-style-type: none;
text-align: center;
}
#dock-container a {
color: #ccc;
text-decoration: none;
}
#dock-container img {
border: 0;
width: 75px;
}
Text Bubbles
Before we add the animations, we’ll stylize the text below the images to make them look like speech bubbles. The bubble is created using border-radius and the arrow using our first transform to rotate the empty div created above forty-five degrees.
#dock-container p {
background: #000;
border-radius: 8px;
-moz-border-radius: 8px;
margin: 0;
position: relative;
padding: 2px 4px;
z-index: 100;
}
#dock-container div {
background: #000;
width: 10px;
height: 10px;
margin: 0 auto;
position: relative;
bottom: 20px;
-moz-transform: rotate(45deg);
-o-transform: rotate(45deg);
-webkit-transform: rotate(45deg);
}
CSS Transitions & Animation
Now we’ll add the CSS to animate the menu. First we’ll define the transition that we’ll apply to all the animation effects on images and paragraphs so they fade (ease) both in and out. After that I’m going to define a transition specifically for the opacity transformation because I want it to show up much faster. Finally, we add the scale transformation to increase the size of any images by 1.4x within the link tag when its hovered over.
#dock-container img, #dock-container p {
-moz-transition: all .4s ease-in-out;
-o-transition: all .4s ease-in-out;
-webkit-transition: all .4s ease-in-out;
}
#dock-container p, #dock-container div {
-moz-transition: opacity .1s ease-in-out;
-o-transition: opacity .1s ease-in-out;
-webkit-transition: opacity .1s ease-in-out;
}
#dock-container a:hover > img {
-moz-transform: scale(1.4);
-o-transform: scale(1.4);
-webkit-transform: scale(1.4);
}
Finishing the Text Bubbles
We’ve just about got a working menu now. The final step is just getting the text bubbles to display only when the links are hovered. To do this we’ll just add opacity of 0 to those elements and then change it to 1 when hovered. The transition we set in the last step will quickly fade it in and out.
#dock-container p, #dock-container div {
opacity: 0;
}
#dock-container a:hover > div, #dock-container a:hover > p {
opacity: 1;
}
Here’s the working example (download the html):
Tags: animation, css, dock, fisheye, html
Posted in CSS | 1 Comment »


