What is the best way to display a header element in front of an article element?

I'm struggling with making my header element sticky and appear in front of my article. Modifying the z-index hasn't given me the desired result so far.

Is the z-index ineffective when dealing with floating elements? Or is there a workaround to make it function as intended? Any assistance on this matter would be greatly appreciated. Thank you

h1,
h2,
h3,
h4 {
  margin: inherit;
}

.top {
  position: sticky;
  background-color: grey;
  margin: 0em;
  z-index: 1000;
  float: none;
}

.header {
  top: 0em;
}

.navigation {
  top: 2em;
}

.aside {
  width: 25%;
  height: 100%;
  float: right;
  background-color: darkgrey;
  clear: right;
}

.section {
  width: 75%;
  height: 100%;
  float: left;
  /*background-color: lightgrey;*/
}

.hidden_link {
  color: inherit;
  text-decoration: none;
}

* {
  z-index: -1;
}
<body>
  <main>
    <header class="header top">
      <h1>
        Toyota JZ Engine
      </h1>
    </header>
    <nav class="navigation top">
      <a>link</a>
    </nav>
    <article>
      <aside class="aside">
        <p><a class="hidden_link" title="Multi-valve" href="https://en.wikipedia.org/wiki/Multi-valve">24 Valves</a> means that there are 4 valves per cylinder</p>
        <p><a class="hidden_link" title="DOHC" href="https://en.wikipedia.org/wiki/Overhead_camshaft_engine#Double_overhead_camshaft_(DOHC)">DOHC</a> means that there are 2 Camshafts per bank of the sylinder head, 1 for intake and 1 for exhaust</p>
        <p>Rear Wheel Drive (RWD) is a type of drivetrain in which the gearbox sends all power to the rear wheels</p>
      </aside>
      <section class="section">
        <h2>Introduction</h2>
        <hr>
        <p>The Toyota JZ engine family is a series of inline-6 automobile engines, which are designed as a replacement for the M-series inline-6 engines. The family features 2.5- and 3.0-litre versions; all are 24-valve DOHC engines.</p>
      </section>
      <section class="section">
        <h3>1JZ</h3>
        <hr>
        <p>The 1JZ engine was produced from 1990-2007. It is 2,492 cc.</p>
        <h4>1JZ-GE</h4>
        <p>This is another common engine version which has a 10:1 compression ratio. The early 1JZ-GE, like all JZ engines, is desigined for longitudnal mounting and RWD. All such models offered only a 4-speed automatic transmission.</p>
        <h4>1JZ-GTE</h4>
        <p>The 1JZ also features a twin-turbocharged version, known as the 1JZ-GTE. It was produced from 1990-2007 as well and uses two CT12A turbochargers which sit parrallel and blow through a side or front mount intercooler. It has an 8:5:1 static compression
          ratio. Early generation 1JZ-GTEs combined the inherent smoothness of an inline 6 with the revving capacity of its short stroke and early power delivery of its turbochargers.</p>
        <figure>
          <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/de/1JZ-GTE_in_a_1991_Toyota_Mark_II_2.5GT_Twin_Turbo.jpg/250px-1JZ-GTE_in_a_1991_Toyota_Mark_II_2.5GT_Twin_Turbo.jpg" title="1JZ-GTE">
          <figcaption>1JZ-GTE in a 1991 Toyota Mark II 2.5GT Twin Turbo</figcaption>
        </figure>
        <figure>
          <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/74/1JZ-GTE_VVT-i_engine_in_1989_Toyota_Cressida.jpg/250px-1JZ-GTE_VVT-i_engine_in_1989_Toyota_Cressida.jpg" title="1JZ-GTE">
          <figcaption>Third Generation 1JZ-GTE VVT-i transplanted into a 1989 MX83 Toyota Cressida</figcaption>
        </figure>
        <h4>1JZ-FSE</h4>
        <p>1JZ-FSE is likely the least known engine of the JZ family. Their goal is to acheive minimal emissions and fuel consumption with no performance loss. It employs the same block as the conventional 1JZ-GE but the cylinder head has a relatively narrow
          angle with swirl conrol valves. Fuel consumpton is reduced by about 20%.</p>
        <h3>2JZ</h3>
        <hr>
        <p>The 2JZ engine was produced from 1991-2007. It is 2,997 cc. It is not merely a stroked version of the 1JZ, but it has longer connectiong rods and a taller block deck.</p>
        <h4>2JZ-GE</h4>
        <p>The 2JZ-GE is a common version with Sequential Electronic Fuel Injection, an auminium head, and 4 valves per cylinder, in addition to a cast-iron cylinder block.</p>
        <h4>2JZ-GTE</h4>
      </section>
    </article>
    <footer>

    </footer>
  </main>
