It seems like Flexbox is ignoring the flex-direction: column property set for a header element

I'm a bit confused because it seems like setting flex-direction: column on an element should display the children of that element in a horizontal row.

My layout is simple - at the top, I have the logo, navigation links, and some controls. Below that, the content should display in a single column with each entry appearing below the previous one.

The content is displaying properly, but within the header tag, only the links are behaving as expected. Why is this happening? What changes should be made to ensure that the logo, link #1, link #2, a, and b all appear in a single row?


            * {
                margin: 5px;
                padding: 5px;
                border: 1px #ccc solid;
            }

            html {
                display: flex;
            }

            .App {
                flex-direction: row;
            }

            header {
                flex-direction: column;
            }

            .Content {
                flex-direction: row;
            }
        
<div class='App'>
            <header>
                <img src='#.png' alt='logo'>
                <nav>
                    <a href='#'>Link 1</a>
                    <a href='#'>Link 2</a>
                </nav>
                <div class='Controls'>
                    <button>a</button>
                    <button>b</button>
                </div>
            </header>
            <div class='Content'>
                <p>Lorem ipsum ...</p>
                <p>Lorem ipsum ...</p>
            </div>
        </div>
        

Thank you for your assistance!

Answer №1

Make sure to apply the flex property to the correct element. In this case, it should be applied to the header element as it is the parent element to all other elements within that div. Here's an example:

header{
display:flex;
flex-direction:row;
}

If you want to learn more about Flexbox, check out this tutorial:

You can also refer to this quick guide: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

I hope this information is helpful.

Answer №2

flex-direction: column allows elements to stack vertically in a column layout instead of horizontally in a row.

When using Flexbox, the relationship between parent and child elements is important. Applying display: flex to the html will only make the body a flex item.

flex-direction is a property of the flex container and should be paired with display: flex to set the direction of the children elements in the container.

In the example below, the properties will cause the children of App to stack vertically, while the children of header will stack horizontally.

Snippet display

* {
  margin: 5px;
  padding: 5px;
  border: 1px #ccc solid;
}

.App {
  display: flex;
  flex-direction: column;          /*  stack vertically  */
}

header {
  display: flex;
  flex-direction: row;              /*  default (can be omitted), horizontal  */
}
<div class='App'>
  <header>
    <img src='#.png' alt='logo'>
    <nav>
      <a href='#'>Link 1</a>
      <a href='#'>Link 2</a>
    </nav>
    <div class='Controls'>
      <button>a</button>
      <button>b</button>
    </div>
  </header>
  <div class='Content'>
    <p>Lorem ipsum ...</p>
    <p>Lorem ipsum ...</p>
  </div>
</div>

Answer №3

When you attempt to apply flex-direction to elements that are not flex-containers, it will not have any effect.

The "display" property is not passed down through inheritance, so specifying flex-direction on display:block items will result in no changes.

To make the children of an element behave like flex-items, you must include display:flex for each specific element.

<style>
* {
margin: 5px;
padding: 5px;
border: 1px #ccc solid;
}
html {
display: flex;
}
.App {
  display: flex;
flex-direction: row;
}
header {
  display: flex;
flex-direction: column;
}
.Content {
  display: flex;
flex-direction: row;
}
</style>
<html>
    <div class='App'>
            <header>
                    <img src='#.png' alt='logo'>
                    <nav>
                            <a href='#'>Link 1</a>
                            <a href='#'>Link 2</a>
                    </nav>
                    <div class='Controls'>
                            <button>a</button>
                            <button>b</button>
                    </div>
            </header>
            <div class='Content'>
                    <p>Lorem ipsum ...</p>
                    <p>Lorem ipsum ...</p>
            </div>
    </div>
</html>

Answer №4

Make sure to apply the display:flex property to the header element in your CSS code.

Without adding display:flex to the necessary elements, the flex-direction statements will not have any effect.

* {
  margin: 5px;
  padding: 5px;
  border: 1px #ccc solid;
}

header {
  display: flex;
}
<div class='App'>
  <header>
    <img src='#.png' alt='logo'>
    <nav>
      <a href='#'>Link 1</a>
      <a href='#'>Link 2</a>
    </nav>
    <div class='Controls'>
      <button>a</button>
      <button>b</button>
    </div>
  </header>
  <div class='Content'>
    <p>Lorem ipsum ...</p>
    <p>Lorem ipsum ...</p>
  </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

Incorporate a JSON file into the body of an HTML document

I'm experiencing an issue with loading a Json file into the html body using the script tag, as illustrated below. Here is a sample of the html file: <html> <head></head> <body> <div id="category"></div> <scrip ...

Generate a div element dynamically when an option is selected using AngularJS

I'm having trouble dynamically creating div elements based on the selected option value, but for some reason ng-repeat isn't working as expected. Can you help me figure out what I'm missing? Here's the HTML snippet I'm using - &l ...

