I'm wondering why using display flex with justify-content center and align-items center is not properly centering the elements both horizontally and vertically. Can anyone explain this?

As I delve into the world of JavaScript, I find myself coding simple JS, HTML, and CSS snippets to enhance my learning experience. These exercises help me grasp the concepts well enough to create various JS web elements and projects. My goal is to eventually apply this knowledge to larger projects down the line.

In my current practice, I am utilizing the following CSS properties:

display: flex;
align-items: center;
/* AND */
justify-content: center;

I have applied these properties to the "body" in my CSS file, expecting it to automatically center elements both vertically and horizontally on the page. Despite not having conflicting CSS rules, I noticed that the elements were centered horizontally but not vertically.

Upon inspecting the element and manipulating the element.style within the <html lang="en"> tag by adding height: 100%;, I was able to achieve vertical centering. This workaround feels unnecessary as I believe elements should center without explicitly setting a height of 100% for the <html lang="en"> element.

This discrepancy has left me puzzled. I typically code using Visual Studio Code and preview the HTML file with the LiveServer extension. My recent focus on mastering Flexbox aligns with my newfound dedication to pursuing a career in coding post-online schooling.

Below is the snippet of my code which may shed some light on the situation:

body {
  background-color: #dbdbdb;
  display: flex;
  align-items: center;
  justify-content: center;
}

.arrows {
  height: 70px;
}


/* .left-arrow {
  display: flex;
  align-items: center;
  justify-content: center; */
}

.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 50px;
}

.top {
  display: flex;
  align-items: center;
  gap: 50px;
}
<div class="container">
  <div class="top">
    <img src="images/left-arrow.svg" class="arrows arrow-left" alt="Left Arrow">
    <div class="frame">
      <div class="slider"></div>
    </div>
    <img src="images/right-arrow.svg" class="arrows arrow-right" alt="Right Arrow">
  </div>
  <div class="bottom"></div>
</div>

<script src="slider.js"></script>

The slider.js file currently contains no code as I haven't progressed that far. Despite experimenting with different heights (100% vs. 100vh) across multiple CSS classes, the issue persists. I have even removed certain HTML and CSS elements in an attempt to troubleshoot, yet the mystery remains unsolved.

Answer №1

Ah, the never-ending struggle of centering content in the body...

The issue lies in the fact that the body's height is not automatically set to 100% of the viewport height. If your content doesn't fill this height, you'll need to manually adjust it like so:

body {
   height: 100vh;
}

The "vh" unit represents "Viewport Height," meaning setting the body's height to 100vh ensures it fills the entire viewport. Interestingly, simply setting the body's height to 100% won't suffice! This can be a bit confusing, but let me explain further. When you use:

body {
   height: 100%;
}

This sets the body's height to be 100% of its PARENT ELEMENT, which is the html tag. Note that if you don't set a height for the html tag, it will be determined by its child elements.

I hope this explanation clears things up and helps you solve your dilemma. Feel free to reach out if you encounter any further issues!

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

Implementing jQuery .load to effectively extract the Title tag from HTML code

I am currently utilizing .load to fetch and load pages, as well as modify their window titles. The page loads successfully; however, the main issue I am facing is my inability to extract the title from the HTML code present in the response. Below is the c ...

SyntaxError: Unexpected '<' symbol found in JavaScript file while attempting to import it into an HTML document

This issue is really frustrating me In my public directory, there is an index.html file Previously, I had a newRelic script embedded within the HTML in script tags which was functioning properly Recently, I moved the script to a separate JavaScript file ...

Creating a dynamic linear gradient background color using Angular 2 binding

Using static values works perfectly <div style="background: linear-gradient(to right, #0000FF 0%, #0000FF 50%, #FFA500 50%, #FFA500 100%);"></div> in my TypeScript file. this.blueColor = '#0000FF'; this.orangColor = '#FFA500&a ...

Understanding Node.js document object

Hey, I'm currently trying to execute a JavaScript function on the server side using node.js, but I've encountered an issue. The function relies on accessing hidden values within the returned HTML using the document DOM, however, the document obje ...

