Altering the flex-direction causes my divs to vanish into thin air

Just starting out with css, trying to get some practice.

I'm attempting to position two divs on opposite sides of a parent div.

Switching flex-direction from 'column' to 'row' causes the divs to disappear.

#assignments-wrapper {
  padding: 1.5rem;
}

.assignment {
  min-height: 40px;
  margin-bottom: .5rem;
  background: black;
  display: flex;
}

.red {
  width: 5%;
  height: 40px;
  background: #DC3220;
}

.green {
  width: 5%;
  height: 40px;
  background: #1AFF1A;
}

.blue {
  width: 5%;
  height: 40px;
  background: #1A85FF;
}

#row7 {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

#first .red {
  height: 10px;
  width: 10%;
}

#first .green {
  height: 15px;
  width: 10%;
}

#second .red {
  height: 10px;
  width: 10%;
}

#second .green {
  height: 15px;
  width: 10%;
}
<div id="assignments-wrapper">
  Row 7
  <div class="assignment" id="row7">
    <div id="first">
      <div class="green"></div>
      <div class="red"></div>
      <div class="green"></div>
    </div>
    <div id="second">
      <div class="green"></div>
      <div class="red"></div>
      <div class="green"></div>
    </div>
  </div>
</div>

With flex-direction:column: https://i.sstatic.net/oF3m4.png

With flex-direction:row:

https://i.sstatic.net/T0M6Q.png

Desired outcome (ignore width/height of colors, screenshots were not sized properly):

https://i.sstatic.net/2tnSX.png

I added flex to all ids, nothing changed.

Answer №1

Your code using flex-direction: row was successful. However, the issue you were facing was actually unrelated to that.

When using percentage values for width or height in CSS, they are calculated relative to the parent element. So, setting width: 10% means 10% of the width of elements with IDs #first and #second. However, within Flexbox, the width is calculated based on content using fit-content. Since these elements have no visible content, their width becomes 0. Multiplying 10% by 0 also results in 0! Therefore, both boxes had a width of 0 and were not visible. To fix this, simply add a defined width like 5vw to classes .green and .red, and you'll see it works correctly:

#assignments-wrapper {
  padding: 1.5rem;
}

.assignment {
  min-height: 40px;
  margin-bottom: .5rem;
  background: black;
  display: flex;
}

.red {
  width: 5%;
  height: 40px;
  background: #DC3220;
}

.green {
  width: 5%;
  height: 40px;
  background: #1AFF1A;
}

#row7 {
  display: flex;
  flex-direction: row;
    justify-content: space-between;
}

.red,
.green {
  width: 5vw;
}

#first .red,
#second .red {
  height: 10px;
}

#first .green,
#second .green {
  height: 15px;
}
<div id="assignments-wrapper">
  Row 7
  <div class="assignment" id="row7">
    <div id="first">
      <div class="green"></div>
      <div class="red"></div>
      <div class="green"></div>
    </div>
    <div id="second">
      <div class="green"></div>
      <div class="red"></div>
      <div class="green"></div>
    </div>
  </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

What is the best way to align headings directly above their own bottom border?

