2017-09-01 31 views
-1

我目前有一個帶有1個按鈕的表單,如果我嘗試添加另一個按鈕,整個css都會被擰緊。表格中的2個按鈕

這是我希望它看起來像:http://prntscr.com/gfuvi8

但是,如果提交按鈕<a href="?do=fetch" class="button"><i class="fa fa-refresh"></i> Fetch Exchange</a>

下加入這一行這是我得到:http://prntscr.com/gfuwka

.tablesection { 
 
    \t padding: 20px; 
 
    \t border-left: 1px solid #e4e4e4; 
 
    \t border-right: 1px solid #e4e4e4; 
 
    \t border-bottom: 1px solid #e4e4e4; 
 
    \t background: #ffffff; 
 
    } 
 
    
 
    .tablesection label { 
 
    \t width: calc(50% - 10px); 
 
    \t float: left; 
 
    \t margin: 0px 0px 10px 0px; 
 
    } 
 
    
 
    .tablesection label:nth-child(odd) { 
 
    \t margin: 0px 20px 10px 0px; 
 
    } 
 
    
 
    .tablesection input, .tablesection select, .tablesection textarea { 
 
    \t width: calc(50% - 10px); 
 
    \t margin: 0px 0px 20px 0px; 
 
    \t padding: 10px; 
 
    \t display: block; 
 
    \t float: left; 
 
    \t border: 1px solid #cecece; 
 
    \t -webkit-border-radius: 0; 
 
    \t border-radius: 0; 
 
    } 
 
    
 
    .tablesection textarea { 
 
    \t resize: vertical; 
 
    } 
 
    
 
    .tablesection input:nth-child(odd), .tablesection select:nth-child(odd) { 
 
    \t margin: 0px 20px 20px 0px; 
 
    } 
 
    
 
    .tablesection input { 
 
    \t padding: 11px 10px; 
 
    \t width: calc(50% - 10px); 
 
    } 
 
    
 
    .tablesection input.submit { 
 
    \t width: auto; 
 
    \t border: 0px; 
 
    \t margin: 0px 0px 0px 0px; 
 
    \t background: #54809b; 
 
    \t color: #ffffff; 
 
    \t text-decoration: none; 
 
    \t display: block; 
 
    \t float: none; 
 
    \t font-size: 11px; 
 
    \t padding: 12px 20px; 
 
    \t -webkit-transition: background 0.5s linear; 
 
    \t -moz-transition: background 0.5s linear; 
 
    \t -ms-transition: background 0.5s linear; 
 
    \t -o-transition: background 0.5s linear; 
 
    \t transition: background 0.5s linear; 
 
    \t clear: both; 
 
    } 
 
    
 
    .tablesection input.submit:hover { 
 
    \t background: #4d758d; 
 
    } 
 
    
 
    .tablesection input.submit::before { 
 
    \t display: block; 
 
    \t content: ' '; 
 
    \t clear: both; 
 
    }
<div class="tablesection"> 
 

 
\t <form action="" method="post"> 
 
\t 
 
\t \t <label for="usd">USD</label> 
 
\t \t <label for="cad">CAD</label> 
 

 
\t \t <input id="usd" type="text" name="usd" value="<?php echo $siteUsd; ?>" maxlength="10" required="required" /> 
 
\t \t <input id="cad" type="text" name="cad" value="<?php echo $siteCad; ?>" maxlength="10" required="required" /> 
 
\t 
 
\t \t <label for="gbp">GBP</label> 
 
\t \t <label for="eur">EUR</label> 
 

 
\t \t <input id="gbp" type="text" name="gbp" value="<?php echo $siteGbp; ?>" maxlength="10" required="required" /> 
 
\t \t <input id="eur" type="text" name="eur" value="<?php echo $siteEur; ?>" maxlength="10" required="required" /> 
 
\t \t 
 
\t \t <label for="aud">AUD</label> 
 
\t \t <label for=""></label> 
 

 
\t \t <input id="aud" type="text" name="aud" value="<?php echo $siteAud; ?>" maxlength="10" required="required" /> 
 

 
\t <div class="clear"> 
 
\t </div> 
 
\t <input type="submit" class="submit" value="Update Exchange Rates" /> 
 
\t <a href="?do=fetch" class="button"><i class="fa fa-refresh"></i> Fetch Exchange</a> 
 

 
\t </form> 
 
</div>

.content a.button:first-of-type { 
    margin: 20px 0px 0px 0px; 
} 

.content a.button { 
    background: #54809b; 
    color: #ffffff; 
    text-decoration: none; 
    display: block; 
    float: left; 
    margin: 20px 0px 0px 16px; 
    font-size: 11px; 
    padding: 12px 20px; 
    -webkit-transition: background 0.5s linear; 
    -moz-transition: background 0.5s linear; 
    -ms-transition: background 0.5s linear; 
    -o-transition: background 0.5s linear; 
    transition: background 0.5s linear; 
} 

.content a.button:hover { 
    background: #4d758d; 
} 

.content a.button i { 
    margin: 0px 4px 0px 0px; 
    font-size: 10px; 
} 

剛好有2個按鈕是這樣的:

<a href="?do=search" class="button"><i class="fa fa-search"></i> Search Coupons</a> 
<a href="?do=add" class="button"><i class="fa fa-plus"></i> Add Coupon</a> 

那麼它的罰款,他們彼此接近的問題是在形式

回答

0

input.submit元素沒有浮動,而a.button是。這將a.button推到input.submit以下,並且邊緣不一致,所以如果沒有發生,它們不會排隊。我會提出兩種方法來修復它,一個最小(只改變兩個css屬性),另一個我認爲更容易閱讀和維護,但是會稍微改變CSS的結構。

