Fitting dynamic text into a table - html

I couldn't find any answers on Google and Stack Overflow on how to fit text into a table so that the words go to the next line. The only solutions I found that were almost helpful were "making table cells fixed, but the text still runs off the page".
When the text is short:
http://puu.sh/d4IQ2/7a7c95512c.jpg
When the text is too long:
http://puu.sh/d4ITM/6edb003a7c.jpg
How do I make it so that the spaces in the table are fixed based on the window size, then have the text that is too long increase the height of the table row and go onto the next line.
For example, my table is like this:
+------+-----+-----+--------+
| 12345|12345|12345| 12345 |
|------+-----+-----+--------|
| 12345| 123 | 1 | 123 |
|------+-----+-----+--------|
|123456|12345| 123 |12345678|
|------+-----+-----+--------|
| 12345| 123 |12345| 1234 |
+-----+-----+-----+---------+
when I want it like this:
+-----+-----+-----+-----+
|12345|12345|12345|12345|
|-----+-----+-----+-----|
|12345| 123 | 1 | 123 |
|-----+-----+-----+-----|
|12345|12345| 123 |12345|
| 6 | | | 678 |
|-----+-----+-----+-----|
|12345| 123 |12345| 1234|
+-----+-----+-----+-----+
style.css:
table, th{
border: 1px solid black;
border-collapse: collapse;
}
th {
table-layout:fixed
padding: 15px;
}
The PHP code is too long, and I don't want to list out all the functions, the rows and text in the cells are looped where I get data from SQL database:
<table style = "width = 100%">
<tr>
<th> looped text </th>
</tr>
</table>

Set the td size equally for each td tag
<td width="25%">Name</td>

Give the columns a specified width in the CSS. This should do the trick.

add this in css file
td {
width : 300px !important;
}

Apply table-layout:fixed to the table element (not the th), and add word-wrap:break-word to the th/td's.
http://jsfiddle.net/5rz1fmvk/

Well,i think a solution is to set a max-width style for your table.In this way the table width won't exceed the given max-width,and the only thing that remains for the value is to go beneath.
<table style="max-width: 500px; width: 500px;">
</table>
Also,specify a width.
Of course the style should be in an extern css file,but you get the idea.Hope it helps.
table{
width: 500px;
max-width:500px
}

try to fixed witdh of th and for td put css word-wrap: break-word; sample below. You can change the width of th.
table{
width:100%:
}
th{
width:30%;
}
td{
word-wrap: break-word;
}

Related

How to set dimension (width) in a div box in this situation

I want to set the width (in pixels) of the orange box.
You can see my page on this: live link
<div align="center" class="copyright" style="border: thin solid #F48C13"><p> </p>
<p><strong>Accesso a MpcPanel</strong></p>
Simply define width property in your stylesheet for the class copyright
.copyright {
width: 100px;
}
Ex.: http://jsfiddle.net/wm754s90/3/
Edit:
For Inline style (that's without modify the css file, and including the style directly in the element)
The style attribute of the div.copyright need to be edited.
Just add width property alongside to border.
<div align="center" class="copyright" style="width: 100px; border: thin solid #F48C13;">
<!-- /* div content */ -->
</div>
You can add as many as you want (semicolon separated) but keep in mind the readability of the code (for that are external CSS files, among many other benefits)
I will recommended to you (if you can of course) to create your own css file and include there all the modifications you want. Will be a good starting point to learn css and how it's work.
Edit 2:
Worth to mention that both properties (width & border) might conflict.
Width prop. do not include the border size.
Ex.
.copyright {
width: 100px;
border: thin solid #F48C13;
}
---------------------
| ----------------- |
| | | |
| | div content | |
| | | |
| ----------------- |
---------------------
| |
|<------------->| -----> A) div width = 100px
| |
| | | |
<-> <-> -----> B) div border size = 1px
\ / (the size you enter is for each side)
border size ("thin" in border size is equal to 1px)
| |
| |
|<----------------->| -----> total/final div width = 102px
B + A + B
1 + 100 + 1 = 102px
or use 98px as width... 1 + 98 + 1 = 100px
Please refer to this box-sizing article for a better and more accurate explanation (including how to avoid/solution)
If I understood your question correctly, you wish to make the border of the orange box to be thicker? If so, you would change the border CSS property "thin" to a pixel value, like so:
style="border: 4px solid #F48C13"
If not, I apologise, I could not comment yet to ask this clarification.

How do I bottom-align grid elements in bootstrap fluid layout

I have a fluid layout using Twitter's bootstrap, wherein I have a row with two columns. The first column has a lot of content, which I want to fill the span normally. The second column just has a button and some text, which I want to bottom align relative to the cell in the first column.
Here's what I have:
-row-fluid-------------------------------------
+-span6----------+ +-span6----------+
| | |short content |
| content | +----------------+
| that |
| is tall |
| |
+----------------+
-----------------------------------------------
Here's what I want:
-row-fluid-------------------------------------
+-span6----------+
| |
| content |
| that |
| is tall | +-span6----------+
| | |short content |
+----------------+ +----------------+
-----------------------------------------------
I've seen solutions that make the first span an absolute height, and position the second span relative to it, but a solution where I didn't have to specify the absolute height of my divs would be preferred. I'm also open to a complete rethink of how to achieve the same effect. I'm not married to this use of the scaffolding, it just seemed to make the most sense to me.
This layout as a fiddle:
http://jsfiddle.net/ryansturmer/A7buv/3/
This is an updated solution for Bootstrap 3 (should work for older versions though) that uses CSS/LESS only:
http://jsfiddle.net/silb3r/0srp42pb/11/
You set the font-size to 0 on the row (otherwise you'll end up with a pesky space between columns), then remove the column floats, set display to inline-block, re-set their font-size, and then vertical-align can be set to anything you need.
No jQuery required.
Please note: for Bootstrap 4+ users, please consider Christophe's solution (Bootstrap 4 introduced flexbox, which provides for a more elegant CSS-only solution). The following will work for earlier versions of Bootstrap...
See http://jsfiddle.net/jhfrench/bAHfj/ for a working solution.
//for each element that is classed as 'pull-down', set its margin-top to the difference between its own height and the height of its parent
$('.pull-down').each(function() {
var $this = $(this);
$this.css('margin-top', $this.parent().height() - $this.height())
});
On the plus side:
in the spirit of Bootstrap's existing helper classes, I named the class pull-down.
only the element that is getting "pulled down" needs to be classed, so...
...it's reusable for different element types (div, span, section, p, etc)
it's fairly-well supported (all the major browsers support margin-top)
Now the bad news:
it requires jQuery
it's not, as-written, responsive (sorry)
You can use flex:
#media (min-width: 768px) {
.row-fluid {
display: flex;
align-items: flex-end;
}
}
You need to add some style for span6, smthg like that:
.row-fluid .span6 {
display: table-cell;
vertical-align: bottom;
float: none;
}
and this is your fiddle: http://jsfiddle.net/sgB3T/
Here's also an angularjs directive to implement this functionality
pullDown: function() {
return {
restrict: 'A',
link: function ($scope, iElement, iAttrs) {
var $parent = iElement.parent();
var $parentHeight = $parent.height();
var height = iElement.height();
iElement.css('margin-top', $parentHeight - height);
}
};
}
Just set the parent to display:flex; and the child to margin-top:auto. This will place the child content at the bottom of the parent element, assuming the parent element has a height greater than the child element.
There is no need to try and calculate a value for margin-top when you have a height on your parent element or another element greater than your child element of interest within your parent element.
This is based on cfx's solution, but rather than setting the font size to zero in the parent container to remove the inter-column spaces added because of the display: inline-block and having to reset them, I simply added
.row.row-align-bottom > div {
float: none;
display: inline-block;
vertical-align: bottom;
margin-right: -0.25em;
}
to the column divs to compensate.
Based on the other answers here is an even more responsive version. I made changes from Ivan's version to support viewports <768px wide and to better support slow window resizes.
!function ($) { //ensure $ always references jQuery
$(function () { //when dom has finished loading
//make top text appear aligned to bottom: http://stackoverflow.com/questions/13841387/how-do-i-bottom-align-grid-elements-in-bootstrap-fluid-layout
function fixHeader() {
//for each element that is classed as 'pull-down'
//reset margin-top for all pull down items
$('.pull-down').each(function () {
$(this).css('margin-top', 0);
});
//set its margin-top to the difference between its own height and the height of its parent
$('.pull-down').each(function () {
if ($(window).innerWidth() >= 768) {
$(this).css('margin-top', $(this).parent().height() - $(this).height());
}
});
}
$(window).resize(function () {
fixHeader();
});
fixHeader();
});
}(window.jQuery);
Well, I didn't like any of those answers, my solution of the same problem was to add this:<div> </div>. So in your scheme it would look like this (more or less), no style changes were necessary in my case:
-row-fluid-------------------------------------
+-span6----------+ +----span6----------+
| | | +---div---+ |
| content | | | & nbsp; | |
| that | | +---------+ |
| is tall | | +-----div--------+|
| | | |short content ||
| | | +----------------+|
+----------------+ +-------------------+
-----------------------------------------------
.align-bottom {
position: absolute;
bottom: 10px;
right: 10px;
}