Customize the appearance of Angular components by altering their color schemes

When working on my project, I often utilize pre-made Angular components. However, some of these components come with colors that do not align with the overall aesthetic of my project (refer to image above). To rectify this issue, I am looking to replace t ...

How can I adjust the regular font size within a paragraph or link using bootstrap?

Looking at the fs-* classes can be very helpful, you can find them here: https://i.sstatic.net/l1Y22.png The issue I'm facing is that these classes only work for h1 to h6 headings, and not for smaller text. For example, I am unable to adjust the fo ...

Tips for restricting tab focus to a modal using TypeScript

Currently, I am facing an issue with a bootstrap modal that contains two button elements. Every time I open the modal, the buttons receive focus, but after tabbing twice, the focus shifts to another element on the main screen, which is not the desired beha ...

Any idea why the HTML Select dropdown isn't functioning properly in Angular 2?

I am facing an issue with an Angular 2 Form. I am trying to include an html select but it is not working. I have checked the Angular 2 Documentation and even the live examples provided, like the HERO FORM, are not working. You can view the Hero Form Live E ...

help with coding in html and css

Hey there, I’m currently working on creating a loading page for my website, but I seem to be facing an issue with a white line appearing along the top and one running down the left side. If anyone could provide me with some assistance on this matter, I ...

Adjust grading system based on user interaction with JavaScript

I am currently working on a grading system for a website and I am attempting to modify the interpretation of grades when a column is clicked. Specifically, I am looking to convert Average (%) to Letters to 4.0 Grade Average. To achieve this, I am using Jqu ...

Guide to positioning text alongside icons with Bootstrap

Struggling to align text next to these icons? Don't worry, we've all been there. Especially with bootstrap - it can be tricky! Can someone please guide me on how to do this? <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstr ...

Exploring the height and overflow properties of nested div elements

Imagine a scenario where you have a fixed-size container and need all the elements to fit inside. The issue arises when some elements contain lots of text, causing the overflow to be hidden but still stretching beyond the container's height. How can t ...

The issue with Angular 4 imports not refreshing in a .less file persists

Currently, I am in the process of developing a small Angular project that utilizes less for styling purposes. My intention is to separate the styling into distinct folders apart from the components and instead have a main import file - main.less. This fil ...

Aligning cards

Struggling to align these cards perfectly at center: Despite thorough research on both Google and Stack Overflow, the solution continues to elude me. I've experimented with various methods such as display: flex, justify-content: center, and align-ite ...

How to use CSS to make a child element's width automatically fill the width of its

Is there a way to make the child div info fill the parent div video-featured width while remaining absolute, allowing it to overlap the parent div without using a fixed width? <div class="video-featured col-md-4"> <div class="video"> < ...

What is the best way to insert an image into an HTML card?

I'm a beginner in PHP and I'm working on creating a registration form that should have a design similar to the picture linked below: https://i.sstatic.net/7P7bn.png However, the form I've created so far looks like this: https://i.sstatic. ...

Tips for verifying if the input in a Material UI textfield is an <iframe> tag

In my ReactJS code, I am utilizing the TextField component from material-ui. I need to verify if the user input is an iframe. How can I achieve this? Currently, I am attempting to use window.parent.frames.length > 0; to determine if the page contains a ...

Add at the beginning of each row in the text area

I have a small text area on my webpage where I want to add ">>" to the beginning of each line. My initial attempt was to use this code: $("#mytextarea").prepend("EvilHacker001>>"); Unfortunately, this did not produce the desired result. I se ...

Utilizing Javascript to Extract Data from Twitter Json Files

Can someone provide assistance with parsing JSON feed text retrieved from Twitter? I am looking to access and apply style tags to elements like the link, created date, and other information. Any tips on how I can achieve this task successfully would be g ...

Enhancing the functionality of the onclick function within the ng-repeat by incorporating a scope

I am looking to pass a parameter to a function that is called via onclick within an ng-repeat: <!-- Sliding bar (bottom) --> <div ng-show="currentSVG && currentLanguage && currentWidth && bannerHeight" pageslide ps-open=" ...

Problem encountered when utilizing Bootstrap's display classes d-none, d-md-block, and d-flex

When using the bootstrap classes d-none d-md-block in the following code block, they seem to cancel out the d-flex class. I want this block to be hidden on viewports smaller than md, but I also need to apply the d-flex class for the align-self-end class ...

Social media platform-inspired grid layout for displaying photos in a stylish and orderly manner

Looking to create a photos grid layout similar to Facebook using angularjs and bootstrap. Came across the angular-masonry plugin which seems like it could work. Here are some sample layouts I'm aiming for: https://i.sstatic.net/6NdNW.png https://i.s ...