I'm trying to put a link called Submit resume in a menu using a li tag. Because of the whitespace between the two words it wraps to two lines. How to prevent this wrapping with CSS?
Use white-space: nowrap;[1] [2] or give that link more space by setting li's width to greater values.
[1] § 3. White Space and Wrapping: the white-space property - W3 CSS Text Module Level 3
[2] white-space - CSS: Cascading Style Sheets | MDN
You could add this little snippet of code to add a nice "…" to the ending of the line if the content is to large to fit on one line:
li {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
If you want to achieve this selectively (ie: only to that particular link), you can use a non-breaking space instead of a normal space:
<li>submit resume</li>
https://en.wikipedia.org/wiki/Non-breaking_space#Encodings
edit: I understand that this is HTML, not CSS as requested by the OP, but some may find it helpful…
display: inline-block; will prevent break between the words in a list item:
li {
display: inline-block;
}
Bootstrap 4 has a class named text-nowrap. It is just what you need.
In your CSS, white-space can do the job
possible values:
white-space: nowrap
white-space: pre
white-space: pre-wrap
white-space: pre-line
white-space: break-spaces
white-space: normal
Related
How can I insert multiple spaces between words in a div but still make it word wrap?
You can use the CSS property white-space: pre-wrap; to indicate to the browser that the contents of the element are pre-formatted (so should consider the whitespace significant and not remove it) and still line wrap.
div {
font-size: 26px;
white-space: pre-wrap;
}
<div>This is text with spaces that wraps.</div>
If you need it for all of the words in the <div> or if there's a good reason to put a <span> or something around the words you do need it for you could also try the css property word-spacing.
selector {
word-spacing: /*some length depending on your font*/
}
I'm trying to set background color for a whitespace within a span. But it won't show up when the whitespace span happens to be at the point when line wraps.
HTML
asdfghjklkqowbvaa<span> </span>a
CSS
body {
font-family: monospace;
font-size: 40px;
}
span {
background-color: black;
}
JSFIDDLE
Alter your css for the span to show white spaces, just like in the <pre> tag.
Take a look at the different white-space options
span {
background-color: black;
white-space:pre;
}
From the mentioned resource here is a nice table what the different options for white-space will do:
New lines Spaces and tabs Text wrapping
normal Collapse Collapse Wrap
pre Preserve Preserve No wrap
nowrap Collapse Collapse No wrap
pre-wrap Preserve Preserve Wrap
pre-line Preserve Collapse Wrap
If you add a to your span, the string will not break anymore on your space but instead 'glue' the two parts together, without wrapping the string on the space.
Another option if you do not want to rely on CSS for any reason is using the non-breaking space: , or
(see here for more information about it)
Is there any way to apply a particular style to an element conditionally on it overflowing?
E.g. if I have say:
<div>
This text is very long, no really it is extremely long; it just goes on and on and on and on. You might say it's a bit like the Duracell(R) bunny, but without the batteries, and the floppy ears.
</div>
and
div {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
// TODO: is there any way to only apply this rule if the text is overflowing?
div {
background-color: red;
}
Is there any way using pure CSS to have the background-color: red rule apply only if the text is overflowing?
Note: I can see a way of doing this using javascript; I just want to know if there is a pure CSS way to do it.
I'm trying to put a link called Submit resume in a menu using a li tag. Because of the whitespace between the two words it wraps to two lines. How to prevent this wrapping with CSS?
Use white-space: nowrap;[1] [2] or give that link more space by setting li's width to greater values.
[1] § 3. White Space and Wrapping: the white-space property - W3 CSS Text Module Level 3
[2] white-space - CSS: Cascading Style Sheets | MDN
You could add this little snippet of code to add a nice "…" to the ending of the line if the content is to large to fit on one line:
li {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
If you want to achieve this selectively (ie: only to that particular link), you can use a non-breaking space instead of a normal space:
<li>submit resume</li>
https://en.wikipedia.org/wiki/Non-breaking_space#Encodings
edit: I understand that this is HTML, not CSS as requested by the OP, but some may find it helpful…
display: inline-block; will prevent break between the words in a list item:
li {
display: inline-block;
}
Bootstrap 4 has a class named text-nowrap. It is just what you need.
In your CSS, white-space can do the job
possible values:
white-space: nowrap
white-space: pre
white-space: pre-wrap
white-space: pre-line
white-space: break-spaces
white-space: normal
I'm trying to find a way to allow really long text in an HTML link to wrap while containing the link in a compact rectangle.
Essentially, I want this:
with a really, really
Here is some text long link that wraps and here is some more text.
around in a rectangle
Instead of:
Here is some text with a really, really long link that wraps
around in a rectangle and here is some more text.
How can I do this?
You can do it like this in Firefox - it doesn't work in IE though :(
<p>Here is some text with a really, really long link that wraps around in a rectangle and here is some more text.
<style type="text/css">
a {display: inline-block; max-width: 100px; vertical-align: middle;}
</style>
I know this is pretty old, but it was still one of the first search results that came up for me on the subject. I found that using:
white-space: pre-wrap;
forced the anchor to wrap the text around my width restrictions
You can use display:inline-block; css property on your link which will give you exactly the effect you desire (dont forget to set the width :).
display:inline-block is not supported in IE, but lucky for you someone has already done the hard work and come up with a workaround here.
I wound up having this issue because I was using bootstrap and it was by default setting this as one of it's styles
.dropdown-menu > li > a{
white-space: nowrap;
}
Which made kept the "word-break" and "width" css properties from actually solving the problem
So I first had to set that white-space property to "pre-wrap" instead
<p>Here is some text with a really, really long link that wraps around in a rectangle and here is some more text.
<style type="text/css">
a {display: inline-block; max-width: 100px; vertical-align: middle;}
</style>
There are many ways:
display : inline-block;
display : inline;
white-space: pre-wrap;
white-spa s: nowrap;