</body>

Answer №1

There are a few potential errors to address, including one related to the arrangement of elements and another involving floating elements and block formatting contexts.

  • The header, main, and footer should ideally be siblings, although headers and footers can also exist within main.

  • Floating elements may overflow their containers if a block formatting context is not created (refer to the link below for more information).

One possible solution is to separate the header and footer from the main element (the nav could either be moved to the header or remain in place).

To establish a block formatting context (BFC) for the main element, consider using properties like overflow:hidden or display:grid. You can learn more about this concept here: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flow_Layout/Intro_to_formatting_contexts

h1,
h2,
h3,
h4 {
  margin: inherit;
}

main {
  overflow: hidden;/* wraps around and aside floats see BFC*/
}

top {
  position: sticky;
  background-color: grey;
  margin: 0em;
  z-index: 1000;
  float: none;
}

.header {
  top: 0em;
}

navigation {
  top: 2em;
}

aside {
  width: 25%;
  height: 100%;
  float: right;
  background-color: darkgrey;
  clear: right;
}

.section {
  width: 75%;
  height: 100%;
  float: left;
  /*background-color: lightgrey;*/
}

hidden_link {
  color: inherit;
  text-decoration: none;
}


/* * {
  z-index: -1;
}*/
<body>
  <header class="header top">
    <h1>
      Toyota JZ Engine
    </h1>
  </header>
  <nav class="navigation top">
    <a>link</a>
  </nav>
  <main>
    <article>
      <aside class="aside">
        <p><a class="hidden_link" title="Multi-valve" href="https://en.wikipedia.org/wiki/Multi-valve">24 Valves</a> means that there are 4 valves per cylinder</p>
        <p><a class="hidden_link" title="DOHC" href="https://en.wikipedia.org/wiki/Overhead_camshaft_engine#Double_overhead_camshaft_(DOHC)">DOHC</a> means that there are 2 Camshafts per bank of the sylinder head, 1 for intake and 1 for exhaust</p>
        <p>Rear Wheel Drive (RWD) is a type of drivetrain in which the gearbox sends all power to the rear wheels</p>
      </aside>
      <section class="section">
        <h2>Introduction</h2>
        <hr>
        <p>The Toyota JZ engine family is a series of inline-6 automobile engines, which are designed as a replacement for the M-series inline-6 engines. The family features 2.5- and 3.0-litre versions; all are 24-valve DOHC engines.</p>
      </section>
      <section class="section">
        <h3>1JZ</h3>
        <hr>
        <p>The 1JZ engine was produced from 1990-2007. It is 2,492 cc.</p>
        <h4>1JZ-GE</h4>
        <p>This is another common engine version which has a 10:1 compression ratio. The early 1JZ-GE, like all JZ engines, is desigined for longitudnal mounting and RWD. All such models offered only a 4-speed automatic transmission.</p>
        <h4>1JZ-GTE</h4>
        <p>The 1JZ also features a twin-turbocharged version, known as the 1JZ-GTE. It was produced from 1990-2007 as well and uses two CT12A turbochargers which sit parrallel and blow through a side or front mount intercooler. It has an 8:5:1 static compression
          ratio. Early generation 1JZ-GTEs combined the inherent smoothness of an inline 6 with the revving capacity of its short stroke and early power delivery of its turbochargers.</p>
        <figure>
          <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/de/1JZ-GTE_in_a_1991_Toyota_Mark_II_2.5GT_Twin_Turbo.jpg/250px-1JZ-GTE_in_a_1991_Toyota_Mark_II_2.5GT_Twin_Turbo.jpg" title="1JZ-GTE">
          <figcaption>1JZ-GTE in a 1991 Toyota Mark II 2.5GT Twin Turbo</figcaption>
        </figure>
        <figure>
          <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/74/1JZ-GTE_VVT-i_engine_in_1989_Toyota_Cressida.jpg/250px-1JZ-GTE_VVT-i_engine_in_1989_Toyota_Cressida.jpg" title="1JZ-GTE">
          <figcaption>Third Generation 1JZ-GTE VVT-i transplanted into a 1989 MX83 Toyota Cressida</figcaption>
        </figure>
        <h4>1JZ-FSE</h4>
        <p>1JZ-FSE is likely the least known engine of the JZ family. Their goal is to acheive minimal emissions and fuel consumption with no performance loss. It employs the same block as the conventional 1JZ-GE but the cylinder head has a relatively narrow
          angle with swirl conrol valves. Fuel consumpton is reduced by about 20%.</p>
        <h3>2JZ</h3>
        <hr>
        <p>The 2JZ engine was produced from 1991-2007. It is 2,997 cc. It is not merely a stroked version of the 1JZ, but it has longer connectiong rods and a taller block deck.</p>
        <h4>2JZ-GE</h4>
        <p>The 2JZ-GE is a common version with Sequential Electronic Fuel Injection, an auminium head, and 4 valves per cylinder, in addition to a cast-iron cylinder block.</p>
        <h4>2JZ-GTE</h4>
      </section>
    </article>
  </main>
  <footer>
    footer
  </footer>
