Is there a more optimized CSS approach for this layout?

I attempted to create this layout using only CSS (no JavaScript, etc.) within an existing HTML page (without altering the HTML code). Unfortunately, I couldn't achieve all the positions that I needed. Let's see if anyone has a better idea. Thanks in advance.

The current HTML structure is as follows (class names are used for illustration purposes):

<body-wrapper>

<logo>
</logo>

<nav>
</nav>

<content>
</content>

<footer>
</footer>

</body-wrapper>

The desired layout looks like this:

|--------------------|
| logo |             |
|------|             |
| Nav  |             |
|      |  content    |
|      |             |
|      |             |
|------|-------------|
|        footer      |
|--------------------|

Or like this:

|--------------------|
| logo |             |
|------|             |
| Nav  |             |
|      |  content    |
|      |             |
|      |             |
|      |-------------|
|      |  footer     |
|--------------------|

Note: I prefer not to make changes to the HTML structure (such as adding another div to wrap the logo and nav bar), and only adjust the CSS to achieve the desired layout. Additionally, I do not know the height of these components.

My attempt involved setting width for the Logo and Nav, and using absolute positioning (top = 0, right = 0) for the content. However, the footer ended up moving just below the Nav and overlapping with the content. Any suggestions on how to achieve the desired layout?

Thank you.

Answer №1

If you haven't already, have you experimented with using display: flex; flex-wrap: wrap; along with order:# ?

You can view an example in this jsfiddle here: http://jsfiddle.net/7d4nke5r/

This method could potentially be a straightforward solution for crafting a responsive website design.

Answer №3

Why not give this a shot?

Here is some code to get you started:

<div class="container">
  <div class="left-column">
    <header class="logo">Logo</header>
    <nav class="navigation">Navigation</nav>
  </div>
  <div class="main-content">Content area</div>
  <footer class="page-footer">Footer section</footer>
</div>

You can style it with the following CSS:

.container {
  width:100%;
  max-width:800px;
  margin: 0 auto;
}
.left-column {
  width:30%;
  float:left;
}
.logo {
  width:100%;
  height:50px;
  background:blue;
}
.navigation {
  width:100%;
  height:100px;
  background:pink;
}
.main-content {
  width:70%;
  height:auto;
  float:right;
}
.page-footer {
  clear:both;
  width:100%;
  padding:1em 0;
  background:red;
}

Take a look at the visual representation on this jsfiddle link.

I hope this information proves useful!

Answer №4

test this out

<html>
<head>
<title>Untitled Page</title>

<style type="text/css">

.body-wrapper{
width:800px;
height:400px;
border:#000 2px dashed;
}

.div_left{
height:400px;
width:200px;
border-right:#000 2px dashed;
float:left;
}

.logo{
width:200px;
height:100px;
border-bottom:#000 2px dashed;
}
.nav{
width:200px;
height:300px;
}

.div_right{
width:590px;
height:400px; 
float:right;
}
.content{
width:590px;
height:300px;
border-bottom:#000 2px dashed;

}
.footer{
width:590px;
height:100px;

}


</style>


</head>

<body>

<div class="body-wrapper">

    <div class="div_left">

        <div class="logo">
            company logo
        </div>

        <div class="nav">
            navigation menu
        </div>
    </div>

    <div class="div_right">
        <div class="content">
            main content
        </div>

        <div class="footer">
            page footer
        </div>
    </div>

    <div style="clear:both"></div>

</div>

   </body>
    </html>

result -> https://i.sstatic.net/iUWsd.png

Answer №5

Here are two different scenarios that demonstrate how to handle the positioning of elements depending on whether the img/nav should be fixed in place or not. By examining the CSS code closely, you can understand how each element's position is accounted for.

Scenario 1: Fixed Navigation Bar

html {
  height: 100%;
  box-sizing: border-box;
}
*,
*:before,
*:after {
  box-sizing: inherit;
}
body {
  position: relative;
  margin: 0;
  padding-bottom: 50px;
  min-height: 100%;
  background: #266080;
  color: #fff;
  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}
