align-content and justify-content in flexbox are not producing the desired results

align-content and justify-content don't appear to be working when I test them in both Chrome and Firefox.

I'm puzzled as to why they're not functioning as expected.

#container {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-content: center;
  height: 90vh;
}
#header {
  flex: 1 0 200px;
  background-color: lightgreen;
  height: 200px;
  border-radius: 1rem;
}
#main {
  flex: 1 0 200px;
  background-color: coral;
  height: 200px;
  border-radius: 1rem;
}
#footer {
  flex: 1 0 200px;
  background-color: silver;
  height: 200px;
  border-radius: 1rem;
}
@media screen and (max-width: 915px) {
  #container {
    flex-flow: column wrap-reverse;
  }
}
<div id="container">
  <div id="header">Header</div>
  <div id="main">Main</div>
  <div id="footer">Footer</div>
</div>

Answer №1

They are following your instructions precisely. The issue lies in the way you have defined your flex properties.

If you want each item to be exactly 200px, do not assign a flex-grow value of 1 as you did with flex-grow: *1* 0 200px.

Instead, redefine the flex properties like this:

#header {
  flex: 0 0 200px;
  ...
}

View working JSFiddle example

The reason why align-content isn't functioning is because you only have one line of items, therefore it does not apply.

align-content

This property aligns a flex container's lines when there is additional space in the cross-axis, similar to how justify-content aligns individual items within the main axis.

Note: When there is only one line of flex items, this property has no effect.

Resource: CSS-Tricks

If you aim to vertically and horizontally center the items, you should use align-items rather than align-content.

Replace align-content: center with align-items: center to achieve vertical centering.

See JSFiddle example demonstrating align-items usage

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

JavaScript/CSS memory matching game

Just starting out in the world of programming and attempting to create a memory game. I've designed 5 unique flags using CSS that I want to use in my game, but I'm feeling a bit stuck with where to go next. I understand that I need some function ...

Inserting a picture using CSS

I attempted to include an image in a footer div, but I have not been successful with the following approach: Here is the HTML code I tried: <footer> <div class="imagenFooter" style="background-image: url(&quot;./images/footer-bg.jpg&quo ...

When making cross-origin requests, characters inside the <style> tags are transformed into Unicode HTML Entities

I am currently working on a NextJS application with react-jss integration. Within my CSS, I have a specific rule targeting '& .working[style="display: block;"] ': {...} Everything works perfectly when accessing the page from the s ...

Utilize Ajax to open and close an HTML tag within separate div elements

I am facing a challenge in my project where I have to dynamically open and close a form using ajax. The form needs to be opened within a div, and then closed in another div below it like the following: Opening form (header div) $('#header').h ...

Suitable HTML container for ASP form over HTML table

Can anyone advise on the best HTML container to use for an ASP form that can be styled with CSS instead of an HTML table? For example, consider the following form on an aspx page: <form id="WPF" runat="server"> <asp:Label ID="lblCCode" Text="Cou ...

Is it possible for a user to change the data stored in sessionStorage variables?

Incorporating client-side JavaScript into my project to save certain variables using Web Storage - specifically, the sessionStorage. However, uncertainty exists regarding whether a user holds the capability to alter these variable values. If this is indee ...

Reorder the nested list structure

Hello there and thank you for stopping by! I am facing a challenge with a list containing multiple nestings. I am interested in changing the order of these two lists, and I am curious if this is achievable using only CSS. List item 1 List item 2 List ...

Conditional rendering is effective for displaying a form item based on certain conditions, but it may not be as effective for

I want a textarea element to appear or disappear based on the selected state of the radio buttons before it. If "no" is chosen, the textarea will be hidden, and if "yes" is chosen, the textarea will become visible. <fieldset class="input-group form-che ...

Ways to conceal components of an external widget on my site

I'm attempting to add a chat widget to my website and I'm looking to hide specific elements within the widget. The chat function is provided by Tidio, but I believe this applies to most widgets. My main goal is to conceal a button that minimize ...

Using Webflow code in ReactJS/NextJS: Best practices and implementation tips

After exporting my Webflow code to my NextJS app, I noticed that many of the HTML tags are being flagged as errors because they appear to be missing closing tags. Can anyone shed some light on why this is happening and suggest potential solutions? It&apos ...

The position of the scroll bar remains consistent as you navigate between different websites

Is it possible for me to click on a link within my HTML website and have the other website load with me in the exact same position? For example, if I'm halfway down a webpage and click a link, can I be taken to that same point on the new page? If so, ...

Elements in Motion

Working on a parallax effect, I've managed to assign unique scroll speeds to (almost) every element. Moreover, these elements are programmed not to activate their scroll speed until they enter the viewport. Below is the JavaScript code for trigger co ...

I customized the navbar in Bootstrap by centering it with custom CSS, but whenever I click on it, it suddenly shifts to one side. I'm puzzled as to why this is happening and

I attempted to center my navbar-toggler element, but was only successful in centering the navbar-contend using Bootstrap. I then took it a step further by trying to center the navbar-toggler icon with custom CSS, however, it now shifts to the left when cli ...

Seeking a material design template for mandatory form patterns

I'm struggling with getting my required field patterns to work using regular expressions. Additionally, I'd like to center my 'onboard' button on the page. You can find my code at this StackBlitz link: https://stackblitz.com/edit/angula ...

What is the best method to adjust the width of the <option> tag within the <select> tag using CSS?

<!DOCTYPE html> <html> <head> <title>Page Title</title> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="s ...

Using the DRY principle in JavaScript to handle dynamic fields

Recently delving into Javascript, I encountered a dynamic field script that allows for adding and subtracting fields with the click of a button. The functionality is effective and serves its purpose well. $(document).ready(function() { var max_fields ...

Firefox fails to apply styling to content loaded through Ajax (particularly when using AngularJS)

I've encountered a strange problem while working on my app (MVC4 + AngularJS). It seems that in Firefox (currently version 42), content loaded through an Ajax call does not always pick up the CSS styles that were loaded before. In the attached images, ...

Iterating through a variety of selectors on a webpage to consolidate and save into a single large data frame

I need help with a project and got a quick response last time, so here I am again. The code below is scraping data from a website and adding a column to indicate which instance of the table it is extracting. My current challenge is loading all Game Recency ...

Clearing user's browsing data

I managed to successfully set up a user cookie, but I'm having trouble getting it to be destroyed or terminated when the user logs out. Any assistance on this matter would be highly appreciated! I am new to PHP and still in the learning process. Here ...

make the chosen text appear on Internet Explorer

1 How to insert text into a text box using code? 2 Moving the caret to the end of the text. 3 Ensuring the caret is visible by scrolling the text box content. 4 Programmatically selecting specific text in a textbox. 5 **How to make selected text visible?** ...