CSS width: Use min-content above a certain width - javascript

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>

Related

Setting max size of contenteditable element

I wonder if I could set the max size of contenteditable element, preventing it from typing more characters than the editable "window" width is.
I have something like this:
<h2 contenteditable="true">My text</h2>
The h2 element has it's own size and style:
h2 {
display: inline-block;
overflow: hidden;
width: 300px;
max-width: 300px;
height: 15px;
max-height: 15px;
}
Now, when I reach the max size, I can type more characters and it looks like I jump into next line. When I hit enter button i also jump down into next line/raw.
What I want to reach is to block this editable content "window" when I reach their max width and block the possibility to increase the height by hitting enter button.
I want to know if it is possible to do it using html5/css or only chance is to create some javascript code?
Thanks in advance for you help!
You use following statement
<h2 contenteditable="true">My text</h2>
But you didn't apply settings for h2.So,replace h4 to h2.
h2 {
display: inline-block;
overflow: hidden;
width: 300px;
max-width: 300px;
height: 15px;
max-height: 15px;
}
First of all your post isn't clear at all,
do you need to control the number of insertions in chars on the contenteditable element or do you just need to style the visible portion of the dom??
If you need to simulate the maxlength attribute that an input element normally has, you need to replicate this behaviour with javascript, CSS can't help you...
otherwise, if it's is just a presentation issue, you can do it with css using em unit...
<div contenteditable style="width: 20em"; height: 1em; line-height:1em; overflow:hidden;></div>

How to increase div width according to the text inside it?

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.

How to make Wrapper div contain only non-overflowing divs

I have a div containing divs with content.
The outer div has a dynamic width (e.g. 80%).
The inner divs have a fixed width (e.g. 100px).
The problem is that i want to show only so much inner div's so that no inner div "overflows" / "is cut" as in figure 1.
I also want to "distribute" the "free" space as margin between the inner divs equally distributed, as shown in figure 2.
I hope somebody understands my problem, and knows how to realize this with css and as less javascript as possible :)
P.S.: If it is easy to do, would it be possible to have the first and the last div have a max. margin to the outer borders ?
Started this before you edited your question with more info, but I believe that the one missing piece you're after is the text-align: justify in the 'outer'.
.outer{
background: red;
width: 80%;
overflow: hidden;
height: 48px;
text-align: justify;
}
.inner{
background: blue;
width: 100px;
height: 40px;
margin: 4px;
display: inline-block;
}​
Here's a fiddle.
Not entirely sure what you mean by max-margin, but it sounds like that could be achieved by giving the container a fixed padding on the left and right.

HTML truncate content based on div height

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;
}

Vertical align text middle in jQuery Mobile Button?

I'm attempting to shrink the height of a jQuery Mobile Button for a better fit in a list view, but I cannot get the text to line up properly. Here's my implementation so far:
.listDelBtn .ui-btn-text {
margin: -5px -15px -5px -15px;
}
<a class="listDelBtn" data-role="button" data-theme="b" style="float: right; width: 75px; line-height: 11px; margin-top: 6px; z-index: 12; padding: 0 0px 0 0px;">delete</a>
the styled margins do affect the width of the button to give it a shorter length, however, the top and bottom margin values have no affect, regardless of the values tried. I've also attempted various padding , height, and other values with no luck. Line height was also the only inline style that had any affect on height of the button, but the text within is misaligned. Attempting on versions 1.0b1+ of jQuery Mobile btw.
Here's an image of the resulting button for reference:
You can give height to your button then give same line-height to it.
For example
.listDelBtn{
height:30px;
line-height:30px;
}
Check this example http://jsfiddle.net/EQash/
#Inator I think vertical align middle works only for the Table elements and not for other cases.
In that case in the class you apply to button:
Set: display: table-cell
and vertical-align: middle
and possibly create a outer element say div and set its display to table
like display: table
and vertical-align: middle
Hope it might work
The Inline margin-top style takes precedence over the the styles defined in the CSS class. That's why CSS defined values have no effect. Solution is to add
margin-top: -5px !important;
to your CSS if you want the negative top margin to take effect.

Categories

Resources