wrap a single line of text in a dynamic div - html

I have this problem with css: a dynamic div contains a single line of text that I need to be wrapped every time the div resizes its width to a smaller size.
But my problem is that the text is inside the table. It is not a pure text, it actually serves as a directory of the contacts somehow like a paging.
Please refer to the images that I have attached for a better view of my problem. I also attached part of the code that I have below. See the attached image for a better understanding of the problem.
I'm not well versed in css, so I'm hoping you can suggest a better layout for this.
Hope you can help me. Thank you! :)
<div id="divSearch" style="width:350px">
<p style="word-wrap:break-word;white-space:pre-wrap;">
<table id="tblGlossary">
<tr>
<td class="glossary"><a href="#" >#</a></td>
<td align="right"> </td>
<td class="glossary">A</td>
<td align="right"> </td>
<td class="glossary">B</td>
<td align="right"> </td>
<td class="glossary">C</td>
<td align="right"> </td>
<td class="glossary">D</td>
<td align="right"> </td>
<td class="glossary">E</td>
<td align="right"> </td>
<td class="glossary">F</td>
<td align="right"> </td>
<td class="glossary">G</td>
<td align="right"> </td>
<td class="glossary">H</td>
<td align="right"> </td>
<td class="glossary">I</td>
<td align="right"> </td>
<td class="glossary">J</td>
<td align="right"> </td>
<td class="glossary">K</td>
<td align="right"> </td>
<td class="glossary">L</td>
<td align="right"> </td>
<td class="glossary">M</td>
<td align="right"> </td>
<td class="glossary">N</td>
<td align="right"> </td>
<td class="glossary">O</td>
<td align="right"> </td>
<td class="glossary">P</td>
<td align="right"> </td>
<td class="glossary">Q</td>
<td align="right"> </td>
<td class="glossary">R</td>
<td align="right"> </td>
<td class="glossary">S</td>
<td align="right"> </td>
<td class="glossary">T</td>
<td align="right"> </td>
<td class="glossary">U</td>
<td align="right"> </td>
<td class="glossary">V</td>
<td align="right"> </td>
<td class="glossary">W</td>
<td align="right"> </td>
<td class="glossary">X</td>
<td align="right"> </td>
<td class="glossary">Y</td>
<td align="right"> </td>
<td class="glossary">Z</td>
</tr>
</table>
</p>
</div>

I removed the <table> and replaced it with a <div> tag:
Live Demo
HTML:
<div id="divSearch" style="width:350px">
<p style="word-wrap:break-word;white-space:pre-wrap;">
<div id="tblGlossary">
#
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
</div>
</p>
</div>
CSS:
#tblGlossary a {
padding: 0 2px
}

Replace your entire table with a div without a fixed width:
<div id="divGlossary">
<a href="#" >#</a>
A
B
C
...
Since you already have the table inside a div, you can use that one.
The word-wrap property does not apply to table cells in the way that you want it.

You can't get a table to wrap. Well, I guess you could try to display: inline on everything...?

If you don't mind using a scrollbar, you can apply the following style :
#longlines {
clip : auto;
overflow : scroll;
}
that will fix your problem i hope let me know
<div width="10%" style="position: absolute; left: 2%; word-wrap:break-word;"> bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb </div> <div width="86%" style="position: absolute; left: 14%;word-wrap:break-word;"> bbbb </div>
Or
use <Span> </span> around your text

Related

How to set width of elements by relative ratio?

