Flexbox and CSS3 width/height declarations causing problems with vertical alignment

Struggling to vertically center a nav element and align it to the right of the screen? There seems to be something causing it to shift slightly off-center.

In this setup, the red represents the flex container, the green is the header, and the grey box is the right-aligned, vertically centered box that should truly be centered.

The issue of it jumping down is puzzling me.

For a clearer visual representation, check out this JSFIDDLE. Essentially, it's about having a fixed flex container overlaying a scrolling page.

<div class="navWrap"><!-- Place less important HTML after main content (My brain hurts!)-->

            <header>

                <nav>
                </nav>

            </header>

            <nav class="pagesNav next">

                <span class="arrow"></span>
                <span class="arrow"></span>

            </nav>

        </div>

CSS:

.navWrap {
    display: -webkit-box;      /* OLD - iOS 6-, Safari 3.1-6 */
    display: -moz-box;         /* OLD - Firefox 19- */
    display: -ms-flexbox;      /* TWEENER - IE 10 */
    display: -webkit-flex;     /* NEW - Chrome */
    display: flex;

    -webkit-justify-content: center;
    -ms-flex-pack: center;
    justify-content: center;

    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center; 

    -webkit-flex-wrap: wrap;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap; 

    position: fixed;
    top: 0;
    width: 100%;
    height: 100vh; 

    background: red;
}

header {
    -webkit-align-self: flex-start;
    -ms-flex-item-align: start;
    align-self: flex-start;
    width: 100%;
    height: 70px;
    background: green;
}

nav:not(.pagesNav) {
}

.pagesNav { 
    margin-left: auto;

    width: 80px;
    height: 155px;

    background: #414141;
}

Answer №1

Your header placement needs adjustment, consider the following:

<div class="pageWrap"><!-- Primary content should be placed first for SEO purposes. Rearrange secondary elements using CSS -->


          <header>

                <nav>
                </nav>

        </header>
     <main role="main">

                <section></section>
                <section></section>
                <section></section>
                <section></section>

            </main>

            <footer>
            </footer>

        </div>

        <div class="navWrap"><!-- Place less important HTML after main content to enhance user experience -->


            <nav class="pagesNav next">

                <span class="arrow"></span>
                <span class="arrow"></span>

            </nav>

        </div>

css

    html {
    color: #222;
    font-size: 1em;
    line-height: 1.4;
}

html, body {
  height: 100%;
}

body {

}

/* Temporary styles */

.pageWrap {

}

main {
    max-width: 1920px;
    height: 100%;
    margin: 0 auto;
}

section {
    height: 100vh;
    width: 100%;
    background: white;
}

section:first-child {
    height: 95vh;
}

section:nth-child(even) {
    background: yellow;
}

footer {
    height: 300px;
    background: red;
}

/* Less important information */
header{
    display: -webkit-box;      /* OLD - iOS 6-, Safari 3.1-6 */
    display: -moz-box;         /* OLD - Firefox 19- */
    display: -ms-flexbox;      /* TWEENER - IE 10 */
    display: -webkit-flex;     /* NEW - Chrome */
    display: flex;

    -webkit-justify-content: center;
    -ms-flex-pack: center;
    justify-content: center;

    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center; 

    -webkit-flex-wrap: wrap;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap; 

    position: fixed;
    top: 0;
    width: 100%;
    height: 100vh;   

}
.navWrap {
    display: -webkit-box;      /* OLD - iOS 6-, Safari 3.1-6 */
    display: -moz-box;         /* OLD - Firefox 19- */
    display: -ms-flexbox;      /* TWEENER - IE 10 */
    display: -webkit-flex;     /* NEW - Chrome */
    display: flex;

    -webkit-justify-content: center;
    -ms-flex-pack: center;
    justify-content: center;

    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center; 

    -webkit-flex-wrap: wrap;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap; 

    position: fixed;
    top: 0;
    width: 100%;
    height: 100vh; 
}

header {
    -webkit-align-self: flex-start;
    -ms-flex-item-align: start;
    align-self: flex-start;
    width: 100%;
    height: 70px;
    background: green;
}

nav:not(.pagesNav) {
}

.pagesNav { 
    margin-left: auto;

    width: 80px;
    height: 155px;

    background: #414141;
}

Fiddle link: https://jsfiddle.net/L4u9t78m/2/

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 to successfully pass a Bootstrap dropdown value back to Flask

I need assistance with extracting a selected value (player) from a dropdown menu. The goal is to send this 'player' value to Flask upon selection, and then render the corresponding player's image on the website. HTML: <form method="P ...

I'm struggling to make my div display inline

