显示:内联,但在不同的行上

发布于 2025-01-01 17:04:45 字数 471 浏览 3 评论 0原文

内联元素很棒,因为它们的宽度就是内容的宽度,并且可以按照 CSS 规则将它们居中:

text-align: center

但是内联元素保持在同一行。是否可以将它们垂直对齐?

小提琴:http://jsfiddle.net/_bop/NhVaF/ 全屏小提琴:http://jsfiddle.net/_bop/NhVaF/show

请不要:

  • 更改示例中的 HTML。改变CSS!
  • 想出其他技术来居中元素,除非您有更好的解决方案,可以处理未指定宽度的元素,并且不需要大量的容器和/或浮动技巧。

提前致谢!

Inline elements are great, because their width is the width of the content and because it's possible to center them with on rule of CSS:

text-align: center

But inline elements stay on the same line. Is it possible to align them vertically?

Fiddle: http://jsfiddle.net/_bop/NhVaF/
Full screen fiddle: http://jsfiddle.net/_bop/NhVaF/show

Please don't:

  • Change the HTML in the example. Change the CSS!
  • Come up with other techniques to center elements, unless you have a better solution that works on elements with unspecified width and doesn't need tons of containers and/or float hacks.

Thanks in advance!

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

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

发布评论

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

评论(4

你是我的挚爱i 2025-01-08 17:04:45

在标记中,如果跨度位于不同行,您可以在父容器上添加:

white-space: pre-line;

使用此 CSS 声明,您的跨度仍居中,并且无需添加 HTML 标记。


行前
- 该值将导致空白序列折叠成单个空格字符。任何地方都会出现换行
填充行框以及标记中的新行(或在
生成的内容中出现“\a”)。换句话说,就像
正常,只是它会遵循显式换行符。

您可以在此处找到有关空白的更多信息:


对于 IE7兼容性,您还可以在父容器上添加:

*white-space: pre /*FixIE7*/;

In your markup, if the span are on different rows you could add on the parent container:

white-space: pre-line;

With this CSS declaration, your span are still centered, and you don`t have to add HTML markup.


pre-line
- This value will cause sequences of whitespace to collapse into a single space character. Line breaks will occur wherever
necessary to fill line boxes, and at new lines in the markup (or at
occurrences of "\a" in generated content). In other words, it’s like
normal except that it’ll honor explicit line breaks.

You can find more informations here about white-space:


For an IE7 compatibility, you could also add on the parent container:

*white-space: pre /*FixIE7*/;
静若繁花 2025-01-08 17:04:45

如果您想将跨度显示在另一个跨度之上,则需要一些固定块来固定跨度。这是我能做的最好的了。

http://jsfiddle.net/NhVaF/5/

You need some holding block to hold your spans if you want to display it on top of another. This is the best I can do.

http://jsfiddle.net/NhVaF/5/

骄兵必败 2025-01-08 17:04:45

如果你想让它在不改变 html 的情况下工作,那么你最好的选择就是简单地 float: left;清除:左;像这样:

span {
    float: left;
    clear: left;
    color: #FFF;
    padding: 30px;
}

显示:块;将无法工作,因为它要求您设置宽度(否则它们将填充可用空间)。

显示:内联块;将无法工作,因为仍然显示在同一行。

If you want to make it work without altering the html, then your best bet is to simply float: left; clear: left; like so:

span {
    float: left;
    clear: left;
    color: #FFF;
    padding: 30px;
}

display: block; will not work because it requires you to set a width (or else they'll fill the available space).

display: inline-block; will not work because still display on the same line.

窝囊感情。 2025-01-08 17:04:45

我也只是在玩这个,并通过简单地在每个 inline-block 元素后面放置
找到了我的解决方案。我知道它会改变 html,但只是轻微改变!

如果您想使用 CSS 创建换行符,请尝试使用 :after 伪类。像这样的东西会起作用吗?

div.class:after {
  content:"\a";
  white-space: pre;
}

打破 :after 技巧:https://stackoverflow.com/a/10934138/6586407

I was just playing around with this too, and found my solution by simply placing <br> after each inline-block element. I know it's altering the html but only slightly!

If you want to create line breaks with CSS try using the :after pseudo class. Would something like this work?

div.class:after {
  content:"\a";
  white-space: pre;
}

break :after trick: https://stackoverflow.com/a/10934138/6586407

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