Showing posts with label CSS. Show all posts
Showing posts with label CSS. Show all posts

Thursday, August 02, 2018

HTML Table Scrollable Tbody

HTML tables can display rows and columns of data in a nicely excel like layout. With CSS, you can make beautiful tables. With huge rows of data, the header tends to be scrolled out of the screen making it difficult to know what the column represents.

Using CSS, you can actually fix the header and able to scroll the data. The trick is to use THEAD and TBODY with CSS.

For THEAD and TBODB, you need to use Display: Block. For TBODY only you need to use overflow-y: auto so that it could scroll up and down.

The problem with this arrangement is that the column width is uncontrollable especially when a column data is all empty. THEAD columns cannot sync with TBODY. Its really painful to have it working yet not able to align the column widths. I wonder why it is still not fixed.

One way to align the tables is to use Javascript. However, if user set to not allow JavaScript then the display is messed up. Therefore, a CSS way is more proper. The catch is to use fixed width columns. Many designer frown on such idea. There is no choice if you want to scroll.

However, Width:50px, for example, does not work well, the width still self adjusts according to the text length especially when the table size is limited. Putting too big a width also make the table look ugly. I used the following to at least make the width more consistent. It is not a cure but at least it looks more presentable.

The HTML/CSS used is as follows :-

  1. Assign the table with a class name.
  2. Set the table width to 100%, Table_Layout: fixed
  3. use table.classname thead to define display:block
  4. use table.classname tbody to define display:block and overflow-y:auto
  5. use table.classname th:nth-child(x), table.classname td:nth-child(x) to define the min-width and other styles.
Item 5 above actually forces both header and body column as defined by x to the same size. Assign a minimum size (min-width) to the column so that it will accommodate the longest single word text in the column (text with spaces will wrap by default). It will take a while to adjust it but at least the columns will align if the tbody cells are empty. Each column must have a corresponding width setting.

Until such a time when W3C solved the scrollable table issue, this is probably the make-do way. Others might have a better solution but this one is an accumulation of various designer's idea.





Friday, October 01, 2010

Fixed menu again

Although the fixed menu worked fine with IE7, Firefox and chrome, when IE 6 is used, the same age old problem of SELECT showing over DIV still exists when you scroll the window.

I have just noticed that when the web page is shown with the SELECT positioned just behind the DIV, it actually works. But when you scroll the SELECT again go on top of the DIV.

I have a stupid but working solution to this. Just do the following.

Create a javascript right at the end of the page which will only appears if the browser is IE 6 and below using the
<!--[if lte IE 6]>... <![endif]--> syntax to ensure that the script will not be used for IE7 and above.

Add the following in the script

mytime =setInterval("goforit()",100)
function goforit() {
hf=document.getElementById("hiframe")
hf.style.display="none";
hf.style.display=""

Finally add a IFRAME between the two DIV. with style similar to the second DIV. The SRC should be <html></html> but replace the greater than and lesser than with the escape code for the two character. The height and width should be same as the second div. Give it and ID (hiframe in the script example).

It will be good to hide the IFRAME if it is not IE 6 and below or other browsers.

Wednesday, September 29, 2010

Fixed menu

Previously I have not found any fixed menu or floating menu using just pure CSS. I just come across this link http://ryanfait.com/resources/fixed-positioning-in-internet-explorer/ that actually makes a fixed menu work even for IE6.

Sunday, September 26, 2010

Dynamic floating DIV

I have been having trouble with floating DIV previously. It does not have any problem when running. However, in IE it always appear behind INPUT's. No matter how much I tried, it just don't go away. After searching the web, there seems to be no solution - at least for IE6 and below.

Besides this issue, the CSS for "position" also caused a lot of headache. "Position:fixed" does not work for IE7 (not sure about IE8 and above). "Position:absolute" has to be used. The problem is that this CSS does not make the DIV stay at the defined position.

I have read from post by others that there is only one choice for IE. Use "Position:absolute" and then add a javascript "window.onscroll" event and set the top and left style property of the DIV element using the scrollTop and scrollLeft offsets respectively.

It is not the best solution for IE as when you scroll the DIV actually jumps a bit. It is still better than nothing right?

Positioning the div itself on the top of the screen is no big issue. What if you want to position the DIV somewhere at the bottom of the page? Surely you have to take into consideration of the height of the DIV to ensure that it does not goes beyond the display.

It will be much more difficult if your DIV changes its size dynamically. Fortunately there is a element.offsetHeight/offsetWidth to help. It is the actual height/width of the element even if you set the style.height to auto.

One more thing. If you want to set the DIV to be at a fixed percentage of the window size instead of a fixed pixle value, you have a problem if your DIV is near the bottom of the window. When you resize the window, the div may shifts accordingly and get hidden or shifts up too high.

Therefore, you will need to offset the percentage with the offsetHeight of the DIV. To do that, you will need to know the height of the window.

The problem is that the height of the window can mean different thing in different browsers. This is especially true for different IE versions and whether it is set to quirk mode.

A easy way out is to do this.


var root = (document.compatMode == "CSS1Compat"?
document.documentElement: document.body);
var myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
myHeight = window.innerHeight;
} else
myHeight=root.clientHeight

Doing offset is then easy if the height of the window is known. First find out what is the pixel by multiplying the window size with the percent. If the pixle value plus the offsetHeight of the DIV is greater than the window size, then just set it to the window size minus the offsetHeight so that the DIV can be shown all the time.