Reputation: 24778
Update 2013-01-04
:before
with CSS content instead of HTML is a better way to go.Information
I have a table with some content. Depending on the depth I have a number of minus characters before the text.
Problem / question
I can't figure out how to make the text align right and the minus characters to align left, even when line break accur. The number of minus and the text length can vary.
Example on jsfiddle
Edit it if you like...
HTML if jsfiddle don't work
<table>
<td>
<span class="lines">----</span> <span class="text">My page that is far too long for it to fit on one row</span>
</td>
</table>
<br>
This is how it should work.<br><br>
<table>
<td>
<span class="lines">----</span> <span class="text">My page that is far<br> too long for it to fit<br>
on one row</span>
</td>
</table>
CSS if jsfiddle don't work
table {
background: #eee;
width: 150px;
}
td {
font-family: Arial;
font-size: 14px;
line-height: 18px;
margin: 40px;
}
My thoughts
Upvotes: 6
Views: 7745
Reputation: 719
For inline displays you kind of have to mix things up, using a negative margin and a negative text indent. Here is a working jsfiddle with an example:
#thisList ul li a {
color:#333;
text-indent:-1.5rem;
margin-left:-2rem;
line-height:1;
}
Upvotes: 0
Reputation: 201728
Since you are using a table
element, put the “minus characters” (actually, Ascii hyphens, formally HYPHEN-MINUS characters) in a column of their own. You should also make the markup valid (a td
element can only be a child of tr
).
<table>
<tr>
<td class="lines">----</td>
<td class="text">My page that is far too long for it to fit on one row</td>
</table>
In CSS, add td { vertical-align: top; }
. (Or add the corresponding HTML markup, <tr valign=top>
, for every tr
element.
The columns will be left-aligned by default, and you can tune this as desired. On modern browsers, you won’t even need the class
attributes, as you can use the selectors td:first-child
and td:first-child + td
for example.
Upvotes: 0
Reputation: 68329
As long as your cells aren't going to be bordered, flexbox can work here for you.
td.dashed {
display: -webkit-flex;
display: flex;
}
Demo works with both a span or using content on a before psuedo element to contain the dashed. Does not work on anything older than IE10. Mozilla doesn't appear to like this, but it is supposed to support flexbox.
Upvotes: 0
Reputation: 167192
text-indent
!A text-indent
indents the first line. So give it a negative value!
.first-line {text-indent: -5em; padding-left: 5em;}
Upvotes: 21
Reputation: 5000
Using CSS's content
attribute, you can insert the minus characters into the page AND position/style them without the need to include them in your HTML or to wrap them in any tags.
I amended your example http://jsfiddle.net/cchana/Rjvc9/2/ and it now works by giving the cell some padding and a position attribute like so:
td {
padding: 0 0 0 24px;
position: relative;
}
I then added in the ----
characters to the beginning of the cell using content
and then positioning it correctly:
td:before {
content: '----';
left: 0;
position: absolute;
}
Upvotes: 0
Reputation: 2602
Something like this should do it:
display: block;
padding-left: 37px;
text-indent: -37px;
You will give a negative indent on the first line so this will be placed X pixels to the left. By giving the whole element a padding-left of the same amount of px this will place it all on the right place again.
Also see: http://jsfiddle.net/Rjvc9/3/
Note: display: inline
will not work on this. display:inline-block
will.
Upvotes: 4