My div elements are not displaying inline, I'm thinking maybe I shouldn't use the nav and div structure like this. But starting over again seems like a lot of work. How can I fix this issue? Just so you know, I'm only on my 4th day of learn ...

Utilizing HTML list elements in conjunction with a switch statement, and connecting the list directly to a database for

Hey there, I'm interested in learning how to access HTML elements and use them within switch statements. <div id="hori"> <ul> <li><a href="#">Aerospace</a></li> <li><a href="#">Automotive</a>< ...

Is it possible to track keystrokes without the use of text input fields?

For various unimportant reasons, I need to use JavaScript to create links instead of using the anchor tag. I want the behavior to mimic the anchor tag, allowing users to open a link in a new tab when holding down the Ctrl key or in a new window when holdin ...

Whenever I press a button, my card holder gradually slides downwards

I'm facing an issue with my code and I'm unsure whether it's related to CSS or JavaScript. Whenever I click the button on my page, my card-container2 moves downward unexpectedly. Here is the snippet of my code: HTML: <div class="car ...

Obtaining an attribute of a row in a table using Selenium and Python

I've been trying to figure out how to check if this button (image) has been clicked: https://i.sstatic.net/Nc3Pe.png It's crucial for me to interact with this button, as it allows me to modify the price of certain items and protect them when ne ...

Is there a method to retrieve all Class elements without using a for-loop?

I am trying to retrieve all elements with a specific class using document.getElementsByClassName(); <body> <div class="circle" id="red"></div> <div class="circle" id="blue"></div> <div class="circle" id="yell ...

What is the best way to fill in predetermined HTML with information using jQuery?

Currently, I am working with several ajax forms that allow me to add rows of data dynamically using ajax. While the form submission is functioning correctly, I am having trouble figuring out the best way to append the newly inserted data into the database. ...

When running `node compile.js`, no combined CSS file is being created

I have finally mastered the art of setting up and executing the "node compile.js" file in the claro theme folder to compile each *.less file into its corresponding *.css file. However, I noticed that the old claro.css file remains unchanged. Is this the in ...

Creating unique CSS class and ID names dynamically in PHP

I have two divs with randomly generated IDs and I would like to use those IDs in my CSS instead of using inline styles. Is there a way to achieve this without creating a separate PHP file with a CSS header? For example, I want the padding for the ID $id_ ...

Align the body of the table with the header after populating the table with values

Issue : The data in the table body is misaligned with the headers of each column. Solution : var test_insert1 = '<tr>' + '<td class="td-trad-9">762261</td>' + '<td class="td-trad-7">1.16176&l ...

JavaScript slice() method displaying the wrong number of cards when clicked

In my code, I have a section called .registryShowcase. This section is designed to display logos and initially shows 8 of them. Upon clicking a button, it should load the next set of 8 logos until there are no more left to load (at which point the button ...

Having trouble with a basic Div/CSS layout? Not turning out how you expected

While experimenting with CSS, I encountered a challenge in creating a simple horizontal stack of words. It seems impossible to align the content of one div in the center while the other div is empty. For example, I tried doing this in Chrome and here is t ...

Place a picture and words within a submit button

I need assistance with aligning text and a small airplane image on a submit button. Currently, the text is overlapping the image and I am unsure how to fix this issue. How can I adjust the position of the text to display to the right of the button? Is ther ...

Navigation bar at the bottom using Twitter Bootstrap 3 that includes a combination of full-width and regular containers

I've been tasked with recreating the designer's layout using Bootstrap. The desired layout consists of two full-width bars and a fixed container for the main content and footer menu. I have successfully implemented this layout, including respons ...

The new Facebook login button in my app seems to be glitchy as it fails to redirect users to the appropriate page after

I am working on a web application and have successfully integrated Facebook login from developers.facebook.com. However, I am facing an issue where after the user logs in with their Facebook credentials, they are not redirected to my application. Additiona ...

Concealed Separator for Text Elements in HTML

I am in search of a method to distinguish certain specific strings within HTML code. Although I can recognize the desired strings, they may also appear as part of longer strings throughout the document. In order to locate them, I currently insert a special ...

What is the best way to add randomness to the background colors of mapped elements?

I am looking for a way to randomly change the background color of each element However, when I try to implement it in the code below, the background color ends up being transparent: { modules.map((module, index) => ( <div className='carou ...

Modifying attributes for individual components in Vue.js classes

Recently, I integrated a reusable component called "LightBox" into my website, which displays images in higher resolution. The LightBox functionality is linked to each element having a thumbnail. However, I encountered an issue. There are multiple elements ...

Submenu failing to appear

After reading a discussion on dropdown menu issues on a popular forum, I am still unable to locate the error on my website: . The submenu on the main top menu is not functioning as it should. My attempt to resolve the problem by changing the overflow sett ...