Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After over an hour of tweaking, testing, and trying different styles of markup, I think I may have a decent solution. The requirements for this particular project were:</p> <ol> <li>Inputs must be on their own line.</li> <li>Checkbox inputs need to align vertically with the label text similarly (if not identically) across all browsers.</li> <li>If the label text wraps, it needs to be indented (so no wrapping down underneath the checkbox).</li> </ol> <p>Before I get into any explanation, I'll just give you the code:</p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-css lang-css prettyprint-override"><code>label { display: block; padding-left: 15px; text-indent: -15px; } input { width: 13px; height: 13px; padding: 0; margin:0; vertical-align: bottom; position: relative; top: -1px; *overflow: hidden; }</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;form&gt; &lt;div&gt; &lt;label&gt;&lt;input type="checkbox" /&gt; Label text&lt;/label&gt; &lt;/div&gt; &lt;/form&gt;</code></pre> </div> </div> </p> <p>Here is the working example in <a href="http://jsfiddle.net/t8EGn/6/" rel="noreferrer">JSFiddle</a>.</p> <p>This code assumes that you're using a reset like Eric Meyer's that doesn't override form input margins and padding (hence putting margin and padding resets in the input CSS). Obviously in a live environment you'll probably be nesting/overriding stuff to support other input elements, but I wanted to keep things simple.</p> <p>Things to note:</p> <ul> <li>The <code>*overflow</code> declaration is an inline IE hack (the star-property hack). Both IE 6 and 7 will notice it, but Safari and Firefox will properly ignore it. I think it might be valid CSS, but you're still better off with conditional comments; just used it for simplicity.</li> <li>As best I can tell, the only <code>vertical-align</code> statement that was consistent across browsers was <code>vertical-align: bottom</code>. Setting this and then relatively positioning upwards behaved almost identically in Safari, Firefox and IE with only a pixel or two of discrepancy.</li> <li>The major problem in working with alignment is that IE sticks a bunch of mysterious space around input elements. It isn't padding or margin, and it's damned persistent. Setting a width and height on the checkbox and then <code>overflow: hidden</code> for some reason cuts off the extra space and allows IE's positioning to act very similarly to Safari and Firefox.</li> <li>Depending on your text sizing, you'll no doubt need to adjust the relative positioning, width, height, and so forth to get things looking right.</li> </ul> <p>Hope this helps someone else! I haven't tried this specific technique on any projects other than the one I was working on this morning, so definitely pipe up if you find something that works more consistently. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload