How word-break property just breaks from space? - html

I have a <td> with word-break: break-all; property. It works, but I have a small problem, If I have a long and integrated (without space) text (more than width), it is very good for me, like this:
html:
<table>
<tr>
<td>
testtttttttttttttttttttttttttttttttttttttttttttttttttttt
</td>
</tr>
</table>
css:
td{
width: 50px;
word-break: break-all;
}
output: (that is OK)
+-----------50px-----------+
|testtttttttttttttttttttttt|
|tttttttttttttttttttttttttt|
|tttt |
+--------------------------+
But when I have a long and Not integrated (with space) text, it won't work correctly. like this:
<td>Hello world.., this is a test text. this is a test text.</td>
output:
+-----------50px-----------+
|Hello world.., this is a t|
|est text. this is a test t|
|ext. |
+--------------------------+
How can I fix it? I want something like this:
+-----------50px-----------+
|Hello world.., this is a |
|test text. this is a test |
|text. |
+--------------------------+

You don't need word-break: break-all;. What you only need is word-wrap: break-word (or overflow-wrap: break-word)
Example:
#container {
width: 70%;
border: 1px solid;
margin: 0 auto;
}
table {
width: 100%;
table-layout: fixed;
}
td { word-wrap: break-word }
<div id="container">
<table>
<tr>
<td class="cc">contentttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt</td>
<td class="cc">Hello world.., this is a test text. this is a test text.</td>
</tr>
</table>
</div>
Fiddle: http://fiddle.jshell.net/m73mcm74/3/
Also, please refer to this answer to your earlier question: https://stackoverflow.com/a/31936379/1355315

Related

Unable to auto set width of HTML columns based on the content

I have HTML table. I want to auto set the width of table columns , which changes as per the content. For example in below image you can see ID column content has to come in a single line, but even though width is set to auto , its not working here. Am i missing out on something?
Please find my code below:
CSS:
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
width: auto !important;
overflow: hidden;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
html table:
<table>
<thead>
<tr>
<th></th>
<th>ID</th>
<th>Company Name</th>
<th>Address</th>
</tr>
</thead>
<tbody>
#foreach (Models.UTP utpResult in lstUtpResult.utp)
{
<tr>
<td>
<input value="1" id="type_radio_1" name="type_radio" type="radio" />
</td>
<td>#utpResult.id</td>
<td>#utpResult.companyName</td>
<td>#utpResult.address.street.Line1,
#utpResult.address.city,
#utpResult.address.zip.code,
#utpResult.address.country.name
</td>
</tr>
}
</tbody>
</table>
ID Text should fit into to a single line but its not.
Try using white-space: nowrap; for the id field
p.a {
white-space: nowrap;
}
<h2>white-space: nowrap:</h2>
<p class="a">
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
</p>
You can try using whitespace: nowrap for your td, th style, if it just overflows out of it's container even with auto sizing you can try width: fit-content.

Table cell same width as child input (type text)

How to force a table cell containing an input of type text, to stretch horizontally and be the same width as its child input? (Where the child input is subject to change width, not fixed).
Code below:
.wrap {
width: 100%;
overflow-x: scroll;
}
table {
min-width: 100%;
width: auto;
}
td {
border: 1px solid #000;
padding: 10px;
white-space: nowrap;
}
input {
width: 100%;
}
<div class="wrap">
<table>
<tr>
<td>
<input type="text" value="testing 123 testing 456 testing 789">
</td>
<td>
<span>Column 2 testing 1234567890 testing 1234567890 testing 1234567890</span>
</td>
<td>
<span>Column 3 testing 1234567890 testing 1234567890 testing 1234567890</span>
</td>
</tr>
</table>
</div>
Check it here:
As you can see, cells containing spans grow but the one containing the input doesn't.
http://jsfiddle.net/atL9f32q
Given all the magic of style sheets there situations where you still need to nail things using javascript:
http://jsfiddle.net/u6szxkp1/
Not elegant like css, but working.
HTML markup:
<div class="wrap">
<table>
<tr>
<td>
<input id="flex-input" type="text" value="testing 123 testing 456 testing 789">
</td>
<td>
<span>Column 2 testing 1234567890 testing 1234567890 testing 1234567890</span>
</td>
<td>
<span>Column 3 testing 1234567890 testing 1234567890 testing 1234567890</span>
</td>
</tr>
</table>
</div>
CSS styles:
.wrap {
width: 100%;
overflow-x: scroll;
}
table {
width: auto;
}
td {
border: 1px solid #000;
padding: 10px;
white-space: nowrap;
}
JAVASCRIPT logic:
function resizable (el, factor) {
var int = Number(factor) || 7.7;
function resize() {el.style.width = ((el.value.length+1) * int) + 'px'}
var e = 'keyup,keypress,focus,blur,change'.split(',');
for (var i in e) el.addEventListener(e[i],resize,false);
resize();
}
resizable(document.getElementById('flex-input'),5.6);
If you set width: 100% for input, the browser will detect it as min-width: auto (by default) and reduce the input width as the table content expands.
You can use min-width: 100% style for input to force a table cell containing an input of type text.
* { box-sizing: border-box }
.wrap {
width: 100%;
overflow-x: scroll;
}
table {
min-width: 100%;
width: auto;
}
td {
border: 1px solid #000;
padding: 10px;
white-space: nowrap;
}
input {
min-width: 100%;
}
<div class="wrap">
<table>
<tr>
<td>
<input type="text" value="testing 123 testing 456 testing 789">
</td>
<td>
<span>Column 2 testing 1234567890 testing 1234567890 testing 1234567890</span>
</td>
<td>
<span>Column 3 testing 1234567890 testing 1234567890 testing 1234567890</span>
</td>
</tr>
</table>
</div>
Remove the input { width: 100% }
.wrap {
width: 100%;
overflow-x: scroll;
}
table {
min-width: 100%;
width: auto;
}
td {
border: 1px solid #000;
padding: 10px;
white-space: nowrap;
}
<div class="wrap">
<table>
<tr>
<td>
<input type="text" value="testing 123 testing 456 testing 789">
</td>
<td>
<span>Column 2 testing 1234567890 testing 1234567890 testing 1234567890</span>
</td>
<td>
<span>Column 3 testing 1234567890 testing 1234567890 testing 1234567890</span>
</td>
</tr>
</table>
</div>

aligning the table row in HTML

My problem is that I can't find a way to fix the table row height,
if the username exceeded it overlaps to the other column.
check the last two row
and it's also scrollable at side and the username is still in their position.
code for single row:
<tr>
<td class="headcol">
<div class="innerHead">
<div class="user-id" style="display:none;">18993</div>
<input type="checkbox" class="checkboxes" name="user_select[]">
TestingalksdjaskldjsalkdjalskdjaksduqwoieuoqweuowqeiTesting#gmail.com </div>
</td>
<td class="forcedWidthUserCode">Tested091237871</td>
<td class="textAlignCenter">Field staff</td>
<td class="forcedWidth">Testing</td>
<td class="forcedWidth">Tested</td>
<td> N/A </td>
<td class="textAlignCenter">Active</td>
<td> N/A </td>
<td class="forcedWidth"> N/A </td>
<td> N/A </td>
<!--<td>N/A</td>-->
</tr>
CSS:
.headcol {
position: absolute;
width: 18em;
border-right: 2px solid #fff;
background-color: #fff;
z-index: 0;
}
table tr td {
/* background: #fff; */
padding: 6px;
text-align: left;
vertical-align: middle;
border-bottom: 1px solid #ddd;
}
how can I align and wrap the text base on the width of the username column?
It happens because there is a white space between those nodes (the checkbox and the text node). The line breaks at white space.
There are two way to handle this.
As mentioned by #Supraja Ganji: Use word-break.
table tr td {
word-break: break-all;
}
or prevent the whole line from breaking, and hide anything that overflows:
table tr td {
white-space: nowrap;
overflow: hidden;
}
Your username is too long and doesnot contain any space, so it is not wrapping.
for td give word-break: break-all
table tr td {
/* background: #fff; */
padding: 6px;
text-align: left;
vertical-align: middle;
border-bottom: 1px solid #ddd;
word-break: break-all;
}
Using a div inside td is a very bad idea.
Using a div instide a td is not worse than any other way of using tables for layout. (Some people never use tables for layout though, and I happen to be one of them.)
If you use a div in a td you will however get in a situation where it might be hard to predict how the elements will be sized. The default for a div is to determine its width from its parent, and the default for a table cell is to determine its size depending on the size of its content.
The rules for how a div should be sized is well defined in the standards, but the rules for how a td should be sized is not as well defined, so different browsers use slightly different algorithms.
Let me know if you require any further help
.headcol {
position: relative;
width: 18em;
border-right: 2px solid #fff;
background-color: #fff;
z-index: 0;
}
.innerHead {
word-break: break-all;
overflow: hidden;
}
<table>
<tbody><tr>
<td class="headcol">
<div class="innerHead">
<div class="user-id" style="display:none;">18993</div>
<input type="checkbox" class="checkboxes" name="user_select[]">
TestingalksdjaskldjsalkdjalskdjaksduqwoieuoqweuowqeiTesting#gmail.com </div>
</td>
<td class="forcedWidthUserCode">Tested091237871</td>
<td class="textAlignCenter">Field staff</td>
<td class="forcedWidth">Testing</td>
<td class="forcedWidth">Tested</td>
<td> N/A </td>
<td class="textAlignCenter">Active</td>
<td> N/A </td>
<td class="forcedWidth"> N/A </td>
<td> N/A </td>
<!--<td>N/A</td>-->
</tr>
</tbody></table>

How to implement a comment structure like stackoverflow