.logo {
  position: fixed;
  width: 150px;
  height: 150px;
}
.nav {
  position: fixed;
  top: 150px;
  bottom: 0;
  width: 150px;
  background: #2EA1C7;
}
.content {
  top: 0;
  margin-left: 150px;
  height: 100%;
  padding: 20px;
  line-height: 30px;
}
.footer {
  position: absolute;
  right: 0;
  bottom: 0;
  left: 150px;
  padding: 20px;
  background-color: #024D66;
  text-align: center;
}
<div class="logo">
  <img src="http://placehold.it/150x150/024D66/fff">
</div>
<nav class="nav"></nav>
<div class="content">
  <p>Lorem Ipsum text...</p>
</div>
<div class="footer">Footer</div>

Scenario 2: Non-fixed Navigation Bar

html {
  height: 100%;
  box-sizing: border-box;
}
*,
*:before,
*:after {
  box-sizing: inherit;
}
body {
  position: relative;
  margin: 0;
  padding-bottom: 50px;
  min-height: 100%;
  background: #266080;
  color: #fff;
  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}
.logo {
  position: absolute;
  width: 150px;
  height: 150px;
}
.nav {
  position: absolute;
  top: 150px;
  bottom: 0;
  width: 150px;
  background: #2EA1C7;
}
.content {
  top: 0;
  margin-left: 150px;
  height: 100%;
  padding: 20px;
  line-height: 30px;
}
.footer {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 100%;
  padding: 20px;
  background-color: #024D66;
  text-align: center;
}
<div class="logo">
  <img src="http://placehold.it/150x150/024D66/fff">
</div>
<nav class="nav"></nav>
<div class="content">
  <p>Lorem Ipsum text...</p>
</div>
<div class="footer">Footer</div>

Answer №6

*{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}

.wrapper{
    display: table;
    width: 100%;    
}
.wrapper .col{
    display: table-cell;
    vertical-align: top;
}
.wrapper .col-1{
    width: 200px;
    background: #ddd;
}
.wrapper .col-2{    
    background: #ccc;
}

.logo{
    height: 100px;
    line-height: 100px;
    background: #f7f7f7;
    color: #555;
    text-align: center;
    display: block;
}
nav,
section{
    min-height: 250px;
    padding: 15px;    
}
footer{
    min-height: 100px;
    background: #999;
    padding: 15px;
}
<div class="wrapper">
    <div class="col col-1">
        <aside>
            <a href="/" class="logo">LOGO</a>
            <nav>nav</nav>
        </aside>    
    </div>
    <div class="col col-2">
        <section>content</section>
        <footer>footer</footer>
    </div>
</div>

Link to Fiddle

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

How can I display an image next to an element when hovering over it?