Repeating table caption after page break

When printing an HTML table, you can use CSS to force the table's header row to display again after the page break. This style:
#media print {
thead { display: table-header-group; }
}
Results in:
Caption
-------------
Col1 | Col2
-------------
Data1 | Data2
Data3 | Data4
--Page Break--
Col1 | Col2
-------------
Data5 | Data6
Is there a way to also repeat the table caption after the page break? I would think you could do something like caption { display: table-caption-group; }, but this doesn't exist. The solution would need to work in IE9.
I’m afraid there is no way to achieve that. In principle, you can set caption { display: table-caption-group; }, but by the specs, “If a table contains multiple elements with 'display: table-header-group', only the first is rendered as a header; the others are treated as if they had 'display: table-row-group'.” So you would not be able to make both the thead and the caption repeat. Besides, IE 9 does not get even let you repeat caption alone (Firefox does).
The workaround is to turn the caption element to a table row that is part of the thead element. E.g., for a two-column table:
<table>
<thead>
<tr><th colspan=2>Caption
<tr><th>Header cell <th>Another header cell
</thead>

Scrollbar resizing the page

Let's say I have a static webpage and the whole page is wrapped, let's say inside a width of 700px, now if the content of the page is too long then (obviously) a scrollbar appears. BUT the appearance of the scrollbar moves everything to the left of like a few pixels (the ones needed to fit the scrollbar on the right side of the page). What I'd like to do is to remove this "moving" effect, so that if a scrollbar is needed this doesn't affect the content of the page in any way.
I don't know if I made myself clear.
let's say that this is a webpage:
| .........contentcontent ........ |
| .........contentcontent ........ |
| .........contentcontent ........ |
| .........contentcontent ........ |
| .........contentcontent ........ |
| .........contentcontent ........ |
| .........contentcontent ........ |
and this is how it looks with the scrollbar:
| .....contentcontent .......... | |
| .....contentcontent .......... | |
| .....contentcontent .......... | |
| .....contentcontent .......... | |
|......contentcontent .......... | |
| .....contentcontent .......... | |
| .....contentcontent .......... | |
but I'd like to have something like this:
| .........contentcontent ...... | |
| .........contentcontent ...... | |
| .........contentcontent ...... | |
| .........contentcontent ...... | |
| .........contentcontent ...... | |
| .........contentcontent ...... | |
| .........contentcontent ...... | |
dots represent whitespace, content represent the webpage content and the column on the right represents the scrollbar.
You can set overflow-y: scroll as rule for your body. This will always display a vertical scrollbar which is disabled unless the content is long enough to be scrolled. This way the content won't shift to the left when its long enough that you can actually scroll and no scripting is needed to make this work.
if you are not need scrollbar at all, you can use this:
body {
overflow-y: hidden;
}
UPD. so if you need scrollbar to scroll content, but want to hide it, you can use the following method (demo on dabblet.com):
css:
body {
overflow: hidden;
}
#fake_body {
position: absolute;
left: 0;
top: 0;
right: -32px;
bottom: 0;
overflow-y: scroll;
}
html:
<div id="fake_body">
<ul>
<li>content content content #01</li>
<li>content content content #02</li>
<li>content content content #03</li>
…
<li>content content content #55</li>
</ul>
</div>
The following is not supported well across browsers, as per
MDN docs, however I think you may find it interesting.
overflow-y: overlay;
In browsers that support the property, such as Google Chrome, this will put the scrollbar on top of your content instead of shifting your content over when a scrollbar is needed. You could then add a sufficient amount of padding so that your content is never covered up by it.
It seems this property is not popular, and even browsers that support it now are planning to drop support for it. I don't understand why, because it certainly has its uses.
I had the same problem and only solution using JS/jQuery was enough good for me.
I used link to Detect if a page has a vertical scrollbar? provided before and those links: http://davidwalsh.name/detect-scrollbar-width, http://api.jquery.com/css/#css2, Center a position:fixed element for generating the following code.
css:
body {
position: absolute;
left: 50%;
margin-left: -400px;
width: 800px;
height: 100%;
}
.scrollbar-measure {
width: 100px;
height: 100px;
overflow: scroll;
position: absolute;
top: -9999px;
}
JS:
$(document).ready(function() {
// Check if body height is higher than window height :)
if ($(document).height() > $(window).height()) {
// Create the measurement node
var scrollDiv = document.createElement("div");
scrollDiv.className = "scrollbar-measure";
document.body.appendChild(scrollDiv);
// Get the scroll bar width
var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
// Delete the DIV
document.body.removeChild(scrollDiv);
// Change margin-left parameter for #container for preventing shift caused by scroll bar
var current_margin_left_px = $("#container").offset().left;
var current_margin_left = parseInt(current_margin_left_px, 10);
var changed_margin_left = current_margin_left + scrollbarWidth/2 + "px";
$("#container").css("margin-left", changed_margin_left);
}
});
In case of flexible body width some more code has to be added.
you can check if the screen has a scroll bar. if true than you could margin the content to the right maybe this link will help you : Detect if a page has a vertical scrollbar?
overflow-x: hidden;
in the body CSS code, HELPED AlOT! Damn, took me 45mins to surf around for a simplistic answer.
what went wrong? ...I had an with a hover=show image thing going on. I resized all the images correctly; when I refreshed the page, the scrollbar had way to much play room!.....So i put an overflow-x into each and every div tag, (each div tag assigned to individual list item), and nothing happened!
solution: The code was correct, but the placement needed to be in the body tag itself. not the individual div tags. Damn.....thankz

