CSS:对齐div内的元素
我有一个包含三个元素的 div,但在正确定位最后一个元素时遇到问题。左边的div必须在左边,中间的div必须居中,第三个div必须在右边。
所以,我有这样的东西:
#left-element {
margin-left: 9px;
margin-top: 3px;
float:left;
width: 13px;
}
#middle-element {
margin:0 auto;
text-align: center;
width: 300px;
}
#right-element {
float:right;
width: 100px;
}
我的 html 看起来像这样:
<div id="container-div">
<div id="left-element">
<img src="images/left_element.png" alt="left"/>
</div>
<div id="middle-element">
I am the text inside the middle element
</div>
<div id="right-element">
I am the text in right element
</div>
</div>
有什么想法吗?
谢谢!
I have a div that contains three elements, and I am having problems correctly positioning the last one. The div at the left has to be at the left, the one in the middle needs to be centered, and the third one needs to be to the right.
So, I have something like:
#left-element {
margin-left: 9px;
margin-top: 3px;
float:left;
width: 13px;
}
#middle-element {
margin:0 auto;
text-align: center;
width: 300px;
}
#right-element {
float:right;
width: 100px;
}
My html looks like this:
<div id="container-div">
<div id="left-element">
<img src="images/left_element.png" alt="left"/>
</div>
<div id="middle-element">
I am the text inside the middle element
</div>
<div id="right-element">
I am the text in right element
</div>
</div>
Any ideas?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您还没有为容器 div 包含 css,但是每当它包含浮动元素时,您都应该隐藏溢出,如下所示:
当您定位中间 div 时,您正在设置跨越整个容器的边距,因此任何其他元素都会定位在该行上以下。请注意,至少对于大多数现代浏览器来说,进一步。如果您重新排序元素,如下所示:
您应该会发现它有效。 更好的方法,因为我不太确定这是否应该工作,是使用CSS定位。应用以下CSS:
You haven't included css for your container div, but whenever it contains floating elements you should hide overflow like so:
When you position the middle div you are setting margins that span the entire container, so any further elements are getting positioned on the line below. Note, at least for most modern browsers, further. If you reorder your elements, like so:
You should find it works. Better method, as I'm not quite sure whether that is supposed to work, would be to use css positioning. Apply the following css:
我认为你的问题是你浮动了左侧和右侧的元素,但没有浮动中心的元素。尝试这样的事情:
CSS:
HTML:
I think you problem is that you floated the left and right element but not the center one. Try something like this:
CSS:
HTML:
问题具体在于中间的 div 设置了宽度但没有浮动,这意味着它仍然是块级元素。尽管 div 本身并不占据容器的整个宽度,但它仍然被视为这样。您需要做两件事 - 浮动中间的 div,然后清除浮动,以便容器随着子 div 的高度而增长。首选clearfix方法,因为它不会导致CSS3属性出现问题,这些属性自然会扩展到它们所应用的元素的边界之外(box-shadow、text-shadow等)。
http://perishablepress.com/press/2009/12/06/新的clearfix-hack/
The problem is specifically that the middle div has a width set but is not floated, meaning it is still a block level element. Even though the div itself does not go the entire width of the container, it is still treated as such. You need to do 2 things - float the middle div, then clear the floats so the container grows with the height of the child divs. The clearfix method is preferred since it does not cause issues with CSS3 properties that naturally extend outside the bounds of the element they are applied to (box-shadow, text-shadow, etc).
http://perishablepress.com/press/2009/12/06/new-clearfix-hack/
我有完全相同的问题。我用了这个方法。我使中心元素显示内联块。这样我就不必给元素特定的宽度或主容器特定的高度。这些块只占用与内部内容一样多的空间。希望这有帮助。
强文本
I had the exact same issue. I used this approach. I made the center element display inline-block. That way I didn't have to give the elements specific width or the main container a specific height. The blocks only took up as much space as the content inside. Hope this helps.
strong text