将浮动按钮置于两个浮动按钮之间(液体尺寸)

发布于 2024-12-29 13:33:17 字数 603 浏览 2 评论 0原文

我在将浮动按钮置于另外两个浮动按钮之间时遇到问题。

到目前为止看起来像这样:

jQuery Buttons; need to center Button B

此时 CSS 非常基本:

A {
  float: left;
}
B {
  float: left;
}
C {
  float: right;
}

注意:按钮 A 位于页面的最左边,按钮 C 位于最右边。 B 应该在中间(无论如何,这就是想法)。

我知道浮动没有“中心”值。我已经阅读了这个问题的一些其他解决方案。但其中大多数都涉及在包装器 div 中设置特定宽度,在我看来,这对于液体布局设计来说不是一个理想的解决方案。如果您必须包裹按钮,我不确定这比使用直接定位更好。

无论哪种方式,我都在寻找使用液体布局方法的解决方案。

我也尝试了以下方法,但没有成功。

B {
  position: relative;
  left: 0;
  right: 0;
}

任何帮助将不胜感激。非常感谢。

I'm having issues centering a floating button between two other floating buttons.

It looks like this thus far:

jQuery buttons; need to center Button B

The CSS is pretty basic at this point:

A {
  float: left;
}
B {
  float: left;
}
C {
  float: right;
}

Note: Button A is positioned to the left-most extreme on a page and Button C is the right-most. B should be in the middle (that's the idea, anyway).

I know there is no 'center' value for float. And I have read some other solutions for this problem. But most of them involve setting a specific width in a wrapper div, which not an ideal solution, imo, for a liquid layout design. If you have to wrap the button, I'm not sure how that's any better than using straight positioning.

Either way, I'm looking for a solution using a liquid layout approach.

I also tried the following but it did not work.

B {
  position: relative;
  left: 0;
  right: 0;
}

Any help would be most appreciated. Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

怎言笑 2025-01-05 13:33:17

如果将 text-align: center; 放在它们的容器上并且根本不浮动 B 怎么样?

(我假设它是一个内联元素,如果不是,也在 B 上执行 display: inline-block;

How about if you put text-align: center; on their container and don't float B at all?

(I'm assuming it is an inline element, if not also do display: inline-block; on B)

黯淡〆 2025-01-05 13:33:17

我的解决方案:

http://jsfiddle.net/UWNTM/1/

希望有帮助。

基本上,我对元素使用了三个包装器:

.button_wrapper {
    float: left;
    width: 33%;
}

然后使用内联 text-align 属性将按钮放置在其中。

My solution:

http://jsfiddle.net/UWNTM/1/

Hope it helps.

Basically, I used three wrappers for elements:

.button_wrapper {
    float: left;
    width: 33%;
}

And then placed button inside them using inline text-align property.

云雾 2025-01-05 13:33:17

尝试这个 HTML 代码:

<div class="left">1</div>
<div class="right">3</div>
<div class="center">2</div>

使用这个 CSS 代码:

.left { float: left; }
.right {float: right; }
.center { margin-left: 50%; }

try this HTML code:

<div class="left">1</div>
<div class="right">3</div>
<div class="center">2</div>

with this CSS code:

.left { float: left; }
.right {float: right; }
.center { margin-left: 50%; }
别念他 2025-01-05 13:33:17

您可以使用 display:table-cell 来实现此目的。尽管我认为其他答案可能更好,但有替代解决方案还是很好的。

http://jsfiddle.net/bES7Q/

<div>
    <span>a</span>
    <span>b</span>
    <span>c</span>
</div>

div { width: 100%; display: table; }
span {
    border: 1px solid gray;
    display: table-cell;
    width: 1%;
}
span:nth-of-type(2) {
    width: 99%;
    text-align: center;
}

You could use display:table-cell for this. Although I think other answers are probably better, it is nice to have alternate solutions.

http://jsfiddle.net/bES7Q/

<div>
    <span>a</span>
    <span>b</span>
    <span>c</span>
</div>

div { width: 100%; display: table; }
span {
    border: 1px solid gray;
    display: table-cell;
    width: 1%;
}
span:nth-of-type(2) {
    width: 99%;
    text-align: center;
}
云朵有点甜 2025-01-05 13:33:17

我用博客导航按钮做到了这一点。我之前使用过xec的解决方案。这是我现在使用的技巧的一个变体:

.button1, .button2, .button3
{
    width: 60px;
    height: 20px;
}
.button1
{
    /* float implies block display */
    float: left;
    background-image: url(http://dummyimage.com/60x20/fc0/000.gif&text=Button+1);
}
.button2
{
    /* set block display to make width, height and auto margin work */
    display: block;
    margin: 0 auto;
    background-image: url(http://dummyimage.com/60x20/fc0/000.gif&text=Button+2);
}
.button3
{
    /* float implies block display */
    float: right;
    background-image: url(http://dummyimage.com/60x20/fc0/000.gif&text=Button+3);
}

演示

I did this with blogger navigation buttons. I've used xec's solution earlier. Here is a variant of the trick I am using right now:

.button1, .button2, .button3
{
    width: 60px;
    height: 20px;
}
.button1
{
    /* float implies block display */
    float: left;
    background-image: url(http://dummyimage.com/60x20/fc0/000.gif&text=Button+1);
}
.button2
{
    /* set block display to make width, height and auto margin work */
    display: block;
    margin: 0 auto;
    background-image: url(http://dummyimage.com/60x20/fc0/000.gif&text=Button+2);
}
.button3
{
    /* float implies block display */
    float: right;
    background-image: url(http://dummyimage.com/60x20/fc0/000.gif&text=Button+3);
}

Demo

不寐倦长更 2025-01-05 13:33:17
    .btn-group{
    	margin-top:2%;
    	display: flex;
        justify-content: center;
    }
    			<div class="btn-group" role="group" aria-label="Basic example">
    			  <button type="button" class="btn btn-secondary">History</button>
    			  <button type="button" class="btn btn-secondary">Expertise</button>
    			  <button type="button" class="btn btn-secondary">Differentiators</button>
    			  <button type="button" class="btn btn-secondary">Values</button>
    			</div>

使用CSS属性而不是使用margin、padding上面的代码也适合所有的按钮对齐

    .btn-group{
    	margin-top:2%;
    	display: flex;
        justify-content: center;
    }
    			<div class="btn-group" role="group" aria-label="Basic example">
    			  <button type="button" class="btn btn-secondary">History</button>
    			  <button type="button" class="btn btn-secondary">Expertise</button>
    			  <button type="button" class="btn btn-secondary">Differentiators</button>
    			  <button type="button" class="btn btn-secondary">Values</button>
    			</div>

Use the CSS property instead of using margin, padding the above code also suitable for all the button alignment

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文