Highlight same nth-child element

Suppose I have the following table:
+----------+
| A | B |
+----------+
| 1 | 2 |
+----------+
I want to make so that when I hover over A that 1 gets certain css styles, ditto for B and 2. Is there a way to do this without using js?
Here's a fiddle to see what I mean
With this markup and pure CSS it's not possible because you would need to use td:hover to apply the CSS rule on mouseover, and there is no selector that lets you travel up the DOM tree (which would be necessary as you want to target cells that live in a different branch from the one being hovered).
If you can modify then a solution such as Dustin's can work; if you can use JS then it's also a matter of sprinkling a little jQuery on the table:
$("td").on("mouseenter mouseout", function() {
var $this = $(this);
$this.closest("table").find("td:nth-child(" + ($this.index() + 1) + ")")
.toggleClass("hover");
});
If you can change the markup, here's a response jsfiddle to do this with CSS.
http://jsfiddle.net/dBrd2/
Edit: Ah, but I see you replied to my comment and said you didn't want to. Anyways, just an idea if you reconsider.
you can create a class called .hover, and do like this for adding the style to all the td's with the same class
$("td.fir").bind("mouseenter", function(){
$(".fir").addClass("hover");
});​​​​
$("td.fir").bind("mouseleave", function(){
$(".fir").removeClass("hover");
});
​
Hi you can do as like this
Css
td {
position: relative;
border: 1px solid black;
padding: 50px;
}
tr .fir{
background:red;
}
table:hover .fir{
background:green;
}
​
HTML
<table>
<tr>
<td class="fir">a</td>
<td class="sec">b</td>
</tr>
<tr>
<td class="fir">1</td>
<td class="sec">2</td>
</tr>
</table>​
Live Demo here http://jsfiddle.net/rohitazad/en5rB/3/