Posts Tagged ‘css’

‘cos I’m a muppet who doesn’t have a development version of this blog, things will shortly get ugly as I’m going to be redoing al the templates, css and javascript on the fly.

Updates

  1. Browser Support
    • As this is a personal blog support for IE6 is dropped from here on.
    • Supported browsers as of today will be what I have installed on this machine – IE8, Opera 9.6, Firefox 3, Safari 3.2, Chrome 2.
    • Support for IE7 will be added as and when I find it convenient to do so
  2. BTW, I’m not totally a muppet – I did back up the old files before I started
  3. Two hours later and the basics of the styling are back in place
  4. Nearly there, threaded comments look okay, moving onto the comments form.
  5. Comments form done, dinosaur pages skin done, cross-browser testing next…
  6. Looking good. Not a bad day’s work, comments and print stylesheet much better than before. CSS and JS reduced in size.
Very True Mood: (nervous) nervous
Very True Music: British Sea Power - Be Gone

I was using this blog as an example of something the other day and noticed something was off in the sidebar when viewed in Internet Explorer. No surprise there, but when I dug a little deeper I discovered that it was only broken in IE7 – both IE6 and IE8 were okay (well, IE6 was a little off but nothing major).

Screenshot taken in IE6
Internet Explorer 6
Screenshot taken in IE7
Internet Explorer 7
Screenshot taken in IE8
Internet Explorer 8

The problem’s likely to have been there since last June which shows how little I use IE at home. Something to do with clearing floats or a calculating heights, but why is IE6 okay?

Very True Mood: (curious) curious
Tags: , ,

Picking up from my first attempt here’s a more methodical approach.

