Achieving a full-width navigation element allows for other elements to be positioned below it, rather than directly beside it

Apologies for my not-so-great English.

To better understand my issue, please refer to this fiddle and this fiddle.

Focus on the <nav></nav> section. If there are only a few elements in the menu, the next block <section></section> will be positioned right after the menu. However, if there are many elements in the menu, everything looks fine because the nav has sufficient width.

How can I achieve the following:

Additionally, the nav should always be 100% width regardless of the number of elements.

HTML <body> snippet:

<body>
    <div class="wrapper">
        <header>
            <center>
                <!-- logo and headers here -->
            </center>
        </header>
        <nav>
            <!-- menu here -->
        </nav>
        <section>
            <header><h1>Lorem ipsum</h1></header>
             <!-- main content here -->
        </section>
        <aside>
            <section>
                <header>
                    <h1>Lorem ipsum</h1>
                    <!-- block content here -->
                </header>
            </section>
        </aside>

        <footer>
        </footer>
    </div>
</body>

CSS snippet:

body {
    color: #8f8f8f;
    background-color: #f8f8f8; 
    background: url(../images/background.jpg) 50% 50% no-repeat #f8f8f8;
    /* border: 5px solid #7e7e7e;*/
    margin: 0;
}

.wrapper {
    max-width: 960px;
    margin: auto;
    border: 1px solid #7e7e7e;
}

header {
    padding: 20px 0;
    margin: auto;
}

aside, section {
    margin-top: 10px;
}

section {
    float: left;
    width: 700px;
    padding-bottom: 50px;
    border: 5px solid #ccc;
}

aside {
    float: right;
    width: 240px;
    border: 1px solid #ccc;
}

aside section
{
    width: 230px;
    margin-top: 0px;
}

footer {
    clear: both;
}

h1, h2, h3, h4, h5, h6 {
    margin: 0px;
    padding: 0px;
}

section header {
    background-color: #CCCCCC;
    padding: 5px 0;
    margin: 0px;
}

section header h1 {
    padding-left: 20px;
}

section p {
    padding: 10px 20px;
    margin: 0px;
}

/* CSS for nav */

Answer №1

The navigation bar is showing up on the left side due to the fact that the floating list items have not been cleared. To fix this issue, simply add the following style to your code:

nav:after {clear:both; display:block; content:'';}

Check out this example for reference.

Answer №2

Ensuring proper layout with floated elements is key in web design. One handy trick I always incorporate is adding a .clearfix css class to my reset stylesheet. This class helps clear out floated regions when needed, giving containers with only floated content back their height.

To implement this in your stylesheet, use the following CSS:

.clearfix:before,
.clearfix:after {
    content: " ";
    display: table;
}

.clearfix:after {
    clear: both;
}

.clearfix{
    *zoom:1;
}

Next, be sure to add class="clearfix" to your <nav> element or any <ul class="nav"> in your code.

For a visual example, check out this JSfiddle: http://jsfiddle.net/Abc123/1/

This clearfix solution is based on the micro clearfix hack, which you can read more about here: http://example.com/micro-clearfix-hack/

Answer №3

To achieve the desired layout, set the navigation to float: left and the pink section to clear: left (Alternatively, you can set the navigation to clear: right for the same effect).

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

default radio option with pre-selected value

Below is a radio option list, but I'm having trouble getting the desired default selection on first render: <label class="radio normalFontWeight"> <input type="radio" name="Criteria" data-ng-value="EVERYONE_REVIEWED" data-ng- ...

Building a custom dialog box using Angular Material with HTML and CSS

I've been exploring different approaches to create a customized angular material dialog with a unique header using CSS/SCSS and the TailwindCSS framework. I am aiming for a specific visual effect, similar to what is shown in the figure below. desired ...

Enable the Angular button only when the radio button has been selected

Just starting out with Angular and I have a query. This is a scenario for my project at work. https://i.stack.imgur.com/R3SxA.png In this screenshot, I want to enable the "ajouter" button when at least one radio button is selected in the secure chest (s ...

My CSS seems to be causing an issue and preventing the function from running correctly. Now I just need to

I'm currently working on a project and following a tutorial to help me create a navigation bar. The tutorial I am using can be found here: https://www.youtube.com/watch?v=gXkqy0b4M5g. So far, I have only reached the 22:05 mark in the video. I have su ...

