Archive for the ‘HTML’ 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

Eric MeyerLast 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:

  1. In the article the pie pieces are pulled out from the sphere and I wanted a solid circle.
  2. 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.
  3. 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 »

How to Setup Your Site for the iPhone

Friday, October 22nd, 2010

iphoneI’ve been working on some mobile apps lately and realizing how simple it is to get started making your site more iPhone compatible. You can either create a separate site specifically for mobile or create a mobile stylesheet to be applied to specific devices. There are some great arguments out there for each option, but what I’m discussing here is just the introductory steps that work for either method. In writing this I’m also considering how I’d like to apply those ideas to my site here.

If you’re planning to create an additional stylesheet rather than a completely new codebase, you’ll want to start by specifying the stylesheet to use with a media query. Ethan Marcotte has a great article called Responsive Web Design that expands upon this idea on A List Apart. To target an iPhone/iPod specifically you can use this code right after your main stylesheet in your header:

<link media="only screen and (max-device-width: 480px)" href="iphone.css"
 type="text/css" rel="stylesheet">

Control the Page Size

Next you’ve got a handful of settings that will help make your site similar to a native iPhone app. If you want your page to resize to the device, you can use this line in your header:

<meta name = "viewport" content = "width = device-width">

or if you’re setting your page width in CSS and would prefer to use a specific scale (with or without the option to disallow users from zooming the page) you can use:

<meta name = "viewport" content = "initial-scale = 1.0, user-scalable = no">

Add a Custom Icon & Startup Image

There are also meta tags for use when your site is added to the home screen of a user’s device (press the ‘+’ in mobile safari to add to the home screen). The first is adding a custom icon to your site that I wrote a previous post about. In addition, you can add a startup screen to give an app like feel. To add a startup image create a png that is 320 x 460 pixels and add this line of code to the header:

<link rel="apple-touch-startup-image" href="/your_image.png">

Make it Look Like an App

You can make your site look more like an app when people save it to their home screen by hiding the top bar that loads with safari and changing the color of the status bar. Before you do this you’ll want to make sure you’re site is setup so it can be easily navigated without needing the back button. To remove the Safari controls add this meta tag to your header:

<meta name="apple-mobile-web-app-capable" content="yes" />

And to change the status bar (network name, time, battery level) from the default grey to either “black” or “black-translucent”:

<meta name="apple-mobile-web-app-status-bar-style" content="black">

Note that if you change the status bar to translucent the site will sit underneath it. Make sure that there is no critical content hidden behind the bar.

These simple lines of code are a great way to get started setting up your site for Apple’s devices. If you’d like to go into some more detail, Apple’s Safari Reference Library is a great place to get started. Then to take this to the next level I’d recommend digging into jQTouch or Sencha. Both of these tools will create an even more app-like experience with animated transitions and app-like bars & buttons using javascript (which you’ll want to have a strong knowledge of to use to their full potential).

Tags: , ,
Posted in HTML, mobile | 1 Comment »

Adding an Icon for iPhone, iPad & Android to Your Website

Friday, September 3rd, 2010

Many sites currently add a favicon before launch that is visible in your user’s browser tabs and bookmarks, but many sites are still missing icons for iOS. By adding an iOS icon, anyone who decides to save your webpage to the home screen of their iPhone, iPad, or Android* will see a nice app-like icon rather than an image of your page.

The first step is creating an image. To support the newest high resolution iPhone you’ll want to create the image at 114 x 114 pixels or larger (the device will resize the image to the correct dimensions). You can save your image as a PNG (if you plan to use transparency) or a JPG and place it in any directory on your site.

Next you’ll just add a simple bit of code to the HEAD of your site so the devices can find your image:

<link rel="apple-touch-icon" href="images/your_ios_icon.png" />

By default Apple devices will add rounded corners, a drop shadow, and reflection to your icon. If you don’t want those styles added or you’d prefer to add your own styles to the image file (like I did for my icon) you can use apple-touch-icon-precomposed instead.

<link rel="apple-touch-icon-precomposed" href="images/your_ios_icon.png" />

If you’re creating a precomposed image you can find templates for use around the web; here’s a good template from cocoia with Photoshop effects you can turn on/off.