無論哪種方式,我建議更改邊距。將右邊的16像素邊距放在右側,不需要使用單獨的規則將其從第一個元素中移除,並且溢出包含元素的邊距無關緊要,因爲在計算寬度和高度時不包括它們。

最小版本
第1步:設置input.submitfloat: left,像.button
第2步:改變input.submit保證金以匹配.button,或者反之亦然,
例如,input.submit {margin:20px 16px 0px 0px; }`

.tablesection input.submit { 
     width: auto; 
     border: 0px; 
     margin: 20px 16px 0px 0px; /* copy the a.button margins */ 
     background: #54809b; 
     color: #ffffff; 
     text-decoration: none; 
     display: block; 
     float: left; /* left instead of none keeps subsequent floats on the same line */ 
     font-size: 11px; 
     padding: 12px 20px; 
     -webkit-transition: background 0.5s linear; 
     -moz-transition: background 0.5s linear; 
     -ms-transition: background 0.5s linear; 
     -o-transition: background 0.5s linear; 
     transition: background 0.5s linear; 
     clear: both; 
    } 

這會讓他們內聯,然後你可能會想要設置爲一致的高度。

更好的(在我看來)版本 你有很多的input.submita.button之間共享樣式 - 你可以考慮這些組合成一個共同的.button類(注意沒有在此選擇一個標籤,這意味着規則適用於任何與班級有關的事情)。
這裏我會成立了一致float: left;和一致的利潤(例如20px 16px 0px 0px
如果您還需要爲input.submit做具體的樣式,你可以給元素兩個類,它們將分別應用於。

.content .button { 
 
    background: #54809b; 
 
    color: #ffffff; 
 
    text-decoration: none; 
 
    display: block; 
 
    float: left; 
 
    margin: 20px 16px 0px 0px; 
 
    font-size: 11px; 
 
    padding: 12px 20px; 
 
    -webkit-transition: background 0.5s linear; 
 
    -moz-transition: background 0.5s linear; 
 
    -ms-transition: background 0.5s linear; 
 
    -o-transition: background 0.5s linear; 
 
    transition: background 0.5s linear; 
 
} 
 

 
.content .button:hover { 
 
    background: #4d758d; 
 
} 
 

 
.content .button i { 
 
    margin: 0px 4px 0px 0px; 
 
    font-size: 10px; 
 
} 
 
.tablesection input.submit { 
 
    /* Styles that are unique to input.submit */ 
 
}
<div class ="content"> 
 

 
    <div class="tablesection"> 
 

 
     <input type="submit" class="button submit" value="Update Exchange Rates"> 
 
     <a href="?do=fetch" class="button"> 
 
      <i class="fa fa-refresh"></i> 
 
      Fetch Exchange 
 
     </a> 
 

 
    </div> 
 

 
</div>

編輯:這些解決方案都共享一個缺陷,這就是浮動元素不會影響其容器的高度。爲了解決這個問題,你可以在它們後面放置一個虛擬元素來填充容器,例如:<div style="height:60px;"></div>。此方法中的重要之處在於,虛擬div的高度與浮動元素的高度+頂部邊距相同。 或者,您可以將float:left一起刪除,並將其設置爲display:inline-block,而在進行佈局時,這比浮動更容易使用。

+0

這會發生什麼,當我嘗試最低限度verysion http://prntscr.com/gfw69i – empire

+0

啊,浮動元素不會在計算高度時被考慮。您可以添加一個虛擬元素來填充它們覆蓋的空間,或者可以將它們更改爲'display:inline-block'而不處理浮動。更新我的答案 – Armando

0

你可以讓自己的立場relative並添加float風格:

.btn-float {  
 
    position: relative; 
 
    width: auto; 
 
    float: left; 
 
    padding-left: 15px; 
 
}
<input class="btn-left" type="submit" class="submit" value="Update Exchange Rates" /> 
 
<a class="btn-right" href="?do=fetch" class="button"><i class="fa fa-refresh"></i> Fetch Exchange</a>

0

.button { 
 
     width: 23%; 
 
     display: block; 
 
     text-align: center; 
 
     line-height: 3rem; 
 
     height: 3rem; 
 
     font-size: 1.2rem; 
 
     border: 1px solid #ccc; 
 
     background: #fafafa; 
 
    } 
 
    .button-container{ 
 
     margin-bottom: 20px; 
 
     width: 100%; 
 
    } 
 
    .button-container::after{ 
 
     content: ''; 
 
     display: block; 
 
     visibility: hidden; 
 
     clear: both; 
 
    } 
 
    .button-float{ 
 
     margin-left: 2%; 
 
     float: left; 
 
    }
<div class="button-container"> 
 
    <button class="button button-float">BTN-1</button> 
 
    <button class="button button-float">BTN-2</button> 
 
</div> 
 

 
<div class="button-container"> 
 
    <a href="?do=fetch" class="button button-float"><i class="fa fa-refresh"></i> Fetch Exchange</a> 
 
    <a href="?do=fetch" class="button button-float"><i class="fa fa-refresh"></i> Fetch Exchange</a> 
 
</div>

我想我知道是錯的。 您應該將這兩個按鈕放在潛水或其他物品中,然後設置float left,然後清除DIV上的浮動。

如果使用引導程序,你可以只是包裝這些按鈕在col-md-12

的目標是讓button浮動,然後清除它的父容器上的浮動。


添加

OK,只需添加.button-container.button-container::after.button-float在你的CSS文件,並與.button-container類包住2個btns與div,加.button-float類的2個btns,會做。

希望這可以幫助你。

+0

1是一個提交表單按鈕^^ – empire

+0

@Empire,如果您不在CSS規則中包含標籤名稱,則無關緊要。 – Armando