For example, I want to set the ratio of width of red:green:blue be 1:2:1 relative to parent, I tried using em, which seems got my desired result:
<table style="width:100%;height:50px;">
<tr>
<td style="height:100%;background-color:red;width:1em;">
</td>
<td style="height:100%;background-color:green;width:2em;">
</td>
<td style="height:100%;background-color:blue;width:1em;">
</td>
</tr>
</table>
but the element doesn't disappear when it has 0em:
<table style="width:100%;height:50px;">
<tr>
<td style="height:100%;background-color:red;width:1em;">
</td>
<td style="height:100%;background-color:green;width:0em;">
</td>
<td style="height:100%;background-color:blue;width:1em;">
</td>
</tr>
</table>
And according to the description of em, it seems unlikely used to define the relative width/height of element.
Is using em the correct way?If not, what is the correct way to achieve this?
Table element has cellspacing and cellpadding. Set cellpadding="0" like following. It will resolve your issue.
<table style="width:100%;height:50px;" cellpadding="0">
<tr>
<td style="height:100%;background-color:red;width:1em;">
</td>
<td style="height:100%;background-color:green;width:0em;">
</td>
<td style="height:100%;background-color:blue;width:1em;">
</td>
</tr>
</table>
This is called "cell-padding" simply give your table cell td a padding: 0;
<table style="width:100%;height:50px;">
<tr>
<td style="height:100%;background-color:red;width:1em;">
</td>
<td style="height:100%;background-color:green;width:0em;padding: 0;">
</td>
<td style="height:100%;background-color:blue;width:1em;">
</td>
</tr>
</table>
EDIT: After reading your post again, what you're searching for is percentage. This will allow you to use a percentage of the parents width.
A 1:2:1 ratio will be 25% : 50% : 25%
<table style="width:100%;height:50px;">
<tr>
<td style="height:100%;background-color:red;width:25%;">
</td>
<td style="height:100%;background-color:green;width:50%;">
</td>
<td style="height:100%;background-color:blue;width:25%;">
</td>
</tr>
</table>
<table style="width:100%;height:50px;border-collapse: collapse;">
<tr>
<td style="height:100%;background-color:red;width:50%;">
</td>
<td style="height:100%;background-color:green;width:0;">
</td>
<td style="height:100%;background-color:blue;width:50%;">
</td>
</tr>
</table>
Instead of em you can use %. check snippet below. Also add cellpadding="0"
<table style="width:100%;height:50px;" cellpadding="0">
<tr>
<td style="height:100%;background-color:red;width:25%;">
</td>
<td style="height:100%;background-color:green;width:50%;">
</td>
<td style="height:100%;background-color:blue;width:25%;">
</td>
</tr>
</table>
or you can go with flexbox
ul {
list-style: none;
display: flex;
padding: 0px;
}
li {
flex-grow:1;
}
li.double {
flex-grow: 2;
}
<ul>
<li style="background-color:red;">1</li>
<li class="double" style="background-color:green;">2</li>
<li style="background-color:blue;">3</li>
</ul>
I would insist not using tables for layout purposes and instead use divs but for your answer
<table style="width:100%;height:50px;">
<tr>
<td style="height:100%;background-color:red;width:50%;">
</td>
<td style="height:100%;background-color:green;width:0;">
</td>
<td style="height:100%;background-color:blue;width:50%;">
</td>
</tr>
</table>

Filling an automatic-width table cell with a fixed-layout table

This sounds very similar to previous questions, but I've not found something that matches what I'm trying to do here.
My current code (very verbose with everything in line) looks like this:
td { border: 1px solid black }
<table style="table-layout:fixed">
<tr>
<td>
<table style="table-layout:fixed">
<tr>
<td style="background-color:red"></td>
<td style="background-color:limegreen;width:30px"></td>
<td style="background-color:blue"></td>
<tr>
</table>
</td>
<td>
<table style="table-layout:fixed">
<tr>
<td style="background-color:red"></td>
<td style="background-color:limegreen;width:30px"></td>
<td style="background-color:blue"></td>
<tr>
</table>
</td>
<td>
<table style="table-layout:fixed">
<tr>
<td style="background-color:red"></td>
<td style="background-color:limegreen;width:30px"></td>
<td style="background-color:blue"></td>
<tr>
</table>
</td>
<td>
<table style="table-layout:fixed">
<tr>
<td style="background-color:red"></td>
<td style="background-color:limegreen;width:30px"></td>
<td style="background-color:blue"></td>
<tr>
</table>
</td>
</tr>
<tr>
<td style="width:25%;text-align:center">Some text here</td>
<td style="width:25%;text-align:center">More text</td>
<td style="width:25%;text-align:center">Hi</td>
<td style="width:25%;text-align:center">Somewhat longer text</td>
</tr>
</table>
What I'm trying to accomplish should look like this:
That is, the four main columns should all be the same width, which is the width of the largest content of any of the columns.
The green columns should always be 30px, and the red and blue columns should fill the remaining space each side of that middle column, only up to the width available in the auto-sized outer column.
Setting the internal tables to 100% width makes this happen, but of course the outer table then takes up the entire page width.
I'm also aware that using tables for this is probably not a great idea now we can use CSS, but I'd like to get this example working in tables before 'translating' it.
I'm very opposed to using JavaScript to solve this, for the record!
Edit: I also tried putting all 'subcolumns' in one row, and setting the text to colspan three at a time, with the 25% then applied to that. This ended up confusing the engine, and the width ended up about 75% of the page.
<table style="table-layout:fixed" border="1">
<tr style="font-size: 1px;">
<td width="25%" colspan="3"> </td>
<td width="25%" colspan="3"> </td>
<td width="25%" colspan="3"> </td>
<td width="25%" colspan="3"> </td>
</tr>
<tr style="font-size: 6px;">
<td style="background-color:red"> </td>
<td style="background-color:green"><div style="width: 30px;"> </div></td>
<td style="background-color:blue"> </td>
<td style="background-color:red"> </td>
<td style="background-color:green"><div style="width: 30px;"> </div></td>
<td style="background-color:blue"> </td>
<td style="background-color:red"> </td>
<td style="background-color:green"><div style="width: 30px;"> </div></td>
<td style="background-color:blue"> </td>
<td style="background-color:red"> </td>
<td style="background-color:green"><div style="width: 30px;"> </div></td>
<td style="background-color:blue"> </td>
</tr>
<tr>
<td colspan="3" style="text-align:center">Some text here</td>
<td colspan="3" style="text-align:center">More text</td>
<td colspan="3" style="text-align:center">Hi</td>
<td colspan="3" style="text-align:center">Somewhat longer text</td>
</tr>
</table>
this might solve your answer, your previous code html elements are not properly closed look on that also.

