HTML layout

Websteder viser ofte indhold i flere kolonner (som magasiner og aviser).

HTML-layout med <div>-elementer

Note:Elementer bruges ofte som layoutværktøjer, fordi de kan placeres let med CSS.

Denne eksempel bruger fire <div>-elementer til at oprette et flerkolonne-layout:

Example

<body>
<div id="header">
<h1>City Gallery</h1>
</div>
<div id="nav">
London<br>
Paris<br>
Tokyo<br>
</div>
<div id="section">
<h1>London</h1>
<p>
London is the capital city of England. It is the most populous city in the United Kingdom,
with a metropolitan area of over 13 million inhabitants.
</p>
<p>
Standing on the River Thames, London has been a major settlement for two millennia,
its history going back to its founding by the Romans, who named it Londinium.
</p>
</div>
<div id="footer">
Copyright codew3c.com
</div>
</body>

Try it yourself

CSS:

<style>
#header {
    background-color:black;
    color:white;
    text-align:center;
    padding:5px;
}
#nav {
    line-height:30px;
    background-color:#eeeeee;
    height:300px;
    width:100px;
    float:left;
    padding:5px; 
}
#section {
    width:350px;
    float:left;
    padding:10px; 
}
#footer {
    background-color:black;
    color:white;
    clear:both;
    text-align:center;
    padding:5px; 
}
</style>

Brug af HTML5's website-layout

HTML5s nye semantiske elementer definerer de forskellige dele af en webside:

HTML5 semantiske elementer

header Definer dokumentets eller afsnittets rubrik
nav Definer en beholder for navigationslinks
section Definer et afsnit i dokumentet
article Definer et selvstændigt, selvindkapslet artikel
aside Definer indhold uden for indholdet (f.eks. sidebalk)
footer Definer dokumentets eller afsnittets fodnote
details Definer yderligere detaljer
summary Definer titlen for details-elementet

Denne eksempel bruger <header>, <nav>, <section> og <footer> til at oprette et flerkolonne-layout:

Example

<body>
<header>
<h1>City Gallery</h1>
</header>
<nav>
London<br>
Paris<br>
Tokyo<br>
</nav>
<section>
<h1>London</h1>
<p>
London is the capital city of England. It is the most populous city in the United Kingdom,
with a metropolitan area of over 13 million inhabitants.
</p>
<p>
Standing on the River Thames, London has been a major settlement for two millennia,
its history going back to its founding by the Romans, who named it Londinium.
</p>
</section>
<footer>
Copyright codew3c.com
</footer>
</body>

Try it yourself

CSS

header {
    background-color:black;
    color:white;
    text-align:center;
    padding:5px; 
}
nav {
    line-height:30px;
    background-color:#eeeeee;
    height:300px;
    width:100px;
    float:left;
    padding:5px; 
}
section {
    width:350px;
    float:left;
    padding:10px; 
}
footer {
    background-color:black;
    color:white;
    clear:both;
    text-align:center;
    padding:5px; 
}

HTML layout using tables

Note:The <table> element is not designed as a layout tool.

The <table> element is used to display tabular data.

Using the <table> element can achieve layout effects because the styles of the table element can be set through CSS:

Example

<body>
<table class="lamp">
<tr>
  <th>
    <img src="/images/lamp.jpg" alt="Note" style="height:32px;width:32px">
  </th>
  <td>
    The table element was not designed to be a layout tool.
  </td>
</tr>
</table>
</body>

Try it yourself

CSS

<style>
table.lamp {
    width:100%;
    border:1px solid #d4d4d4;
}
table.lamp th, td {
    padding:10px;
}
table.lamp td {
    width:40px;
}
</style>