在CSS中定义浮动为中心

发布于 2024-08-09 02:07:12 字数 81 浏览 2 评论 0原文

如何在CSS中定义浮动作为中心?实际上我需要左右之间的布局,而且我尝试了“文本对齐”,但它不起作用,而“浮动”属性刚刚起作用。

谢谢

How can I define a define float as center in css? actually i need a layout between right and left and also i tried "text-align" but it doesn't work and the "float" property just working.

Thank you

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

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

发布评论

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

评论(3

风启觞 2024-08-16 02:07:12

你想要这样的东西:

<div style="width: 500px; text-align: center;">

    <div style="margin: 0 auto; width: 100px;">I am centered</div>

</div>

关键是 text-align: center 在父元素上,而 margin: 0 auto 在内部元素上

You want something like this:

<div style="width: 500px; text-align: center;">

    <div style="margin: 0 auto; width: 100px;">I am centered</div>

</div>

The key is text-align: center on the parent, and a margin: 0 auto on the inner element

月隐月明月朦胧 2024-08-16 02:07:12

您只能将元素向左或向右浮动。

也许描述一下你想要实现什么样的布局。

You can only float an element to the left or the right.

Maybe describe what kind of layout you are trying to achieve.

_失温 2024-08-16 02:07:12

没有浮动:中心。但是当您希望 div 以其父级为中心时,这应该可以解决问题:

#parent-element{
    position: relative;
}
#element
{
  width: 500px; 
  left: 50%;
  margin-left: -250px; /* - (width/2)  */
  position: absolute;
}

但是,您到底想要实现什么目标?

There is no float: center. but when you want a div to be centered to its parent this should do the trick:

#parent-element{
    position: relative;
}
#element
{
  width: 500px; 
  left: 50%;
  margin-left: -250px; /* - (width/2)  */
  position: absolute;
}

but, what exactly are you trying to achieve?

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