27

I have a page with labels which are contained in a div, the labels has a variable with and i want to fill spaces between both with characters, dots or '-'.

For example.

My label text 1 ----------- Some Text Here.

My text 2 ----------------------- Another Text.

If you notice both text are justified (or at least i try), could be a problem of counting characters but character can have different width, any one knows a clean way of doing this programmatically in Asp.Net, css, jquery or anything else?

Update

................

Someone suggested in answers align both labels with css, but i think i will be having the same problem with the characters in the middle, which can be another label of course. Any thoughts?

Update

.................

Answer from Patrick is really close to the solution, but now hyphens are not shown in IE8, maybe there is a miss understand in one of my comments, it should work on IE7, IE8 and Firefox, only these browsers.

Thanks to everyone.

dev-cu
  • 331
  • 1
  • 5
  • 13

6 Answers6

20

Try this: http://jsfiddle.net/FpRp2/4/ (updated, now works in ie7)

The solution @Marcel gave to use a dashed border instead of text hyphens solved the final issue with IE7.

(Note, I've only tested in Safari, Firefox and Chrome so far.)

EDIT: IE8 works. Working on a fix for IE7.

.outer {
  display: inline-block;
  *display: inline;
  zoom: 1;
  position: relative;
  clip: auto;
  overflow: hidden;
}

.container {
  position: relative;
  text-align: right;
  white-space: nowrap;
}

.filler {
  position: absolute;
  left: 0;
  right: 0;
  border-bottom: 1px dashed #333;
  height: 50%;
}

.label {
  background: white;
  float: left;
  margin-right: 20px;
  padding-right: 4px;
  position: relative;
}

.text {
  background: white;
  padding-left: 4px;
  position: relative;
}
<div class='outer'>
  <div class='container'>
    <div class='filler'></div>
    <span class='label'>some label</span>
    <span class='text'>some text</span>
  </div>
  <br>
  <div class='container'>
    <div class='filler'></div>
    <span class='label'>some other label</span>
    <span class='text'>some other text</span>
  </div>
</div>
Adam
  • 5,495
  • 2
  • 7
  • 24
user113716
  • 318,772
  • 63
  • 451
  • 440
  • your are right, it works in Firefox, how to make it work in IE7, which fix?, i only need this two browsers, thanks for your answer. – dev-cu Jun 23 '10 at 00:00
  • @dev-cu: I'm trying to figure out the proper IE7 fix. CSS hacks are not my strength. I'll keep tinkering. :o) – user113716 Jun 23 '10 at 00:03
  • I don't want to nit-pick too much, as your solution looks nice, but I would have used spans for those texts (`.filler`, `.label`, `.text`). – Marcel Korpel Jun 23 '10 at 00:23
  • @Marcel - I would welcome any thoughts. I agree, spans make a little more sense given the content. Any thoughts on the IE7 fix? I'm not making much progress. – user113716 Jun 23 '10 at 00:26
  • The `float:right` on the `.text` is forcing the width of the `.container` all the way to the right. – user113716 Jun 23 '10 at 00:33
  • Hmm, I tried it with `span`, but I'm muddling through those `display` settings. Regarding IE 7: sorry, no IE 7 here at the moment. BTW, isn't it nicer to use a dashed border (moved upwards using relative positioning) as a filler? – Marcel Korpel Jun 23 '10 at 00:34
  • Updated the example. I'm a step closer. Almost works. Just an issue with the height of each row. @Marcel - Your idea about the dashed border may be the solution. – user113716 Jun 23 '10 at 00:47
  • @dev-cu: Works now. Instead of doing `float:right` for the text on the right, I did `text-align` right for the `.container`. Then the suggestion @Marcel gave to use a dashed border instead of hyphens took care of a line height issue. Thanks Marcel. :o) – user113716 Jun 23 '10 at 00:59
  • @patrick: First, thanks for your time, solution is really closer, but is just me or hyphens or border is not shown in IE7 – dev-cu Jun 23 '10 at 13:40
  • @dev-cu: I just tested in IE7 and IE8. Both work well for me. Are you viewing the version in jsFiddle? http://jsfiddle.net/FpRp2/4/ – user113716 Jun 23 '10 at 14:05
  • ok, i tried this, add to .filler left: -100%; top:-8px; width:100%; now hyphens are shown in IE8 but not in Firefox, :-( – dev-cu Jun 23 '10 at 14:13
  • @dev-cu: Are you testing in the jsFiddle, or on your page? The jsFiddle displays properly for me in IE8. – user113716 Jun 23 '10 at 14:48
  • @patrick: i am testing in a local page only with that style and html, and it doesn't work on IE8, anyway i added to .filler this attributes top: -8px; height: 23px; width:100%; and now works in both browser, what do you think? – dev-cu Jun 23 '10 at 15:25
  • @dev-cu: You perhaps didn't have your DOCTYPE set on your test page? ` ` for example. The exact code from the jsFiddle works for me as long as the DOCTYPE is set. – user113716 Jun 23 '10 at 15:40
  • another example how the basic css premise of its fitness to separate style (including layout) from content fails. +1 regardless – n611x007 Jun 27 '12 at 04:12
7

I have implemented this on a table with pure CSS and even without using any span or div.

CSS is:

.dot-table td {
    max-width:200px;
    overflow:hidden;
    white-space:nowrap;
}
.dot-table td:first-child:after {
    content:" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "
}

HTML is

<table class="dot-table">
  <tr>
    <td>
       Coffee
    </td>
    <td>
       45 INR
    </td>
  </tr>
  <tr>
    <td>
       Tea
    </td>
    <td>
       36 INR
    </td>
   </tr>
</table>

A detailed output (from a site I developed) A detailed table with filling dots

View it live here.

ghosh'.
  • 1,567
  • 1
  • 14
  • 19
6

A solution using flexbox, with support for text-overflow: ellipsis to keep the content on 1 line:

http://codepen.io/anon/pen/jPZdgr

.item {
  display: flex;
  justify-content: space-between;
}
.descripcion {
  /*background-color: green;*/
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.descripcion:after {
  content: " ........................................................................................................................................................................................................................................................................................."
}
.precio {
  /*background-color: red;*/
  flex-shrink: 0;
}
<div class="item">
  <div class='descripcion'>Junta la trócola</div>
  <div class='precio'>0´33</div>
</div>
<div class="item">
  <div class='descripcion'>Gamusinos en oferta c/u</div>
  <div class='precio'>6´47</div>
</div>
<div class="item">
  <div class='descripcion'>Saco de rafia y linterna a pedales</div>
  <div class='precio'>12´663584153,351,5,154</div>
</div>
<div class="item">
  <div class='descripcion'>Jaula de bambú con led para gamusinos</div>
  <div class='precio'>21´99</div>
</div>
<div class="item">
  <div class='descripcion'>Otro concepto más repartido entre más de una, o de dosOtro concepto más repartido entre más de una, o de dosOtro concepto más repartido entre más de una, o de dosOtro concepto más repartido entre más de una, o de dosOtro concepto más repartido entre
    más de una, o de dosOtro concepto más repartido entre más de una, o de dos</div>
  <div class='precio'>1.694</div>
</div>
<div class="item">
  <div class='descripcion'>Chismes y achiperres surtidos</div>
  <div class='precio'>0´10</div>
</div>
<div class="item">
  <div class='descripcion'>Yugo, barzón, cavijal y mancera</div>
  <div class='precio'>33´7433333333333333333333</div>
</div>
<div class="item">
  <div class='descripcion'>Coyunda, sobeo, ramales y cordel</div>
  <div class='precio'>125´87</div>
</div>
<div class="item">
  <div class='descripcion'>Media, cuartilla, celemín y 1 envuelza</div>
  <div class='precio'>48´04</div>
</div>
Thomas Stock
  • 10,927
  • 14
  • 62
  • 79
  • 1
    THANK YOU! This should be the accepted answer, since it doesn't rely on a fixed background. Meaning this is the only solution i could use this on, say, a background image. – LukasKroess Jul 29 '20 at 13:40
3

You need to use CSS to adjust the layout of the two pieces of content. Either break up the label into two labels and apply css classes to each, or wrap each bit of text inside your label with a <span> tag and style them that way.

Filling empty space with characters to try to adjust the layout is not the correct approach, and wouldn't be recommended for the same reasons you don't format your text documents by adding space characters everywhere.

womp
  • 115,835
  • 26
  • 236
  • 269
  • Hi womp, thanks for your response, i understand for your answer that i need to separate this in 3 labels, right? but what happen with the label in the middle, i will be having the same problem. I mean, how many dots or '-' to add – dev-cu Jun 22 '10 at 23:42
  • 1
    Don't add any characters, that's the point. Use CSS to control the alignment and placement of your text, not more text. – womp Jun 22 '10 at 23:52
1

A simple solution, that supports line breaks and works in IE 8+, FF & Chrome. IE below 8 is supported, when dots are placed directly in the spacefill-dots div.

CSS is

.spacefill,
.spacefill-dots {
  line-height: 18px;
  font-size: 16px;
}

.spacefill {
  position: relative;
  padding: 0;
  margin: 0;
  border: 0;
}

.spacefill-dots {
  z-index: -1;
  /* Push dots behind text */
  height: 18px;
  /* Same as line-height */
  border: 0;
  margin: 0;
  padding: 0;
  left: 0;
  right: 0;
  bottom: 0;
  position: absolute;
  overflow: hidden;
}

.spacefill-text {
  background: white;
  /* Very important. Choose backgroundcolor*/
  padding-right: 4px;
  /* Optional space before first point*/
}

.spacefill .spacefill-dots::after {
  content: "........................................................................................................................................................................................................................................................................................................................................................................................................................................"
}

HTML is:

<div class="spacefill">
   <div class="spacefill-dots"></div>
   <span class="spacefill-text">Short title</span>
</div>

Here's a simple example for a contents table: https://jsfiddle.net/dua2tp11/

0
  1. Find the first instance of three spaces. I am assuming that this is the minimum number needed to constitute a "break"

  2. Find the first non-space character after that.

  3. Replace all the spaces between the index in #1 (+1) and the index in #2 (-1). That would give you the results above.

Check other answers for more clean ways to do it with CSS. That would be the cleanest way to display the text. Dashes look ghetto. :)

Craig
  • 11,614
  • 13
  • 44
  • 62