Here is the scenario: I want exactly something like stackoverflow's comments. A table contained 3 <td>. one for the number of votes, one for shapes (vote up and flag), one for text of comment. So each <td> has its own property:
VN (Votes Number): If the number not exist, then width = 0;, also the with of this cell should be dynamic. (for every-digit number)
S (Shapes): This cell has fixed width, it should be noted that the visibility of its shapes is hidden in first and they will be show on hover of <tr>, then the width of this cell always should be assigned.
CT (Comment Text): The width of this cell should be all the remaining width. It should be word-wrap: break-word;.
The structure:
+--+-+---------------------------------------------------+
|VN|S|CT |
+--+-+---------------------------------------------------+
example1:
+-+-+---------------------------------------------------+
|4|^|this comment is a test...! |
+-+-+---------------------------------------------------+
example2:
+-+------------------------------------------------------+
|^|this comment has not any voteup ...! |
+-+------------------------------------------------------+
example3:
+---+-+---------------------------------------------------+
|123|^|the width of number of vote up cell should be |
| | |changeable and it should be noted that this cell is|
| | |break-word. |
+---+-+---------------------------------------------------+
Here is my try: But it does not work correctly.
HTML:
<div class="container">
<table>
<tr>
<td class="VN">4</td>
<td class="S">^</td>
<td class="CT">this is a sample.</td>
</tr>
</table>
</div>
CSS:
.container{
width: 60%;
}
table{
table-layout: fixed;
width: 100%;
}
td{
word-wrap: break-word;
vertical-align: top;
}
.VN{
width: auto;
}
.S{
width: 10px;
}
.CT{
width: 98%;
}
As I said, I don't know why my code does not work correctly (It should be noted my container is responsive), How can I fix it ?
Here is a fiddle of what I did.
Your code is correct but when user try to type "sssssssssssssssssssssssssssss" this will count as one word, so you need to add Hyphenation to your CSS code for recognition of long text since you have word-break: break-word;. https://jsfiddle.net/q7o6m2ka/2/
.CT {
-webkit-hyphens: auto;
-moz-hyphens: auto;
hyphens: auto;
padding: 5px;
width: 98%;
}
here is a demo you can do it jsfiddle
<div class="container">
<table>
<tr>
<td>
<table>
<tbody>
<tr>
<td><span>1</span></td>
<td><a>^</a>
</td>
</tr>
</tbody>
</table>
</td>
<td>
<div>
<span>text of the comment</span>
</div>
</td>
</tr>
</table>
</div>
With only this CSS your table works:
http://jsfiddle.net/1485tLf2/1
.container{
width: 600px;
}
.S, .VN{
width: 2%;
}
You don't need the other things. Tables fits automatically with content if you don't specifiy width. To avoid movements of elements and all comments will be symetric, only specify the small columns width. If the votes column needs more space, the table shrinks automatically to fix it without problems
EDIT
In order to complete the code with your comments, here you are new css and fiddle:
.container{
width: 60%;
}
td{
vertical-align: top;
border: 1px solid;
}
td.CT {
word-break: break-all;
}
.S, .VN{
width: 2%;
}
td:empty {
visibility:hidden;
}
See it working: https://jsfiddle.net/rubhxdm1/11/

The elements of my page are not sized/aligned as I want them to be

The fiddle for my code is here:
http://jsfiddle.net/wasingej/k2GPw/
I think the problem has something to do with how I specify right/left aligned divs but I'm not sure:
div.right
{
float:right;
margin-right: 2%;
border-style:solid;
}
As you can see, the fiddle produces a jumbled mess of garbage. My goal was to have the page look similar to this:
https://i.imgur.com/DVyk7s6.png
I'm fairly new to css so I'm guessing that my problem is caused by something fairly obvious. Any ideas?
Okay here's a very simple example of doing this with a table. This data appears to be tabular in nature, so while there are wonks who insist a table is NEVER okay, using a table for tabular data is appropriate.
HTML:
<table>
<thead>
<tr>
<th colspan=2>List 1 Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>Owner 1 Name:</td>
<td>Owner 1 Status</td>
</tr>
<tr>
<td>Owner 2 Name:</td>
<td>Owner 2 Status</td>
</tr>
<tr>
<td>Owner 3 Name:</td>
<td>Owner 3 Status</td>
</tr>
</tbody>
</table>
CSS:
table {
border: 1px solid;
width: 50%;
padding: 2%;
border-radius: 6px;
}
td {
border: 1px solid;
padding: 1% 2%;
}
td:first-child {
text-align: right;
}
thead th {
text-align: center;
}
The fiddle: http://jsfiddle.net/BUp82/
Your issues are stemming from two things... proper handling of floats and overflows
This forked fiddle should show you your expected results: JSFIDDLE
The main bit of css that you need to add is this:
div.outer
{
//... your existing css ...
overflow: hidden;
}
I also added a 100% wide DIV around the title areas like this:
div.title { width: 100%; overflow: hidden; text-align: center; }
*my fiddle has min-height and min-width set for demonstration purposes