Css break line after element
Is it possible in pure css, that is without adding additional html tags, to make a line break like Show
HTML
CSS
I have found many questions like this, but always with answers like "use display: block;", which I can't do, when the I had a little situation where I had a header with a span in it, and I wanted to make sure to put a line break before the span. For the record, there really isn’t anything wrong with just chucking a So let’s take a journey. A journey in which we say “But…” a lot.
A block level element would do itRather than a But we’re using a span on purpose, because of the design. The text after the break should be inline/inline-block, because it’s going to have a background and padding and such. It’s easy: But… the We can force that line
break to work by making white space meaningful… That actually works. But… because of the padding and background, it leaves a little chunk of that behind when the line breaks: We could
fix the awkward-left-edge-hugging on by using If
we made the span inline-block, the break would happen within that block, which isn’t what we want either: Making the pseudo element block-level and leaving the span alone doesn’t do the trick
either: This was Aaron Bushnell’s
idea. The trick here is to make the span block level, but then inject the text with a pseudo element and style it as an inline element. I’ve long been a fan of pseudo-element trickery, but this feels slightly dangerous in that you may be hurting accessibility. I think some screen readers read pseudo-elements, but I don’t think all, nor are they
supposed to. Not to mention you can’t copy and paste all the text this way. At least the text is still maintained entirely in the HTML! My favorite idea came from Thierry Koblentz. Just make the span Including one where we just use a See the Pen Attempting a line break before and inline-block within a header by Chris Coyier (@chriscoyier) on CodePen. In this post, we'll explore various ways to create a line-break in our content using only CSS. We can display the line-breaks in text without adding any extra mark-up by using the Using either of these properties would make the element act like a
Lorem ipsum dolor sit amet.
Consectetur adipiscing elit.
Mauris eget pellentesque lacus.
This does not work in IE-7 and below. Block-level elements by default start on a new line (unless the default behavior is overridden by a CSS rule). To force inline elements
to a new line, however, you could do any of the following: This may be the most obvious one; a block-level element starts on a new line, and takes up the entire width available to it. So, you could either use a block-level element or set any HTML element's CSS property to You can add a new-line using the
Lorem ipsum dolor sit amet.Consectetur adipiscing elit.
The Hope you found
this post useful. It was published 15 May, 2018 (and was last revised 03 Jun, 2020). Please show your love and support by sharing this post. |