Applying CSS3 flex styles to elements cascading downwards: A guide

After stumbling upon CSS3's display: flex; property, I decided to implement it in my website's sticky header. However, I encountered a problem.

The main objective was to evenly distribute all links across the entire width of the header, but achieving this became challenging when grouping elements semantically using, for instance, a <nav> tag.

Take a look at the example on jsfiddle: https://jsfiddle.net/9bua0n7o/

<header>
  <div class="logo">
   Logo
  </div>

  <nav class="navigation">
    <a href="#">Home</a>
    <a href="#">FAQ</a>
    <a href="#">Contact</a>
  </nav>

  <div class="search">
    <a href="#">Search</a>
  </div>

  <div class="login">
   <a href="#">Register</a>
   <a href="#">Login</a>
  </div>

</header>

CSS:

header {
    width: 100%;
    height: 60px;
    line-height: 60px;

    display: flex;
    flex-flow: row wrap;    
    justify-content: space-between;

    position: fixed;
    top: 0;
    left: 0;

    padding: 0 25px;
    box-sizing: border-box;

    background: #ccc;
}

Answer №1

To achieve this layout, follow these steps.

I assigned a flex value to each wrapper element (such as navigation and search) based on the number of children they contain.

By doing so, each wrapper will take up a proportionate amount of available space, resulting in evenly distributed items.

Furthermore, I specified that each link should have a flex: 1 property to occupy the entire space available to them.

header {
  width: 100%;
  height: 60px;
  line-height: 60px;
  display: flex;
  flex-flow: row wrap;
  position: fixed;
  top: 0;
  left: 0;
  padding: 0 25px;
  box-sizing: border-box;
  background: #ccc;
}

header .logo,
header .search {
  flex: 1;
  display: flex;
}

header .navigation {
  flex: 3;
  display: flex;
}

header .login {
  flex: 2;
  display: flex;
}

header .navigation a,
header .search a,
header .login a {
  flex: 1;
}
<header>
  <div class="logo">
    Logo
  </div>

  <nav class="navigation">
    <a href="#">Home</a>
    <a href="#">FAQ</a>
    <a href="#">Contact</a>
  </nav>

  <div class="search">
    <a href="#">Search</a>
  </div>

  <div class="login">
    <a href="#">Register</a>
    <a href="#">Login</a>
  </div>

</header>

Answer №2

My solution is somewhat similar (though not entirely identical) to the one provided by LGSon.

1) First, I included a link within the .logo element:

<div class="logo">
<a href="#">Logo</a>
</div>

2) Next, I applied the same flex style to all elements in the header:

.logo,
.logo a,

.navigation,
.navigation a,

.search,
.search a,

.login,
.login a {
flex: 1 0 auto;
}

3) Finally, I modified the flex-grow properties of .navigation and .login:

.navigation {
flex-grow: 3;
}

.login {
flex-grow: 2;
}

Here's the complete example:

header, .logo, .navigation, .search, .login {
display: flex;
justify-content: space-between;
}

.logo, .navigation, .search, .login, .logo a, .navigation a, .search a, .login a {
flex: 1 0 auto;
}

.navigation {
flex-grow: 3;
}

.login {
flex-grow: 2;
}

header {
width: 100%;
height: 60px;
line-height: 60px;

position: fixed;
top: 0;
left: 0;

padding: 0 25px;
box-sizing: border-box;
background: #ccc;
}
<header>

<div class="logo">
<a href="#">Logo</a>
</div>

<nav class="navigation">
<a href="#">Home</a>
<a href="#">FAQ</a>
<a href="#">Contact</a>
</nav>

<div class="search">
<a href="#">Search</a>
</div>

<div class="login">
<a href="#">Register</a>
<a href="#">Login</a>
</div>

</header>

Answer №3

Even though it's not yet fully developed for production use as of July 2016, the CSS Display Module Level 3 Working Draft introduces the display: contents property, which states:

The element itself won't create any boxes, but its children and pseudo-elements will still generate boxes normally. In terms of box generation and layout, the element should be treated as if it had been replaced with its children and pseudo-elements within the document tree.

If implemented in your project, it could resemble something like this (currently only supported in Firefox):

header {
  width: 100%;
  height: 60px;
  line-height: 60px;
  display: flex;
  flex-flow: row wrap;
  justify-content: space-between;
  position: fixed;
  top: 0;
  left: 0;
  padding: 0 25px;
  box-sizing: border-box;
  background: #ccc;
}

.header-section {
  display: contents;
}

.header-section a {
  flex: 1;
}
<header>
  <div class="logo">
    Logo
  </div>

  <nav class="header-section navigation">
    <a href="#">Home</a>
    <a href="#">FAQ</a>
    <a href="#">Contact</a>
  </nav>

  <div class="header-section search">
    <a href="#">Search</a>
  </div>

  <div class="header-section login">
    <a href="#">Register</a>
    <a href="#">Login</a>
  </div>
</header>

This insight may offer some enlightenment on the potential new CSS functionalities

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

Issue with the final transition of the last image in the Bootstrap carousel

