I have a div with width of 130px a height of 200px.
Various text content gets displayed in this div (I've written some JS to fade in and out different content). The problem is even though I've tried truncating the text to say 180 characters, sometimes the content loaded into this div may contain a picture (or it may not) or might contain some line breaks (or may not) so a fixed character count for truncating sometimes does not clip enough of the text (i.e. the line breaks or perhaps an image will have taken up more vertical space in the div).
Ideally I'd like to truncate and add an ellipsis to the content when it is about to go over the 200px height limit - is this possible? I've looked at the CSS text-overflow property...this seems to work only really for width based truncating (or is that an incorrect assumption?)
Perhaps there is a JS based solution or maybe calcuating how many characters and image (the image sizes ARE fixed) and line break will take up and truncating after that.
Any ideas are much appreciated.
I used dotdotdot jQuery plugin to solve a similar problem. Line breaks should not be an issue with dotdotdot but inserted images should have width and height atributes specified in html.
If you are generating content dynamically you could determine image dimensions server-side (e.g. getimagesize function if you are using PHP). If this is not an option you can initialize dotdotdot inside $(window).load() but this will probably show content without ellipsis till all the media on the page is loaded.
Here's a solution:
http://jsfiddle.net/2jCHg/2/
There's a dangling ellipsis container at the end of the text container. The ellipsis is hidden using JavaScript if the text fits the container. The <span> for the ellipsis has to have a background to occlude the original text. I used flat white in my example. You can optionally use a PNG with alpha transparency to occlude the text with a nice gradient (transparent to white).
You can optionally inject the ellipsis markup with your script to keep your original markup pristine.
It's also unfortunate that the ellipsis is right-justified instead of immediately following the last character that's displayed.
Markup:
<div class="container">
<div class="summary">
Your text goes here
</div>
<div class="ellipsis"><span>…</span></div>
</div>
Style:
.container {
width: 200px;
border: 1px solid #888;
padding: 0.5em;
line-height: 1.2em;
margin-bottom: 10px;
}
.summary {
height: 6em; /* adjust based on line-height * rows desired */
overflow: hidden;
}
.ellipsis {
height: 0;
position: relative;
top: -1.2em;
text-align: right;
}
.ellipsis span {
background: white; /* occlude text with background color */
padding-left: 0.5em;
position: relative;
top: -0.25em;
}
Script:
$(".summary").each(function () {
$(this).next().toggle(this.scrollHeight > this.offsetHeight);
});
Try this:
.ellipsis{
white-space: word;
overflow:hidden;
}
.ellipsis:after{
content:'...';
}
Try this:
.ellipsis{
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
Related
Text in html has a line-height attribute that determines how much vertical space the characters are given. By default, font size and vertical space do not match. If a span has 50px font size, line-height of 50px will produce a spacing at the bottom and top of the text.
http://jsfiddle.net/7vNpJ/773/
You can fix this by manually adjusting the line-height, such as in this example:
http://jsfiddle.net/7vNpJ/774/
<span>
BIG TEXT
</span>
span {
display: inline-block;
font-size: 50px;
background-color: green;
line-height:0.7;
}
Line height of 0.7 only works for this particular font. I am looking for a way to perform this automatically, regardless of the font applied.
So basically I am looking for equation or procedure to set font-size and line-height so that there is zero spacing on top and bottom. What do you recommend that I do? A library that contains a matching line-height for many fonts would also do the trick.
You can try like this. may be this code helps you:
var fontSize = $('.lineHeightCss').css('font-size').split('px')[0];
var t = fontSize/100*1.42857;
$('.lineHeightCss').css('line-height',t>0?.7:t);
span {
display: inline-block;
font-size: 100px;
background-color: green;
line-height:50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="lineHeightCss">
BIG TEXT
</span>
visit here:https://medium.com/#zkareemz/golden-ratio-62b3b6d4282a
Update: Added an additional image and added red arrows to more clearly point to my issue.
I'm trying to style a tooltip with text properly. the issue I have, is I used a max-width value to force the text to wrap beyond a certain width.
Then I found out about min-content, but it shrinks to the largest word in the text. So for long bits of text, this creates a long vertical tooltip.
Is there a way to combine both approaches, and use min-content only beyond a certain minimal width, while allowing very short texts to also display right?
Here are the 3 examples:
The first, with a max-width: 120px, as you can see there is unnecessary space on each side of the text, compared to the smaller one (which is correct).
Using width: min-content, everything shrinks to the largest word, making it hard to read.
A final example using width: min-content, with text short enough I wouldn't want it to wrap
Use display: inline-block, min-width, max-width, padding and you will get what you are looking for.
.tooltip {
min-width: 50px;
max-width: 150px;
background-color: rgba(0,0,0, .4);
border-radius: 4px;
padding: 7px 7px;
text-align: center;
display: inline-block;
}
<div class="tooltip">
<span>Little tooltip</span>
</div>
<br><br>
<div class="tooltip">
<span>Very long tooltip but not that long as expected, looks cool.</span>
</div>
Here is my emoji one fiddle.
https://jsfiddle.net/L8a9zazh/
How do i resize the emojis and make it align in center with text and make it look neat.
Css:
img.emojione {
// Override any img styles to ensure Emojis are displayed inline
margin: 0px !important;
display: inline !important;
}
Your content should always be placed within HTML tags that give context to what structural component you're trying to build. In the fiddle example below, we encapsulate the emoji inside a span tag which in turn is wrapped in a p tag for the wording content. By doing this, we can target specific CSS on the emoji content to vertical align within the p tag without having to deal with line-height. The font-size CSS property can control unicode characters and font-face content.
Update 1
I see now that you're trying to convert the chars to an image; your jsfiddle was broken because it wasn't using the jquery framework and you don't have to declare onLoad; instead this should all be set in the js settings window pane.
Update 2: updated jsfiddle
Example
https://jsfiddle.net/L8a9zazh/15/
HTML
<br><br>
<p>Hello world I'm buzz 😠</p>
<br><br>
<p id="wrong-test">Hello world I'm buzz <span class="emoji">😠</span></p>
CSS
img.emojione {
// Override any img styles to ensure Emojis are displayed inline
margin: 0px !important;
display: inline !important;
height: auto;
width: 50px;
}
p#wrong-test {
border-top: 1px solid black;
border-bottom: 1px solid black;
display: inline-block;
}
span.emoji {
font-size: 30px;
vertical-align: middle;
line-height: 2;
}
What it sounds like you want would be expressed by the following: vertically centered at a point half the x height of the font above the baseline. For that you need to address line-height.
Your text and inline images will never align vertically if the image size exceeds the line-height of the font. You need to make the line-height of the font to be at least equal to the image height.
Experiment with different line-heights and see where the images line up. If you get a good fit, you're done. If you still can't find get there, get yourself close and then give the image a pixel or two of margin or padding top or bottom where appropriate.
You can wrap your emoji in a container element (such as a <div> or a <span> and style that element, such as:
<div style="font-size:5rem;width:100%;text-align:center;">😠</div>
I did this for your jsFiddle, here:
jsFiddle Demo
I have a div that users input text in it. But I want to increase it's width according to it's text, until a max of 50% of the screen. My CSS code:
.messages {
max-width:50%;
min-width:150px;
background: #ffeec0;
padding:2px;
margin:3px;
-webkit-border-radius: 2px;
border-radius: 2px;
border:1px solid #ffdd7c;
}
Result:
There's a lot of space after the "555" message, I want this size only if the user inputs some text like:
So, how can I increase the div's width dinamically, depending on the text size?
There are many ways to achieve this, but IMHO the cleanest is the following.
Your problem is that the boxes are "greedy" and will try to expand to the available width.
To prevent this, you can:
Make it "float: left;"
But also "clear: left;" to prevent additional "left floating" elements to use the available space on the right.
The CSS becomes:
.messages {
max-width:50%;
min-width:150px;
background: #ffeec0;
padding:2px;
margin:3px;
border-radius: 2px;
border:1px solid #ffdd7c;
float: left;
clear: left;
}
I provided full code and additional explanation (on mouseover) on the Liveweave here: http://liveweave.com/DFCZFj
Try changing display type of the div to table.
Example Here
.messages {
display: table;
max-width: 50%;
min-width: 150px;
/* other declarations omitted due to brevity */
}
Just add display:inline;. You can also remove the min width property, otherwise if the text is smaller, you will still have that gap.
Block elements (div's default display type) will attempt to take up the maximum horizontal space of the container. Imagine an implicit width:100% whenever you see them. inline-block will create block level elements in which the next element will attempt to render horizontally adjacent (provided there is enough room). This is what you want to use (display: table will work in this solution as well, but it has its own idiosyncrasies. I avoid them.
So your solution requires three parts:
First, you need to specify that the rows will be no larger than 50% of the available area. You will do this with an outer frame:
.frame {
max-width:50%;
}
Next, the messages themselves should each be given space entire row(s) at a time. So we'll use an undecorated div tag around each message.
Finally, you will use display: inline-block for your innermost messages elements. Since they are the only child of their parent tag, you won't have to worry about elements winding around on one another. By using the inline-block, width is respected and this gives us a great place to apply the background color.
.messages {
display: inline-block;
min-width: 150px;
background: #ffeec0;
padding:2px;
margin:3px;
-webkit-border-radius: 2px;
border-radius: 2px;
border:1px solid #ffdd7c;
}
Just as a reference, one would expect your markup will look like the following:
<div class="frame">
<div><div class="messages">2014</div></div>
<div><div class="messages">2014</div></div>
<div><div class="messages">
2014-09-20 17:46:41 minhavidaemquotes:555
</div></div>
<div><div class="messages">
2014-09-20 17:46:41 minhavidaemquotes:555 this is some extra
text
</div></div>
</div>
I think you'll find this gives you the intended effect. By the way, this is a general solution -- but if you choose a min-width that is larger than 50%, you will ensure that two siblings of type inline-block will be too wide for a line. If you do this, then you can dispense with the extra div in the markup.
Basically what the title says. Though the spacing needs to be the same on any resoulution. I tried to do it with css but on different resolutions it moves around a bit. It dosn't matter how you do it (javascript, css, html), as long as it works.
You can view the site that im having issues on here.
If the error is the Fatal Error. Check Code. bit at the top, then do this
Change
#newscontent {
top: 4px;
left: 14%;
position: fixed;
}
to
#newscontent {
top: 4px;
left: 18%; //CHANGE HERE
position: fixed;
}
This will keep the text from overlapping the Latest News bit, at least until the page shrinks smaller than the BB.
Even better would be to make #newscontent a span and place it inside the #news div, so there would be no overlapping or separation no matter what the screen size.
only #topbar should be positioned absolute (if needed), child divs can have float left and margin/padding right
OK, so bottom line is you don't want to solve this using absolute or fixed positioning with left-offset percentages. This approach will fail depending on screen resolution and length of text. A better approach is to float the items, which will allow them to "push" the next element to the right, if need be. Try this:
First, remove all your CSS for your #serverstats, #news, and #newscontent selectors.
Second, on all three of those divs, add a menu-item class:
<div id="serverstats" class="menu-item">...</div>
<div id="news" class="menu-item">...</div>
<div id="newscontent" class="menu-item">...</div>
Third, add the following CSS to your style sheet:
.menu-item {
float: left;
font: bold 120% Arial,Helvetica,sans-serif;
margin-left: 15px;
padding-top: 3px;
text-decoration: none;
}