Justifying content is failing to perform its duty, while aligning items is successfully fulfilling its role

Can someone explain why the align-items property in this code is centering child elements on the x-axis instead of the y-axis? Also, the justify-content: center doesn't seem to have any effect. Even when I remove justify-content, the items are still aligned on the x-axis due to align-items property:

/* HTML: */

<div class="page">
    <div class="navbar">
      <div class="leftSide">
        <img src="./assets/images/logo.png">
      </div>
      <div class="rightSide">
        <li><a href="#">Home</a></li>
        <li><a href="#">Menu</a></li>
        <li><a href="#">What's New</a></li>
        <li><a href="#">Contact Us</a></li>
      </div>
    </div>

    <div class="content">
      <h1>It's not just Coffee</h1>
      <h1>It's <span>Starbucks</span></h1>
    </div>
  </div>

/* CSS: */

@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700;800;900&display=swap');

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: 'Poppins', sans-serif;
}

.page {
  display: flex;
  align-items: center;
  flex-direction: column;
}

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
  position: sticky;
  top: 0;
}

.rightSide {
  display: flex;
  list-style: none;
}

.rightSide li{
  margin-right: 30px;
}

.rightSide li a {
  text-decoration: none;
  color: black;
  position: relative;
  transition: all 0.5s;
}

.rightSide li a::before {
  content: '';
  width: 0%;
}

.rightSide li a:hover::before {
  content: '';
  position: absolute;
  width: 100%;
  height: 1px;
  background: #000;
  bottom: 0;
  left: 0;
}

.leftSide img {
  width: 80px;
  height: 80px;
  margin-left: 15px;
}

I've tried various solutions and searched extensively, but so far, I couldn't find a way to correct it. This isn't the first time facing this issue.

Answer №1

These two issues are not directly intertwined.

The initial problem lies in your misunderstanding of how Flexbox operates. I strongly advise you to delve into the first three sections of "A Complete Guide to Flexbox" by Chris Coyier. Particularly, the "Background" section is where your misconception stems from.

To quote Chris Coyier:

Main axis – The main axis of a flex container is the primary axis along which flex items are laid out. Beware, it is not necessarily horizontal; it depends on the flex-direction property (see below).

This implies that the main axis aligns with the flex direction. Hence, justify-content pertains to the main-axis and align-items relates to the cross-axis. These properties do not explicitly target the Y-axis or X-axis!

By default, when flex-direction is set to row, the items align horizontally along the main axis:

body {
  margin: 0;
}

.flex-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center; 
}

.flex-item {
  width: 50px;
  height: 50px;
  border: 2px dashed blue;
}
<div class="flex-container">
  <div class="flex-item"></div>
  <div class="flex-item"></div>
</div>

Changing flex-direction to column switches the main axis to be vertical (Y-axis), consequently aligning items along the Y-axis using justify-content:

body {
  margin: 0;
}

.flex-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  flex-direction: column;
}

.flex-item {
  width: 50px;
  height: 50px;
  border: 2px dashed blue;
}
<div class="flex-container">
  <div class="flex-item"></div>
  <div class="flex-item"></div>
</div>

The second issue involves a pure logical discrepancy that might not be immediately noticeable. By default, an element's height is set to auto, meaning the browser adjusts the height to fit the content precisely. In this snippet, you can see that the element (marked with a blue border) perfectly fits its parent (indicated by a red border):

body {
  margin: 0;
}

.flex-container {
  display: flex;
  align-items: center; 
  border: 2px dotted red;
}

.flex-item {
  width: 50px;
  height: 50px;
  border: 2px dashed blue;
}
<div class="flex-container">
  <div class="flex-item"></div>
</div>

Logically speaking, to visually center an element, the parent element must have a greater height than the element itself. You cannot center an element within a tightly fitting container. If you increase the container's height to exceed the content's height, you'll observe that centering functions correctly:

body {
  margin: 0;
}

.flex-container {
  display: flex;
  align-items: center; 
  border: 2px dotted red;
  height: 80vh;
}

.flex-item {
  width: 50px;
  height: 50px;
  border: 2px dashed blue;
}
<div class="flex-container">
  <div class="flex-item"></div>
</div>

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

Needing to scroll within a dropdown menu has become a necessity for me

While working with Selenium for automation, I encountered a challenge in clicking on a hidden element within a dropdown menu. The issue is that when scrolling down the page, the entire page moves instead of just the specific dropdown. Here is the HTML cod ...

Can an animation be created that transforms an SVG line into a flawless circle?

