Putting an element directly after text that may/may not be truncated - html

So I want something like this:
Hello [element]
but also this case:
Hello blahblahblah... [element]
floating the element right does this, which I do not want:
Hello [element]
Hello blahblahblah... [element]
EDIT:
the html I tried was this:
<h3 id="container">
<span>[element]</span>
<a>dynamic text</a>
</h3>
css:
span{
float: right;
}
#container {
width: 100px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
Another problem is [element] could be of variable length. I want to truncate the a tag so that it fits all of [element] and put it all on one line. I know that might've just made the question much harder...
EDIT 2:
Realized it wasn't clear that I want the container h3 to be of fixed max width. So basically if the a tag and span's combined width is greater than that max width, the span will put the truncation location to the left. Like so:
the string is: Hello World
So it could look like this in that case:
Hello Worl...[small el]
Hell...[bigger span el]
Ideas?

Updated Idea
This is a refined version of my original idea below, but attempts to create your fixed width of container and general look that you mentioned in the chat using an extra wrapping div. The only way I see that this could work purely by css is for you to know before hand what your maximum width of your "badge" area could be, and then set your max-width of the text area to be the width of the container minus that potential maximum width of the badge area. Here is the revised code below and fiddle example, where I am assuming a 200px container, with a maximum of 100px badge area width. You will end up with some of the text being cut off "prematurely" (as in, it would appear that it could go wider, but we are limiting it to a certain space; see the third line in the fiddle).
HTML
<h3 class="container">
<div>
<span>[elem]</span>
<a>dynamic text</a>
</div>
</h3>
CSS
.container {
width: 200px; /* this defines your right edge */
background-color: cyan;
overflow: hidden;
}
.container > div {
float: left;
clear: left;
position: relative;
padding-right: .1em;
white-space: nowrap;
}
.container span {
position: absolute;
left: 100%;
white-space: nowrap;
}
.container a {
display: block;
/* the following assumes the span "badge" area is 100px max,
so it is 200px - 100px = 100px in width */
max-width: 100px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
Original Idea
Perhaps this would work.
.container {
float: left; /* this collapses the container around the 'a' tag */
position: relative; /* this is going to position the 'span' */
padding-right: .1em; /* gap to element 'span' */
white-space: nowrap;
}
.container span {
position: absolute;
left: 100%; /* push behind 'a' tag */
white-space: nowrap; /* keep this from wrapping */
}
.container a {
display: block;
max-width: 100px; /* set your max width for the 'a' tag only */
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
See a fiddle example.

Related

CSS: two divs in one line, dynamic(left) and fixed(right) width. make overflow text on dynamic div

I have two divs, they should be displayed on same line.
Div one is dynamic width, user typed text.
Div two is date, which is placed on right side.
When there's not enough width (mobile phone) I want the user typed text that doesn't fit the screen width to be overflowed.
I tried the following but max-width is not working here, as it only overflows text till some point:
.parent {
display: table;
width: 100%;
}
.dynamic {
display: inline-block;
max-width: 80%;
}
.fixed {
display: table-cell;
width: 75px;
}
.cutting-text {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
<div class="parent">
<div class="dynamic cutting-text">
could be very long text. overflows till max-width reached, so doesn't work for small screens
</div>
<div class="fixed">
30.11.16
</div>
</div>
How to achieve this for arbitrary text?
JSFiddle as well
Use CSS Flexbox. Its quite easy to implement it in flexbox. Make your .parent a flex container and apply flex properties to .dynamic { flex: 1; }. Everything will get in place automatically.
Have a look at the snippet below:
.parent {
display: flex;
width: 100%;
}
.dynamic {
flex: 1;
}
.cutting-text {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
<div class="parent">
<div class="dynamic cutting-text">
could be very long text. overflows till max-width reached, so doesn't work for small screens
</div>
<div class="fixed">
30.11.16
</div>
</div>
Hope this helps!
Take a look at the Updated Fiddle
Changes:
.fixed {
width: 70px;
right: 0;
position: absolute;
}
.cutting-text {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
width: 100%;
}
.dynamic {
max-width: 80%;
}
.parent {
display: flex;
width: 100%;
}

How to make inline-block elements to grow vertically

I have two inline-block elements (first image). The first one has fixed width. The second one doesn't have fixed width because the container may grow horizontally so it should fill it.
When second element text is large (second image) then it wraps down.
But what I want is the element to grow vertically (third image).
I need also text to preserve line breaks.
You can apply max-width: calc( 100% - LABEL_WIDTH ) to your .element class. Replace LABEL_WIDTH with the width of the label. This way you can define a width in em for the label instead of using two percentual values.
See this JSFiddle for a working example: http://jsfiddle.net/QL78X/2/
See this link for a table of browsers supporting calc(): http://caniuse.com/calc
li {
display: inline-block;
vertical-align: top;
list-style-type: none;
}
.label {
width: 7em;
}
.element {
width: calc( 100% - 7em );
white-space: pre-line;
}
Here's a fiddle: http://jsfiddle.net/Pv8yH/1/
Basically, I've set a width on the label, then set a max-width on the element. I've set the white-space to 'nowrap' so that the second LI doesn't wrap down. Then I have to make sure that white-space is reset back to 'normal' within the LI itself. The max-width is just for show, really, the magic is the white-space property (at least in terms of your question).
ul { white-space: nowrap; }
ul li {
display: inline-block;
white-space: normal;
}
li.label { width: 30%; vertical-align: top; background: red; }
li.element { max-width: 70%; background: green; }
NB. If you are setting a width in ems for the first element, it may be tricky to get it to all fit. It will flow like you want, but you will definitely have to tweak it to make it look nice.
You can use
float:left;
Check this example here:
http://jsfiddle.net/3M5MF/
I suggest you use max-width on the right element. I did an example here: http://codepen.io/mattdrose/pen/qCLzG?editors=110
.field__item {
display: inline-block;
vertical-align: text-top;
}
.field__item--1 {
width: 30%;
}
.field__item--2 {
max-width: 70%;
}
I use percentages, but you can replace these with your preferred method.
However, I think you should use floats so you don't have to deal with the html white space when calculating your widths.
I used relative position of container with fixed height http://jsfiddle.net/6qMvy/
.container{
width: 400px;
height: 600px;
position: relative;
}
.left{
width: 100px;
position: absolute;
}
.right{
position: absolute;
left: 120px;
}

CSS fill remaining horizontal space and show an ellipsis

I'm trying to create a table-like-display containing 2 columns using div elements. One column is for an image URL while the other is for the file size. The file size column has a fixed width of 150px and the URL column should fill the remaining space. If there is insufficient space for the URL it should cut off and display an ellipsis. However what happens is the text just continues to the next line. I've also tried setting white-space: nowrap; but that causes the whole div to break onto a new line.
My HTML code look like this.
<div id="container>
<div class="row">
<div class="imageURL">http://mywebsite.com/image1.jpg</div>
<div class="fileSize">586 KB</div>
</div>
<div class="row">
<div class="imageURL">http://mywebsite.com/image2.jpg</div>
<div class="fileSize">785 KB</div>
</div>
<div class="row">
<div class="imageURL">http://mywebsite.com/image3.jpg</div>
<div class="fileSize">258 KB</div>
</div>
</div>
And here is the CSS.
#container {
width: 100%;
}
.row {
padding: 5px;
}
.imageURL {
display: inline;
text-overflow: ellipsis;
overflow: hidden;
word-break: break-all;
}
.fileSize {
display: inline-block;
width: 150px;
float: right;
}
I think you are looking for something more like this in the following JSFIDDLE. I have rearranged your CSS to look like the following:
#container {
width: 100%;
}
.imageURL {
display: inline-block;
text-overflow: ellipsis;
overflow: hidden;
width:auto;
min-width:100px;
max-width:65%;
white-space:nowrap;
}
.fileSize {
display: inline-block;
margin-left: 100px;
}
.row {
overflow: hidden;
white-space:nowrap;
}
Mainly, I have added the white-space nowrap property along with keeping both divs on the same line, I took out the float, which had made it a block level element, and just placed a margin to be a specific amount from it. The key comes from the width in the .imageURL class, where I added an auto width, a min width, and a max width, which allows the ellipsis to take place at certain points.
Change .imageURL to the following:
.imageURL {
white-space: nowrap;
text-overflow: ellipsis;
width: 400px;
display: block;
overflow: hidden
}
Hope This Helps!;)
JSFiddle Example

How to auto-shrink text with CSS/HTML

I am trying to display 2 values on the same row and give the one on the right priority to grow (it is a mobile app and needs to detect the width of the screen and "squash" the left cell to be smaller.
Here is my attempt: http://jsfiddle.net/rodneyjoyce/TxBhD/
HTML
<div id="screen">
<div id="leftDesc">This is a Long Description</div>
<div id="rightDesc">1000</div>
</div>
CSS
#screen
{
width: 200px;
}
#leftDesc
{
display: inline-block;
overflow: hidden;
float: left;
height: 20px;
max-width:160px;
color: blue;
}
#rightDesc
{
float: right;
display: inline-block;
text-align: right;
color: red;
}
What should happen: Increase "1000" to "1000 000". Blue text should chop off the end of the word "Description" and the red and blue text should stay on the same line.
Disclaimer: I am not very good at CSS - in XAML I use the * value on width so that a cell auto-grows and shrinks the others.
I do not want to use Javascript or JQuery.
I'm not sure if you can dynamically change the size of your floated elements with CSS based on the content, but part of the problem can be solved with:
Adding to #leftDesc:
text-overflow:ellipsis;
white-space:nowrap;
The white-space property keeps the text on one line; text-overflow should be pretty self-explanatory.
JSFiddle
Use the flexible box layout:
#screen
{
width: 200px;
display: -webkit-flex;
display: -moz-flex;
display: flex;
}
#leftDesc
{
overflow: hidden;
height: 20px;
color: blue;
white-space:nowrap;
}
#rightDesc
{
text-align: right;
color: red;
}
I've removed your floats and your inline-blocks, and added display: flex to get the boxes to behave.
I've also added white-space:nowrap; to make sure the description gets cut off, like you've asked.
I've also removed max-width:160px;, because it didn't appear to have any effect in this scenario.
Keep in mind that this will not work in IE.

Aligning two DIVs horizontally where one DIV is a constant width

I want to create two DIVs, a container DIV (which contains arbitrary content) and an arrow DIV which allows the user to scroll the content horizontally.
Ignoring the Javascript aspect, the basic layout and CSS could be something like:
<!DOCTYPE html>
<html>
<head>
<style>
.outer-wrapper {
min-width:275px;
overflow: hidden;
border: 1px solid #000000;
height: 40px;
}
.container {
width: 90%;
min-width:100px;
margin-left: 0.5em;
margin-right: 0.5em;
height: 40px;
overflow: hidden;
white-space: nowrap;
float: left;
}
.inner-content {
margin-top: 10px;
white-space: no-wrap;
position: relative;
display: inline-block;
white-space: nowrap;
}
.inner-element {
display: inline-block;
}
.arrow {
margin-top: 12px;
min-width: 30px;
font-size: 10px;
text-align: right;
margin-right: 2px;
}
</style>
</head>
<body>
<div class = "outer-wrapper">
<div id = "container" class = "container">
<div class = "inner-content" id = "inner-content">
Options Options Options Options Options Options Options Options Options
</div>
</div>
<div id = "arrow" class = "arrow">
▶
</div>
</div>
</body>
</html>
Here's a jsfiddle link showing the rendering: http://jsfiddle.net/RSTE9/1/
The problem I have is that, ideally, I'd like the DIV containing the arrow to be as small as possible, so that most the width of the screen is comprised of the container DIV.
To achieve this, I thought I'd set the container DIV to a width of like 98%, and the arrow DIV to a width of like 2%. Unfortunately, this causes the arrow DIV to wrap to the next line on smaller screen sizes.
The essential problem is that I want the arrow DIV to always take up a very small portion of the screen, but I can't find a way to do this using percentages. If the screen width is large, the arrow DIV always takes up too much space. But if the screen width is very small (say on a mobile device), the arrow DIV might be pushed to the next line. I played around with different percentage values, but there's seemingly no way to get an ideal value. I settled at a width of 90% - this looks good on small screens, but on a large screen it means the arrow DIV is taking up 10% of the screen!
I was thinking of using CSS3 media queries to adjust the percentages dynamically, but I am wondering if there is some easier solution that I'm just not thinking of.
I would suggest that using css calc would be the answer:
CSS Calc on MDN
give the arrow div a fixed size and the container a calc(100%-30px):
.container {
width: calc(100%-30px);
min-width:100px;
margin-left: 0.5em;
margin-right: 0.5em;
height: 40px;
overflow: hidden;
white-space: nowrap;
float: left;
}
Here is an example on jsFiddle:
http://jsfiddle.net/RSTE9/5/
Notice I removed a few of the options options so you can see the effect better.
You do have a minimum width on the main container, which prevents more collapsing.
Why not set width of container as "*"?
.container {
width: *;
min-width:100px;
margin-left: 0.5em;
margin-right: 0.5em;
height: 40px;
overflow: hidden;
white-space: nowrap;
float: left;
}
jsFiddle: http://jsfiddle.net/RSTE9/6/
seems like you messed a bit with float , display and white space.
display and white space is a good clue, width a little less.
the idea is:
set the block container width no width nor overflow, but margin and white-space,
for inner content, reset white-space to normal , use display instead float.
Set min-width to text-content (100% - margin given to container)
Finally , vertical-align on both inline boxe containers text + arrow.
.outer-wrapper {
min-width:275px;
white-space: nowrap;
margin:0 1%;
}
.container {
min-width:98%;
margin-left: 0.5em;
margin-right: 0.5em;
min-height: 40px;
vertical-align:middle;
border: 1px solid #000000;
display:inline-block;
white-space:normal;
}
.arrow {
font-size: 10px;
width:1em;
text-align: right;
display:inline-block;
vertical-align: middle;
}
http://jsfiddle.net/GCyrillus/2e3du/1/