在CSS中,在元素之后强制换行的更好的方法是使它成为块元素? [英] In CSS, what is a better way of forcing a line break after an element than making it a block element?

查看:95
本文介绍了在CSS中,在元素之后强制换行的更好的方法是使它成为块元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个H3标题,我想要风格为具有特定的背景颜色,但没有元素的背景占据父元素的全部宽度。默认情况下看到H3是块元素,我的样式需要将元素更改为inline-block元素,或者只是一个内联inline元素,如下所示:

I have an H3 heading that I'd like to style as having a particular background color, but without having the element's background take up the full width of the parent element. Seeing as H3 is by default a block element, my style would need to change the element to an inline-block element, or just an inline inline element like so:

h3 {
    background-color: #333;
    color: white;
    display: inline-block;
}

这将工作正常,但前提是它紧跟一个块元素。我不想改变标记只是为了迎合这种风格,所以我想知道是否有一种方法,使任何相邻的元素,无论如何显示,从下一行开始?

This will work fine, but only if it is immediately followed by a block element. I do not want to change the markup just to cater for this style, so I was wondering if there is a way to cause any adjacent element, irrespective of how it displays, to start on the next line?

假设我可以使用CSS3。

Assume I can use CSS3.

推荐答案

请尝试:

h3:after {
  content: ".";
  display: block;
  height: 0;
  clear: both;
  visibility: hidden;
}

这篇关于在CSS中,在元素之后强制换行的更好的方法是使它成为块元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