Is there a way to create an animation that starts with an SVG line and gradually widens from the center to form a perfect circle? I've been researching this, but haven't found a solution that meets my needs. It could be because of the keywords I ...

Eliminating the background color upon clicking

Here is the link structure I have set up: <ul> <li><a href="home.html"><span>home</span></a></li> </ul> However, when I click on the link, a shadow appears. I would like the link to appea ...

Press the button to reveal the table - jQuery/JavaScript

Can you please provide guidance on how to hide the inner HTML table when the page loads and then display the table with results only after clicking the search button? I do not want to show an empty table. Below is the code snippet that I have tried: Howev ...

The hover selector in Material UI and React is not functioning as expected

My code is experiencing an issue where the hover selector does not appear to be working. Although all other styling aspects function correctly, the hover effect is not visible. import React from "react"; import Paper from '@material-ui/core/Paper&apo ...

Aligning text in the middle of an image both vertically and horizontally

I've been experimenting with different methods to vertically align caption text in the middle of an image, but I haven't had much luck. I've tried using table-cells and other techniques, without success! Currently, I have an <li> elem ...

How do I dynamically adjust class names for menu items as I scroll using jQuery?

I have a website with different sections identified by unique IDs, such as: <section id="home"> First Section </section> <section id="about_us"> Second Section </section> <section id="what_we_do&q ...

Every file downloaded through the iframe is automatically stored in a designated server folder

Is it possible for my website to have an iframe that enables users to browse the web, and when they click on a download button on any external website, the file can be saved directly to a specific folder on my server instead of their own computer? I'm ...

Strategies for delivering CSS exclusively to up-to-date browsers

What are some methods for serving CSS exclusively to modern browsers while displaying plain, unstyled HTML to outdated versions of Internet Explorer? I am currently employing the following approach, but I am not particularly thrilled about having my main ...

What is causing the radio button's background color not to change after it is selected?

I've been searching and exploring various solutions, but I'm encountering some difficulties with the following scenario: There are a few restrictions: Cannot utilize Javascript or Jquery. Must be achieved using pure CSS. My objective is to chan ...

The art of applying styles through styled components

I am attempting to customize the styling of my component with the following code: export const StyledCascader = styled(Cascader)` background-color: gray; ul.ant-cascader-menu { background: red !important; } `; Despite using styled components ...

Elements that are hidden or not displayed are still able to receive mouse over events

I am in the process of learning how to handle events within svgs and I have encountered a strange issue. Currently, I am working on an infovis project where I create an interface to display various column-graphs. This part is functioning well so far. Ho ...

Arranging text in alignment on a single line with Vuetify

I am attempting to format text in a way that it is aligned with part on the left side of the card and the other part on the right (with space between them). However, when using class="text-right", the text gets moved down. Does anyone have any ideas on h ...

Displaying images on hover that were not initially incorporated into the project

I am currently developing a website where the content is not controlled by me and the incoming content is unknown, making it impossible for me to store images. My goal is to display the referenced image when a specific class is hovered over. For example: ...

Undoing alterations to the user interface in Angular 2 after discontinuing bi-directional data binding

UPDATE: Including code for further clarification. client.component.ts import { Component } from "@angular/core"; import { ClientService } from "../services/client.service"; import { Client } from "../types/client"; @Component({ selector: "rpcs-client", ...

HTML5 elements for displaying search results

Similar Post: Improving search result list structure with HTML5 semantics I have retrieved a list of items from a database query. Now I need to present them to the user using HTML. This is my current approach: <ol> <li> Search Elem ...

What's causing Angular to not display my CSS properly?

I am encountering an issue with the angular2-seed application. It seems unable to render my css when I place it in the index.html. index.html <!DOCTYPE html> <html lang="en"> <head> <base href="<%= APP_BASE %>"> < ...

Stylish CSS: Jagged 3-dimensional rotated text

Have a look at this Codepen to see the issue. As the text moves into the background, more aliasing appears. A screenshot taken in Chrome on macOS (similar appearance in FF & Safari): https://i.sstatic.net/n746I.png I've experimented with different ...

<p> The box is massive, and its size is a mystery to me

https://i.sstatic.net/xPoDz.png Why is the box around my paragraph tag so large? I suspect this could be why I am unable to move the <p> tag down with margin-top: 50px; .train p { margin: 0; font-size: 2.5vw; } <div class="train"> < ...

Troubleshooting problem with displaying HTML content on Internet Explorer 10

My website was developed using jquery with a dynamic coding style. It was functioning properly in IE10 while working from Visual Studio. However, after deploying it to the production server, the entire style seemed broken in IE10. In all other versions o ...