2017-10-20 175 views
0

請注意:我已經看了幾個帖子SO和各種建議浮動並排2 div的一面,但他們都不似乎爲我工作。浮動DIV左,右按鍵

的建議摘要如下:

display: inline-block 
float: left 

他人蔘考overflow: hiddenoverflow: auto各種實施方案。

一個工作過,我需要確立正確的div

position: absolute; 
right: 0px 

這是不理想的,因爲該按鈕將自身附着右側,忽略所有父容器的約束。

enter image description here

上面就是我想要什麼來實現的。

div有藍色的背景。 右側 div包含按鈕。

我的代碼:

的Html

<div class="row"> 
       <div class="display-inline-block float-left"> 
        <h1>Your Order Schedule 
         <a id="editScheduleName" onclick="changeScheduleName()"> 
          <img class="schedule-heading small-image" src=""images/icons/edit.png"> 
         </a> 
        </h1> 
       </div> 
       <div class="display-inline-block float-right"> 
        <input id="btnScheduleStatus" type="button" class="btn button-status btn-success" value="my button"> 
       </div> 
      </div> 

的CSS 注:使用自舉的基礎,我的大部分CSS的需要

.display-inline-block { 
    display: inline-block; 
} 

.schedule-heading { 
    position: relative; 
} 

.small-image { 
    width: 30px; 
    height: 30px; 
    margin-bottom: 5px; 
} 

.button-status { 
    width: 120px; 
    height: 50px; 
    font-weight: bold; 
    font-size: 18px; 
} 

幫助將非常讚賞

+0

退房CSS'flexboxes'; 'display:flex; justify-content:space-between'可以解決你的問題。 –

+0

你的行應該是100%的寬度,並且50%的子div都應用浮動左邊。 '.row {width:100%} .display-inline-block {width:50%,float:left}'不要忘記flex是Nandu Kalidindi建議的更好的選擇 – Muzammil

回答

1

沒有任何變化的CSS,純粹使用引導:

幾個關鍵的事情:確保您指定<div class="row">

後添加列(<div class="col-md-12">)可以使用pull-left & pull-right類浮動內容:

<div class="row"> 
    <div class="col-md-12"><!-- define columns in bootstrap --> 
     <div class="pull-left"><!-- use pull-left --> 
      <h1> 
       Your Order Schedule 
       <a id="editScheduleName" onclick="changeScheduleName()"> 
        <img class="schedule-heading small-image" src="images/icons/edit.png"> 
       </a> 
      </h1> 
     </div> 
     <div class="pull-right"><!-- use pull-right --> 
      <input id="btnScheduleStatus" type="button" class="btn button-status btn-success" value="my button"> 
     </div> 
    </div> 
</div> 

Fiddle

這比老版本的Internet Explorer不支持display:flex更好的整體瀏覽器支持。

1

.row{ 
 
    display: flex; 
 
    justify-content: space-between; 
 
    align-items: center; 
 
} 
 
.display-inline-block { 
 
    display: inline-block; 
 
} 
 

 
.schedule-heading { 
 
    position: relative; 
 
} 
 

 
.small-image { 
 
    width: 30px; 
 
    height: 30px; 
 
    margin-bottom: 5px; 
 
} 
 

 
.button-status { 
 
    width: 120px; 
 
    height: 50px; 
 
    font-weight: bold; 
 
    font-size: 18px; 
 
}
<div class="row"> 
 
    <div class="display-inline-block float-left"> 
 
    <h1>Your Order Schedule 
 
     <a id="editScheduleName" onclick="changeScheduleName()"> 
 
          <img class="schedule-heading small-image" src="images/icons/edit.png"/> 
 
         </a> 
 
    </h1> 
 
    </div> 
 
    <div class="display-inline-block float-right"> 
 
    <input id="btnScheduleStatus" type="button" class="btn button-status btn-success" value="my button"> 
 
    </div> 
 
</div>

.row{ 
    display: flex; 
    justify-content: space-between; 
    align-items: center; 
} 

嘗試使用display: flex

您可以在谷歌搜索,你可以輕鬆地學習display: flex

+0

首先感謝您的快速響應。雖然我忘了提及它,但我已經使用了flex。 flex的問題是,在父div中使用它,左邊和右邊的div是孩子,左邊的div顯示爲需要,但是按鈕在float-right存在的情況下向左浮動。我現在要實施你的建議 – KGCybeX

+0

@KGCybeX你能接受嗎? – zynkn