Objectives

  1. Buttons (both <input type="submit"/> and links masquerading as buttons that have rounded corners.
  2. Works in recent versions of Gecko, WebKit, Opera and Internet Explorer
  3. As little extra mark up as possible
  4. Any JavaScript used must be generic and not tied to one particular style of button, i.e. change element.className not element.style

HTML + CSS

Let’s start with the standards based solution that works in Gecko and WebKit based browsers.

<input class="button-rounded" type="submit" value="Go"/>

<a href="#" class="button-rounded right">Go

.button-rounded {
  font: bold 100%/1 Verdana, sans-serif;
  text-decoration: none;
  background-color: #2e4c37;
  color: #fff;
  border: 2px solid #fff;
  border-radius: 4px;
  -moz-border-radius: 4px;
  -webkit-border-radius: 4px;
}

As you can see from Example 2A this works very nicely in Gecko but has a few issues with padding and line heights in WebKit. If you examine the code of the example you’ll see some extra styles to tweak the padding so that the button and the link look the same (at least in Firefox) and also to produce examples where the buttons are floated (this variation is widely found on the web and also through up some problems during testing).


Add elements via JavaScript

As prevously we’re now going to use JavaScript (in the example code jQuery) to wrap the button in a span and then insert four extra elements into that span. We also add a new class to the button itself.

if($.browser.msie || $.browser.opera)  {
  $('.button-rounded').addClass('wrapped').wrap(
    '<span class="button-rounded"></span>'
  );
  $('span.button-rounded').append(
    '<i class="tl"></i><i class="tr"></i><i class="bl"></i><i class="br"></i>'
  );
};

.button-rounded.wrapped {
  border: none;
  background: transparent none;
  float: none;
  margin: 0;
}
span.button-rounded {
  position: relative;
}
span.button-rounded i {
  position: absolute;
  width: 4px;
  height: 4px;
  background: url(corners.png) no-repeat;
}
span.button-rounded i.tl {
  top: -2px; left: -2px;
  background-position: 0 0;
}
span.button-rounded i.tr {
  top: -2px; right: -2px;
  background-position: -4px 0;
}
span.button-rounded i.bl {
  bottom: -2px; left: -2px;
  background-position: 0 -4px;
}
span.button-rounded i.br {
  bottom: -2px; right: -2px;
  background-position: -4px -4px;
}

This removes most of the styles from the button and applies them to the wrapping span – this helps a lot as spans are more predictable than inputs.

But a quick look at Example 2B reveals that the floated elements don’t seem to be in the right place anymore. We forgot that there are two classes and hence two sets of styles on these buttons.

$('.button-rounded.right').removeClass('right').parent('span').addClass('right');
$('.button-rounded.left').removeClass('left').parent('span').addClass('left');

Okay, Example 2C is a step in the right direction but there are still a number of issues. In IE the floated buttons are two wide and the inline input is missing its bottom border. In Opera the two inline examples have a problem with the location of the right side corners.


Fine Tuning Internet Explorer

Add display: inline-block; to the styles for .button-wrapped. Simple and works brilliantly, Example 2d.


Fine Tuning Opera

As the browser with the smallest market share we’ve left Opera to last and hence it’s been the victim of a few CSS style choices that have been made with IE and Firefox in mind. We could deploy some CSS hacks to feed it different styles but this time I decided to use JavaScript instead.

if ($.browser.opera) {
  $('input.button-rounded').addClass('opPad');
  $('span.button-rounded').each( function() {
    if($(this).css('float') == 'none') {
      $(this).children('i.tr, i.br').addClass('opNoFlo');
    };
  });
};

input.button-rounded.opPad {
  padding: 1px 5px 2px 5px;
}
span.button-rounded i.opNoFlo {
  right: -4px;
}

So there we are, Example 2e.


If you want a hover effect on your buttons just add .button-rounded:hover and change the background and border properties. It may be easier to change the span.button-rounded:hover i background image but it’s probably better practice to change the background-position values instead and use a single corners.png as your CSS sprite holder for both normal and hover states. (Or take the easy way out and just change the text colour as I did in the last example.)

If you have multiple styles of buttons then use contextual selectors to set different CSS whilst keeping the JavaScript common across the entire site. In a real world case I have a common style that gets changed for a few <form id="foo">.


Now for the bad news. I’ve tested this in IE 7, FF 3, Opera 9.6, Safari 3, Chrome 0.4.154.22 on Windows XP. I’ll be testing in IE 6 and IE 8b2 shortly but would appreciate feedback regarding other platforms and browsers.

Very True Mood: geeky
Very True Music: Gang of Four – Damaged Goods

That last post? Some of you may have noticed that it doesn’t work very well.

The styles applied to the button and the button’s parent element make a difference in how IE (and to a lesser extent Opera) handle the positioned elements.

My current line of attack is to use more jQuery to remove some styles from the button itself and apply them to the inserted span. For Opera, I’m looking at the SVG solution.

This time I may wait until I’ve finished testing before posting.


Today I decided to try and build rounded corners on a button that would work in both CSS 3 compliant browsers (Gecko and WebKit based browsers, i.e. Firefox, Camino, Safari, Chrome, etc.) and also in IE and Opera.

The HTML

<input type="submit" value="Go" class="button-rounded"/>

Nice and clean.

The CSS

.button-rounded {
        border: 2px solid #fff;
        padding: 0 2px;
        background-color: #2e4c37;
        color: #fff;
        font-weight: bold;
        border-radius: 4px;
        -moz-border-radius: 4px;
        -webkit-border-radius: 4px;
}

This is enough to do the trick in the known good browsers and in any unknown CSS 3 compliant browsers. (This part of the CSS 3 spec, whilst still not finalised, is unlikely to change.)

More CSS

span.corners {position: relative;}
span.corners i {position: absolute; width: 4px; height: 4px; background: url(corners.png) no-repeat;}
span.corners i.tl {top: 0; left: 0; background-position: 0 0;}
span.corners ispan.corners ispan.corners i.tr {top: 0; right: 0;  background-position: -4px 0;}
.bl {bottom: 0; left: 0; background-position: 0 -4px;}
.br {bottom: 0; right: 0; background-position: -4px -4px;}

Yet more CSS for IE (use a conditional comment or your hack of choice)

span.corners i.tl, span.corners i.tr {top: 1px;}
span.corners i.bl, span.corners i.br {bottom: 1px;}

(There’s a one pixel difference betwen the way Opera and IE draw the button, hence the branched CSS. This may not always be the case, depending on the CSS styles you use for the border but with a 2px wide border with a 4px corner radius it was the case.)

[Update] It seems that IE8 differs from IE7 in this respect. I’ll revisit this article when IE8 leaves beta.

The graphic

, a bit small, here it is ×4 . That’s all four corners in one graphic, for use as CSS Sprites in the above CSS code.

But where are these elements that this extra CSS references? They get added by JavaScript, in this case using the jQuery library.

The JavaScript

if(($.browser.msie && $.browser.version == '7.0') || $.browser.opera) {
        $('.button-rounded').wrap('<span class="corners">>/span>');
        $('span.corners').append('<i class="tl"></i><i class="tr"></i><i class="bl"></i><i class="br"></i>');
};

Why <i>? Because it uses the least code. If you like you can use <span> instead.

Why browser sniff? Because we don’t want to write extra code into the page for browsers that don’t need it and it’s hard to include IE and Opera but exclude FF and Safari by other means.

Remember that any browser excluded from the JavaScript and incapable of using CSS 3 will get a button with square corners. We are not excluding anyone from functionality, nor from the general design – just from one small detail.

The result

So Firefox and Safari get rounded corners via CSS. IE7 and Opera get some extra code which positions graphics over the corners of the buttons.

Still to do

IE6 has some problems with using the right and bottom CSS properties. Hence for the moment I exclude it from the JavaScript. This is what I hope to resolve in part 2.

There’s also a potential future-proofing issue – when Opera and IE start supporting border-radius this code will position the graphics over the rounded corners, which may or may not work depending on how closely their rounded corners match the ones Gecko draws (the graphic was produced from a screen shot of the corners in Firefox).

Part 2, in a few days.


Why did no one tell me that the layout of comments form was seriously messed up?

Oh well, I’ll fix it over the weekend.

Update – Fixed, in so much that nothing overlaps anymore. A poke in the HTML reveals what an un-semantic mess my template has become as it’s been tweaked and twisted over the years. Time for a clean start perhaps.

Tags:

The past couple of columns extolling the virtues of Firefox were enough to tell that he was ‘one of us’, but this week Stephen Fry is blogging about the W3C and WHATWG. In fact, this makes a lot of sense, if the W3C’s efforts were to be compared to a gameshow then one, like Mr Fry’s QI, where the contestants regularly end up with a negative points total would be an appropriate analogy.

Recently: Opera takes Microsoft to court, which leads to calls for the CSS Working Group to be disbanded, which is, unsurprisingly, shrugged off by the working group itself, and then Microsoft announces that IE8 passes Acid2.

And as you’d expect there’s been a lot of froth and nonsense across the interested blogs.

My thoughts are that progress is being made, both by people like the the IE team (the current versions of Opera and Safari already pass Acid2 and Firefox 3 will pass it as well) and by the W3C which has made some good efforts this year to be more open and transparent.

It’s good to question the way things are, and Andy Clarke’s post about the working group has certainly made people take a good look at the status quo. But I feel that his proposed alternative would take us back to the time where the W3C created specifications that bore no relation at all to what the browsers were actually doing or planning to do.

As far as Opera and Microsoft goes, this is more about commerical advantage and business models than it is about web standards per se. Opera’s current business model aligns itself with web standards. Microsoft’s business model is so large and complex that it can be both for and against web standards and as the Acid2 result shows the team building IE8 are for them. I think the lawsuit is a sideshow and shouldn’t be allowed to dominate the standards discussion.

For many of us the shenanigans of the CSS working group hold a strange fascination, but I think that Mr Fry is right to point out that it’s in the areas of video and audio that the next big battle will be fought. As such Microsoft aren’t the main bad guys, Apple and Adobe probably are. Going back to business models, these companies are both secretive and fond of closed proprietary solutions. I’m not saying that either of them are evil through and through, but I’d love to see a lot more openness and cooperation from them in 2008.

Anyway, Stephen Fry is blogging about W3C working groups and open source video formats. He’s so one of us.

Very True Mood: (rushed) rushed
Tags:

Spilling olive oil on the keyboard.
Pros: gives it that nice shiny look.
Cons: too many to mention really.

Sigh. Not a bad weekend really. Lovely weather, which I saw out the window as I was working. Got some nice JavaScript written and identified a few more IE CSS bugs that will need to be worked around. And a cracking episode of Doctor Who yesterday.

Very True Mood: (morose) morose
Tags:

Recently I had a conversation with a web developer who had never used HTML tables. They’d come into the business after the web standards movement had established itself and had never learnt to use tables. As a consequence they were using divs and CSS floats, etc. to lay out things that could (or even should) have been done with tables and running into the sort of issues you might expect when you use a generic tool to do a specialised job.

So I was wondering if there were other people like this out there, and if so would they benefit from a short tutorial explaining HTML tables from a CSS perspective? Such a tutorial might prove useful for others as well – it might provide an alternative way of approaching the tables-to-CSS transition that some people are still struggling with, and it might help explain just how CSS and tables interact. After all the table elements have their own layout model in the CSS specification and it’s not the easiest thing to grasp.

If I were to write such a tutorial I guess it would fall into two halves. The first half would look at the “simple” table model (table, tr, td and th elements) used for simple data tables and *gasp* layout tables and how their default behaviour compares with the standard CSS box model; whilst the second half would look at the “advanced” table model (caption, col/colgroup and thead/tbody/tfoot plus the accessibility enhancing attributes) and how to build complex data tables.

Anyone interested in seeing this?

Very True Mood: (contemplative) contemplative
Very True Music: Tired of Hanging Around - The Zutons
Tags:

So Jack’s asked me what I think of version seven of the “that damned browser” (aka the “browser-like operating system component”). Well, I’ve been using the betas for quite a while and really I don’t really see a lot to get very excited about.

The interface is strange, with all the useful stiff tucked away on the bottom right hand side of the toolbars (and I understand that the next release of Office will have a similar layout) and several obscure buttons way down there on the status bar.

Tabbed browsing. Great if you’re wedded to IE, but last year’s news (well year before last, or was it the year before that?) for everyone else. Thumbnail view of tabs is nice but not as nice as Opera’s tooltip thumbnails.

RSS reader is nice. Much better than Firefox’s dire live bookmarks feature and nicer to look at than Opera’s reader.

Security features. Obviously needed, not my area of expertise, nothing much to say.

CSS improvements? Yep, at last we have max-width support and so on. But really just playing catch up with the competition. Not seen anything in the way of new bugs or incompatabilities that’s going to cause problems for my sites but time will tell.

Page zoom. Copied from Opera and not quite as well done. Very good to see both page and text zoom available. Shame that text zoom is still as crippled as it was in IE 6. And no minimum font size setting – probably the single best invention since the back button, a very simple setting that solves so many problems.

Search settings, nice, better than IE 6, not as good as Opera.

Which, just about sums it up. A huge improvement on IE 6 but not as good as Opera or even Firefox.

Very True Mood: (relaxed) relaxed
Very True Music: Fun Lovin' Criminals – I Can't Get With That
Tags: