将CSS样式应用于<div>

pho*_*tom 6 html css

我的问题是下面的HTML

<div class="editor-container">
   <div class="editor-row curFocus">
       <div class="editor-label">
           <label for="FirstName">First Name</label>
       </div>
       <div class="editor-field">
          <input class="text-box single-line valid" id="FirstName" 
                name="FirstName" type="text" value="Nancy" maxlength="20">
       </div>
   </div>
</div>
Run Code Online (Sandbox Code Playgroud)

当用户选择输入字段时,我通过一些javascript将类"curFocus"添加到外部div以突出显示标签和输入字段.

我的css是 -

.editor-container {
    border: thin solid #444444;
    display: table; width: 100%;
 }
.editor-row {
  width: 100%; display: table-row;
}
.editor-label {
  padding-left: .4em; width: 40%;
}
.editor-label, .editor-field {
   padding-right: .4em; padding-bottom: .2em; padding-top: .2em;
   display: table-cell;
 }
 .curFocus {
   border: 2px solid #05365b;
   background-color: #d3e5f2;
   margin: 3px; padding: 3px;
  }
Run Code Online (Sandbox Code Playgroud)

我的问题是,在Chrome 12和IE9中使用调试器时,它们都显示应用于外部div的边框设置.但是,在查看表单时,浏览器都不会显示指定的边框.所有其他CSS设置正常工作.我也尝试将".curFocus"的定义更改为".curFocus div".但是这也将样式应用于每个嵌套div,但确实在所有div上显示了边框.

虽然我不是CSS专家,但为什么这不起作用并不明显.


编辑这里是jsfiddle链接 - http://jsfiddle.net/photo_tom/KmsF5/1/.在测试它时,如果在IE7兼容模式下,它在IE9中可以正常工作.否则,它无法正确显示.抱歉不包括链接,仍然使用jsfiddle甚至存在的事实.

hug*_*hes 5

好吧,我可以告诉您是什么原因造成的,但我不能告诉您原因。的元素display: table-row;不能具有边框。您可以将边框应用于元素的table-cell.curFocus元素,但不能应用于元素table-row本身。

同样,不知道为什么存在这个愚蠢的规则,但是您可以使用一些CSS来解决问题:

.curFocus {
   background-color: #d3e5f2;
   margin: 3px; padding: 3px;
}

.curFocus>div {
   border: 2px solid #05365b;
   border-width: 2px 0px;  /* top and bottom border for all the table-row's immediate children (table-cells) */
}

.curFocus>div:first-child {
    border-width: 2px 0px 2px 2px; /* left border for the leftmost table-cell */
}

.curFocus>div:last-child {
    border-width: 2px 2px 2px 0px; /* right border for the rightmost table-cell */
}
Run Code Online (Sandbox Code Playgroud)

参见http://jsfiddle.net/d772N/