Whenever I hover over a .tags element, I want to display the image next to it. Feel free to test this feature on this specific page. This is how I envision it should look when not hovering over any .tags cell (the cells in the final column all have the ...

What are the best methods for detecting devices in order to create customized CSS styles for iPad and Galaxy Tab?

Ensuring my web application is compatible with various devices is crucial. While I have created a common CSS for all mobile and tablet devices, it seems to be causing some problems specifically on the iPad. After finding a fix tailored for the iPad, I now ...

Creating a custom blockquote style for a WordPress Twenty Fifteen child theme

Currently, I am creating a new child theme for Twenty Fifteen completely from scratch. The one area I am struggling with is customizing the appearance of the blockquote using CSS. Here is an example of the custom CSS I have added to the blockquotes within ...

jQuery - Modify Turn.js to exclusively implement the page Peel effect

I am attempting to implement the peel effect from turn.js into my code, where hovering over the bottom right corner of the page causes it to peel slightly and appear as if it is bending upwards. I specifically want only the peel effect and not the entire ...

JavaScript failing to validate radio button option

There seems to be an issue with the script not properly checking the radio button during testing. <script type="text/javascript"> function checkButton(){ if(document.getElementById('Revise').checked = true){ a ...

What steps should I take to make my alert vanish?

I'm facing a challenge with an alert I created using Bootstrap in my Django project. Despite following instructions from the bootstrap website, I can't get the alert to disappear when I click on the x button. The errors I encountered are: Uncau ...

Issue with form not capturing information from dynamically generated fields

I'm working on creating a dynamic form for user input of on/off instructions in pairings. The HTML code defaults with one pair, and the user can add additional pairs using a button. However, upon form submission, Angular is only reading the values fro ...

Shading half of the progress bar

I need to fill 50% of the progress bar with a red color. This is my attempt using HTML and CSS: .progress { width:100%; display: inline-block; height: 0.4615384615384615em; padding: 0.0769230769230769em; background: #fff; border: 1px solid # ...

How to use jQuery to select an element by using 'this' on a button

I have a total of 5 divs, each containing 5 different texts. <div class="text"> <%= theComment.text %> </div> I am working with node.js, MongoDB, and Mongoose. In addition to the divs, I also have a button labeled EDIT with a cl ...

nsIStyleSheetService - The Gateway to Stylish CSS

I am currently utilizing nsIStyleSheetService within my Firefox extension to apply a custom stylesheet to every webpage. While everything is functioning properly, there are a few styles that are not being applied as expected. One of the styles causing iss ...

Although Angular allows for CSS to be added in DevTools, it seems to be ineffective when included directly in

Looking to set a maximum length for ellipsis on multiline text using CSS, I tried the following: .body { overflow: hidden; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; } However, this approach did not work. Interesti ...

Creating a static HTML page using Flask and incorporating redirects

I have a web application built using Flask that processes a URL, performs some operations, and then redirects the user to another URL. I am looking for a way to display a simple "please wait" message in a static HTML page while the processing is happenin ...

The html function does not contain the value of the textarea

While attempting to retrieve the HTML content of a div using the code below, I noticed that it does not include the text entered in the textarea or input field. var mywindow = $(printDiv).html(); The variable mywindow will contain all the HTML except fo ...

Can a form be selected and submitted using only its class as a selector?

When trying to select and submit a form using only the class as a selector, I encountered an issue: $("form.class").submit(...) doesn't seem to work... however: $("form#id").submit(...) works perfectly fine. Any idea what could be causing this di ...

Tips for resolving highlighted font awesome Facebook icon issue

Looking to create a circle around a fontawesome social media link. The circle works for most icons, but the Facebook icon is causing issues. The highlight circle does not fit correctly with the Facebook icon. How can this be resolved? .social-media span ...

display a loading spinner for number input fields in HTML5

In my HTML5 project, I am currently utilizing a numeric control (input type="number"). The default behavior displays the spinner (up and down arrows) only on hover. Is there a way to make the spinner permanently visible using CSS or another method? ...

What is the best way to adjust the size of a kendo ui combobox?

Is there a way to adjust the width of the kendo ui combobox? My current version is 2012.3.1114 I've attempted to modify it using the following CSS: #options { padding: 30px; } #options h3 { font-size: 1em; font-weight: bold; margin: 2 ...

Iterate over JSON dates in JavaScript

Trying to utilize JavaScript in looping through a JSON file containing time periods (start date/time and end date/time) to determine if the current date/time falls within any of these periods. The provided code doesn't seem to be working as expected. ...

What is the best way to eliminate the right margin from my div element?

On my contact page, there's a div with the ID of contactdetails that appears to have a margin on the right side when viewed in Chrome's developer tools. This margin is causing some layout issues, specifically preventing me from placing the Google ...

What is the best way to make twitter-bootstrap navbar subitems collapse/expand on smaller screens?

After experiencing success with the twitter-bootstrap 'hero' example, it has become evident that on small screens, the navbar menu only displays first-level items, with sub-items collapsed. However, tapping on an item with child elements expands ...