views:

49

answers:

3

I am developing a web application which needs to work with both IE7 and Firefox 3.6. I am laying out the forms using CSS. The forms are pretty standard. I want each div (data-group) on its own line with the labels and options lined up together within the div (as a row). Here is the CSS that works in Firefox:

.data-group 
{
    /*display: inline-block;*/
    vertical-align: top;
    padding-top: 5px;
    padding-bottom: 5px;
}

.editor-label, .option1, .option2
{
    display: inline-block;
    /*float: left;*/
}

.editor-label
{
    width: 250px;
    vertical-align: top;
}

Unfortunately, this does not work in IE7. To achieve the same effect, I have to uncomment the two commented lines and comment the inline-block on .editor-label, .option1, .option2 selection. Unfortunately this breaks Firefox...and this really does not seem like the correct thing to do anyway (the Firefox way makes far more sense). (I can get this way to work in Firefox if I add a "clear" div where I have "clear: both" on that div's class...but this seems like a hack.)

Am I missing something obvious here? Suggestions to help make this work in both browsers would be great.

Edit: Provide requested (trivialized) HTML.

    <div class="data-group">
        <div class="editor-label">
            <%: Html.CheckBoxFor(model => model.cb1)%>
            <%: Html.LabelFor(model => model.label1) %>
        </div>
        <div class="option1">
            <%: Html.TextBoxFor(model => model.tb2)%>
            <div class="label"><%: Html.LabelFor(model => model.label2) %></div>
        </div>
        <div class="validation">
            <div><%: Html.ValidationMessageFor(model => model.tb2) %></div>
        </div>
    </div>

    <!-- Repeat many diff. data-groups. -->
    <div class="data-group">
        <div class="editor-label">
            <%: Html.CheckBoxFor(model => model.cb1)%>
            <%: Html.LabelFor(model => model.label1) %>
        </div>
        <div class="option1">
            <%: Html.TextBoxFor(model => model.tb2)%>
            <div class="label"><%: Html.LabelFor(model => model.label2) %></div>
        </div>
        <div class="validation">
            <div><%: Html.ValidationMessageFor(model => model.tb2) %></div>
        </div>
    </div>
+1  A: 

I use the following code which works in all browsers.

The HTML

<div class="field clearfix">
    <label for="firstName" id="firstNameLabel">First Name</label>
    <input name="firstName" type="text" id="firstName" />
</div>

The CSS

.field {
    margin: 0 0 10px;
}
.field label {
  font-size: 13px;
  padding: 8px 0;
  margin: 0 18px 0 0;
  display: block;
  float: left;
  width: 90px;
}
.field input,
.field textarea {
  width: 220px;
  font-size: 11px;
  line-height: 11px;
  padding: 6px 10px;
  color: #666;
  background: #fff;
  border: 1px solid #c4c6c7;
  display: block;
  float: left;
}

And the all too famous clearfix hack (prevents you from having to add clearing elements. Just set on the parent div that contains floated elements and it works like a charm.

.clearfix:after {
    content: ".";
    display: block;
    height: 0;
    font-size:0;
    clear: both;
    visibility:hidden;
}
   .clearfix{display: inline-block;}
  * html .clearfix {height: 1%;}
  .clearfix {display:block;}

You can fiddle around with the widths to achieve your effect, and also you can set a width on the "field" element as well, if you don't want it to fill up the whole space available.

Good luck.

Marko
A: 

As of this writing, there's no (x)html sample in your question, so I can only offer you methods -rather than specific solutions- to address your problem, but the techniques can be adapted easily enough.

You've got two obvious ways of targeting different browsers (though it's using IE conditional comments, so it's basically targetting IE and non-IE, which might not be as useful as you need), either use a conditional comment to import styles specifically for Internet Explorer:

<head>
    <link rel="stylesheet" type="text/css" href="path/to/general/stylesheet.css" />

    <!--[if ie]>
        <link rel="stylesheet" type="text/css href="path/to/ie/stylesheet.css" />
    <![endif]-->
</head>

The downside of this approach is that you've got two separate stylesheets to maintain.

Or you can use conditional comments to alter the specificity of your selectors:

<body>
    <!--[if ie]>
        <div id="ieOnly">
    <![endif]-->

    <div id="somethingToStyle">
        ...content...
    </div>

    <!--[if ie]>
        </div>
    <![endif]-->
</body>

This approach lets you target IE differently than for proper browsers, without using hacks in your CSS:

body > div#somethingToStyle { /* for non-IE */ }

body > div#ieOnly > div#somethingToStyle { /* IE only */}

The downside of this method is that you've got all the styles in one stylesheet, which can make it rather unwieldy to maintain long-term.

Also, it's worth mentioning that using a CSS reset stylesheet, and a valid doctype, will likely reduce the impact of major browser differences (though not all, obviously).

David Thomas
Why use hacks for something that can be achieved with one piece of code for all browsers?
Marko
@Marko, which part in particular is a 'hack'? The first approach simply targets a different stylesheet for IE, which is pretty non-hacky. The second makes the (x)html a little messier, but still allows one to target styles specifically to a particular browser.
David Thomas
+1  A: 

inline-block doesnt properly work in IE, http://work.arounds.org/using-inline-block-ie7-and-ie6/

You have to re-declare display:inline; on the block level elements that are given inline-block to IE. Meaning you have to redeclare the rule for the div you gave inline-block in a separate rule.

<!--[if lt IE 8]>
<style>
.editor-label { display:inline; zoom:1; }
</style>
<![endif]-->

This will most likely solve your issue. If not, please post a demo.

meder