2017-08-25 52 views
1

我有我的div內浮動按鈕的問題。當我設置按鈕float屬性,它不僅移動左/右,但略有改變位置和向上移動這不是我想要的......爲什麼浮動移動我的按鈕

這裏是我工作的

http://jsfiddle.net/o1u1cLbm/2/

<div id='wrapper' style='display:flex; height:88px;align-items:center;text-align: center;background-color:yellow'> 
<div style="background-color:blue"> 
    <h1> 
    Some header text 
    </h1> 
</div> 
<div style='flex:1;background-color:red'> 
    <div style='display:block; vertical-align:middle'> 
    <button style="height:24px;"> 
     Some stuff 
     </button> 
    <button style="height:40px"> 
     Other stuff 
    </button> 
    </div> 

</div> 

的按鈕必須在容器中標題文本後將增長到最大空間。現在,我想第一個小按鈕,接近冠軍,並在右側的大個這樣

http://jsfiddle.net/o1u1cLbm/3/

<div id='wrapper' style='display:flex; height:88px;align-items:center;text-align: center;background-color:yellow'> 
    <div style="background-color:blue"> 
     <h1> 
     Some header text 
     </h1> 
    </div> 
    <div style='flex:1;background-color:red'> 
     <div style='display:block; vertical-align:middle'> 
     <button style="height:24px;float:left"> 
      Some stuff 
      </button> 
     <button style="height:40px;float:right"> 
      Other stuff 
     </button> 
     </div> 

    </div> 
</div> 

正如你可以看到後我加入float屬性的按鈕,它是不再以父div爲中心,而是依附於頂層。

如何浮動按鈕而不改變它們的垂直位置只有CSS?

+0

爲什麼要使用內嵌樣式? – LGSon

+0

爲了快速顯示我的問題,在我的應用程序樣式中定義了css文件 – Nadir

+0

非常棒........ :) – LGSon

回答

1

沒有必要floats,您可以使用flexbox,然後使用margin-left:auto持續flex-item

<div id='wrapper' style='display:flex; height:88px;align-items:center;text-align: center;background-color:yellow'> 
 
    <div style="background-color:blue"> 
 
     <h1> 
 
     Some header text 
 
     </h1> 
 
    </div> 
 
    <div style='flex:1;background-color:red'> 
 
     <div style='display:flex;align-items:center;'> 
 
     <button style="height:24px;"> 
 
      Some stuff 
 
      </button> 
 
     <button style="height:40px;margin-left:auto;"> 
 
      Other stuff 
 
     </button> 
 
     </div> 
 
     
 
    </div> 
 
</div>

或者你可以使用justify-content: space-between;to flex-container

<div id='wrapper' style='display:flex; height:88px;align-items:center;text-align: center;background-color:yellow'> 
 
    <div style="background-color:blue"> 
 
    <h1> 
 
     Some header text 
 
    </h1> 
 
    </div> 
 
    <div style='flex:1;background-color:red'> 
 
    <div style='display: flex;width: 100%;justify-content: space-between;align-items: center;'> 
 
     <button style="height:24px;"> 
 
      Some stuff 
 
      </button> 
 
     <button style="height:40px;margin-left:auto;"> 
 
      Other stuff 
 
     </button> 
 
    </div> 
 

 
    </div> 
 
</div>

1

如果使用display: flax;不需要使用漂浮在亞麻有原生解決方案here你可以在JS小提琴找到解決

+0

它是'display:flex;' –

+0

在我的示例中,您將獲得想要的而不會浮動 http ://jsfiddle.net/df3oLr5b/ –

1

<div id='wrapper' style='display:flex; height:88px;align-items:center;text-align: center;background-color:yellow'> 
 
    <div style="background-color:blue"> 
 
     <h1> 
 
     Some header text 
 
     </h1> 
 
    </div> 
 
    <div style='flex:1;background-color:red'> 
 
     <div style='display: flex; align-items: center; justify-content: space-between;'> 
 
     <button style="height:24px;"> 
 
      Some stuff 
 
      </button> 
 
     <button style="height:40px;"> 
 
      Other stuff 
 
     </button> 
 
     </div> 
 
     
 
    </div> 
 
</div>

2

而不是inline-block只是讓你div Flex容器:

display: flex; 
align-items: center; 
justify-content: space-between; 

justify-content: space-between讓你浮動的效果 - 見下面的演示:

<div id='wrapper' style='display:flex; height:88px;align-items:center;text-align: center;background-color:yellow'> 
 
    <div style="background-color:blue"> 
 
    <h1> 
 
     Some header text 
 
    </h1> 
 
    </div> 
 
    <div style='flex:1;background-color:red'> 
 
    <div style='display: flex; 
 
    align-items: center; 
 
    justify-content: space-between;'> 
 
     <button style="height:24px;"> 
 
      Some stuff 
 
      </button> 
 
     <button style="height:40px"> 
 
      Other stuff 
 
     </button> 
 
    </div> 
 

 
    </div> 
 
</div>

+0

這應該是被接受的答案。你的情況不需要浮動。 – Stavm

+1

@Stavm有沒有接受答案的問題? –