</body>

When creating layouts, consider utilizing modern tools like flex or grid for easier and more efficient design without potential side effects associated with floats. Check out this example featuring a sticky header, nav, and footer: https://jsfiddle.net/6r1o0sug/

Answer №2

When designing a navigation bar, it is recommended to use position fixed and encapsulate all elements within a div:

    <body>
    <main>
      <!-- Combine the header and navigation in one div -->
      <div class="navbar">
        <header class="header">
          <h1>Toyota JZ Engine</h1>
        </header>
        <nav class="navigation">
          <a>link</a>
        </nav>
      </div>

      <article>
        <aside class="aside">
          <p>
            <a
              class="hidden_link"
              title="Multi-valve"
              href="https://en.wikipedia.org/wiki/Multi-valve"
              >24 Valves</a
            >
            means that there are 4 valves per cylinder
          </p>
          <p>
            <a
              class="hidden_link"
              title="DOHC"
              href="https://en.wikipedia.org/wiki/Overhead_camshaft_engine#Double_overhead_camshaft_(DOHC)"
              >DOHC</a
            >
            means that there are 2 Camshafts per bank of the sylinder head, 1
            for intake and 1 for exhaust
          </p>
          <p>
            Rear Wheel Drive (RWD) is a type of drivetrain in which the gearbox
            sends all power to the rear wheels
          </p>
        </aside>
        <section class="section">
          <h2>Introduction</h2>
          <hr />
          <p>
            The Toyota JZ engine family is a series of inline-6 automobile
            engines, which are designed as a replacement for the M-series
            inline-6 engines. The family features 2.5- and 3.0-litre versions;
            all are 24-valve DOHC engines.
          </p>
        </section>
        <section class="section">
          ...
     

I have organized all elements under one container within the top bar.

Css:

body {
  margin: 0;
}
h1,
h2,
h3,
h4 {
  margin: inherit;
}

.navbar {
  /* Use position fixed, z-index, set width to maximum */
  position: fixed;
  z-index: 2;
  width: 100%;
  margin: 0;
  background-color: grey;
  float: none;
}

article {
  /* Add padding to content so navbar doesn't overlap */
  padding-top: 3.45rem;
}

.aside {
  width: 25%;
  height: 100%;
  float: right;
  background-color: darkgrey;
  clear: right;
}

.section {
  width: 75%;
  height: 100%;
  float: left;
  /*background-color: lightgrey;*/
}

.hidden_link {
  color: inherit;
  text-decoration: none;
}

I've also adjusted the body margin to ensure proper alignment of all components.

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

Trouble with the Bootstrap navbar loading correctly

Recently I started using Bootstrap and decided to implement one of their templates. Everything is working fine, except for my navbar which is not loading correctly. It should look like this: Desired Navbar However, in both Chrome and Firefox, it appears l ...

Working with both Javascript and jQuery code within a single HTML file

I recently created a website and on one of the pages, I added some jQuery elements like a start button and progress bar. However, when I tried to implement a JavaScript timer on the same page by adding the script into the header, the jQuery elements stoppe ...

What could be causing the issue preventing me from updating my SQL database through AJAX?

