Styling Your Website: Embrace CSS or Stick with Tables?

My goal is to create a layout with the following features, as shown in the screenshot:

The main features include:

  • The screen is divided into 3 regions (columns);
  • The left and right columns have fixed widths;
  • The middle column expands based on the browser width;
  • The right column is subdivided into two regions;
  • The bottom region has a fixed size and is always at the bottom;
  • The top region expands according to the browser height.

It took me about 2 hours to generate the above screenshot using HTML tables with these features.

Despite spending two days working with CSS, I haven't been able to achieve the desired layout. My attempt at CSS and the associated screenshots are provided below:

<html> 
<head> 
    <title>My Layout</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style type="text/css"> 
    body{
        height:100%;
        background:beige;
    }

    #header { 
        width:100%;
        height:60px;
        text-align:center;
        background:#A7C942; 
        color:#fff;
        float:left; 
        font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
        font-size:2em;
    } 

    #leftDiv { 
        float:left; 
        width:150px;
        height:90%;
        background:aquamarine; 
    } 
    #midDiv { 
        float:left; 
        width:auto; 
        height:90%;
        background:lightsalmon; 
        display:block;
    } 
    #rightDiv { 
        float:right; 
        width:365px; 
        height:90%;
        background:green; 
        display:block;
    }


    #topRow {
        background-color:lightgoldenrodyellow;
    }

    #bottomRow {
        background-color:lightpink;
        height:200px;
    }
    </style> 
</head> 

<body> 
<div id="body"> 
    <div id="main"> 
        <div id="header">my header</div>

        <div id="leftDiv">
            <p>LEFT</p>
        </div> 

        <div id="midDiv">
            <p>MIDDLE</p>
        </div> 

        <div id="rightDiv">
            <p>RIGHT</p>
            <div id="topRow">
                TOP
            </div>
            <div id="bottomRow">
                BOTTOM
            </div>
        </div>
    </div> 
</div> 
</body> 
</html>

Here is the screenshot with CSS:

The issues with my CSS attempt are:

  • The middle column does not expand properly (salmon colored part);
  • There's an unexpected appearance of white space instead;
  • I can't make the pink region stay at the bottom constantly;
  • I struggle to make the yellow region stretch vertically;
  • There is unwanted wrapping where the right region goes under the left and middle regions.

At this point, if I don't get a working solution using CSS soon, I might resort back to using tables. Unless a CSS expert comes to rescue me with a solution:-)

Update As of now, there were four great answers provided. I tested all of them on Firefox and Chrome, and each one works well. However, I must choose just one as the accepted answer. I'll go with the simplest one that uses only CSS and absolute positioning (no flexbox or CSS tables).

Big thanks to @matthewelsom, @Pangloss, @Shrinivas, and @Paulie_D for their helpful answers. Anyone who comes across their solutions will surely find them useful for their own projects. Everyone gets upvotes, and your efforts are truly appreciated!

Answer №1

Take a look at this coding example.

This demonstration utilizes fundamental CSS and HTML without any external frameworks.

The crucial CSS properties for achieving this type of layout are: left, right, top, bottom.

The demo showcases the application of these properties in action.

Below is the code snippet.

body {
  background: beige;
  margin: 0;
}
#header {
  width: 100%;
  height: 60px;
  text-align: center;
  background: #A7C942;
  color: #fff;
  font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
  font-size: 2em;
}
#leftDiv {
  position: absolute;
  float: left;
  width: 150px;
  top: 60px;
  bottom: 0;
  background: aquamarine;
}
#midDiv {
  position: absolute;
  float: left;
  top: 60px;
  bottom: 0;
  left: 150px;
  right: 365px;
  min-width: 50px;
  background: lightsalmon;
}
#rightDiv {
  position: absolute;
  float: right;
  width: 365px;
  top: 60px;
  bottom: 0;
  right: 0;
  background: green;
}
#topRow {
  position: absolute;
  width: 100%;
  top: 0;
  bottom: 50px;
  min-height: 20px;
  background-color: lightgoldenrodyellow;
}
#bottomRow {
  position: absolute;
  background-color: lightpink;
  width: 100%;
  height: 50px;
  bottom: 0;
}
<div id="body">
  <div id="main">
    <div id="header">my header</div>
    <div id="leftDiv">
      <p>LEFT</p>
    </div>
    <div id="midDiv">
      <p>MIDDLE</p>
    </div>
    <div id="rightDiv">
      <div id="topRow">TOP</div>
      <div id="bottomRow">BOTTOM</div>
    </div>
  </div>
