2014-11-25 74 views
1

如何確保兩個內聯元素保持在同一行上,而不管視口有多窄?如何確保兩個內聯元素保持在同一行上

在我的情況下,輸入字段和一個提交按鈕(作出看起來像一個長按鈕,你把你的電子郵件進入)

+0

你可以發佈你現在的代碼嗎?聽起來像你需要設置元素所在容器的溢出屬性或最小寬度屬性。 – 2014-11-25 01:06:48

+0

如果只有兩個元素,你可以給它們兩個50%的寬度和一個「inline- block'。 – Fizzix 2014-11-25 01:07:40

回答

2

他們需要被包裹在一個父元素:

.singleLineChildren { 
 
    /* prevents the contents from wrapping to a new line: */ 
 
    white-space: nowrap; 
 
    /* prevents the overflow being seen/scrolled-to; 
 
    adjust to taste: */ 
 
    overflow: hidden; 
 
    /* to emphasize/speed-up the effect: */ 
 
    width: 50%; 
 
    /* just for visibility: */ 
 
    box-shadow: 0 0 6px 4px #f90; 
 
} 
 

 
.singleLineChildren { 
 
    display: inline-block; 
 
    box-sizing: border-box; 
 
    /* purely so that narrowing the browser screen has 
 
    an obviously visible effect: */ 
 
    font-size: 4em; 
 
    min-width: 5em; 
 
    width: 50%; 
 
}
<div class="singleLineChildren"> 
 
    <a href="#">Link 1</a> 
 
    <a href="#">Link 2</a> 
 
</div>

1

你也可以試試這個:

div { 
 
    padding:5px; 
 
    } 
 
.father { 
 
    display: block; 
 
    } 
 
.child { 
 
    display:inline-block; 
 
    } 
 
.red { 
 
    background: red; 
 
    color:white; 
 
    } 
 
.blue { 
 
    background: blue 
 
    }
<div class="father"> 
 
    
 
    <div class="child red"> 
 
    I am red 
 
    </div> 
 
    <div class="child blue"> 
 
    I am blue 
 
    </div> 
 
    <div class="child red"> 
 
    I am red 
 
    </div> 
 
    <div class="child blue"> 
 
    I am blue 
 
    </div> 
 
    </div>

相關問題