1

There are several methods for (absolute) positionning of a DOM element in a HTML page :

  • style directly in the CSS with top / left :

    position: absolute; top: 100px; left: 50px;

  • style directly in the CSS with transform:

    transform: translate(50,100)

  • and of course, CSS style modification with Javascript :

    element.style.top = "100px";

    ...

But these are all CSS methods.

Question:

Is it possible to set DOM elements' position without CSS? (for example thanks to some methods that were available in browsers before CSS became famous or simply before CSS was added to browsers)

Note: This question is for pure knowledge / browsers' history / culture. I won't necessarily want to use it.

Basj
  • 41,386
  • 99
  • 383
  • 673
  • 12
    Why? Oh dear, sweet [deity, or secular figure, of your own choice] *why*? Those days are not to be revisited, the migraines that those flashbacks bring back are not good for us. Or you. Or for any family, or pets, you might have. – David Thomas Nov 25 '14 at 16:09
  • For my culture and better understanding of how things are implemented and how they were before :) – Basj Nov 25 '14 at 16:12
  • Why would you avoid CSS? Going back to the 90's pre-CSS style is going to give you ugly, inconsistent, imprecise and awkward results. Use CSS. That's what it's for. Before that we had tables and spacer images. It was an ugly time. It's not going to teach you much. – Surreal Dreams Nov 25 '14 at 16:12
  • 2
    Painful. They were *painful* before. – David Thomas Nov 25 '14 at 16:12
  • 3
    Here's one, just as an illustration how painful: `
    AB
    ` to have A and B separated by 30 pixels. (`spacer.gif` would be a 1x1 transparent gif.) Strictly for historical education, never ever again for implementation. (I had to remove the quotes for historical accuracy. The horror.)
    – Amadan Nov 25 '14 at 16:15
  • 1
    @DavidThomas I added a note to the question. To the downvoters: why the downvote? Can't I ask a question just for programming culture? – Basj Nov 25 '14 at 16:15
  • 3
    Well, basically there was 'nested tables' using various combinations of 'align', 'valign' and spacer (transparent) images. Also: optimism, and the tears. So many, many tears. – David Thomas Nov 25 '14 at 16:16
  • 2
    @Basj the downvotes are probably due to the tears you are evoking in some people... poor them. – DRC Nov 25 '14 at 16:18
  • possible duplicate of [Why not use tables for layout in HTML?](http://stackoverflow.com/questions/83073/why-not-use-tables-for-layout-in-html) – Greg Burghardt Nov 25 '14 at 16:20
  • 2
    @GregBurghardt How is that a duplicate? Because Amadan mentions tables in his comment? – GolezTrol Nov 25 '14 at 16:21
  • @GolezTrol: Because there is no other means for laying out a page without CSS. – Greg Burghardt Nov 25 '14 at 16:22
  • 1
    There are cases where the _past_ might be better, at least when it comes to _personal preferences_ (I still listen to and collect vinyl records). This is not one of them. – emerson.marini Nov 25 '14 at 16:24
  • And I don't think this question belongs here anyway. – emerson.marini Nov 25 '14 at 16:25
  • 1
    @MelanciaUK: I was so very prepared to heartily disagree with your comment (and, subsequently, go flush away the memories of those days with anything alcoholic), right until that last sentence. And yeah, I'm not sure where it *does* belong, to be honest. And it's a question I'm relatively interested in, sadly. Meta suggests that this question would be "[on-topic at](http://meta.stackoverflow.com/a/276368/82548)" [Programmers.SE], though. – David Thomas Nov 25 '14 at 16:26
  • 1
    @DavidThomas It's kinda weird for one to come here looking for some _trick_ using `LESS` or `SASS` (just examples) and find someone asking how to put up a layout without using any `CSS`. Sad. But I got the point. For historical reasons it would be OK, just not here. `Programmers` may fit. – emerson.marini Nov 25 '14 at 16:32
  • 1
    @GregBurghardt That's not entirely true, and even if it was, just because the answer might apply to this question doesn't mean the question itself is a duplicate. – GolezTrol Nov 25 '14 at 17:10

2 Answers2

7

(Warning: Sarcasm and humor abounds)

A Clean Table For Layout

Tables were the only way to position elements without CSS.

Let's take the legendary "3 column layout" as an example (with room for the new 768px wide banner ad AND a 300px wide box ad on the right!).

<table cellspacing="1" cellpadding="0" width="1318" bgcolor="gray">
    <tr>
        <td width="200" bgcolor="white">
            <img src="spacer.gif" width="200" height="1">
        </td>
        <td width="768" bgcolor="white">
            <!-- body content goes here -->
        </td>
        <td width="300" bgcolor="white">
            <img src="spacer.gif" width="300" height="1">
        </td>
    </tr>
</table>

So nothing majorly crappy here. Just some widths and spacer GIFs. Perfectly acceptable for late '90s and early century browsers. Pretty standard fare. But wait, you need to support Netscape 4 as well!? Now we have a problem.

The "Netscape 4" Problem

Netscape 4 didn't display a table background through the cellspacing so we need some additional HTML:

<table cellspacing="0" cellpadding="0" width="1268">
    <tr>
        <td bgcolor="gray" colspan="7">
            <img src="spacer.gif" width="1" height="1">
        </td>
    </tr>
    <tr>
        <td width="1" bgcolor="gray">
            <img src="spacer.gif" width="1" height="1">
        </td>
        <td width="200" bgcolor="white">
            <img src="spacer.gif" width="200" height="1">
        </td>
        <td width="1" bgcolor="gray">
            <img src="spacer.gif" width="1" height="1">
        </td>
        <td width="768" bgcolor="white">
            <!-- body content goes here -->
        </td>
        <td width="1" bgcolor="gray">
            <img src="spacer.gif" width="1" height="1">
        </td>
        <td width="300" bgcolor="white">
            <img src="spacer.gif" width="350" height="1">
        </td>
        <td width="1" bgcolor="gray">
            <img src="spacer.gif" width="1" height="1">
        </td>
    </tr>
    <tr>
        <td bgcolor="gray" colspan="7">
            <img src="spacer.gif" width="1" height="1">
        </td>
    </tr>
</table>

Hey, now it works on all supported browsers. Ha ha! Ho, man! It's all coming back to me now! The hours. The days spent counting td tags in nested tables and making sure the widths of my spacer GIFs matched the widths on the td tags. Oh, and if you put a <%DOCTYPE %> tag at the top of your document (a proper one) then mysterious spaces appeared beneath the spacer GIFs. Remember that!? That was a hard lesson in how standards compliant browsers positioned inline elements.

The Decent Into True Madness

Now if only Internet Explorer supported the <layer> tag we could remove all CSS from our lives!

<layer top="300" left="25">
    I am floating on top of everything in front of your face!
</layer>

It makes me truly miss this peppered all over my JavaScript code as well:

if (document.all) {
    // Optimized for MSIE of course!
} else if (document.layers) {
    // Support Netscape 4 as well
} else if (document.getElementById) {
    // What is this new DOM? Is this "DHTML"?
} else {
    // That's right. Don't throw an error. Show a big fat, annoying alert that
    // the user is an idiot
    alert("Your browser does not support the Internet. Please open Notepad and try again.");
}

Epilogue

The days before CSS were truly dark. It was an age before time. Before fire was invented (literally if you interpret "fire" as "Firefox"). Internet Explorer was the standards compliant browser. Imagine that! Now I know you'll say "But this question was about layout, not browser support" and yet when we used tables for layout, the two were intertwined. Nowadays, I don't think browsers will even recognize presentational attributes on table related tags if you use a proper doctype, though I think basic attributes like width and height will still work. I only hope that the HTML and JavaScript snippets serve as historical context and to provide a warning to all those that follow. Tread not on these roads. For despair, war, and the gnashing of teeth are but close behind.

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
1

Short answer: no.

There has surely been implementations of style-related properties (presentational attributes) in browsers before CSS, but not in terms of positioning. Furthermore, going back that far would also mean going back to the time where there was no standardized DOM to use for setting properties in that sense.

Even if contemporary browsers have legacy support for presentational properties (e.g. bgcolor), I'm pretty sure the rendered appearance of HTML that by default has any sort of effect on layout (such as tables) are defined in terms of user agent CSS these days.

Emil
  • 1,949
  • 2
  • 16
  • 25