</div>

Answer №2

Check out this simple flex layout example to get a basic understanding of how it works. If you want to learn more about Flexbox, visit CSS tricks.

I have also ensured that the design is responsive, so the layout will adjust to 100% width on screens that are under 480px wide.

View the CODEPEN example here

html, body {
  height: 100%;
  padding: 0;
  margin: 0;
}
.wrapper {
  height: 100%;
  display: flex;         /* NEW, Spec - Firefox, Chrome, Opera */
  display: -webkit-box;  /* OLD - iOS 6-, Safari 3.1-6, BB7 */
  display: -ms-flexbox;  /* TWEENER - IE 10 */
  display: -webkit-flex; /* NEW - Safari 6.1+. iOS 7.1+, BB10 */
  
  -webkit-flex-direction: column;
  flex-direction: column;
}

.wrapper > * {
   flex: 1 100%;
  -webkit-flex: 1 100%; 
}

.header {
  flex: 1;
  -webkit-flex: 1; 
  
  background: green;
  width: 100%;
  max-height: 80px;
}

.below {
  flex: 1;
  -webkit-flex: 1; 
  
  display: flex;        
  display: -webkit-box;  
  display: -ms-flexbox; 
  display: -webkit-flex;  
  
  -webkit-flex-direction: row;
  flex-direction: row;
}

.main {
  background: deepskyblue;
  flex: 3 0px;
  -webkit-flex: 3 0px;
  
  -webkit-order: 2;
  order: 2;
}

.left {
  background: yellow;
  max-width: 100px;
  flex: 1 auto;
  -webkit-flex: 1 auto;
  
  -webkit-order: 1;
  order: 1;
}

.right {
  background: hotpink;
  max-width: 300px;
  position: relative;
  
  display: flex;        
  display: -webkit-box;  
  display: -ms-flexbox; 
  display: -webkit-flex;  
  
  flex: 1 auto;
  -webkit-flex: 1 auto;
  
  -webkit-order: 3;
  order: 3;
  
  -webkit-flex-direction: column;
  flex-direction: column;
}
.top {
  background: lightpink;
  
  flex: 1 auto;
  -webkit-flex: 1 auto;
}
.bottom {
  background: salmon;
  max-height: 200px;
  width: 100%;
  
  flex: 1 auto;
  -webkit-flex: 1 auto;
}
@media screen and (max-width: 479px) {
  .below {
    -webkit-flex-direction: column;
    flex-direction: column;
  }
  .left, .right, .main {
    max-width: 100%;
    
    flex: 1;
    -webkit-flex: 1;
  }
  body, html, .wrapper {
    height: auto;
  }
}
<div class="wrapper">
  <header class="header"><h3>Header</h3><p>Fixed height 80px, 100% width</p></header>
  <div class="below">
    <article class="main">
    <h3>Flexible width Article</h3>
    <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est.
      Mauris placerat eleifend leo.</p>
  </article>
    <aside class="left">
      <h3>Left</h3>
      <p>100px fixed width</p>
    </aside>
    <aside class="right">
    <div class="top">
      <h3>Right Top</h3>
      <p>Flexible Height, Fixed width 300px</p>
    </div>
    <div class="bottom">
      <h3>Right Bottom</h3>
      <p>Fixed width 300px, Fixed Height 200px</p>
    </div>
  </aside>
  </div>
</div>