$(document).ready(function(){ $('.button').click(function(){ var clickBtnValue = $(this).val(); var ajaxurl = 'functions/delivered.php', data = {'action': clickBtnValue}; $.post(ajaxurl, da ...

Prevent incorrect data input by users - Enhancing JavaScript IP address validation

I have been trying to create a masked input field for entering IP addresses, but every solution I come across seems to have some flaws. The best solution I found so far is , but it still allows values higher than 255 to be entered. It works fine initially ...

I need to access the link_id value from this specific actionid and then execute the corresponding function within the Ionic framework

I have a JavaScript code in my TypeScript file. It retrieves the attribute from a span element when it is clicked. I want to store this attribute's value in a TypeScript variable and then call a TypeScript function. Take a look at my ngOnInit method, ...

CRUD operations are essential in web development, but I am encountering difficulty when trying to insert data using Express

I am currently attempting to add a new category into the database by following the guidelines provided in a course I'm taking. However, I am encountering difficulties as the create method does not seem to work as expected. When I try it out in Postman ...

Navigating between pages using the ExpressJS and Angular 1 routing system

Can someone help me troubleshoot an issue I'm having with my Express API and Angular front-end? Whenever I try to access the /about route, it keeps defaulting back to index.html and displaying a 404 error message. Can you take a look at my code and pi ...

Iframe Loading at Lightning Speed

I have set up a script to load my iframe, but I noticed that the script loads the iframe content too quickly. Is there a way for me to configure it to preload all CSS images and content in the background before showing the full iframe content? Here is my ...

Adjust the CSS styling to ensure that the div or iframe expands to fit the entire height of the

Having trouble making a map embedded via an iframe on a WordPress page display responsively at full height independent of the device? Check out: . To clarify, by full height, I mean that the bottom of the map should not extend beyond the screen, eliminati ...

Using Jest to mock a single function within a class

I'm a beginner when it comes to node and javascript, and I am currently attempting to create a unit test using jest where I only need to mock one function of a class (and object). Below is the code template I am using: // myModule.js class MyModule ...

Refreshin the attached DOM to a directive without a page reload

Within a directive, I have implemented some text and a video tag in the code below: app.directive('ngAzuremediaplayer', function () { return { restrict: 'AE', priority: 10, link: function (scope, elem, attr ...

The ajax code is failing to retrieve the data from the table and populate it in the

hi guys i have an issue on ajax. where ajax can't work to take the data of second rows. This's code in model function getdtbarang($barcode = FALSE) { if ($barcode === FALSE) { $query = $this->db1->get(&a ...

Utilize a single function to toggle the visibility of passwords for multiple fields upon clicking

Is there a way to optimize the function used to hide passwords for multiple fields when clicked? Instead of having separate functions for each field, I would like to have one function that is only active on the clicked button. For example, if the toggle ...

Achieving a sleek line effect with a gradient border

Is there a way to make the gradient border transition between two colors look like a straight line? Additionally, is it possible to have the line start in the bottom left corner instead of the middle of the right side of the button? The current CSS code I ...

Issue with sending data from JQuery Ajax to PHP codeExplanation:The problem

Here is the output from myscript.js: [{"orcamento":"10","atual":"20","desvio":"","data":"2015-01-01","nome_conta":"BBB","nome_categoria":"abc","nome_entidade":"def"}] This is the content of myscript.js: if (addList.length) { $.ajax($.extend({}, ajax ...

Display the text from a function in an imported component using React

Attempting to break down HTML chunks into smaller pieces stored in the components folder. (Note: the HTML is actually written in JSX). The goal is to import the [Navigation] component and have it display its content. I am aware that there are tools avail ...

Creating a formatted JSON string from the data retrieved using a GET request and embedding it into an HTML template to be sent as the

Struggling to send JSON data retrieved from a GET METHOD as an email. The challenge is defining the body of the email using the obtained JSON object. Looking for solutions! Below is a snippet of my code: app.get('/userinfo',(req,res)=>{ ...

Attempting to retrieve dynamically generated input fields and cross-reference them with structured length .json data

In the process of developing code, I've implemented a for loop to dynamically generate HTML input fields based on the length of some data in a .json file. My goal is to use jQuery to compare the text entered in each input field with the corresponding ...

Rearranging components in React does not automatically prompt a re-render of the page

I'm currently working on a project to display the update status of my Heroku apps. The issue I encountered was that the parent component (Herokus) was determining the order, but I wanted them sorted based on their update dates starting from the most r ...

Is it possible to view the CSS of a PDF file in print template mode using Chrome?

https://i.stack.imgur.com/XlcWm.png Currently facing an issue with debugging CSS on my PDF template. I am unable to inspect the styles directly in the CSS due to limitations. When using inspect element in Chrome, only the styles for text and elements are ...