Creating a 2-column CSS layout with the navigation column stopping before reaching the bottom of the page

Creating a 2 column CSS layout may seem simple based on what is taught in class, but the reality can be quite different. The textbook explains how to make the Nav column float left, however, it doesn't always extend all the way down the page as illustrated in their example, although it does align properly in the upper left corner.

CSS:


    body{
    background-color: #ffffff;
    color: #666666;
    font: sans-serif;
    background-image: url(background.jpg);
}

header{
    background-color: #000033;
    color: #ffffff;
    font: sans-serif;
}

nav{
    float: left;
    width: 90px;
    position: absolute;
    background-color: #90c7e3;
    text-decoration: none;
    font-weight: bold;
    padding: 5px;
}

main{
    display: block;
}
h1{
    line-height: 200%;
    background-image: url(sunset.jpg);
    background-repeat: no-repeat;
    background-position: right;
    padding-left: 20px;
    height: 72 px;
    margin-bottom: 0;
}

h2
{
    color: #3399cc;
    font: serif;
}

h3
{
    colo...

HTML:

<!doctype html>
<html lang="en">
<head>
    <link rel="stylesheet" type="text/css" href="pacific.css">
    <title>Pacific Trails Resort</title>
    <meta charset="UTF-8">
</head>
<body>
<div id="wrapper">
<header>
    <h1>Pacific Trails Resort</h1>
</header>

<nav>
    <a href="index.html">Home</a> &nbsp; &nbsp; &nbsp;
    <a href="yurts.html">Yurts</a> &nbsp; &nbsp; &nbsp;
    <a href="activities.html">Activities</a> &npb;&nbsp; &nbsp;
    <a href="reservations.html">Reservations</a>
</nav>

<div id="homehero">
</div>

<main>
    <h2>Enjoy Nature in Luxury</h2>
    <p><span class="resort">Pacific Trails Resort</span> offers a special     lodging experience on the California North Coast. Relax in serenity with     panoramic views of the Pacific Ocean.</p>

<ul>
    <li>Private yurts with decks overlooking the ocean</li>
    <li>Activities lodge with fireplace and gift shop</li>
    <li>Nightly fine dining at the Overlook Cafe</li>
    <li>Heated outdoor pool and whirlpool</li>
    <li>Guided hiking tours of the redwoods</li>
</ul>
    <div>
        <span class="resort">Pacific Trails Resort</span><br>
        12010 Pacific Trails Road<br>
        Zephyr, CA 95555<br>
        888-555-5555<br>
    </div>
</main>
<footer>
    <div id="footer">
    Copyright &copy; 2016 Pacific Trails Resort<br>
    </div>
</footer>
...

Answer №1

As mentioned earlier, a simple solution to aligning the main element next to the nav is by adding a float right property to the main element and setting a height for the nav. Additionally, it's important to limit the width of the main element so it fits properly alongside the nav.

Take a look at this code snippet to visualize the changes:

body{
    background-color: #ffffff;
    color: #666666;
    font: sans-serif;
    background-image: url(background.jpg);
    position: relative;
}

header{
    background-color: #000033;
    color: #ffffff;
    font: sans-serif;
}

nav{
    width: 90px;
    position: absolute;
    background-color: #90c7e3;
    text-decoration: none;
    font-weight: bold;
    padding: 5px;
    height: 100%;
}

main{
    display: block;
    float: right;
    width: 80%;
}
h1{
    line-height: 200%;
    background-image: url(sunset.jpg);
    background-repeat: no-repeat;
    background-position: right;
    padding-left: 20px;
    height: 72 px;
    margin-bottom: 0;
}
...
<!doctype html>
<html lang="en">
<head>
    <link rel="stylesheet" type="text/css" href="pacific.css">
    <title>Pacific Trails Resort</title>
    <meta charset="UTF-8">
</head>
...

In my modifications, I utilized the position absolute on the nav element and positioned it relative to the body. The header and footer were moved outside the wrapper div to ensure that only the nav and main elements are contained within it. Furthermore, I relocated the home hero section to be inside the main element, thus allowing it to be floated right as well.

By enclosing only the main and nav elements in the wrapper, we can easily set the height: 100%; for the nav to occupy the entire height of the wrapper. If the header and footer remained within the wrapper, the nav would extend beyond the intended height due to not accounting for the header and footer heights.

One important adjustment was removing the float: left; from the nav since its position: absolute; already dictates its positioning.

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

Ensure consistent switching of flexbox directional layout

I'm attempting to create a unique layout using only css3. My challenge is to achieve this without explicitly setting sizes, allowing the layout to flow freely. I am utilizing flexbox in my css for this purpose. After an automatic wrap, I want the layo ...

Customize the color of Bootstrap active tabs to have a unique and distinct hue for each tab

I have successfully managed to change the color of both active and non-active tabs as a group, but I am wondering if it is possible for the color to remain the same when moving to the next tab. Here is what I've tried: http://jsfiddle.net/pThn6/2031/ ...

Tips for vertically aligning elements within the body while excluding the navbar from being centered

On my webpage, I have a navigation bar and a lot of content. The code structure looks something like this: .container { display: flex; justify-content: center; align-items: center; } <div class="navbar"> <!-- Various links for navigation ...

What is the best way to choose the initial element in every group using jQuery?

If we have the following HTML input: <p>A</p> <p>B</p> <p>C</p> <div>D</div> <p>E</p> <p>F</p> <p>G</p> We can select B, C, F and G using the following code: $('p + ...

Converting an HTML table into an Excel spreadsheet

In the process of developing an application that populates a table based on a JSON dataset, I am seeking a way to store the filtered data into an Excel file or even a CSV. The structure includes two script files - app.js and mainController.js (organized fo ...

Prevent Cordova from shrinking the view when focusing on an input

Currently working on developing an app using Cordova/Phonegap (v6.5.0). Platforms where I've installed the app: - IOS (issue identified) - Android (issue identified) - Browser (no issue found) I am facing a known problem without a suitabl ...

Unable to adjust the dimensions of the image

I recently added two images to my website successfully, but I am facing an issue when attempting to modify their dimensions and shape using CSS. Despite adjusting the height, width, border-radius, etc., the images remain unchanged. Can anyone provide insig ...

Persist the checkbox value in the database and update the status of the label accordingly

I need a checkbox for user preference selection that is stored in a MySQL database for constant availability. The label should change to "Enabled" when the checkbox is selected and vice versa. Upon saving (submitting) the page, an alert message should disp ...

What are the placeholders for the "localIdentName" query tag in a custom CSS-loader for Webpack?

Just starting out with Webpack and experimenting with the css-loader. I came across the option to specify a localIdentName query tag on the Github page under "Local Scope". This tag allows us to define our own custom values for how classes should be named ...

Developing a unique attribute using AngularJS

As a beginner in AngularJS, I am experimenting with creating directives to manipulate the background-color of a <div> based on specific conditions. I aim to write code like this within my view: <div effect-color="#2D2F2A">content here</div& ...

Are the connections from a single array unique?

A generous user shared this code with me: $singles = valley_images(); $groups = valley_group_images(); $images = array_merge($singles, $groups); $sortArray = array(); foreach($images as $image){ foreach($image as $key=>$value){ if(!iss ...

The loading of the Bootstrap tagsinput has encountered an error

I am facing an issue with my Django application where tags are not loading properly in an input field using jquery. It seems like the application is unable to locate the bootstrap-tagsinput.css and bootstrap-tagsinput.js files. Can anyone provide guidance ...

Issue with jQuery scrolling functionality

Can someone explain why the site is loading in this specific part of the page? My guess is that it has something to do with jQuery. You can view the live website here: Instead of loading in the home section, the site is loading in the portfolio section. I ...

Chrome displaying elements out of alignment

Anyone have any insights into why my webpage elements are displaying differently in Chrome compared to IE? It's driving me crazy! Here's a screenshot of how it looks in Chrome: https://i.sstatic.net/xAlEi.jpg And here's how it appears in IE ...

delete the initial background color assigned to the button

The "ADD TO CART" and "save design" buttons are currently displaying as shown in the image below. I would like to make the "SAVE DESIGN" button look similar to the "ADD TO CART" button, meaning I want to remove the background-color and dot symbol. Code ...

The SQL query in my PHP code is not functioning properly with the specified value

After attempting to organize data from my SQL table by username, I realized that this is based on the user connected to the website. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewpor ...

Solution: Resolve clientY scrolling issue within an HTML document

I am facing an issue with the positioning of my context menu. When I right-click and scroll down, the menu does not maintain its regular position. Instead of appearing to the right of the mouse cursor when stationary, it moves below the cursor based on how ...

Transforming traditional HTML table layout to modern div structure

We are currently in the process of transitioning old pages from our previous website structure, which used tables, to our new layout based on DIVs. Our website is structured using a PHP include system with separate components for the header, body, and foot ...

Enable the modal window to be scrollable

I've been struggling with configuring my modal to allow scrolling when the content overflows. I attempted to add "overflow: auto" to the modal, but it didn't seem to do the trick. It's important to note that I am not utilizing Bootstrap. One ...

Are there any methods to improve the coding, align images to the center, and ensure that elements stay in their designated positions?

Is there a way to tidy up the code, center images, and keep everything in place? This is the look I desire: The main outer box should remain centered on the browser page. Pause, play, and stop buttons should float to the left of the box. My image sh ...