2014-11-25 53 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>