链接 在显示 Flex 中具有 100% 宽度,
我希望我的按钮向右对齐,无论容器是什么(所以没有弹性父级)。 所以我想使用 margin-left: auto
将按钮推到右侧。我知道其他解决方案(浮动、弯曲等),但想知道为什么会附加此行为。
这是我的问题:
这项工作关于<按钮>但不与 < a>标签。
<< a>似乎缩放到 100% 宽度。
这是说明问题的示例:
a, button {
display: flex;
margin-left: auto;
}
<a href="">test</a>
<br>
<button>test</button>
我搜索隐藏的用户代理属性但找不到任何内容。
这是来自<的本机行为吗? a >,或者可能是 margin-left: auto 奇怪的行为? 如果有人有想法?
与引导实用程序相同:
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="ms-auto w-25">This is working</div>
<a class="ms-auto w-25">This is not working</a>
为什么这不适用于 < a>标签?
I want my button to be aligned to the right, no matter the container (so no flex parent).
So i want to use a margin-left: auto
to push the button to the right. I'm aware of other solutions (float, flex,...) but want to know why this behaviour append.
Here is my problem :
This work on < button > but not with < a > tag.
The < a > seems scale to 100% width.
Here is a sample that illustrate the problem :
a, button {
display: flex;
margin-left: auto;
}
<a href="">test</a>
<br>
<button>test</button>
I searched for hidden user agent properties but could'nt find anything.
Is this a native behaviour from < a >, or maybe a margin-left: auto weird behaviour ?
If someone has an idea ?
Same with bootstrap utility :
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="ms-auto w-25">This is working</div>
<a class="ms-auto w-25">This is not working</a>
Why is this not working on < a > tags ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
按钮有点特殊,其行为与链接不同。您可以使用
table
代替flex
来获得相同的行为buttons are a bit special and won't behave the same as a link. Instead of
flex
, you can usetable
to get the same behavior我发现了同样的问题,并通过向内部链接添加
包装器并将其设置为
inline-block
来创建解决方法https://codepen.io/pikilon/pen/KKBpdKg?editors=1100
I founded the same problem and I created a workaround by adding a
<span class="flex">
wrapper to the inner link and setting it asinline-block
https://codepen.io/pikilon/pen/KKBpdKg?editors=1100
为了让边距自动工作,您还需要指定宽度,
因此,要使用 margin auto 将其推到右侧,您必须使用以下命令:
另外,我建议您使用 display: block; 而不是 flex 。或者如果您不希望它是全宽的,则使用内联块。如果您不打算使用 Flexbox,我认为没有必要包含它。
不过,它的 Flex 版本会更容易和干净,你可以这样做:
In ordered to have margin auto work you need also to specify a width,
So to push it on the right with the use of margin auto you have to use this :
Also instead of flex i'd recommand you would use display: block; or inline-block if you don't want it full width. If you're not going to use flexbox i don't see the need to include it.
The flex version of it would be much easier and clean though, you can do it this way :
因为按钮是块级元素,而 a 是锚内联元素,这就是为什么 margin-left: auto;不适用于锚标记。
您可以尝试:
Because button is a block-level element and a is anchor inline element that's why margin-left: auto; not working on the anchor tag.
You can try:
您还可以使用此代码来设置它的位置
You can also use this code to set it's position