2015-04-01 53 views
1

我對於其左側有圖標的輸入具有以下HTML結構。錶行寬度正在改變,以匹配不在表格行內的元素

  <div class="fieldset"> 
       <p>Normal Input</p> 
       <div> 
        <span><i class="icon-cart"></i></span> 
        <input name=""> 
       </div> 
      </div> 

我使用display:table屬性的結構類型爲以下的圖像中表現出所需的佈局:根據標題

enter image description here

但是,圖標(<span>)是拉伸它的寬度(<p>),正如下圖所示:

enter image description here

這裏使用當前CSS:

/*Fieldsets*/ 
.fieldset { 
    width: 100%; 
    display: table; 
    position: relative; 
    white-space: nowrap; 
    margin-bottom: 15px; 
} 

.fieldset:last-of-type { 
    margin-bottom: 0; 
} 

     /*Fieldsets > Labels*/ 
     .fieldset > p { 
      width: 1%; 
      margin-bottom: 3px; 
     } 

     /*Fieldsets > Input Container*/ 
     .fieldset > div { 
      display: table-row; 
      position: relative; 
     } 

      .fieldset > div > * { 
       display: table-cell; 
       white-space: nowrap; 
       vertical-align: middle; 
       position: relative; 
      } 

      /*Fieldsets > Input + Icon*/ 
      .fieldset > div > span { 
       border: 1px solid #B0C2CE; 
       padding: 5px 15px; 
       font-weight: bold; 
       width: 1%; 
      } 

      /*Fieldsets > Input + Icon Senarios*/ 
      .fieldset > div > span:first-of-type { 
       border-right: 0; 
       border-radius: 4px 0 0 4px; 
      } 

      .fieldset > div > span:not(:only-of-type) + input { 
       border-radius: 0; 
      } 

      .fieldset > div > input + span { 
       border-left: 0; 
       border-radius: 0 4px 4px 0; 
      } 

      .fieldset > div > span + input, 
      .fieldset > div > span + textarea, 
      .fieldset > div > span + select, 
      .fieldset > div > span + .select-dropdown-single .select-dropdown-input, 
      .fieldset > div > span + .select-dropdown-multi .select-input { 
       border-radius: 0 4px 4px 0; 
      } 

      /*Fieldsets > Input + Help ToolTip Icon*/ 
      .fieldset > div [class^="tooltip-"], 
      .fieldset > div [class*=" tooltip-"] { 
       text-align: center; 
       width: 30px; 
      } 

我怎樣才能阻止根據<p>它上面改變圖標的​​寬度?甚至沒有包含在同一個Div內。

+0

你不能給一個固定的寬度爲'.fieldset> div' – Abhi 2015-04-01 14:05:02

回答

0

下面是我將如何處理這個問題。你的標記是好的,不需要改變。

首先,我會將display: table應用於子div,並讓p元素爲常規塊或內聯塊(取決於您的設計)。

您現在可以將第一個表格單元元素(span)的寬度設置爲1%,並獲得您需要的縮小到適合的寬度。

在您的原始CSS中,p元素由匿名錶格單元格和表格行元素包裝。最終結果是第一個表格單元列的寬度由文本的寬度決定,而不是圖標的寬度。

/*Fieldsets*/ 
 

 
.fieldset { 
 
    width: 100%; 
 
    margin-bottom: 15px; 
 
    border: 1px dotted blue; 
 
} 
 
.fieldset:last-of-type { 
 
    margin-bottom: 0; 
 
} 
 
/*Fieldsets > Labels*/ 
 

 
.fieldset > p { 
 
    display: inline-block; 
 
    margin-bottom: 3px; 
 
    border: 1px dotted gray; 
 
} 
 
/*Fieldsets > Input Container*/ 
 

 
.fieldset > div { 
 
    display: table; 
 
} 
 
.fieldset > div > * { 
 
    display: table-cell; 
 
    white-space: nowrap; 
 
    vertical-align: middle; 
 
    position: relative; 
 
} 
 
/*Fieldsets > Input + Icon*/ 
 

 
.fieldset > div > span { 
 
    border: 1px solid #B0C2CE; 
 
    padding: 5px 15px; 
 
    font-weight: bold; 
 
    width: 1%; 
 
} 
 
/*Fieldsets > Input + Icon Senarios*/ 
 

 
.fieldset > div > span:first-of-type { 
 
    border-right: 0; 
 
    border-radius: 4px 0 0 4px; 
 
} 
 
.fieldset > div > span + input { 
 
    border-radius: 2px; 
 
    width: auto; 
 
} 
 
.fieldset > div > input + span { 
 
    border-left: 0; 
 
    border-radius: 0 4px 4px 0; 
 
} 
 
.fieldset > div > span + input, 
 
.fieldset > div > span + textarea, 
 
.fieldset > div > span + select, 
 
.fieldset > div > span + .select-dropdown-single .select-dropdown-input, 
 
.fieldset > div > span + .select-dropdown-multi .select-input { 
 
    border-radius: 0 4px 4px 0; 
 
} 
 
/*Fieldsets > Input + Help ToolTip Icon*/ 
 

 
.fieldset > div [class^="tooltip-"], 
 
.fieldset > div [class*=" tooltip-"] { 
 
    text-align: center; 
 
    width: 30px; 
 
}
<div class="fieldset"> 
 
    <p>Normal Input</p> 
 
    <div> 
 
    <span><i class="icon-cart">Icon</i></span> 
 
    <input name=""> 
 
    </div> 
 
</div>

+0

工作就像一個魅力,我沒有嘗試這個唯一的原因是因爲我認爲你需要有結構設置爲錶行,然後單元格,而不是表格,然後單元格。保持現狀會好嗎?表格然後單元格?沒有一排? – Leo 2015-04-01 15:41:13

+1

如果需要確保元素保持在行內,則只需定義表格行元素。如果您定義了表格然後表格單元格,那麼CSS佈局引擎會在內部生成一個匿名錶格行元素。 – 2015-04-01 16:03:18

+0

所以使用yes是安全的? – Leo 2015-04-01 16:04:12

0

試試這個:

/*Fieldsets > Input + Icon*/ 
.fieldset > div > span { 
    border: 1px solid #B0C2CE; 
    padding: 5px 15px; 
    font-weight: bold; 
    width: 10px; // This will set the width 
    display:inline-block; // Add this and width will work. 
} 
+0

它將工作,但不是我所想要的是,跨度的高度將不再適應匹配它旁邊的單元格的高度。 – Leo 2015-04-01 14:18:11

+0

然後在輸入上增加一個高度以匹配跨度的高度? – 2015-04-01 14:22:06

+0

我需要它,所以它會自動適應 - 這是將它放在桌子內的全部要點。請問是否有另一種實現保存結果的方式? – Leo 2015-04-01 14:23:01

0

,我建議你到你p標籤放在外面.fieldset。所以它不會在顯示屏上顯示:表。我覺得很有道理不把塊元素在表根級別像

<table> 
    <p></p> 
    <tr> 
    <td></td> 
    </tr> 
</table> 

通過移動p你外面將刪除的影響是p事業顯示你的元素:表細胞

Demo

或更改你的CSS,所以顯示:塊元素不會內顯示:表元素

相關問題