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

"Transform the appearance of the datepicker input field with Material 15's dynamic

I am in need of assistance to change the color to white for the input date and add an underline to a datepicker element <mat-form-field class="date-criteria-select " [floatLabel]="'always'"> <mat-label class=" ...

When utilizing CKEDITOR, the default TEXTAREA is obscured, and CKEDITOR does not appear

I am trying to incorporate CKEDITOR into my project. I have added the ckeditor script in the footer and replaced all instances of it. <script src="<?= site_url('theme/black/assets/plugins/ckeditor/ckeditor.min.js') ?>" type="text/javasc ...

Leveraging the Power of jsPDF in Your Google Apps Scripts

Is it feasible to integrate jsPDF into a Google Apps Script project? I am attempting to create documents using templated HTML, but certain CSS styles like background-color do not translate to PDF when using the getAs() function. Given the limitations of ...

What is the best way to showcase search outcomes using ajax in a Django project?

In my current Django project, I am developing a feature that allows users to search for a name using a form. The view will then search for that name in the database after some transformation and display the results below the form. Currently, the entire pa ...

JavaScript: Share module contents internally through exporting

When working with Java, there are 4 different visibility levels to consider. In addition to the commonly known public and private levels, there is also the protected level and what is referred to as the "default" or "package-local" level. Modifier Clas ...

Having trouble getting an HTML form to function with Ajax and PHP?

Seeking assistance from anyone who can lend a hand. I am delving into the complexities of Ajax, and I'm encountering issues where it seems like the script is being completely ignored or perhaps I'm just making a rookie mistake. Prior to display ...

The second AJAX request isn't functioning properly; it should only be executed once the first one has successfully completed

I am experiencing an issue with my ajax calls. The second call should only be triggered after the first one is successful. However, even though the first call always returns success, I keep getting an error for the second one. function proceedWithUnlock(t ...

Using the set() method in Firestore with the merge option does not function properly when implemented in Node.js

const user = {name : myUsername}; databaseRef.set(user, { merge: true }); An error is occurring which states: Invalid use of type "undefined" as a Firestore argument. Despite following the Firebase documentation here, and seeing others use it in online ...

Override the default HTML style overflow to hidden with Material UI Swipable Drawer

Currently, I'm utilizing the Material UI SwipableDrawer component which has an unexpected drawback of causing the scrollbar to disappear when the drawer is displayed. This issue overrides my specified style of "overflow-y: scroll" on the Html tag and ...

Tips for displaying all 'ul li' lists in media queries to ensure responsiveness in design

When I view my navigation bar snippet on desktop mode, it displays all the list items. However, when I adjust the browser width to fit my media queries, only the Home list is shown in the nav bar. Here's an example of the issue: Before Clicking the ...

Executing two distinct SQL queries within one nodejs function

I'm facing an issue with updating two tables in my database - the stockmaster table and the prodstock table. I've been trying to run a query using a function to update both tables simultaneously, but unfortunately, it's not working as expect ...

How is it possible that this component is able to work with slotting without needing to specify the slot name

I have created two Web Components using Stencil.js: a Dropdown and a Card. Within my Dropdown, the structure is as follows: <div class='dropdown'> <slot name="button"/> <slot/> </div> The nested chil ...

Applying background color within an *ngFor loop

My question may not be perfectly described by the title, but here it is. In my Angular Material2 project, I have an md-toolbar where I am dynamically looping through values: <md-toolbar (click)="selectedToolbarValue(key.Name)" *ngFor="let key of array ...

How can I execute a Python script on an HTML webpage by clicking a button?

Below is the HTML code I have written: <script> function goPython(){ $.ajax({ url: "MYSCRIPT.py", context: document.body }).done(function() { alert('finished python script');; ...

Tips for transforming JSO into JSON data format

Here is an example: var info = [{"name":"john","age":"30"},{"name":"smith","age":"28"}] I am looking to convert the above json object to a format like this result: [{name:"john",age:30},{name:"smith",age:28}] Any suggestions on how to achieve this? ...

Having difficulty with Angular's ng-options feature in a Select element

I'm currently tackling an issue with using ng-options to iterate through an array of objects and display specific properties in a select element. Upon querying the api/admin endpoint, I receive JSON data containing details of all users holding the ad ...

I'm encountering some issues trying to install next-auth in my project built with Next.js and React

I recently set up my Next.js project with React using yarn create next-app. However, I am facing an issue as the next-auth package is not installed in my project. Currently, my node version is LTS 16.15.1, yarn version is 1.22.18, and npm version is 8.9. ...

React encountered an error: Unable to destructure the property 'id' of '_ref' as it has been defined

After spending several hours trying to solve this issue, I am completely stuck. The console shows that the patch request successfully updates the information, but then my map function breaks, leading to a blank page rendering. Here is the problematic comp ...

Cloning Div repeatedly instead of the specified quantity

My JSON data contains four sections and I want to clone my html div based on the number of sections. For example, if there are 100 sections in my JSON, I would like the div to be cloned 100 times. Currently, my div is getting cloned with the JSON data app ...

Flow object with Angular using ng-flow: Existing flow object

Just a quick question that I can't seem to find an answer for on my own or through searching online. Currently, I have a button set up for clicking and uploading files. However, I also want to incorporate drag and drop functionality using the same fra ...