如何右对齐内联块元素?

发布于 2025-01-03 15:03:08 字数 786 浏览 1 评论 0原文

正如您在下面的小提琴中看到的: http://jsfiddle.net/EvWc4/3/ ,我目前正在寻找一种方法将第二个链接 (link-alt) 与其父链接 (p) 的右侧对齐。

为什么不使用浮动或位置:绝对你会说,主要原因是我喜欢链接的显示(内联块)属性允许它们以自然的方式垂直对齐。

通过使用 float 或position:absolute;我将被迫计算并放置一些额外的边距顶部或顶部值来垂直对齐链接。

这是代码,但最好看看小提琴 http://jsfiddle.net/EvWc4/3/

    <p>
        <a href="#" class="link">link</a>
        <a href="#" class="link link-alt">link alt</a>
    </p>

    p {
       padding: 20px;
       background: #eee;
    }
    .link {
       display: inline-block;
       padding: 10px;
       background: #ddd;
    }
    .link-alt { padding: 20px; }

As you can see in the following Fiddle: http://jsfiddle.net/EvWc4/3/, I'm currently searching a way to align the second link (link-alt) to the right side of its parent (p).

Why not using float or position:absolute you'll say, well the main reason is that I like the fact that the links' display (inline-block) property allow them to be verticaly aligned in a naturally kind of way.

By using float or position:absolute; I'll be forced to calculate and put some extra margin-top or top value to vertically aligned the links.

Here is the code but better see the Fiddle http://jsfiddle.net/EvWc4/3/ :

    <p>
        <a href="#" class="link">link</a>
        <a href="#" class="link link-alt">link alt</a>
    </p>

    p {
       padding: 20px;
       background: #eee;
    }
    .link {
       display: inline-block;
       padding: 10px;
       background: #ddd;
    }
    .link-alt { padding: 20px; }

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

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

发布评论

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

评论(7

你的他你的她 2025-01-10 15:03:08

要使用 CSS3 执行此操作,您可以使用弹性框模型

HTML:

<div class="content">
    <div class="box box1"><a>Link 1</a></div>
    <div class="box box2"></div>
    <div class="box box3"><a>Link 2</a></div>
</div>

CSS:(

.content {
    display: box;
    box-orient: horizontal;
    box-pack: center;
    box-align: center;
}
.box2 {
    box-flex: 1;
}

需要供应商前缀)

http://jsfiddle .net/EvWc4/18/

To do this with CSS3 you can use the flex box model

HTML:

<div class="content">
    <div class="box box1"><a>Link 1</a></div>
    <div class="box box2"></div>
    <div class="box box3"><a>Link 2</a></div>
</div>

CSS:

.content {
    display: box;
    box-orient: horizontal;
    box-pack: center;
    box-align: center;
}
.box2 {
    box-flex: 1;
}

(needs vendor prefixes)

http://jsfiddle.net/EvWc4/18/

向地狱狂奔 2025-01-10 15:03:08

CSS3 flex 和网格项应该可以解决这些问题,但截至 2013 年,标准支持仍然参差不齐。

回到现实世界。我不认为在没有像素黑客的情况下纯粹在 CSS2.1 (IE8+) 中做到这一点是可能的。问题是,文本对齐方式由父元素控制,并且由于两个锚点共享其父元素,因此它们要么向左对齐,要么向右对齐。并且 justify 在最后一行不起作用。

如果您可以忍受一点额外的 HTML,有两种方法:

1) 添加另一个保证换行的内联,然后尝试隐藏空行。这允许您在父级上使用 text-align justify。

<p>
    <a href="#" class="link">link</a>
    <a href="#" class="link link-alt">link alt</a>
    <span class="boom"></span>
</p>

<style type="text/css">
    p {
        padding: 20px;
        background: #eee;
        text-align: justify
    }

    .link {
        display: inline-block;
        padding: 10px;
        background: #ddd;
    }

    .link-alt {
        padding: 20px;
    }

    span {
        display: inline-block;
        height: 0;
        width: 100%
    }
</style>

优点:适用于任意数量的内联块,而不仅仅是两个。只需要一点额外的 HTML。

缺点:需要额外的努力来隐藏最后一行(空)文本(将其内部的内联块设置为 0 高度不会有帮助),并且您将不得不摆弄边距或其他东西来实现它真的有效。进一步讨论:我如何*真正*证明水平对齐HTML+CSS 中的菜单?

2) 在锚标记顶部添加另一层内联块,并将其大小设置为 50%。然后您可以应用单独的文本对齐来获得您请求的最终布局。重要的是,两个大小为 50% 的内联块之间不允许有空格,否则您将换行。

<p>
    <span class="left">
        <a href="#" class="link">link</a>
    </span><span class="right">
        <a href="#" class="link link-alt">link alt</a>
    </span>
</p>

<style type="text/css">
    p {
        padding: 20px;
        background: #eee;
    }

    .link {
        display: inline-block;
        padding: 10px;
        background: #ddd;
    }

    .link-alt {
        padding: 20px;
    }

    span {
        display: inline-block;
        width: 50%
    }

    .left {
        text-align: left
    }

    .right {
        text-align: right
    }
</style>

优点:产生您所要求的精确布局,而不会污染外盒模型。

缺点:仅适用于两个内联块(您可以尝试扩展它,但它很快就会变得非常复杂)。依赖于没有额外的空格,这可能会危及您格式良好的标记。

CSS3 flex and grid items are supposed to address these issues, but standard support remains spotty as of 2013.

