本文介绍了你真的需要浮动元素的宽度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个Div( id =toolbar),并且在该工具栏中有一个Div( id =ButtonHolder),其中包含2个按钮。如果你把 #ButtonHolder 浮动到右边,并且没有设置一个明确的宽度,是kosher吗?

Say you have a Div (id="toolbar"), and inside that toolbar you have a Div (id="ButtonHolder") that contains 2 buttons. If you float the #ButtonHolder to the right and don't set an explicit width on it, is that kosher?

我读过堆栈溢出,你应该总是设置浮动元素的宽度。按钮文本可能会改变,从保存到应用,我不想每次都调整 #ButtonHolder 的宽度。

I've read on stack overflow that you should always set a width on a floated element. The buttons text might change, from save to apply, and I don't want to have to adjust #ButtonHolder's width every time.

我想到将 #ButtonHolder 的宽度设置为 auto 默认情况下,似乎没有必要将其宽度设置为自动。我担心浏览器可能不会总是浮动 #ButtonHolder 我认为应该的方式。

I thought about setting #ButtonHolder's width to auto, but the browser does that by default so it seems unnecessary to set it's width to auto. I'm worried the browser might not always float #ButtonHolder the way I think it should.

推荐答案

float 位置通常带有成本。您应该尝试首先找到其他方法在布局中定位元素。您应该考虑其他选项,如 margin padding display:inline-block; text-align ...等。

float and position usually come with a cost. You should try to first find other ways to position elements within your layout. You can should consider other options such as margin, padding, display: inline-block;, text-align ... etc.

href =http://css-tricks.com/all-about-floats/ =nofollow> this 。

I would recommend reading this.

回答您的问题问题直接。浮动的设置宽度不是写在石头上,但不这样做,通常意味着以后的麻烦。至少在我个人的角度来看。

To answer your question directly. Setting width for floats is not written on stone but not doing so, usually means trouble later. At least in my personal point of view.

这篇关于你真的需要浮动元素的宽度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 18:38