Table Cells with different widths in different rows

I am weak in CSS, and I am trying to put a table in my html page, it has two rows and five columns per row(of course it is simplified), and it should look like this (the table is a hand-drawing table, it does not come so precise, I`m sorry for that.):
But mine looks like this:
This is my code:
<table border="1">
<tr>
<td style="width:50px" colspan="2"> </td>
<td style="width:50px" colspan="2"> </td>
<td style="width:50px" colspan="2"> </td>
<td style="width:50px" colspan="2"> </td>
<td style="width:25px"> </td>
</tr>
<tr>
<td style="width:25px"> </td>
<td style="width:50px" colspan="2"> </td>
<td style="width:50px" colspan="2"> </td>
<td style="width:50px" colspan="2"> </td>
<td style="width:50px" colspan="2"> </td>
</tr>
</table>
Code in jsfiddle is here.
NOTE:Any styles could be added, but structure of table could not be changed.
My problem is not the border style of table, but the width of cells, it seems that cells has a erratic width, I hope the right-border of first cell in second row could reach to the middle of bottom-border of first cell in first row, and the right-border of first cell in first row could reach to the middle of top-border of second cell in second row, so is others.
I have tried my best, but it still does not work. How could I do to match the requirement? Any suggestion will be appreciated. Thanks in advance.
You can use a <colgroup> element to achieve this:
<table border="1">
<colgroup>
<col style="width: 25px"/>
<col style="width: 25px"/>
<col style="width: 25px"/>
<col style="width: 25px"/>
<col style="width: 25px"/>
<col style="width: 25px"/>
<col style="width: 25px"/>
<col style="width: 25px"/>
<col style="width: 25px"/>
</colgroup>
<tr>
<td colspan="2"> </td>
<td colspan="2"> </td>
<td colspan="2"> </td>
<td colspan="2"> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td colspan="2"> </td>
<td colspan="2"> </td>
<td colspan="2"> </td>
<td colspan="2"> </td>
</tr>
</table>
It will tell the table that there are 9 columns and each row will span the columns as you originally had.
There are other non-table ways to acheive what you are looking for. Here is one quick example:
<div>
<div class="row">
<div> </div>
<div> </div>
<div> </div>
<div> </div>
<div> </div>
</div>
<div class="row">
<div> </div>
<div> </div>
<div> </div>
<div> </div>
<div> </div>
</div>
</div>
div.row
{
clear:both;
}
div div div
{
width: 50px;
border-width: 1px;
border-style: solid;
border-color: black;
display: inline-block;
float: left;
margin: -1px;
}
div div:nth-child(2n+1) div:first-child,
div div:nth-child(2n) div:last-child
{
width: 25px;
}
Use tables within tables..
<table>
<tr>
<td>
<table border="1">
<tr>
<td style="width:50px"> </td>
<td style="width:50px"> </td>
<td style="width:50px"> </td>
<td style="width:50px"> </td>
<td style="width:25px"> </td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table border="1">
<tr>
<td style="width:25px"> </td>
<td style="width:50px"> </td>
<td style="width:50px"> </td>
<td style="width:50px"> </td>
<td style="width:50px"> </td>
</tr>
</table>
</td>
</tr>
</table>
This way you will never have that problem...
For this to work, you need to have at least one row that defines the width of individual cells (ones that are not using cellspans):
http://jsfiddle.net/cR2qd/7/1
HTML:
<body>
<table border="1">
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="2"> </td>
<td colspan="2"> </td>
<td colspan="2"> </td>
<td colspan="2"> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td colspan="2"> </td>
<td colspan="2"> </td>
<td colspan="2"> </td>
<td colspan="2"> </td>
</tr>
</table>
</body>
CSS:
td {
width: 25px;
}

Simple html table design issue

I'm trying to solve a problem: it's a HTML exercise in which I must write the HTML code for a table with this design:
But I can't seem to set it straight, here's my code:
<table border>
<tr>
<td rowspan="2" colspan="2"> Batatas </td>
<td rowspan="3" colspan="2"> Couves </td>
<td> Alhos </td>
<td> Cebolas </td>
</tr>
<tr>
<td rowspan="2" colspan="2"> Alface </td>
</tr>
<tr>
<td colspan="2"> Nabos </td>
</tr>
</table>
And here's the result:
Shouldn't the rowspan="2" in the first "td" tag make the first cell larger (in height)?
What am I doing wrong?
Try this online tool here: http://html-tables.com/
and you will see how the 3 rows collapse to 2 rows visually if you are using just cell merging.
I think you need to nest tables to achieve that effect.
Actually the problem you are facing is not because of your code it is because of general rules of html table rendering, this arises because of merging of table cells
To resolve this drawback of <table> tag, I'll recommend to use <div> tag as better approach.
Try this....
<table border>
<tr>
<td rowspan="2" colspan="1"> Batatas </td>
<td rowspan="3" colspan="1"> Couves </td>
<td rowspan="1" colspan="1"> Alhos </td>
<td rowspan="1" colspan="1"> Cebolas </td>
</tr>
<tr>
<td rowspan="1" colspan="2"> Alface </td>
</tr>
<tr rowspan="1" colspan="2">
<td> Nabos </td>
</tr>
</table>
Update
As I've noticed, this can't be done using <table> tag, You can use the div approach. These div tag are able to generate your layout.
I just finished working on your problem, and I've just solved your problem using <div> tag, Have a look
<div style=" background-color: powderblue;border:1px solid black; width:410px; height:310px">
<div style="float:left;">
<div style="border:1px solid black; width:100px; float:none; height:200px">Batatas
</div>
<div style="border:1px solid black; width:100px; float:none; height:99px">Nabos
</div>
</div>
<div>
<div style="border:1px solid black;width:100px; float:left; height:301px">Couves
</div>
<div style="border:1px solid black;width:100px; float:left; height:100px">Alhos
</div>
<div style="border:1px solid black;width:100px; float:left; height:100px">Cebolas
</div>
<div style="border:1px solid black;width:202px; float:left; height:199px">Alface
</div>
</div>
</div>
Good question. The closest I got (without getting crazy with nested tables) is simply this:
<table border>
<tr>
<td colspan="2"> Batatas </td>
<td rowspan="3" colspan="2"> Couves </td>
<td> Alhos </td>
<td> Cebolas </td>
</tr>
<tr>
<td colspan="2"></td>
<td rowspan="2" colspan="2"> Alface </td>
</tr>
<tr>
<td colspan="2"> Nabos </td>
</tr>
</table>
If this doesn't work for you (and you don't want to get into complex nested html tables), then I the common belief seems to be to move away from html tables to using CSS. Obviously with CSS/divs you have much more control.

Replace XML Value in HTML

I have a scoreboard on my website that gets scores from an XML file. It's very easy for others to update, except for highlighting the winner.
This is a sample game from the XML file:
<game>
<month>05</month><day>25</day><year>11</year>
<type>Football</type>
<homeName>Wildcats</homeName><homeScore>45</homeScore><homeWinner></homeWinner>
<awayName>Bruins</awayName><awayScore>55</awayScore><awayWinner>y</awayWinner>
</game>
As you can see, I want them to simply check the winner. However, when it loads in HTML, I want the y to be replaced with an arrow image.
It's a Spry scoreboard, so here's the relevant HTML:
<script type="text/javascript">
var dsScoreboard = new Spry.Data.XMLDataSet("scoreboard.xml", "scoreboard/game", {sortOnLoad: "date", sortOrderOnLoad: "descending"});
dsScoreboard.setColumnType("date", "date");
</script>
<div spry:region="dsScoreboard">
<table class="scoreboard" cellspacing="15" cellpadding="0" border="0">
<tr>
<td spry:repeat="dsScoreboard">
<table class="game" cellspacing="0" cellpadding="0" border="0">
<tr>
<td class="date month">{month}</td>
<td class="type" colspan="3">{type}</td>
</tr>
<tr>
<td class="date day">{day}</td>
<td class="winner">{awayWinner}</td>
<td class="name">{awayName}</td>
<td class="score">{awayScore}</td>
</tr>
<tr>
<td class="date year">{year}</td>
<td class="winner">{homeWinner}</td>
<td class="name">{homeName}</td>
<td class="score">{homeScore}</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
So the output HTML for the winner cell should be something like:
<td class="winner"><img src="arrow.png" /></td>
instead of:
<td class="winner">y</td>
Can this be easily done? Thank you.
What you want is a spry:choose. I have never used spry, but it's something vaguely like this:
<td class="winner">
<span spry:choose="spry:choose">
<img src="arrow.png" spry:when="'{homeWinner}' == 'y'" />
<span spry:default="spry:default"></span>
</span>
</td>