Monday, May 05, 2008

floating window working with resize event

Add to Delicious Digg this links to this post -

I spent a little while trying to figure out today how to get a little window sticking in the right hand-side corner of my browser, while resizing the screen.

Many examples are available out there, but found difficult to pick up a simple way to achieve this.

the best way I have found was this (suing JQUery code for DOM selection):


<script>
function findwidth(){
ns4 = (document.layers)? true:false
ie4 = (document.all)? true:false
winW = (ns4)? window.innerWidth-16 : document.body.offsetWidth-20
return winW;
}

function positionmydiv(){
var myposition = findwidth()-$("div.mydiv").width();
$("div.mydiv").css({position: "absolute", left:myposition});
}

$(document).ready(function() {
$(window).resize(function() {
positionmydiv();
});
});

positionmydiv();
</script>


You then can use

</div class="mydiv" id="mydiv">My text here..... </div>

Anywhere in your code, the div will float nicely and adjust with resize.

Labels: , , ,

Saturday, March 22, 2008

CSS trick: center horizontal menu

Add to Delicious Digg this links to this post -

First time a came across this issue today, and then realised how common the issue was amongst front-end designers.

Issue: web designers tend to rely on cut and paste code when designing menus, without understanding fully what the actual CSS does.

If you google for "CSS horizontal menus", you'll find loads of valid examples.

e.g.: http://www.sovavsiti.cz/css/horizontal_menu.html

the trick is to use li {display: inline} in order to get the listed items aligned horizontally (li is by default defined as "block"). But if you then need to apply specific styles such as background, or padding (which you will probably need to make the menu look pretty), you are then stuck - display: inline prevents you from applying these types of style.

The recommendation from sovavsiti is to apply a float: left, instead of display: inline, which achieves the same effect (align menu items horizontally) while keeping the block property for display, which allows more styling options.

But this is where things start going wrong
The float: left property prevents you from using the align: center property, and you end up banging your head on the wall trying to work out how to modify the actuall css in order to center your menu => wrong approach.

The right approach (well, in my opinion any way) is to get back to the basics.
1) You cannot use float: left because it prevents you from centering your menu
2) You cannot use display: inline alone without loosing the ability to style your menu items with background images/colour, padding, etc ..

Solution: once you have described the issue properly, the solution is then straightforward: use span within the li to be able to apply the missing styling properties. Easy, isn't it?

An example is available at: strctlycss


Conclusion: it is often a matter of finding what the actual problem is. Once you understand exactly what your CSS does, and its limitations, the solution is then straightforward to find.

Labels: ,

Friday, November 30, 2007

Safari 3 - CSS hack

Add to Delicious Digg this links to this post -

A quick tip about how to set up a CSS rule for Safari (all versions) only.

Problem: The way Safari renders some fonts is different from IE or Firefox (exemple: courier).

For courier, the bold version renders well in Safari, but not in IE or Firefox. And I had to ensure that the font would be normal in IE or firefox, and bold in Safari.

The "#" hack doesn't work with Safari 3 beta, but is the one which is the most popular. The following post offers alternative hacks which work with all versions of Safari:
http://www.evotech.net/blog/2007/06/targeting-safari-30-with-css/

Note: the javascript one is probably the best bet
Safari is the only browser that supports the window property devicePixelRatio. So, similar to that old fashioned document.all, you can browser sniff this way:

<script type="text/javascript">
isSafari3 = false;
if(window.devicePixelRatio) isSafari3 = true;
</script>

Other solutions rely on the fact that some CSS properties are only supported by Safari at the moment (but may very well be supported in the future).

-------------------------------------------------------
Conclusion: only use safe web fonts. Once you start looking into using non standard stuff, you get into a lot of hassle....

Labels: , ,

Wednesday, November 28, 2007

Tidy up your CSS. Keep specificity in mind

Add to Delicious Digg this links to this post -

I had to clean up a large CSS file recently (actually made out of 5 different style sheets), for a large website. The CSS had become totally unmaintainable, and tackling the CSS from scratch was necessary.

This is a bit of a daunting task, and turned out very challenging. I ended up writing a guideline for this type of job, ensuring that I had a process available next time I have to do this again, or ask someone else to do it.

One of the most painful task was to solve conflicts. And I recently found a article summarising the issue very well:
http://www.htmldog.com/guides/cssadvanced/specificity/

- p has a specificity of 1 (1 HTML selector)
- div p has a specificity of 2 (2 HTML selectors; 1+1)
- .tree has a specificity of 10 (1 class selector)
- div p.tree has a specificity of 12 (2 HTML selectors and a class selector; 1+1+10)
- #baobab has a specificity of 100 (1 id selector)
- body #content .alternative p has a specificity of 112 (HTML selector, id selector, class selector, HTML selector; 1+100+10+1)

If you keep these rules in mind when you create your CSS file, you'll be able to structure it well, limiting the risk of conflicts.

If you have to assess an existing CSS document, pay attention to all nested document, and restructure first the CSS document in order to reflect this.


Other related articles & tools :: CSS specificity
- http://www.stuffandnonsense.co.uk/
- http://www.w3.org/TR/CSS21/cascade.html: which also describes inheritance, etc ..
- http://www.rebelinblue.com/specificity.php: nice little tool indicating specifity calculation for each item of your CSS

Labels: , ,