Extracting data from a dropdown menu and displaying it

I've created a form with a select tag that pulls options from a .csv file. However, I'm struggling to retrieve the selected option and display it later in the form. The variable to hold the selected currency is named $selectedCurrency. UPDATE: ...

Design interactive elements such as buttons and input fields inspired by the layout of Google websites

Does anyone know how to achieve the sleek, lightweight buttons and text boxes seen on Google's web pages like Google Search, Google Plus, and Google Play? I am currently using JSP and CSS, but I am open to incorporating jQuery if necessary. Any help w ...

concealing components during screen adjustments

There are 3 identical <div>s available: <div class="box">Hello World!</div> <div class="box">Hello World!</div> <div class="box">Hello World!</div> I need these <div>s to ...

Bootstrap 3 with a dual sidebar layout positioned on the left side

I am currently working on a project where I need to create a fluid layout with a left sidebar. However, now I would like to add an expandable sidebar next to the existing one. Below is an image to illustrate my goal: Initially, the left sidebar contains a ...

Having trouble accessing certain nodes while extracting HTML text with R

I have numerous water extraction permits that are accessible online and I am looking to extract specific data from them. For example url <- "https://www.ecan.govt.nz/data/consent-search/consentdetails/CRC000002.1" While I don't have a strong know ...

Utilizing JavaScript for enhancing the appearance of code within a pre element

Is there a way to dynamically highlight the code inside a pre element using vanilla JavaScript instead of JQuery? I'm looking for a solution that colors each tag-open and tag-close differently, displays tag values in another color, and attributes with ...

Tips for displaying just the image name without the entire image file path

When I try to upload an image, the path displayed is "C:\fakepath\promotion1.JPG". This fake path is causing the image not to upload to my project media folder. How can I solve this issue? Instead of the complete path, I only want to capture the ...

Issues with the FlexBox styling of Bootstrap 4 Modals in Internet Explorer causing malfunction

I have designed a confirmation dialog using Bootstrap 4 with the following code snippet: <div class="modal fade show" id="completeAlertDialogue" role="dialog" style="display: block;"> <div class="modal-dialog"> <div class="modal-co ...

Apache conf file configured with CSP not functioning properly when serving PHP files

While configuring the Apache CSP lockdown for a site, I encountered an unusual behavior when opening the same file as a PHP script compared to opening it as an HTML file. The HTML file looks like this: <html> <head> <meta http-equiv= ...

Should the letter 'p' be inserted into the letter 'a'?

After viewing a video on Bootstrap, I noticed an unusual occurrence where there was a p element nested inside an a element. Is this considered acceptable in terms of HTML semantics? ...

Transitioning with an ellipsis

Currently, I am working on a project where I am trying to utilize the CSS property text-overflow: ellipsis to achieve a specific animation. However, despite applying this property along with white-space: nowrap and overflow: hidden to .card-dish__info, I a ...

In Angular/Typescript, dynamically add a class to a `td` element when it is clicked. Toggle the class on and off

My problem arises when trying to individually control the arrow icons for each column in my COVID-19 data table. By using Angular methods, I aim to show ascending and descending arrows upon sorting but run into the challenge of changing arrows across all c ...

Issues with clicking in JS eventListener

I'm in need of some assistance with a small header issue in my project. I've included all the necessary header files so that you can run it in a snippet and see what's going on. The problem lies within my JS file. When running the file, you ...

Adjusting the link color within a div when hovering over it

Is it possible to change the color of all links in a div when hovering over it, rather than just the individual link itself? I have a div with multiple links and would like them all to transition to a different color when the div is hovered over. I would ...

Guide on changing the CSS of MUI parent components to set the display of buttons to 'block' in React

I'm facing a challenge with my modal design for mobile view. I need the buttons to display in a stacked format, one above the other, taking up the full width of the modal. I've been exploring ways to override the existing CSS within the component ...

Shake up your background with a random twist

Seeking assistance with customizing the Aerial template from HTML5UP. I am interested in selecting a scrolling background randomly from a pool of images. Can someone guide me on how to achieve this? Presently, the background is defined within a code block ...