How to achieve table layout without using tables?
Asked Answered
B

3

11

In the name of progress (and learning) how can I rid tables from my code and achieve the same layout?

For example, here is my table:

<table cellspacing="0px">
    <tr>
        <td>
            <img src="http://www.poltairhomes.com/images/footerlogo.png" />
        </td>
        <td id="footertext">
            <p>Poltair Homes Plc<br />Registered Office: The Old Chapel, Greenbottom, Truro, Cornwall, TR4 8QP.<br />Registered in England & Wales: 3955425<br />www.poltairhomes.com<br />[email protected]</p>
        </td>
        <td id="footertext">
            <p>Terms and Conditions | Privacy Policy | Sitemap</p>
        </td>
        <td id="footertext">
            <p>SIGN UP FOR OUR NEWSLETTER:</p>
            <img src="http://www.poltairhomes.com/images/signup(temp).png" />
        </td>
    </tr>
</table>

And the relevant CSS:

.footertext {
    margin: 0;
    padding:0 5px 0 5px;
    color: #AAA;
    font-size: 10px;
    font-family:Arial, Helvetica, sans-serif;
    text-align:center;
    border-left: 1px solid #CCC;
}

http://jsfiddle.net/userdude/tYjKw/

Bevatron answered 15/6, 2012 at 10:41 Comment(3)
Do not reuse id tags like you are. Those should be class="footertext".Carrycarryall
With the class="footertext": jsfiddle.net/userdude/tYjKw/1Carrycarryall
Sorry yes that is how it is on the site actually (hence the CSS being ".footertext" and not "#footertext"... my bad when typing it on here... thanks!Bevatron
J
1

Create a style as:

.footerItem { float: left; }
<div class="footerItem">
        <img src="http://www.poltairhomes.com/images/footerlogo.png" />
    </div>
    <div class="footerItem">
        <p>Poltair Homes Plc<br />Registered Office: The Old Chapel, Greenbottom, Truro, Cornwall, TR4 8QP.<br />Registered in England & Wales: 3955425<br />www.poltairhomes.com<br />[email protected]</p>
    </div>
    <div class="footerItem">
        <p>Terms and Conditions | Privacy Policy | Sitemap</p>
    </div>   
    <div class="footerItem">
        <p>SIGN UP FOR OUR NEWSLETTER:</p><img src="http://www.poltairhomes.com/images/signup(temp).png" />
    </div>

and then create your body using DIVs to separate the blocks and apply the class to each one:

Jada answered 15/6, 2012 at 10:49 Comment(2)
Thank you - one of two great examples! :)Bevatron
This is the answer that I went for - but there are others here that are really helpful too... thanks all! Great work :)Bevatron
D
15

CSS:

.table {
  display: table;
}
.table-row {
  display: table-row;
}
.table-cell {
  display: table-cell;
}

HTML:

<div class="table">
  <div class="table-row">
    <div class="table-cell">Table cell 1</div>
    <div class="table-cell">Table cell 2</div>
  </div>
</div>
Dorser answered 15/6, 2012 at 10:45 Comment(1)
Regarding the question to get a table without using a <table>-tag, this would be the best and a modern solution. And you could also use "vertical-align:middle" which is not so easy to approach just with a grid, but it is shown in the fiddle link. Note here, that this doesn't work on IE7 and below.Imelda
S
9
<div class="table">
    <div class="row">
       <div class="cell twocol">
           <span>Content1</span>
       </div>
       <div class="cell twocol">
           <span>Content2</span>
       </div>
    </div>
    <div class="row">
       <div class="cell onecol">
           <span>Content3</span>
       </div>
    </div>
</div>

And the CSS

.table {width: 100%; height: 100%;}
.row {width: 100%; min-height: 1px; height: auto; margin: 0;}
.cell {float: left; margin: 0; padding: 0;}
.onecol {width: 100%;}
.twocol {width: 50%;}

I suggest you look into some gridsystems, like 960grid (http://960.gs/) or 1140grid (http://cssgrid.net/), will help you a lot.

Stubbs answered 15/6, 2012 at 10:56 Comment(9)
Checked out 960grid slides - looks very interesting... will certainly explore further. Thanks!Bevatron
Personally, I use 1140px, for wider screens, and so far it hasn't failed me yet, it's flexible, and using @media-queries you can adapt it simply. Personal advice: get. rid. of. the. tables. and. the. .<br>. :DStubbs
Cool I'll have a look - tables are history for me now thanks to some of the answers to this question! Take a look at my source for the footer on poltairhomes.com (excuse the mess created by wordpress! client's orders!) - how would I get rid of the <br>'s ?Bevatron
<br>'s are created by Wordpress? Btw, I suggest adding .textwidget > .footerItem:last-child {padding: 0;}, as your footer is kinda breaking up.Stubbs
No <br>'s were me trying to get the layout right! I was referring to the html tags being overcomplicated - wordpress builds the html from CSS and PHP - but no, the <br>'s were my fault! What do you mean by "breaking up"? Looks okay to me?Bevatron
In both Chrome and FF, the last footer item goes to the bottom, as the footer has not enough space.Stubbs
Oooh... I didn't have that issue - I put the padding in because using "float: left" meant there was a gap to the RHS of the footer. I've changed it now so instead of "float: left" I've used "display:inline-block" meaning I could add a container and use "min-width" so the footer doesn't have a gap - meant I could take out some padding... does it look okay for you now?Bevatron
Nope. Your items still have too much width for your container. You can check out a preview of how's on my browsers here (awesomescreenshot.com/0cf84egf5). Both Chrome (v19) and FF (latest stable).Stubbs
Odd! I'm using latest Safari and Chrome on OSX Lion and all good?!Bevatron
J
1

Create a style as:

.footerItem { float: left; }
<div class="footerItem">
        <img src="http://www.poltairhomes.com/images/footerlogo.png" />
    </div>
    <div class="footerItem">
        <p>Poltair Homes Plc<br />Registered Office: The Old Chapel, Greenbottom, Truro, Cornwall, TR4 8QP.<br />Registered in England & Wales: 3955425<br />www.poltairhomes.com<br />[email protected]</p>
    </div>
    <div class="footerItem">
        <p>Terms and Conditions | Privacy Policy | Sitemap</p>
    </div>   
    <div class="footerItem">
        <p>SIGN UP FOR OUR NEWSLETTER:</p><img src="http://www.poltairhomes.com/images/signup(temp).png" />
    </div>

and then create your body using DIVs to separate the blocks and apply the class to each one:

Jada answered 15/6, 2012 at 10:49 Comment(2)
Thank you - one of two great examples! :)Bevatron
This is the answer that I went for - but there are others here that are really helpful too... thanks all! Great work :)Bevatron

© 2022 - 2024 — McMap. All rights reserved.