将两个元素放在一起,两个元素仍然有边距

时间:2012-02-19 18:05:48

标签: position margin

在我写作的时候,我似乎经常遇到这个问题。我在同一行上有两个元素。我需要他们两个都有边距,因此无法将他们的位置设置为absolute。如果我没有设置第一个元素的位置,它看起来很好。但是,第二个元素将出现在第一个元素下方。这是不好的;我希望他们出现在同一条线上。换句话说,我想将两个元素都设置为position: absolute; top: 0;但是我不能使用我需要的保证金。

HTML:

#userQuestion{
//border: 1px solid yellow;
width: 400px;
position: relative;
top: 0px;
left: 40px;
line-height: 1.28;
display: inline-block;
}

.container{
padding: 0;
margin 0;
border-top: 1px solid #ccc;
//border: 1px solid blue;
margin-top: 30px;
width: 480px;
}

HTML:

<div id='userQuestion'></div>  //this is fine
<div class='container'></div>  //this appears underneath userQuestion.  I would like them to be on the same line.

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

.container {
    border-top: 1px solid #CCCCCC;
    display: inline-block;
    margin-top: 30px;
    padding: 0;
    width: 480px;
}