iphone icon

* Note on Android Devices: versions 1.5 & 1.6 work with the “-precomposed” images only and 2.1 & new will read the tag without “-precomposed”.

Posted in HTML, mobile | 2 Comments »

HTML 5 Video with Flash Fallback

Thursday, July 15th, 2010

HTML5 has some exciting new changes, most notably tags for embedding audio and video. Currently not all browsers will fully support these tags, but with the ability to fallback on different formats until one that works for your browser you can start using them today. The greatest benefit of using HTML5 video tags right now is its compatibility across multiple devices, notably mobile devices. The majority of mobile devices, including the iPad and iPhone, are currently unable to run flash based players. Adding the video tag allows these browsers to play your videos with their built in controls, expanding your potential audience.

Video Formats

The first item to look at when setting up the video tag is your video formats. Web browsers don’t currently agree on which video format should be the leader for the web so to support them all we’re currently stuck providing video in multiple formats. The two most common formats right now are H.264 (typically .mp4) and Theora (typically .ogv). H.264 is great format and common across the web as YouTube and the iTunes store both use it, but has a disadvantage in that MPEG LA owns the patent to the format and could potentially charge to encode videos in the future. Theora has the benefit of being royalty-free, but isn’t currently as widely used or integrated into as many tools. Currently Firefox and Opera only support Theora, while Safari (iPhone & iPad), Android, and Flash only support H.264. Internet Explorer is behind in supporting the video tag, which makes the ability to fallback to a Flash based player very useful.

Encoding Video

To encode .ogg files I’ve used Firefogg, which is a plugin for Firefox. This tool makes it very simple to create ogg files (for batch encoding try ffmpeg2theora). Simply install the plugin and then in FireFox go to the “Tools” menu and select “Make Web Video”. From there select a file that you’ll convert and if you want you can change the output settings for quality or video size. Make sure to leave the default video and audio codecs of Theora and Vorbis both set.

To encode h.264 files I recommend HandBreak. To get started open the video file you want to convert. Choose the iPhone & iPod Touch Default settings in the right menu (window > presets drawer if its not up). Next you’ll want to set the dimensions of your video (Picture tab on PC / Window > Picture Settings on mac). Choose either a specific size you want or change it to match the source settings (as they have changed to a size specific to the iPhone). Check the “Web Optimized” box and in the video you can adjust the quality if necessary to keep your file small. If your video has audio click on that tab and change the setting to “AAC (faac)”. Now you’re ready to encode, either click start or add to queue and repeat this process with another video.

handbreak

Setting up the Video Tag

If you got all the conversions done correctly, now is the easy part. Simply add a video tag with the height and width of your video. Adding “controls” to the tag will allow the user to pause/play the video and adjust the volume (other options for the video tag include preload, loop and autoplay). Then list the source location of the videos you created above in the order you want browsers to attempt to play them. If they are unable to play the first file they will continue to the next one.

<video width="710" height="400" controls>
     <source src="video_file.mp4" type="video/mp4" />
     <source src="video_file.ogv" type="video/ogg" />
</video>

Tips: The iPhone currently reads only the first video source so you’ll want your mp4 file listed first. If you’re maintaining your own server you’ll want to make sure you have these MIME types added: video/ogg .ogv & video/mp4 .mp4.

Falling back to Flash

The next step to cover yourself for old browsers and the always behind Internet Explorer is adding a Flash Player to the end of the video tag. The nice thing is this will work just like it did for the video files, if the first two don’t work it’ll move right on to the Flash. You can add in whichever Flash player you’d like to use, but my preferred player is the JWPlayer, so thats what I’ll be using.

<video width="710" height="400" controls>
     <source src="video.mp4" type="video/mp4" />
     <source src="video.ogv" type="video/ogg" />

     <object width="710" height="424" type="application/x-shockwave-flash"
     data="js/player.swf?image=image.jpg&file=video.mp4">
       <param name="movie" value="js/player-licensed.swf?image=image.jpg&file=video.mp4" />
     </object>
</video>

That’s all there is to setting up an html 5 video tag. Here’s a sample:

Related Links

Here’s a few projects and informational sites to check out for html video:

Tags: , , , ,
Posted in HTML | No Comments »