Back to the real world. I don't think it is possible to do this purely in CSS2.1 (IE8+) without pixel hacks. The thing is, text alignment is controlled by the parent element, and since the two anchors share their parent, they either both align to the left or to the right. And justify doesn't work on the last line.

If you can suffer a little additional HTML, there are two approaches:

1) Add another inline that is guaranteed to wrap the line, and then try to hide the empty line. This allows you to use text-align justify on the parent.

<p>
    <a href="#" class="link">link</a>
    <a href="#" class="link link-alt">link alt</a>
    <span class="boom"></span>
</p>

<style type="text/css">
    p {
        padding: 20px;
        background: #eee;
        text-align: justify
    }

    .link {
        display: inline-block;
        padding: 10px;
        background: #ddd;
    }

    .link-alt {
        padding: 20px;
    }

    span {
        display: inline-block;
        height: 0;
        width: 100%
    }
</style>

Pros: works on any number of inline blocks, not just two. Only a little extra HTML required.

Cons: takes extra effort to hide the last (empty) line of text (setting the inline block inside of it to 0 height won't help you), and you're going to have to fiddle with margins or something else to make it really work. Further discussion: How do I *really* justify a horizontal menu in HTML+CSS?

2) Add another layer of inline blocks on top of your anchor tags and size them to 50%. Then you can apply separate text-align to get the final layout you requested. It is important that no whitespace is allowed between two inline blocks sized to 50%, or you'll wrap the line.

<p>
    <span class="left">
        <a href="#" class="link">link</a>
    </span><span class="right">
        <a href="#" class="link link-alt">link alt</a>
    </span>
</p>

<style type="text/css">
    p {
        padding: 20px;
        background: #eee;
    }

    .link {
        display: inline-block;
        padding: 10px;
        background: #ddd;
    }

    .link-alt {
        padding: 20px;
    }

    span {
        display: inline-block;
        width: 50%
    }

    .left {
        text-align: left
    }

    .right {
        text-align: right
    }
</style>

Pros: produces the exact layout you requested without polluting the outer box model.

Cons: only works for two inline blocks (you can try to extend it, but it quickly gets really complicated). Relies on having no extra whitespace, which could jeopardize your nicely formatted markup.

千寻… 2025-01-10 15:03:08

您可以将 position 设置为 absolute 并使用 right: 0

.wrapper {
    position: relative;
}
.right {
    position: absolute;
    right: 0;
}

You could set the position to absolute and use right: 0

.wrapper {
    position: relative;
}
.right {
    position: absolute;
    right: 0;
}

http://jsfiddle.net/EvWc4/13/

掩耳倾听 2025-01-10 15:03:08

我相信这可以满足您的需求:

.link-alt {
    position: absolute;
    right: 0; top: 0; bottom: 0;
    margin: auto;
    max-height: 1em;
}

您可以使用 position:absoluteright: 0 来获得正确的对齐方式。要保持垂直居中,可以使用 top: 0;底部:0;边距:自动;。当然,您还需要设置元素的高度,否则它将拉伸到其父元素的完整高度。

这是一个jfiddle: http://jsfiddle.net/pHppA/

I believe this accomplishes what you're looking for:

.link-alt {
    position: absolute;
    right: 0; top: 0; bottom: 0;
    margin: auto;
    max-height: 1em;
}

You can use position: absolute and right: 0 to obtain the right alignment. To keep the vertical centering, you can use top: 0; bottom: 0; margin: auto;. Of course, you'll also need to set a height on the element, or it will stretch to the full height of its parent.

Here's a jfiddle: http://jsfiddle.net/pHppA/

相守太难 2025-01-10 15:03:08

我已经更新了 Pethas 示例,因此可以在纯 CSS2 中完成。它在 IE7 中不起作用,因为它不支持我使用的 display: table-cell;

http://jsfiddle.net/EvWc4/133/

I've updated Pethas example, so it can be done in pure CSS2. It doesn't work in IE7, as it doesn't support display: table-cell; which I use.

http://jsfiddle.net/EvWc4/133/

情泪▽动烟 2025-01-10 15:03:08

属性 float 与元素的垂直定位无关。

p{padding:20px;background:#eee;overflow:auto;}
.link-alt{padding:20px; float:right}

应该完成你正在寻找的东西。将父级的溢出设置为默认值(可见)之外的其他值将强制它将浮动子级视为普通元素。

参考文章

The attribute float has no bearing on the element's vertical positioning.

p{padding:20px;background:#eee;overflow:auto;}
.link-alt{padding:20px; float:right}

should accomplish what you're looking for. Setting the overflow of the parent to something besides it's default (visible) will force it to treat floating children like normal elements.

Reference article

踏月而来 2025-01-10 15:03:08

我还没有在 Chrome 之外测试过这个,所以它对于 IE 来说可能很糟糕。

这个简单(且有限)的解决方案在对齐的子项上利用了 text-align: rightwidth: 50% 以及 white-space: nowrap家长才能达到想要的结果。

示例: http://jsfiddle.net/erikjung/ejcJZ/

.vertically-centered-module {
    white-space: nowrap;
}

.vertically-centered-module > * {
    display: inline-block;
    vertical-align: middle;
    width: 50%;
}

.vertically-centered-module > :last-child {
    text-align: right;
}

I haven't tested this at all outside of Chrome, so it might suck for IE.

This simple (and limited) solution leverages text-align: right and width: 50% on the aligned children, and white-space: nowrap on the parent to achieve the desired result.

Example: http://jsfiddle.net/erikjung/ejcJZ/

.vertically-centered-module {
    white-space: nowrap;
}

.vertically-centered-module > * {
    display: inline-block;
    vertical-align: middle;
    width: 50%;
}

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