I have a static width div element (400px), I want this div to hold any kind of text, no mater how short or long it could be.
This text comes from the user, so I have no-way to determine what length it could hold.
I want the text to fit in the box.
to fit means to fit.
So, this means that the text will adjust its font size based on its parent,
if there were few words, the text will be with big font-size, while if the text has too many words, then it will become smaller and smaller and smaller until it fits inside the box,
think of this picture, for example:
The more words you add, the smaller the text becomes.
Is it possible with pure css?
No_jQuery
I got a solution without jquery.
This is in pure JS.
Think you can convert this to react yourself or just use it in pure JS.
Hope this helps! :)
function resize_to_fit(container) {
const output = container.getElementsByClassName("output")[0];
let fontSize = window.getComputedStyle(output).fontSize;
output.style.fontSize = (parseFloat(fontSize) - 1) + 'px';
if(output.clientHeight >= container.clientHeight){
resize_to_fit(container);
}
}
function processInput(container) {
const output = container.getElementsByClassName("output")[0];
output.style.fontSize = '100px'; // Default font size
resize_to_fit(container);
}
document.querySelectorAll('.container').forEach(function(container) {
processInput(container);
});
<div id="outer" class="container"
style="width:80%; height:100px; border:2px solid black; font-size:20px;">
<div class="output" style="word-break: break-all; word-wrap: break-word;">
this is a small text
</div>
</div>
<br/>
<div id="outer2" class="container"
style="width:80%; height:100px; border:2px solid black; font-size:20px;">
<div class="output" style="word-break: break-all; word-wrap: break-word;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
Related
I got some slogans that can either be displayed on a single line or multiple lines if wrapped at specific places. Is there a CSS option to respect a <br> tag only if necessary to fit the container width and leave it unwrapped otherwise?
For example
<div>
They told me that<br> aesthetics matter.
</div>
You can use CSS code to break work
#content::after {
content: "\a";
white-space: pre;
}
<div id="content">They told me that</div>
<div id="break">aesthetics matter.</div>
and also you can define padding instead of height because padding expands the container according to its children and also prevent children to overflow the example is the following
#container {
width: 300px;
padding: 5px;
background-color: grey;
}
<div id="container">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
You can CSS property word-wrap: break-word; in div to solve your issue. And if it doesn't solve your issue, let me know in comments I will try my best to help you.
I would like to highlight sentences in some text. The normal way is to wrap the sentence in a span element and set a background color:
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. <span class="highlight">Duis aute irure dolor in reprehenderit
in voluptate velit esse cillum dolore eu fugiat nulla pariatur</span>.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.
</p>
And:
.highlight{
background-color: yellow;
}
If I now want to use some padding and other styles with this highlight, I have a problem:
.highlight{
background-color: yellow;
padding: 5px;
border-radius: 5px
}
With small highlights, this problem is adequately solved by display: inline-block;. But here I now have the problem of my sentence being, well, a block, breaking the text in three paragraphs:
.highlight{
background-color: yellow;
padding: 5px;
border-radius: 5px;
display: inline-block;
}
Is there a way to use inline-block without all the trouble?
Working JS fiddle
Use line-height property. Do little bit math, if your font-size is 16px and you are adding 5px padding then line-height will be 26px
p{
width: 300px;
}
.highlight{
background-color: yellow;
padding: 5px;
border-radius: 5px;
line-height: 26px;
}
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. <span class="highlight">Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur</span>. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
It sounds to me like you're not entirely clear what you want. Observer has given you a solution that might work for you and might not, since the line height persists throughout your entire paragraph, making all of the lines wider. If that is indeed what you want, then you have your answer.
From a UX perspective, though, it seems to me that you need to take a couple of steps back. Do you really want to set highlighted text apart in such a way as it interrupts the flow of reading? That's what you will do, if you add extra padding to it. The idea that you don't like it to be set apart as a paragraph suggests that you don't want padding to be added to it, because after all that's how a paragraph is set apart.
If you want to keep the sentence in with the rest of the paragraph and pad it as well, you can probably go to the trouble of figuring out exactly how to do it, but you'll notice that a line that has a combination of both highlighted and normal text will have the entire line padded. So you'll have some normal text that will be set apart farther than other normal text, as in Observer's sample. That draws the reader's attention to the text in such a way as to be distracting.
If you wanted it to stick out more than it would by just highlighting it, you could put a border around it, as in my example. But I think that looks a bit off, too, because the border gets broken on line breaks. You might consider putting a border on just the top and bottom as well.
In the end, though, I would stick to what everyone else does, and just change the color. That's what readers are used to, and if you change it, they'll have to take a little time to figure out what you're up to.
.highlight{
background-color: yellow;
border: 1px solid green;
padding: 0 5px;
}
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. <span class="highlight">Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur</span>. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
At the moment, I have a p tag that has it's text center-aligned. The issue is when a block of text with properly placed line-breaks is shown in this p tag it looks wrong. But, when a string is so short it doesn't get a line-break it needs to stay center-aligned to look good. So to me the solution would be to change the way the text aligns once the text starts to wrap. Any tips on how to do this?
You can achieve that by using flexbox to center a shrink-wrapped version of <p> as a whole:
<!DOCTYPE html>
<style>
.container {
display:flex;
justify-content: center;
}
</style>
<div class="container">
<p>short text</p>
</div>
<div class="container">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
Say I have a table with one column per row, where the cells contain text. One of the rows has much longer text than the others. I'd like to display a table where that row is not taken into consideration when the table width is computed; that is, the width would be the same as if that long row were removed. The long text would then be wrapped over several lines.
Example:
<html>
<head>
<title>tabtest</title>
<style type="text/css">
#tab1 { border-style: solid; border-collapse: collapse }
table#tab1 td { border-style: solid; border-width : 1px;
padding: 5px }
</style>
</head>
<body>
<table id="tab1">
<tr><td>Veni, vidi, vici</td></tr>
<tr><td>All's well that ends well</td></tr>
<tr><td>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</td></tr>
<tr><td>Let them eat cake</td></tr>
</table>
</body>
</html>
This makes the table width the entire width of the browser window (with the really long text wrapping over 3 lines). This isn't what I want. I'm trying to get it displayed so that the table width is based on the second row (the longest of the remaining three rows). The third row would then wrap over many lines (which is ugly, but it won't be so bad in the real-life example I'm working on).
I've tried various combinations of width: inherit, width: 100%, word-wrap: break-word, either in the <td> for the long row or in singly- or doubly-nested <div> elements inside that cell, but I haven't gotten anything to do what I want.
How can I make the first character of each paragraph look like this:
I'd prefer using CSS only.
p:first-letter {
float: left;
font-size: 5em;
line-height: 0.5em;
padding-bottom: 0.05em;
padding-top: 0.2em;
}
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
Tweak the font, padding, line-height as needed.
Example: http://jsfiddle.net/RLdw2/
add this p:first-letter{font-size:50px}
DEMO
Here is the exact solution for your requirement shown in the image
DEMO 2
WIKIPEDIA EXPLANATION
see DEMO here...
CSS///
p{ width:300px; border:1px solid #000;}
p:first-letter
{font-size:52px;color:#8A2BE2;font-weight:bold;float: left;margin-top:4px;}
HTML///
<p>The first character of this paragraph will be 52px big
as defined in the CSS rule above. Rest of the
characters in this paragraph will remain normal. This example
shows how to use :first-letter pseduo element to give effect to
the first characters of any HTML element.</p>
<p>The first character of this paragraph will be 52px big
as defined in the CSS rule above. Rest of the
characters in this paragraph will remain normal. This example
shows how to use :first-letter pseduo element to give effect to
the first characters of any HTML element.</p>