Is there a way to make a heading align perfectly with its bottom border? Can negative padding achieve this effect, or should I use underline instead? Perhaps positioning it at the top border of the element below is an option. Thank you! h1 { font: bol ...

Styling the content within Template Strings is not supported by VSCode

Recently, I've noticed that there are two scenarios in which my VSCode doesn't properly style the content within my template strings. One is when I'm writing CSS in a JavaScript file, and the other is when I'm fetching data from GraphQL ...

What is the best way to ensure that images on a webpage adjust to a maximum width without distorting smaller images?

I'm working on my website blog and I need to create a specific area for images at the top of my articles. I want these images to have a maximum width so they don't exceed a certain size, but I also want smaller images to remain unaffected. Curren ...

styles.css is generating an ERROR message, indicating that it is unable to read properties of null when trying to access the 'classList'

Hey there, I've been working on adding a background color change to my navbar when the scrollY exceeds 30px, and it's functioning properly. However, I'm encountering an error in the console which states that the new classList cannot be added ...

I'm having trouble getting the "spacing" styling attribute to work in Material UI when I attempt to space out elements within my Box component. Can anyone explain why this might be happening?

Within my React 16.10 application, I am utilizing materialUI components. Specifically, I have incorporated two buttons inside a Box element. <Box style={{ marginTop: "3rem", marginLeft: "1rem", display: "f ...

What is the reasoning behind jQuery UI employing CSS classes as opposed to pseudo-classes?

In this detailed explanation found on this website, it is discussed why jQuery UI uses CSS classes like .ui-state-active that are applied through JavaScript, rather than utilizing existing CSS pseudo-classes like :active. What is the reasoning behind thi ...

line of checkboxes aligned horizontally with the help of Bootstrap

My goal is to create a horizontal row of checkboxes within a form. I've been able to achieve a functional but unattractive version by using the following code: div.form-group(ng-show = 'avariable') label 1 input(type = 'checkbo ...

Can you tell me which specific font Adobe Edge Code (Brackets) uses in its code editor?

Can someone please help me? I'm trying to identify the syntax highlighter being used in this code. Is it Prettify or something else? ...

What methods are available to keep a component in a fixed position on the window during certain scrolling intervals?

I need help creating a sidebar similar to the one on this Airbnb page. Does anyone have suggestions for how to make a component stay fixed until you scroll past a certain section of the page? I am working with functional React and Material-UI components, ...

Is it practical to extract the page type/name from the CSS of the selected tab?

My website has multiple tabs on a single page and I want to integrate Google Analytics code. However, since all the tabs are part of the same page, I need a way to track which tab the user is interacting with. Would using CSS to check for the selected tab ...

Ensuring Navigation Elements Remain Aligned in a Single Line

I'm in the process of developing an interactive project that will be showcased on a touch screen. One of its key features is a fixed footer navigation. Currently, I am using floats to position the main navigation elements and within each element, ther ...

Bidirectional binding of attributes in an Angular directive

I have developed a custom Angular Directive known as evoEventMirror. Its main purpose is to attach a jQuery event to an inserted element and apply a specified CSS style to the "root" element. Refer to the example below: <div id="#mydiv" evo-event-mirro ...

Padding in Bootstrap rows is not uniform

My grid template has 3 columns, but the first row in one of the columns is slightly pushed. Which CSS property should I adjust to fix this issue? Using Bootstrap framework.https://i.sstatic.net/CLSYK.png <div class="col"> <div class="col-sm"> ...

Animating Angular ng-template on open/close state status

I am looking to add animation when the status of my ng-template changes, but I am unable to find any information about this component... This is the code in my report.component.html <ngb-accordion (click)="arrowRotation(i)" (panelChange)="isOpen($even ...

Utilize Bootstrap 4 to seamlessly integrate a small element between two lengthy items in a row, ensuring a gap

I would like to achieve a similar look to this design. https://i.sstatic.net/p1hq4.png hr { border: none; color: black; background-color: black; height: 5px; margin-top: 10px; } <div class="row code-line"> <div class="col-5 ...

Choosing an option will display a specific div while hiding another using JQuery

My question involves a Select Option <select class="input_select" name="customer_type" id="central_company"> <option value="standard">Standard</option> <option value="central">Central Station</option> </select> ...

Using border-spacing on table body rows exclusively

In my table, I have 2 header rows and multiple body rows. To create a spacing of 10 pixels between body rows, I used the following CSS: border-collapse: separate; border-spacing: 10px; However, this also affected the spacing in the header rows, which I w ...

Does defining the width and height of images go against the principles of material design?

Currently, I am immersed in a project utilizing material design. In this context, I find myself contemplating the size of an image that has been hard-coded as (200x200). I am curious to know if this approach aligns with the principles of material design. ...

Is it possible to change the text displayed when hovering over various images using just CSS and HTML?

I have created an image and text hover effect. There are four images and corresponding paragraphs for each image. When hovering over an image, the specific paragraph should replace the previous one using only HTML and CSS. Please note the following: The de ...

Continual dark mode throughout page navigation with the ability to switch between light and dark modes

I've been experimenting with creating a persistent dark mode feature for web pages. My goal is to remember the user's preference as they navigate between pages. Additionally, I included a toggle button for switching between dark and light modes t ...