Answer №3

Exploring the CSS table layout, I made some slight adjustments to the markup by adding a couple of <div> containers to apply styles.

Check out the JsFiddle Demo

html, body {
    height: 100%;
    margin: 0;
}
#page, #main, #rTable {
    display: table;
    border-collapse: collapse;
    width :100%;
    height: 100%;
}
#page > div {
    display: table-row;
}
#page #hRow {
    background: lightgreen;
    text-align: center;
}
#page #mRow {
    height: 100%;
}
#main > div {
    display: table-cell;
    vertical-align: top;
}
#leftDiv {
    width: 100px;
    background: aquamarine;
}
#midDiv {
    background: lightsalmon;
}
#rightDiv {
    width: 200px;
    background: green;
}
#rTable > div {
    display: table-row;
}
#rTable #topRow {
    background: lightgoldenrodyellow;
}
#rTable #bottomRow {
    height: 100px;
    background: lightpink;
}
<div id="page">
    <div id="hRow">
        <div id="header">HEADER</div>
    </div>
    <div id="mRow">
        <div id="main">
            <div id="leftDiv">
                <p>LEFT</p>
            </div>
            <div id="midDiv">
                <p>MIDDLE</p>
            </div>
            <div id="rightDiv">
                <div id="rTable">
                    <div id="topRow">RIGHT-TOP</div>
                    <div id="bottomRow">RIGHT-BOTTOM</div>
                </div>
            </div>
        </div>
    </div>
</div>

Answer №4

Learning the concepts of flexbox can make layout design much simpler.

See Flexbox in Action on Codepen

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
html,
body {
  min-height: 100%;
}
.wrapper {
  width: 80%;
  margin: auto;
  display: flex;
  flex-direction: column;
  border: 1px solid red;
  height: 100%;
}
header {
  height: 50px;
  background: green;
  text-align: center;
  line-height: 50px;
}
main {
  flex-grow: 1;
  border: 1px solid red;
  height: 100%;
  display: flex;
}
.left {
  width: 200px;
  background: lightblue;
  flex: none;
}
.center {
  flex-grow: 1;
  background: salmon;
}
.right {
  width: 300px;
  flex:none;
  background: yellow;
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
}
.top {
  flex-grow: 1;
  background: #000;
}
.bottom {
  height: 50px;
  flex: none;
  background: pink;
}
<div class="wrapper">
  <header>
    <h2>Header</h2>
  </header>
  <main>
    <div class="col left">Fixed Left</div>
    <div class="col center">Flexible Center</div>
    <div class="col right">
      <div class="top">Top Flexible Height inside fixed Right</div>
      <div class="bottom">fixed Height Bottom</div>
    </div>
  </main>
</div>

Explore the Ultimate Guide to Flexbox on CSS-Tricks.com

Remember to use prefixes for browser compatibility.

Check browser support at: CanIUse.com

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Setting the height of the Bootstrap Navbar to accommodate the logo

I've designed a navigation bar with a logo, but the placement of the logo is not right (refer to the image). I am aware of how to resize the image to fit within the navbar, however, I intend to adjust the height of the navigation bar to align with the ...

Tips for centering links in the navigation bar on Bootstrap 5 when viewing in a small size

I had previously inquired about customizing the Bootstrap 5 navbar in this post: Adjust navbar height to be smaller and I received some helpful answers. Thank you to everyone who responded. However, I now have a follow-up question: I am interested in ch ...

Create a shape using a series of points without allowing any overlap between the lines

JS fiddle There is an array of coordinates that gets populated by mouse clicks on a canvas. var pointsArray = []; Each mouse click pushes x and y values into this array using a click event. pointsArray.push({x: xVal, y: yVal}); The script iterates thr ...

Manipulate Input Classes by Adding and Removing Them

I am currently using masked JS in some input fields, specifically for telephone numbers. However, I am facing an issue where phone numbers can vary in length, either 10 or 11 digits. I need to switch the class based on whether a checkbox is checked. Here i ...

