Showing posts with label HTML and CSS. Show all posts
Centre Image : The strange combination of position absolute, margin auto and an image tag
by Developer @ pebbl.co.uk on Thursday, 25 April 2013
Just recently I was charged with the task of upgrading the Slideshow ability on the Tate's website, basically finishing off the work so that it could display more than just images, and to finalise it's mobile support. Most of the work was quite straightforward and entailed reverting some of the previous bespoke modifications made to the default Flexslider plugin, and then enhancing the out-of-the-box functionality with things like full-screen mode, and managing audio or video files that might be contained within.
Stop it and tidy up
One of the main improvements I made was to try and get things working with as little JavaScript as possible, this meant refactoring the CSS somewhat, and due to not having worked closely with CSS for the last year — save for the odd stackoverflow answer — I was quickly reminded just how frustrating-a-language CSS is to debug and repair... especially in it's pure form, outside of any compilation framework like LESS.
Anyway, re-realisations aside, I got pretty much everything working via CSS save for two issues. The first of these problems was the age old annoyance of how to centre an element in the middle of a container on both the vertical and the horizontal. Now I've been using CSS since the early days — if you could refer to them as early — so I'm quite aware of the different vertical alignment tricks that can be employed. I was also aware that the only way(s) I knew of to get an element of unknown or variable dimensions to centre was either to use a table, display:table, or use a background-image instead (at least for images anyway). Any one of these would have taken some crowbarring to get working harmoniously with the slideshow system. Unfortunately, every one of my slideshow images had variable dimensions, which not only varied depending on itself, but was also responsive to the display size... so I had to find some nicer way of doing it.
Solution?
Considering the fact that the well known browsers are now developing at a rate of knots, I decided that there must be a better way by now. Obviously I could have used flexbox, but it's fluctuating and unreliable support would have meant more headaches in testing than I had time for. So I spent quite a bit of time searching around, finding pretty much all the ways I already knew about, until I stumbled upon this random suggestion:
img { position:absolute; left:0;top:0;right:0;bottom:0; margin:auto; }
At first glance that snippet made no sense to me, and I almost automatically dismissed it as nonsense. I'm quite glad I ignored my first thoughts however and tested it out, because — at least for every user agent I checked — it did exactly what I needed. Now, if anyone can explain why this has been built in, and why images should behave differently to any other element, I'd be interested to hear... until then I'll just mark it down to another strange oddity in the interpretation of the rather wholly strange language/construct that is CSS.
- Leave your comment • Category: Coding, HTML and CSS
- Share on Twitter, Facebook, Delicious, Digg, Reddit
I didn't quite get round to finishing my original post about this, it was just at that time that work went a little hectic, and I don't think I've properly stopped since.
Anyways at the time the information was fresh in my memory, it's gone a little stale now unfortunately. I remember there were quite a few things I wanted to write about, so if I do get some spare time (and a reason to look in to it) I'll pick up my collapsible email template again. Before I consign this to the "most probably never" however, there are a few things that I can remember that may help out other people trying to get around particular mail agent inconsistencies.
Outlook 2007/2010
Next to Lotus Notes, Outlook 2007 is almost fun... but compared to the other e-mail clients, it is quite simply a joke. Annoyingly it is a joke that is installed on a large number of machines worldwide. So as per usual, us developers have to find some way of limping on with awful software. In case it helps, here are a few nice style properties, that affect only microsoft products.
mso-hide
If you need to hide a particular element only in Outlook 2007/2010 use the following inline style.
style="mso-hide: all;"
It should work just like display:none; but is Microsoft specific. I've mainly used this on items that really are already hidden, but in some cases their presence still creates padding or spacing issues for Outlook.
mso-table-lspace and rspace
Whilst trying to get my collapsible email to layout nicely I kept getting annoying horizontal spacing appearing between tables in Outlook. Eventually, the only way I could find out what was actually causing it was to send my email to my Outlook 2007 account and grab the raw message content / source. Obviously for most email clients you would assume - logically - that the raw message source would be the same as the e-mail i sent out. As usual though Microsoft do their "magic" and translate the mark-up in to bloated gibberish that is barely recognisable as my original.
After poring over the source for a while I spotted two additional style properties in amongst all the nonsense things that were being dynamically generated. These were "mso-table-lspace" and "mso-table-rspace". Don't ask me why Outlook 2007 thought it prudent to add them, my only guess would be that because the HTML renderer used by OL2007 is primarily designed as a WYSIWYG interface, this extra spacing is added to allow users to click between tables and type content. This however makes no sense from the context of "viewing" an e-mail, so my assumption is that I am 100% correct... Thankfully these properties are not automatically overridden, so all I had to do was specify them on each of my tables, and set them to 0pt.
style="mso-table-lspace: 0pt; mso-table-rspace: 0pt;"
- Leave your comment • Category: Coding, Email Building, HTML and CSS
- Share on Twitter, Facebook, Delicious, Digg, Reddit
If at first you don't succeed - make sure the element isn't set to "display:none"
by Developer @ pebbl.co.uk on Saturday, 9 July 2011
Ok, this one is going to be a slight personal moan - but I see this happening time and again with front-end web developers who haven't learned the hard way (i.e. building everything from scratch for themselves). If you are an ardent developer, and you don't find the following obvious, then maybe I should give up and go learn to make traffic cones instead.
So ... If you are having a problem with an DOM Element either not returning an expected dimension (in your own code), or seemingly having it's dimensions incorrectly set (in someone else's library code) usually to NaN or 0... Make sure you are not, or the library code is not, operating on a hidden element!
By hidden I mean having the ever-present "display" attribute set to "none" either on your target element, or on one of it's parents.
Display Nothing...
Setting display none on an element before the browser has rendered anything of that element, will mean that decent browsers will not render anything - at all - of that element..! That is the purpose of display none, it is designed to avoid the rendering of elements you want to hide. This means that accessing offsetWidth or offsetHeight (or any of the other dimension properies) will not return a correctly calculated value, becuase the rendering engine hasn't calculated one.
When is a bug not a bug?
Now the symptoms of this problem can be wide an varied, but I am still amazed at the number of people out there who seem oblivious to this restriction.. and to make matters worse, when coming across this problem either solve it entirely by accident or put it down to a bug in the browser / library they are using.
One such example of this was coming across a post on a forum where the OP was having problems working with the Google Map api. He/She was asking the forum if they had any ideas as the map just wouldn't center/position it's content correctly in IE7. The poster was describing how they were using jQuery to initially hide the map and then fade it in by way of a PopOver... and how that it worked fine when not being faded in. Now this is all well and good, the poster was clear about what they were trying to do. The thing that got me was that not one of the replies had an answer anywhere near the actual cause.
To make it almost comical, one poster actually aluded to it being a "known jquery bug" and linked off to other pages of similar discussions - where thankfully there were a few people who had described the actual cause... if not very clearly.
Examples...
So here are some examples of this confusion out in the wild, at least in each case someone does find an answer - even by way of Facebook's rather cryptic explanation in one. But the fact of the matter for me is they do not seem to fully understand that the actual cause is not a bug:
- http://forum.developers.facebook.net/viewtopic.php?id=95735
- http://forum.developers.facebook.net/viewtopic.php?id=92951
- http://stackoverflow.com/questions/604933/jquery-datatables-table-width-problem
Solution?
So please... If this sounds like it may be your problem, then simply change the method you are using to hide your element in the first place.
.codeSafeHide {
position: absolute;
left: -99999px;
visibility: hidden;
height: 0px;
overflow: hidden;
}
The above will produce almost the same affect as display:none - save for the positive factor that the browser will render and calculate all widths and heights of your target element. Obviously the only value that may need changing (depending on your site) is the -99999px.. although unlikely that you will have a ridiculously wide page - it is possible - so if you are having problems getting the element to disapear off the left of the screen you can use this (although position fixed is not supported by certain older browsers):
.codeSafeHide {
position: fixed;
right: 100%;
visibility: hidden;
height: 0px;
overflow: hidden;
}
- Leave your comment • Category: Coding, HTML and CSS, JavaScript
- Share on Twitter, Facebook, Delicious, Digg, Reddit
Opera 10.10 and multipart form posting problems...
by Developer @ pebbl.co.uk on Sunday, 26 June 2011
Yet again another problem I've come across recently in my travels. Implementing a YouTube Video uploader for work (via Zend/gData API) I found that the standard example form that YouTube/Google suggest for posting videos just failed to work entirely in Opera. Albeit, I was running this in a localhost environment on my MacBook (powered by MAMP) but out the of nearly 10+ different senarios I set up to test this problem, all failed (senarios that all worked on the other top browsers).
Opera did seem to action something when the submit button was pressed (the browser started loading), and I do have to admit I didn't actually test to see if the Videos where actually getting through to YouTube - but the most worrying thing about this problem is what happens next, after the browser has finished it's pretty loading animation...
. .. which is absolutely nothing! The screen just would not refresh to show the newly downloaded page. Instead the old page would remain.
To make matters even more confuddling I ran a test that involved changing the target of the form to open a new window. This did indeed open the new window, but the new page was entirely blank, and inspecting the Dom returned an error in Dragonfly stating something along the lines of "This window has no runtime".
Dragonfly also wouldn't track that a HTTP request had occured - subsequent clicks on the submit button would trigger HTTP calls that were tracked (and that return a succesful URL) but the page still would not refresh/redirect to the newly downloaded page. Very odd...
The only time I could get the form to actually properly post and redirect was when YouTube returned a failure code (400,500 ...) so this led me to believe that Opera has some problems when dealing with upload URLs that 302 redirect somewhere else - which is exacly what YouTube does.
I only had a short time to look in to this, and haven't tested on a live linux server set up yet (that should happen next week) but so far I could not find rhyme nor reason for this strange behaviour (even after extensive Googling). My only option so far has been to disable the video uploads specifically for Opera.
- Leave your comment • Category: Coding, HTML and CSS
- Share on Twitter, Facebook, Delicious, Digg, Reddit
I know it might not sound like something to rally much interest, but when you spend much of your working life submerged in particular technologies - using them to find the best solution to a multitude of problems - it's always nice to hear about a new way to achieve something. Especially when that "certain something" has seemed almost too obvious for words, and really should have been implemented years ago...
I stumbled across this rather useful new strand of the W3C CSS proposals completely by chance - I can't quite recall what it was that I was looking for at the time - but it definitely wasn't anything to do with an entirely new box model. And especially not the kind of box model that could put an end to a great many HTML layout problems; problems that happily fuel the frustrations of web developers everywhere.
Now I've discovered new properties and attributes before, or even completely new methodologies - only to read further in to the documentation and find that the proposal has either been thrown out or isn't even a gleam in any self-respecting browser developer's eye. But what was even more surprising about this particular CSS addition, was that it had already been partially implemented in some of the "contemporary" browsers.
Well I think that's probably enough of an introduction... Was I a little over-the-top? :) Anyway the name of this particular little find is what is currently known as the "Flexible Box Layout" or FlexBox for short - however I do get the feeling this may change before it actually becomes more mainstream.
http://www.w3.org/TR/css3-flexbox/
Flexible Box Model?
Now if you aren't up on web-developer terms, you may have no idea what a Box Model is... If that's so then I'm pretty certain you wont loose any sleep if I don't go in to detail here. Sufficed to say, it is the "building block" logic that controls the way HTML Elements are rendered - in terms of their dimensions and positioning. Whilst that might sound extremely uninteresting, you may at least realise that a change to anything described as a "building block" can have fundamental affects.
In my experience there are two rather major layout desires that crop up time and again when web-developing. And the fact that there is no elegant way of implement a solution for either (with current HTML or CSS) it normally leaves only two options. Using a css/js hack... or the complete avoidance of the problem by design.
Find your center
FlexBox totally solves the vertical center problem. You can have any number of child elements positioned to the exact centers of their parents - along the horizontal and the vertical - without the need to know the parent's specific dimensions. Just like the good old days and the "evil" table cell. :)
To Infinity and ...
I have yet to test this, but as far as I can tell FlexBox should do away with the problem of "premature new line collapse" (more correctly known as wrapping) when dealing with horizontally floated elements. Currently (afaik) if you wish to create - for example - a horizontally scrolling website made of out many floated elements. You have to set the sum total width of the floated children on the parent container. Otherwise the elements will just wrap when then hit the side of the viewport.
Now I know there are possibly white-space css properties or even word-wrapping properties that could be used, but these never seem to work 100% cross-browser, and probably shouldn't be used to affect floated elements. Which is why setting the sum total has always been a better solution.. but obviously this involves either having fixed hard-coded content, or some programmatical way of calculating the width for the parent (PHP, JS). FlexBox actually allows you to control the flow of the children which should do away with this annoying problem entirely - just need to state the flow as being constantly horizontal. Obviously I need to research this though.
- Leave your comment • Category: Coding, HTML and CSS
- Share on Twitter, Facebook, Delicious, Digg, Reddit
Have you every tried building a pixel perfect HTML email that works across a range of destination user-agents?
If the answer is yes then you'll know just how frustrating it can be - especially when you include the likes of Lotus Notes (6.5.4) and Outlook 2007/2010. If no, then it's probably only a matter of time, but I wish you luck in avoiding the problem... :)
Now usually - at my company - we stick to a rather rigid design ethic in order to minimize the problems we'll undoubtedly encounter when trying to build complex layouts for these archaic systems. But the other day we had a request from a Designer who wanted to create a layout that would work across all the usual agents we support (which already includes mobile devices) - but he also wanted the layout to keep it's readability on mobile devices without the need for zooming.
Obviously this is achievable, but it's only achievable using a collapsible template (something we usually employ for websites) and then only using quite modern (but degradable) standards. This causes two problems for email:
- Email is anything but modern - most popular user-agents are at least 5 years old now (if not more).
- Pretty much the only way to achieve a pixel perfect design in an email is to use tables - and tables are anything but "collapsible" or "degradable".
If at first...
Undeterred by the possible problems, we set about testing different ideas, and just experimenting with the different user-agents at our disposal. Initially everything was far too easy, we almost "seemed" to have a layout that worked in the first 30 minutes, using only the old HTML Table attribute "align" and using separate tables like "cells".
The following example shows two separate tables aligned using align="left"
|
Now the same code, but forced to wrap at 200px across, as you can see the layout collapses to the parent's width - which is exactly what we needed
|
So the above handled the collapsing behaviour we needed, but we also required the ability to create "rows" from these left aligned tables. If we just put a whole load of left aligned tables together, they would all wrap along side each other, not really creating the most typical of email layouts... :) So the most obvious way of achieving this was to do what we would normally do in a HTML Email... wrap the aligned tables with another table.
The following shows aligned left tables that would all float in a line, if it weren't for the outer wrapping tables acting as the "row" part of our structure (creating a 2x2 grid).
|
The above all seemed to work perfectly - mainly in Mac Mail, Win/Mac Thunderbird, Internet Explorer, FireFox, Opera, Chrome and Safari and even Entourage (these being the quickest and easiest to test in). But then, as usual, the respective rugs were pulled out from underneath ourselves once we tested the layout in Outlook 2007.
... you don't succeed?
Whoever thought it would be a good idea to use the Microsoft Word WYSIWYG HTML Engine to render HTML Emails in Outlook 2007 and 2010, must have been mad, lazy, lost or ever-so-slightly confused (delete as appropriate). It causes no end of rendering problems for developers, usually with random and inexplicable sizing calculations or padding problems.
Anyway - complaining aside - it took a little while to work out but it seemed there were a few issues befalling the layout in Outlook:
- Outlook 2007 automatically shrunk any table with no width set to the smallest width it could have. This obviously affected our outer tables (our "rows") as these only work in a collapsible design if we don't specify a width. This idea had worked in everything save for OL2007 which just ended up auto-collapsing our structure to it's minimum width (rather useless).
- Our aligned tables did line up as we'd hoped, but there seemed to be inexplicable horizontal spacing (what a surprise!) of about 10px between each table - pretty much useless for a pixel perfect design.
- There also seemed to be about 10px of vertical space between each group of aligned tables (our "rows").
So after the usual trial and erroring we managed to solve the above problems (almost).
- Rather than relying on outer tables to create our "rows" we had to remove any non-dimensioned tables. Instead we used a more web page based approach by using a page break with clearing set to "all" (the value of "both" failed to work in OL2007). This introduced another problem, yet more vertical space between each "row". This was fixed by wrapping the <br clear="all"> tag with a <font> tag with it's size and line-height all set to 0.
- 3 comments • Category: Coding, Email Building, HTML and CSS
- Share on Twitter, Facebook, Delicious, Digg, Reddit