I'm currently in the process of creating my own portfolio website. I've integrated a Carousel feature inspired by Bootstrap's example. However, I've noticed some glitches in the transitions between the first or second slides and the thi ...

Email form successfully retains session variables but fails to accurately update form inputs upon refresh

After experimenting with ChatGPT to implement an email form, I was pleasantly surprised by its performance. However, I encountered a challenge when I wanted the form inputs to retain the information from the previous user session. While it does display one ...

Is it possible to have square corners on the Vuetify Mobile Drawer component?

Currently, I am working with the Vuetify mobile drawer component and my goal is to give it square corners instead of the default rounded corners that are prevalent in Material design for all Vuetify components. While the Vuetify card component offers a " ...

Ensure that bulleted lists and numbered lists are aligned to the left side

Check out this website where the ordered and unordered lists are not aligned correctly. The ideal alignment would have the bullets (or numbers) left aligned. For instance, the number "1." should be aligned to the left on the same line as the heading "Per ...

Create a dynamic dropbox using JavaScript/JQuery in an HTML page

I am currently working on implementing 3 different drop down boxes that are triggered by 4 different buttons. These drop down boxes should appear within the #apple div. When Button B1 is clicked, the drop down options include Apple, Mango, and Papaya. Whe ...

Effective strategies for integrating Bootstrap and AngularJS without relying on jQuery

Currently exploring AngularJS after experimenting with Twitter's Bootstrap. I appreciate Bootstrap for its simplicity, sleek design, and mobile-friendliness. On the other hand, I have noticed a trend where people recommend using AngularJS over jQuery, ...

Revamp Button content in HTML and Angular 2

My goal is to dynamically change the button text once it is selected. Initially, the YES button is highlighted and slightly changes upon being clicked. I want the button to switch to NO when clicked, and if hovered over, a disable symbol should be displaye ...

Function for triggering character or camera movement upon the pressing of a button

Just starting out here with web coding and I'm trying to create a function that moves my game character when a button is pressed. The idea is to change a boolean value to "true" when the button is pressed down so that the character moves. I've at ...

Having trouble with animating the background color of an input button using jQuery?

I'm completely confused as to why the background color isn't changing despite investing a significant amount of time into it: <input type="button" class="front-consultation-btn" value="Get A Free Consultation"> <script> $(' ...

Is it possible to incorporate Bluetooth connectivity into an HTML5/JS Web application?

Currently, I have developed a WebApp using HTML5, JS, and Jquery. I am now looking to integrate Bluetooth functionality into it to enable data transfer to another Bluetooth-enabled device. After downloading the Bluetooth Dev Kit and going through numerous ...

Steps to create a toggle feature for the FAQ accordion

I am currently working on creating an interactive FAQ accordion with specific features in mind: 1- Only one question and answer visible at a time (I have achieved this) 2- When toggling the open question, it should close automatically (having trouble with ...

creating an HTML document with 2 interactive elements

In my app.js file, I have defined routes using Angular and included references to HTML files. However, when I run the file, it only displays two controls instead of what is expected. Below is a snippet from my login.html file: ![<!DOCTYPE html> < ...

Arranging a flex item below another flex item within a flexbox container

Is there a way to use flexbox to ensure that the 2.5 element is placed below the 2 element instead of beside it on the same row within the flex container? Given the HTML provided, how can I achieve this layout using flexbox? I attempted to accomplish thi ...

accommodating multiple background images in CSS

I am currently working on a WordPress website using the Twenty Twelve theme. On one of the pages, I am trying to incorporate three background images - one at the top, the next one right below the top image, and the third one at the very bottom... Is there ...

Error Alert: jQuery Ajax Not Executing

Looking to send form data to PHP using jQuery. Check out the code below. -------------HTML FORM-------------- <div id="createQuestionBlock"> <form id="createQuestionForm" action="" method="POST"> Question Code: <input id="code" ...

CSS selectors can be used to alter the styling of the container surrounding the text

My current content includes: <span> Hello </span> I am looking to apply CSS selectors to either replace or enclose the text inside the element with an <h1> Is it doable using only CSS selectors? ...

The log out button is unresponsive and does not function properly

I am having trouble positioning the logout button on the left toolbar. I have tried using the position property, but it is not responsive. When I resize the Chrome window, the button disappears. I also attempted to use margin, but that did not work either. ...

Preserving spacing using minimum widths

Looking for some help with a page layout issue. I have a header on my page with a title and menu. Currently, the header has a margin and a minimum width set to the width of the menu. However, when the user resizes the window and reaches the minimum width, ...

What does the term "root element" refer to in the Material-UI documentation?

I am finding the terminology used in material ui confusing and would appreciate if someone could clarify it for me. For example, let's consider this information from https://material-ui.com/guides/api/: Spread Undocumented properties supplied ar ...

Displaying information from a database in an HTML form using PHP

Seeking assistance with managing data and populating: I'm in the process of creating an Employee database with two pages: 1. Add Employee 2. Modify Employee The "Add Employee" page requires input fields for EMP Name, EMP ID, Manager name (from a dro ...