9. July 2008 12:19 by Janko in Tutorials | tags: , ,

When creating a web form you have to make a functional and visually aligned layout. The simplest way to do this is to place elements in a table or by fixing width of labels. Tables stretch its cells according to width of largest element in a column. That way you can have aligned form. Fixing label width will also also allow you to have a hard-coded but aligned form.

But...

But what if you don't want to use tables? Or what if you don't know how long labels could be because you are developing an application that has many localized strings? And you still want to align input elements according to the width of the longest label?

Then you can justify all labels. By justifying I mean to make them same width. And the simplest way to do this is to use jQuery.

If you create a simple web form like in the example below all you have to do is to set float:left and display:block for both label and input field.

without justify

These are the CSS styles:

label, input[type="text"]{
    float:left;
    display:block;
}
label
{
    margin-right: 5px;
}
.field{
    width:100%;
    overflow:auto;
    margin:5px 0px;
}

Looks familiar? Usually, you would set the width of labels to some number, let's say 100px. But, let's do another thing. Let's calculate the width of a largest label and apply it to all labels. jQuery function below does exactly what I wrote:

$(document).ready(function() {
    var max = 0;
    $("label").each(function(){
        if ($(this).width() > max)
            max = $(this).width();   
    });
    $("label").width(max);
});

And visually it looks like this:

with justify

Conclusion

It's easy to do many tricks using jQuery and this was just one of them. Maybe there are simplest solutions to do this, but it was fun to see putting jQuery in most common scenarios like this one.

If you liked this article why don't you share it:

Stumble it delicious Digg it Float it DZone it Kick it Bump it E-mail

Comments

Pingbacks and trackbacks

  1. Trackback from DotNetKicks.com Justify elements using jQuery and CSS
  2. Trackback from Design Bump Story on DesignBump.com
  3. Pingback from nazzz.wordpress.com Justify elements using jQuery « Nazzz Blog
  4. Pingback from montadaphp.net ÇÓÊÝÓÇÑ ããßä ãÓÇÚÏÉ ... ßæÏ ajax - ãäÊÏì Èí ÇÊÔ Èí PHP
  5. Pingback from cnet.ro Janko ÅŸi aspectul formularelor web | CNET.ro
  6. Pingback from noupe.com Form Elements: 40+ CSS/JS Styling and Functionality Techniques
  7. Pingback from anieto2k.com Mejora los elementos de tus formularios con CSS/JS | aNieto2K
  8. Pingback from webdesignjunkies.com Form elements 40 css js styling and functionality techniques
  9. Pingback from ricoterox.com.ar Mejora los elementos de tus formularios con CSS/JS » Ricotero's Blog
  10. Pingback from canturko.com 40 Adet Css Template ve EÄŸitimi |
  11. Pingback from thetoptenme.wordpress.com “The Complete Guide” for jQuery Developer- Reblog « Dynamic Disruption
  12. Pingback from edgargranados.es 40 Elementos interesantes para tus formularios | Edgar Granados
  13. Pingback from tutorialo.com Justify elements using jQuery and CSS
  14. Pingback from sandeepmundra.com S A N D E E P [ I N D I A N I C ] » Blog Archive » Form Elements: 40+ CSS/JS Styling and Functionality Techniques
  15. Pingback from hmvrulz.wordpress.com Form Elements: 40+ CSS/JS Styling and Functionality Techniques « HMV.co.in
  16. Pingback from feo.yi.org FeoLab » Blog Archive » 40+ CSS/JS 樣式小工具
  17. Pingback from sulvision.com Form Elements: 40+ CSS/JS Styling and Functionality Techniques | Css Edge
  18. Pingback from blog.thuongtin.net Một số Flugin của JQuery trang trí cho form | IT Blog
  19. Pingback from yogodoshi.com Top 50 plugins gratuitos e tutoriais para jQuery - Blog do yogodoshi
  20. Pingback from noupe.com Making of Beautiful Forms - Designing, Styling, Validating & Ajax Work | Noupe
  21. Pingback from wpconfig.com Wordpress Blog Services - Beautiful Forms - Design, Style, & make it work with PHP & Ajax
  22. Pingback from invisibile.org Beautiful Forms - Design, Style, & make it work with PHP & Ajax
  23. Pingback from reader.misrit.org 10 AJAX-based & PHP WebMail Clients For a Great User Experience | Feed Reader (Beta)
  24. Pingback from ajaxupdates.com Justify elements using jQuery and CSS
  25. Pingback from angelspicks.com Angels Picks » Blog Archive » Beautiful Forms - Design, Style, & make it work with PHP & Ajax
  26. Pingback from neurosoftware.ro Beautiful Forms - Design, Style, & make it work with PHP & Ajax | Programming Blog
  27. Pingback from iwanna.cn 我想网 » Blog Archive » 表单元素:40个CSS/JS风格和功能技术处理
  28. Pingback from keanhui.com Beautiful Forms - Design,Style, & make it work with PHP & Ajax | KeanHuiâ„¢ - Creative Resources and Inspirations
  29. Pingback from webmaster.cyooh.com Form Elements: 40+ CSS/JS Styling and Functionality Techniques | Webmaster Zone - Cyooh.com
  30. Pingback from codetricky.com Trick out your forms with jQuery. 6 Plugins and Tutorials. | CodeTricky
  31. Pingback from my2tech.com Form Elements: 40+ CSS/JS Styling and Functionality Techniques | www.my2tech.com

Add comment

   
        Country flag
biuquote
Loading