Enhance your website with footer animations using jQuery!

I am implementing a sticky footer on my website to keep the footer at the bottom using CSS. I want the footer to initially be collapsed and expand when the user clicks on a button, swapping the "expand" link with a different container div inside the footer ...

Utilizing a hidden file-input, IE10 is able to transmit a FormData object via ajax

I'm encountering difficulties when trying to send a form that includes a display:none file-input via ajax. It works fine in Chrome (46.0.2490.71), but not in IE10 (10.0.9200.17492). I've tried various solutions without success, and it's cruc ...

Updating the jQuery mobile webpage

I have set up a small demo with two pages - the Home page and the Team 1 page. By clicking on the navigation menu, you can navigate to the Team 1 page from the Home page. However, if you are already on the Team 1 page and click on the Team 1 button again ...

Is there a way to automatically redirect my page after clicking the submit button?

I'm having trouble with the code below. I want it to redirect to NHGSignin.php if 'new horizon gurukul' is entered. When I click the Next button, it's supposed to take me there but it's not working as expected. Can anyone help me f ...

Creating a unique look for unordered list items in a dropdown navigation

I'm currently working on creating a drop-down menu using nested unordered lists. The functionality of the menu is all set, but I'm running into some styling issues. The main link that triggers the drop-down should have a blue background with whit ...

Error encountered in angular.forEach syntax

Can someone help me figure out why my array sum calculation using angular.foreach is resulting in a "syntax error" message? I believe my syntax is correct, as indicated by var sum += value.fare;. Any insights on why this error occurs would be appreciated. ...

Resetting CSS attributes in Kendo UI Mobile when transitioning between views after removing them with jQuery

Dealing with the flicker of unstyled content in my kendo mobile web app has been a challenge, but I found a solution by using a specific CSS rule within my stylesheet: [data-role="content"] { visibility: hidden; } This effectively hides all ...

The vertical tabs in JQueryUI lost their functionality when a few seemingly unrelated CSS styles were added

Check out the JsFiddle demo here I embarked on a mission to craft JQueryUI Vertical tabs by following the guidance provided in this example. The source code within the aforementioned link contains specific CSS styles: .ui-tabs-vertical { width: 55em; } ...

Exploring an unusual HTML structure using Python's Beautiful Soup and urllib for web scraping

While extracting data may not be a challenge, the real issue lies in locating it. My focus is on scraping football data from a website that presents statistics either for all years or for specific seasons. However, despite selecting a particular season, ...

Using jquery to refresh several backgrounds at once

While I have some knowledge of CSS and various JavaScript libraries, I've encountered difficulty in integrating multiple backgrounds into the code that I found on this link: http://codepen.io/quasimondo/pen/lDdrF. My goal was to overlay a transparent ...

Button within the Magnific Popup (do not use to close)

I am currently developing a website with a gallery page that utilizes the Magnific Popup plugin. My client has provided lengthy 'captions' for each image, almost like short stories. To display these captions effectively, I have utilized the titl ...

What are the negative effects of placing an external CSS stylesheet link outside of the <head>

At the outset, it is well-known that the <link> tag used to connect an external CSS style sheet should ideally be placed within the <head> section of an HTML document. It is considered unconventional to place it elsewhere. However, due to my u ...

CSS might not always work properly on all web browsers, but it functions perfectly on Eclipse

When developing JSF pages with stylesheets, everything seems to be working fine in the Eclipse preview function. However, when I test the pages on IE8, the styles do not seem to have any effect. I am utilizing composite views to define a general layout fo ...

Parallax scrolling and Swiper slider, a match made in digital design

Currently facing an issue with Swiper Slider and Simple Parallax Scrolling. Whenever I switch slides, the same image is displayed repeatedly. I suspect the problem lies in the fixed position of the image within the parallax effect. Attempted to resolve b ...

Generating an Xpath for the selected element with the help of Javascript or Jquery - here's how!

On my website, I have implemented a click event on an element. When a user clicks on this element, I want to generate the XPath of that particular element starting from either the html or body tag, also known as the Absolute Xpath. For example, if I click ...

Issue with calculating positions in the jquery.quicksand plugin

I've been playing around with a website for the past couple of days and I have successfully implemented the jquery.quicksand plugin to organize my portfolio entries. However, I've run into an issue when selecting the 'all' filter. If a ...