I am having trouble accessing the database. Jacky @ localhost Password: NO

What is preventing me from accessing the database? Username: jacky @ local host Password: NO ...

How can I connect the box using the Interactive Picture jQuery tool?

When using the Interactive picture jQuery, I have the following code snippet within my document: jQuery(document).ready(function(){ $( "#iPicture6" ).iPicture({ animation: true, animationBg: "bgblack", animationType: "ltr-slide ...

Tips for implementing z-index property on table border

I am encountering an issue with an html file that contains a table. The table has one row element with the css property of position: sticky, while the other rows are just example rows with "some text" in each cell. Within column 5 of the regular rows, I h ...

Substitute using JQuery

Hello, I am attempting to update some html using Jquery, here is my current progress. $(".old").html($(".new").html()); It's almost working. The problem is that the .new content is being copied instead of replaced. I want to Cut/Paste instead of Co ...

Mobile FPS suffering due to slide-out drawer performance issues

My application features a drawer menu that functions smoothly on desktop, but experiences issues on mobile devices with noticeable lag. Within the header, I have implemented a boolean value that toggles between true and false upon clicking the hamburger i ...

I am having trouble getting any value back from my POST request using HTML, Node.js, and Express

I've been attempting to make a simple post request from an HTML page, but when I check the console after the post, it doesn't return anything. I'm confident that I have correctly set up the body parser on the server side. Could there be some ...

Linking all styles with Angular 2

Is it possible to apply a style when the exact nature of that style is unknown? Consider a scenario where I have a model containing string variables defining styles, as shown below: myStyle1:string="margin-left:10px"; myStyle2:string="margin ...

Creating a contact form using HTML and PHP

Hey there, I'm new to web development and I've been trying to implement a contact form on my website using some source code. Unfortunately, it's not working as expected and I could really use some help. Here is the code snippet: HTML & ...

Maximizing the potential of Angular forms through native FormData

Currently, I am revisiting an older project that still uses traditional methods for form submission. The HTML includes a form element with defined method and action. My goal is to submit the form in the old-fashioned way using the action attribute to post ...

Issues with the alignment of ion-grid in Ionic/Angular application

I am facing an issue in my ionic/angular project where I am attempting to create two columns within a row using ion-grid, ion-row, and ion-col. However, the content of the second column is unexpectedly appearing below the first column instead of beside it. ...

Python HTMLParser: Extracting HTML tag content made easy!

After working through an HTML page, I have now reached a point where I have lines that look like this: <td class="border">AAA</td><td class="border">BBB</td> I am trying to extract AAA and BBB into variables using HTMLParser, but ...

Make the minimum height of one div equal to the height of another div

My query is somewhat similar to this discussion: Implementing a dynamic min-height on a div using JavaScript but with a slight twist. I am working on a dropdown menu within a WordPress site, integrated with RoyalSlider. The height of the slider div adjust ...

What methods can be utilized to ensure that my wistia video player/playlist is responsive on different devices

I need some assistance with making my Wistia player responsive while using a playlist. I want the video player to adjust its size based on the screen size. Here is an example: My current code utilizes their iframe implementation: <div id="wistia_1n649 ...

Conceal a different div unless it includes

Hi everyone, I need help with a filter code snippet: $(".title").not(":contains('" + $("[name=filter]").val() + "')").hide() The issue I'm facing is that the .title class is nested within the .sortAll class along with many other divs. I w ...

Unable to modify the border radius of the carousel indicators in Bootstrap 5

Currently, I am working with the carousel component in Bootstrap 5.1. I have encountered an issue where I am unable to add border-radius to the carousel indicators, despite trying various methods. Even though the inspector shows that the indicators (HTML ...

Adjust the vertical positioning according to the percentage of the width

Currently, I am attempting to modify the position of my navigation bar on the screen in relation to the screen width rather than the height. I previously attempted using top: 10% in CSS, but realized this is based on the screen's height instead of its ...