How to line-break from css, without using
?
Note that this won’t work with react because the text is eventually bundled and will be put on the same line, which won’t trigger white-space CSS rule.
Impossible with the same HTML structure, you must have something to distinguish between Hello and How are you .
I suggest using span s that you will then display as blocks (just like a actually).
note also how much additional mark-up there is — the
element exists for a very good reason. If you want the line break because they are separate paragraphs, then simply mark them up as separate paragraphs.
You might need structured lines without actually using paragraphs. To markup a poem, a song or an address for example
@VincentRobert Right, but a poem is the canonical example for when
is the correct markup. Spans for a poem would be “wrong.”
Note that assigning display: block to an element will force a line break before and after, and so is not at all the same as having one line break.
Definitely use the
elements. A element should NOT be made into display:block, the whole point of is that it’s inline. I would do it this way:
hello
How are you
. No wonked out CSS required.
Use
as normal, but hide it with display: none when you don’t want it.
I would expect most people finding this question want to use css / responsive design to decide whether or not a line-break appears in a specific place. (and don’t have anything personal against
)
While not immediately obvious, you can actually apply display:none to a
tag to hide it, which enables the use of media queries in tandem with semantic BR tags.
The quick brown fox
jumps over the lazy dog
@media screen and (min-width: 20em) < br < display: none; /* hide the BR tag for wider screens (i.e. disable the line break) */ >>
This is useful in responsive design where you need to force text into two lines at an exact break.
Simon, you are spot on — the example you name is the exact reason I was researching this question and the display: none solution is by far the most appropriate and useful.
Note that for cases where it would cause words to run together, you could use something like display: inline-block; width: 1em; instead of none .
Another «why didn’t I think of this?!» answer.
is great at what it does; no need to reinvent the wheel. Thanks!
@amh15 he doesn’t say WHY he wants to do that. Or rather what is wrong with BR. Is it preformatted text from a weather report web service, or is it a responsive design question. At time of answering all the pre/word wrap answers were already there. You’re right that the details matter but my answer has helped a lot of people that found this question based on keywords so I don’t see why you have to be so nit picky. I’d love to know what he really wanted to do. But I also don’t care.
There are several options for defining the handling of white spaces and line breaks. If one can put the content in e.g. a
tag it is pretty easy to get whatever one wants.
For preserving line breaks but not white spaces use pre-line (not pre ) like in:
If another behavior is wanted choose among one of these (WS=WhiteSpace, LB=LineBreak):
white-space: normal; /* collapse WS, wrap as necessary, collapse LB */ white-space: nowrap; /* collapse WS, no wrapping, collapse LB */ white-space: pre; /* preserve WS, no wrapping, preserve LB */ white-space: pre-wrap; /* preserve WS, wrap as necessary, preserve LB */ white-space: inherit; /* all as parent element */
The «\a» command in CSS generates a carriage return. This is CSS, not HTML, so it shall be closer to what you want: no extra markup.
In a blockquote , the example below displays both the title and the source link and separate the two with a carriage return ( «\a» ):
+1 because its CSS only, and doesn’t recommend use of pre, br tags nor changing the display mode to block (which adds different behavior, might break if the parent is in display:flex and therefore is a hack in this context). Its not fancy, really, just a modern technique. If you want the exact same markup, but to actually react differently that’s the way to go.
Brilliant idea. Note the » – do not use simple quotes ‘ because you want to allow the \a to get parsed as a special character.
I would have up-voted this if there was some HTML and maybe a Fiddle to help visualize what you’re doing.
With this CSS every enter inside the P tag will be a break-line at the HTML.
This is exactly what I was looking for! Works perfectly for element content generated from JS (e.g. JSON result from AJAX query, angular-schema-form, etc.) that gets passed through escaping/sanitization (e.g. normal AngularJS escaping behavior when not using ngBindHtml)
Building on what has been said before, this is a pure CSS solution that works.
span < display: inline; >span:before First line of text. Next line.
I just found a variation on this approach to be helpful for multi-line input type=’text’, wrapping the input, and then laying the text over it with a wrapper div . That also requires pointer-events:none;` on the :before in order to still be able to click the button below.
To make an element have a line break afterwards, assign it:
Non-floated elements after a block level element will appear on the next line. Many elements, such as
and are already block level elements so you can just use those.
But while this is good to know, this really depends more on the context of your content. In your example, you would not want to use CSS to force a line break. The
is appropriate because semantically the p tag is the the most appropriate for the text you are displaying. More markup just to hang CSS off it is unnecessary. Technically it’s not exactly a paragraph, but there is no tag, so use what you have. Describing your content well with HTMl is way more important — after you have that then figure out how to make it look pretty.
Disable line breaks using CSS
Solution 3: use this : this will override any other line height being set in other css file , alternativly you can put this in HTML style elements in HTML override all css files or. you can do this inline css in elements overrides all css files , and elements Question: I have a parent and two main children. The second child has some text and when I resize text is shown or hidden in response to page size.
Disable line breaks using CSS
I have a canvas element inside a div element. The canvas size can change, and I want it vertical centered. I’m using this CSS approach:
html, body < height:100%; width:100%; margin:0; padding:0; >#container < width:100%; height:100%; text-align:center; font-size:0; background:#aae; >#container:before < content:''; display:inline-block; height:100%; vertical-align:middle; >canvas