Change <br> height using CSS - html
I have seen a question here about the same, but I can't get any of the answers to work (at least on Chrome).
This question is only for <br>, I know plenty of other techniques to change the height but in this case I can't change the HTML.
bla<BR><BR>bla<BR>bla<BR><BR>bla
CSS:
br {
display: block;
margin-bottom: 2px;
font-size:2px;
line-height: 2px;
}
Desired effect: smaller inter-line height.
The only thing I can get to work is display:none, but then all line break are removed.
Here's a fiddle for it using some of the techniques, but see that it renders the exact same as without any CSS.
This feels very hacky, but in chrome 41 on ubuntu I can make a <br> slightly stylable:
br {
content: "";
margin: 2em;
display: block;
font-size: 24%;
}
I control the spacing with the font size.
Update
I made some test cases to see how the response changes as browsers update.
*{outline: 1px solid hotpink;}
div {
display: inline-block;
width: 10rem;
margin-top: 0;
vertical-align: top;
}
h2 {
display: block;
height: 3rem;
margin-top:0;
}
.old br {
content: "";
margin: 2em;
display: block;
font-size: 24%;
outline: red;
}
.just-font br {
content: "";
display: block;
font-size: 200%;
}
.just-margin br {
content: "";
display: block;
margin: 2em;
}
.brbr br {
content: "";
display: block;
font-size: 100%;
height: 1em;
outline: red;
display: block;
}
<div class="raw">
<h2>Raw <code>br</code>rrrrs</h2>
bla<BR><BR>bla<BR>bla<BR><BR>bla
</div>
<div class="old">
<h2>margin & font size</h2>
bla<BR><BR>bla<BR>bla<BR><BR>bla
</div>
<div class="just-font">
<h2>only font size</h2>
bla<BR><BR>bla<BR>bla<BR><BR>bla
</div>
<div class="just-margin">
<h2>only margin</h2>
bla<BR><BR>bla<BR>bla<BR><BR>bla
</div>
<div class="brbr">
<h2><code>br</code>others vs only <code>br</code>s</h2>
bla<BR><BR>bla<BR>bla<BR><BR>bla
</div>
They all have their own version of strange behaviour. Other than the browser default, only the last one respects the difference between one and two brs.
You can't change the height of the br tag itself, as it's not an element that takes up space in the page. It's just an instruction to create a new line.
You can change the line height using the line-height style. That will change the distance between the text blocks that you have separated by empty lines, but natually also the distance between lines in a text block.
For completeness: Text blocks in HTML is usually done using the p tag around text blocks. That way you can control the line height inside the p tag, and also the spacing between the p tags.
Take a look at the line-height property. Trying to style the <br> tag is not the answer.
Example:
<p id="single-spaced">
This<br> text
<br> is
<br> single-spaced.
</p>
<p id="double-spaced" style="line-height: 200%;">
This<br> text
<br> is
<br> double-spaced.
</p>
The line height of the br tag can be different from the line height of the rest of the text inside a paragraph text by setting font-size for br tag.
Example: br { font-size: 200%; }
Use the content property and style that content. Content behavior is then adjusted using pseudo elements. Pseudo elements ::before and ::after both work in Mac Safari 10.0.3.
Here element br content is used as the element anchor for element br::after content. Element br is where br spacing can be styled. br::after is the place where br::after content can be displayed and styled. Looks pretty, but not a 2px <br>.
br { content: ""; display: block; margin: 1rem 0; }
br::after { content: "› "; /* content: " " space ignored */; float: left; margin-right: 0.5rem; }
The br element line-height property is ignored. If negative values are applied to either or both selectors to give vertical 'lift' to br tags in display, then correct vertical spacing occurs, but display incrementally indents display content following each br tag. The indent is exactly equal to the amount that lift varies from actual typographic line-height. If you guess the right lift, there is no indent but a single pile-up line exactly equal to raw glyph height, jammed between previous and following lines.
Further, a trailing br tag will cause the following html display tag to inherit the br:after content styling. Also, pseudo elements cause <br> <br> to be interpreted as a single <br>. While pseudo-class br:active causes each <br> to be interpreted separately. Finally, using br:active ignores pseudo element br:after and all br:active styling. So, all that's required is this:
br:active { }
which is no help for creating a 2px high <br> display. And here the pseudo class :active is ignored!
br:active { content: ""; display: block; margin: 1.25em 0; }
br { content: ""; display: block; margin: 1rem; }
br::after { content: "› "; /* content: " " space ignored */; float: left; margin-right: 0.5rem; }
This is a partial solution only. Pseudo class and pseudo element may provide solution, if tweaked. This may be part of CSS solution. (I only have Safari, try it in other browsers.)
Learn web development: pseudo classes and pseudo elements
Pay attention to global elements - BR at Mozilla.org
You can control the <br> height if you put it inside a height limited div. Try:
<div style="height:2px;"><br></div>
As the 'margin' doesn't work in Chrome, that's why I used 'border' instead.
br {
display: block;
content: "";
border-bottom: 10px solid transparent; // Works in Chrome/Safari
}
#-moz-document url-prefix() {
br {
margin-bottom: 10px; // As 'border-bottom' doesn't work in firefox and 'margin-bottom' doesn't work in Chrome/Safari.
}
}
The BR is anything but 'extra-special': it is still a valid XML tag that you can give attributes to. For example, you don't have to encase it with a span to change the line-height, rather you can apply the line height directly to the element.
You could do it with inline CSS:
This is a small line
<br />
break. Whereas, this is a BIG line
<br />
<br style="line-height:40vh"/>
break!
Notice how two line breaks were used instead of one. This is because of how CSS inline elements work. Unfourtunately, the most awesome css feature possible (the lh unit) is still not there yet with any browser compatibility as of 2019. Thus, I have to use JavaScript for the demo below.
addEventListener("load", function(document, getComputedStyle){"use strict";
var allShowLineHeights = document.getElementsByClassName("show-lh");
for (var i=0; i < allShowLineHeights.length; i=i+1|0) {
allShowLineHeights[i].textContent = getComputedStyle(
allShowLineHeights[i]
).lineHeight;
}
}.bind(null, document, getComputedStyle), {once: 1, passive: 1});
.show-lh {padding: 0 .25em}
.r {background: #f77}
.g {background: #7f5}
.b {background: #7cf}
This is a small line
<span class="show-lh r"></span><br /><span class="show-lh r"></span>
break. Whereas, this is a BIG line
<span class="show-lh g"></span><br /><span class="show-lh g"></span>
<span class="show-lh b"></span><br style="line-height:40vh"/><span class="show-lh b"></span>
break!
You can even use any CSS selectors you want like ID's and classes.
#biglinebreakid {
line-height: 450%;
// 9x the normal height of a line break!
}
.biglinebreakclass {
line-height: 1em;
// you could even use calc!
}
This is a small line
<br />
break. Whereas, this is a BIG line
<br />
<br id="biglinebreakid" />
break! You can use any CSS selectors you want for things like this line
<br />
<br class="biglinebreakclass" />
break!
You can find our more about line-height at the W3C docs.
Basically, BR tags are not some void in world of CSS styling: they still can be styled. However, I would recommend only using line-height to style BR tags. They were never intended to be anything more than a line-break, and as such they might not always work as expected when using them as something else. Observe how even after applying tons of visual effects, the line break is still invisible:
#paddedlinebreak {
display: block;
width: 6em;
height: 6em;
background: orange;
line-height: calc(6em + 100%);
outline: 1px solid red;
border: 1px solid green;
}
<div style="outline: 1px solid yellow;margin:1em;display:inline-block;overflow:visible">
This is a padded line
<br id="paddedlinebreak" />
break.
</div>
A work-around for things such as margins and paddings is to instead style a span with a br in it like so.
#paddedlinebreak {
background: orange;
line-height: calc(6em + 100%);
padding: 3em;
}
<div style="outline: 1px solid yellow;margin:1em;display:inline-block;overflow:visible">
This is a padded line
<span id="paddedlinebreak"><br /></span>
break.
</div>
Notice how the orange blob above is the span that contains the br.
#biglinebreakid {
line-height: 450%;
// 9x the normal height of a line break!
}
.biglinebreakclass {
line-height: 1em;
// you could even use calc!
}
This is a small line
<br />
break. Whereas, this is a BIG line
<br />
<br id="biglinebreakid" />
break! You can use any CSS selectors you want for things like this line
<br />
<br class="biglinebreakclass" />
break!
You can write br tag as show
<br style="content:''; padding: 10px 0;" />
Change padding value to 10px to anything you like.
Note: As padding is specified, height increases in both directions(top and bottom)
The line height of the <br> can be different from the line height of the rest of the text inside a <p>. You can control the line height of your <br> tags independently of the rest of the text by enclosing two of them in a <span> that is styled. Use the line-height css property, as others have suggested.
<p class="normalLineHeight">
Lots of text here which will display on several lines with normal line height if you put it in a narrow container...
<span class="customLineHeight"><br><br></span>
After a custom break, this text will again display on several lines with normal line height...
</p>
<font size="4"> <font color="#ffe680">something here</font><br>
I was trying all these methods but most didn't work properly for me, eventually I accidentally did this and it works great, it works on chrome and safari (the only things I tested on). Replace the colour code thing with your background colour code and the text will be invisible. you can also adjust the font size to make the line break bigger or smaller depending on your desire. It is really simple.
Related
How I can set margin between lines in one paragraph?
I need to set margins between lines in one paragraph. How I can do this? I need to make something like this: HTML: <p>Any creative project is unique and should be provided with the appropriate quality</p> I tried to put each line in <span> and set margin-bottom to it, but it not working.
Just wrap your whole text in a <span> tag and use line-height for margins and padding for spacing between text and background Stack Snippet p { font: bold 30px Verdana; } span { background: red; line-height: 45px; color: #fff; padding: 3px; } <p><span>Any creative project is unique and should be provided with the appropriate quality</span></p>
If you want to use <span> with margin you need to set also display: inline-block; or display: block; to the <span> p { width: 200px; } p > span { display: inline-block; margin-bottom: 5px; background-color: orange; } <p> <span>Any creative project is unique</span> <span>and should be provided with</span> <span>the appropriate quality</span> </p>
U can use <br> between each lines or just put a span with a height between each line, something like this: <p>Any creative project is unique</p> <span style="height: 10px;"></span><p>panand should be provided with</p>
Try using line-height property in your .css file referencing te element enclosing the text.
p element's length same as text in it?
I simply want all of my p elements to be the length of the text in it. It works if I put .intro p at inline-block, but I would like my p elements all to be displayed underneath each other. Is there a way to do this? HTML: <div class="intro"> <p>fjsdqk dhksjfd kjsh kdshjkd</p> <p>hsdqjkf kjdsh</p> <p>hdsqkhfj sdhkjf fsjqkfhdks hjs</p> </div> CSS: .intro { margin-top: 80px; color: #ffffff; text-align: center; } .intro p { margin-bottom: 12px; padding: 6px 12px; background: #25d6a2; }
Just add br tag after each p element <div class="intro"> <p>fjsdqk dhksjfd kjsh kdshjkd</p><br> <p>hsdqjkf kjdsh</p><br> <p>hdsqkhfj sdhkjf fsjqkfhdks hjs</p><br> </div> Demo If you don't want to add <br /> in the DOM or for some reason you cannot modify your HTML, you can simulate line break using CSS :after pseudo with content property having an value of \a and white-space set to pre As far as the explanation goes for \a, its an hexadecimal value for line feed which is equivalent to 10. p:after { content:"\a"; white-space: pre; } Demo
In a sense, you want to eat your cake and keep it: make the p elements inline elements as far as box formatting is considered (in this case, for the purpose of setting background behind the text only) but block elements in the sense of starting on a fresh line. There are various methods and tricks, but perhaps the simplest one is to use floating (instead of display: inline-block): float the elements to the left but also clear floating, so that no real floating takes place—you just want the side effect of floating, the effect of making the element just as wide as its content requires: .intro p { float: left; clear: both; } In addition, you need to set clear: both on the element after the intro.
How to make the last span element inside a paragraph no-wrap?
I have a paragraph surrounded by decorative quotes. The layout is fluid, and for some width, only the decorative quote is wrapping, which I want to avoid. Screenshot : Code : http://jsfiddle.net/84EDh/1/ (Note: not tested outside of Chrome) <p> <span class="bigchar ldquo"></span> Begin paragraph [...paragraph content...] end paragraph. <span class="bigchar rdquo"></span> </p> css: p { position: relative; } .bigchar { display: inline-block; width: 20px; } .bigchar:after { color: #aaa; font-size: 50px; position: absolute; } .ldquo:after { content: '“'; top: -10px; } .rdquo:after { content: '”'; bottom: -30px; } Possible solution: Wrap the last word with the closing span in another span [...paragraph content...] end <span class="nowrap"> paragraph. <span class="bigchar rdquo"></span> </span> Question: Is there a more elegant way to achieve the no-wrapping of the last span of the paragraph ? This is not very semantic, nor easy to achieve because, as you would expect, the content of the paragraph is dynamic, hence not easy to split at the template level. Edit: css added
Instead of using a span, it's better to use a q, because that's what q elements were designed for! So the HTML becomes <p><q class="bigchar">This text is surrounded by quotes. I want the text to wrap according it's parent width. This is no problem, it's the default behaviour. However, I would like to avoid the last span, containing a decoration quote, to wrap.</q></p> with the CSS q.bigchar::before {content:'\201C'; font-size:2em; line-height:0; position:relative; top:.3em; margin-right:.13em;} q.bigchar::after {content:'\201D'; font-size:2em; line-height:0; position:relative; top:.3em; margin-left:.13em;} resulting in this fiddle. No extra markup is needed. Note that since I leave the p alone, you can put all kinds of styles (like text-indent) on it, and it will behave as it should.
What about nesting span inside other span? What we achieve this way is rdquo acting just as a regular text (that means: if you put either no space or non-breaking space between rdquo and last word, it's not going to break into two lines). Demo here: http://jsfiddle.net/HFE9T/1/
Instead of using additional span elements, try using :before and :after on paragraph like this: p:after { color: #aaa; font-size: 50px; position: absolute; content: '”'; bottom: -30px; } p:before { color: #aaa; font-size: 50px; position: absolute; content: '“'; top: -10px; } Updated fiddle here
Here is the final markup and CSS to achieve the expected behaviour, inspired by Michal Rybak but without the compromises (except the two span in the markup) : HTML : <p> <span class="quote" attr-char="“"> </span> Paragraph content Here. Note the no-line-break here.<span class="quote" attr-char="”"> </span> </p> The attr-char attribute is pretty handy to be able to change the quote characters for different languages as with the q element (somewhat) CSS : p .quote { position: relative; margin-right: 30px; /* Indent text at paragraph beginning */ } p .quote:before { position: absolute; top: 10px; line-height: 20px; font-size: 50px; content: attr(attr-char); /* Take the character from markup */ } p .quote:last-child:before { margin-left: 10px; /* Give the closing quote some space */ } Fiddle : http://jsfiddle.net/HFE9T/4/
You can add a non-breaking spaces \00a0 before and after the quotes: <style> element:before {content: "“\00a0";} element:after {content: "\00a0”";} </style> And then work your way around with negatives margins if you don’t want those space to show.
Writing colour coded XML code in HTML with <span> tag causing spaces
I am working on a project and I need to display some XML on the web page. I am trying to colour code the XML using various span tags for the different xml parameters. It is more a less working, but to keep the code tidy, I am putting each tag on a new line. This is kind of working as each each tag stays on the same line, which is what I want unless I put a <br /> tag but it puts a space in between each span tag. For example, my XML is being shown as < Application x : Class ="YourApp.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" and I want <Application x:Class="YourApp.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Below is how I am trying to write the code in my HTML <div class="codeBlock"> <span class="stringAndOperands"><</span> <span class="xmltag">Application</span> <span class="xmlParameter">x</span> <span class="stringAndOperands">:</span> <span class="xmlParameter">Class</span> <span class="stringAndOperands">="YourApp.App"</span><br /> <span class="xmlParameter">xmlns</span><span class="stringAndOperands">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span> </div> Below is my CSS .xmltag { color: #a31515; white-space: pre; } .stringAndOperands { color: #0034ff; white-space: pre; } .xmlParameter { color: #ff0000; white-space: pre; } .codeBlock { width: 1000px; margin-left: auto; margin-right: auto; height: auto; background-color: #cdcdcd; font-family: courier new; padding: 5px; } How can I remove the spaces when the span is on a different line. I know I can put each span on the same line would prevent this but this it makes my source code look a bit of a mess. Thanks for any help you can provide.
You can't really, HTML automatically interprets line breaks as a single white space. You basically need them all on the same line in the markup for there to truly be no white space. However you can use CSS to kind of hack your way around it. If you set font-size: 0px on the .codeBlock div and font-size: 12px or whatever on span the white spaces will not take up any space and it will fix your problem, assuming that you have no text inside .codeBlock that is NOT contained within a span element. JSFiddle example
You may want to make sure you are using a reset to strip out any padding, margin etc. span {margin: 0;padding: 0;border: 0;outline:0;vertical-align: baseline;}
How to line-break from css, without using <br />?
How to achieve same output without <br>? <p>hello <br> How are you </p> output: hello How are you
You can use white-space: pre; to make elements act like <pre>, which preserves newlines. Example: p { white-space: pre; } <p>hello How are you</p> Note for IE that this only works in IE8+.
Impossible with the same HTML structure, you must have something to distinguish between Hello and How are you. I suggest using spans that you will then display as blocks (just like a <div> actually). p span { display: block; } <p><span>hello</span><span>How are you</span></p>
Use <br/> 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 <br/>) While not immediately obvious, you can actually apply display:none to a <br/> tag to hide it, which enables the use of media queries in tandem with semantic BR tags. <div> The quick brown fox<br /> jumps over the lazy dog </div> #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. jsfiddle example
There are several options for defining the handling of white spaces and line breaks. If one can put the content in e.g. a <p> 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: <style> p { white-space: pre-line; /* collapse WS, preserve LB */ } </style> <p>hello How are you</p> 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 */ SOURCE: W3 Schools
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"): blockquote[title][cite]:after { content:attr(title)"\a"attr(cite) }
In the CSS use the code p { white-space: pre-line; } With this CSS every enter inside the P tag will be a break-line at the HTML.
Building on what has been said before, this is a pure CSS solution that works. <style> span { display: inline; } span:before { content: "\a "; white-space: pre; } </style> <p> First line of text. <span>Next line.</span> </p>
To make an element have a line break afterwards, assign it: display:block; Non-floated elements after a block level element will appear on the next line. Many elements, such as <p> and <div> 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 <br /> 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 <greeting> 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.
<pre> <--------------------------------------- lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum </pre> <-------------------------------------- OR <div style="white-space:pre"> <----------------------------------- lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum </div> <----------------------------------- source: https://stackoverflow.com/a/36191199/2377343
Here's a bad solution to a bad question, but one that literally meets the brief: p { width : 12ex; } p:before { content: "."; float: right; padding-left: 6ex; visibility: hidden; }
Use overflow-wrap: break-word; like: .yourelement{ overflow-wrap: break-word; }
Maybe someone will have the same issue as me: I was in a element with display: flex so I had to use flex-direction: column.
For a List of Links The other answers provide some good ways of adding line breaks, depending on the situation. But it should be noted that the :after selector is one of the better ways to do this for CSS control over lists of links (and similar things), for reasons noted below. Here's an example, assuming a table of contents: <style type="text/css"> .toc a:after{ content: "\a"; white-space: pre; } </style> <span class="toc"> Item A1 Item A2 Item B1 Item B2 </span> And here's Simon_Weaver's technique, which is simpler and more compatible. It doesn't separate style and content as much, requires more code, and there may be cases where you want to add breaks after the fact. Still a great solution though, especially for older IE. <style type="text/css"> .toc br{ display: none; } /* comment out for horizontal links */ </style> <span class="toc"> Item A1<br/> Item A2<br/> Item B1<br/> Item B2<br/> </span> Note the advantages of the above solutions: No matter the whitespace in the HTML, the output is the same (vs. pre) No extra padding is added to the elements (see NickG's display:block comments) You can easily switch between horizontal and vertical lists of links with some shared CSS without going into every HTML file for a style change No float or clear styles affecting surrounding content The style is separate from the content (vs. <br/>, or pre with hard-coded breaks) This can also work for loose links using a.toc:after and <a class="toc"> You can add multiple breaks and even prefix/suffix text
Setting a br tag to display: none is helpful, but then you can end up with WordsRunTogether. I've found it more helpful to instead replace it with a space character, like so: HTML: <h1> Breaking<br />News:<br />BR<br />Considered<br />Harmful! </h1> CSS: #media (min-device-width: 1281px){ h1 br {content: ' ';} h1 br:after {content: ' ';} }
I like very simple solutions, here is one more: <p>hello <span>How are you</span></p> and CSS: p { display: flex; flex-direction: column; }
How about<pre> tag? source: http://www.w3schools.com/tags/tag_pre.asp
The code can be: <div class="text-class"><span>hello</span><span>How are you</span></div> CSS would be: .text-class { display: flex; justify-content: flex-start; flex-direction: column; align-items: center; }
You need to declare the content within <span class="class_name"></span>. After it the line will be break. \A means line feed character. .class_name::after { content: "\A"; white-space: pre; }
You can add a lot of padding and force text to be split to new line, for example p { padding-right: 50%; } Worked fine for me in a situation with responsive design, where only within a certain width range it was needed for text to be split.
Using white-space will not work for long sentences without spaces like HiHowAreYouHopeYouAreDoingGood...etc to fix this consider using word-wrap: break-word; instead it's made to allow long words to be able to break and wrap onto the next line., its used by Facebook, Instagram and me 😆 Example #container { width: 40%; background-color: grey; overflow:hidden; margin:10px; } #container p{ white-space: pre-line; background-color: green; } .flex{ display: flex; } #wrap { width: 30%; background-color: grey; overflow:hidden; margin:10px; } #wrap p{ word-wrap: break-word; background-color: green; } <h1> white-space: pre-line;</h1> <div class='flex'> <div id="container"> <h5>With spaces </h5> <p>Sample Text 1 Sample Text 1 Sample Text 1 Sample Text 1 Sample Text 1 Sample Text 1 Sample Text 1</p> </div> <div id="container"> <h5>No specaes (not working )</h5> <p>HiHowAreYouHopeYouAreDoingGoodHiHowAreYouHopeYouAreDoingGoodHiHowAreYouHopeYouAreDoingGood</p> </div> </div> <h1> word-wrap: break-word;</h1> <div class='flex'> <div id="wrap"> <h5>With spaces </h5> <p>Sample Text 1 Sample Text 1 Sample Text 1 Sample Text 1 Sample Text 1 Sample Text 1 Sample Text 1</p> </div> <div id="wrap"> <h5>No specaes (working )</h5> <p>HiHowAreYouHopeYouAreDoingGoodHiHowAreYouHopeYouAreDoingGoodHiHowAreYouHopeYouAreDoingGoodHiHowAreYouHopeYouAreDoingGood</p> </div> </div>
On CSS-tricks, Chris Coyier have tested lots of options and the final and pretty neat one was using display:table, Each one have their own problems which you will find out when you use background-color on the span! body { padding: 20px; font-family: 'Open Sans', sans-serif; } h1 { font-weight: 300; font-size: 24px; line-height: 1.6; background: #eee; padding: 20px; margin: 5px 0 25px 0; text-align:center; } h1 span { color: white; font-weight: 900; } h1 span { background: black; padding: 1px 8px; display: table; margin:auto; } <h1 class="one"> Break right after this <span> and before this </span> </h1> Here You can see all other options on codepen: Injecting a Line Break
A modern and simple solution could be setting the width like that: width: min-content; This CSS rule is mostly useful for text content, but not only: https://developer.mozilla.org/en-US/docs/Web/CSS/min-content p { margin: 20px; color: #222; font-family:'Century Gothic', sans-serif; border: 2px dotted grey; padding: 3px; } .max { width: max-content; } .min { width: min-content; } <!DOCTYPE html> <html lang="en"> <head /> <body> <p class="max"> Max width available </p> <p class="min"> Min width available </p> </body> </html>
Both Vincent Robert and Joey Adams answers are valid. If you don't want, however, change the markup, you can just insert a <br /> using javascript. There is no way to do it in CSS without changing the markup.
In my case, I needed an input button to have a line break before it. I applied the following style to the button and it worked: clear:both;
In case this helps someone... You could do this: <p>This is an <a class="on-new-line">inline link</a>?</p> With this css: a.on-new-line:before { content: ' '; font-size:0; display:block; line-height:0; }
Using instead of spaces will prevent a break. <span>I DONT WANT TO BREAK THIS LINE UP, but this text can be on any line.</span>
I'm guessing you did not want to use a breakpoint because it will always break the line. Is that correct? If so how about adding a breakpoint <br /> in your text, then giving it a class like <br class="hidebreak"/> then using media query right above the size you want it to break to hide the <br /> so it breaks at a specific width but stays inline above that width. HTML: <p> The below line breaks at 766px. </p> <p> This is the line of text<br class="hidebreak"> I want to break. </p> CSS: #media (min-width: 767px) { br.hidebreak {display:none;} } https://jsfiddle.net/517Design/o71yw5vd/
This works in Chrome: p::after { content: "-"; color: transparent; display: block; }