currently experiencing layout discrepancies with Flexbox

Experimenting with flexbox has been challenging, but I am close to achieving my desired layout:

  1. Place a label in the top left of the first container
  2. Position a label in the top right of the first container
  3. Align a label in the bottom middle of the first container
  4. Create a box that displays "number" on top of "Label"
  5. Add text to the center area
  6. Display multiple of these containers on the same line (currently they stack)

Current Issues

  1. Need spacing between the green and blue boxes. Attempted to use "space-around" but it's not achieving the desired effect. Also, looking to make the containers the same size for responsiveness.
  2. Want the text in the center to be aligned at the center of itself, not just the "p" tag
  3. Would like to fit more than one container on a line for bigger screens. Unsure if another flex container needs to be wrapped around it.
  4. Improve spacing between the root containers.

.flex-container {

  display: flex;

  flex-direction: column;

  background-color: grey;

  padding: 0;


}

.flex-sub-container {

  display: flex;

  flex-direction: row;

  justify-content: space-between;

}

.flex-item-left-corner {

  /*background-color: red;*/

  margin: 0;

  padding: 0;

}

.flex-item-right-corner {

  /*background-color: red;*/

  margin: 0;

  padding: 0;

  justify-content: flex-end;

}

.flex-bottom-middle {

  /*background-color: red;*/

  align-self: center;

  margin: 0;

}

.flex-sub-container2 {

  /*background-color: yellow;*/

  display: flex;

  flex-direction: row;

  justify-content: center;

}

.flex-sub-item {

  background-color: green;

  display: flex;

  flex-direction: column;

  align-items: center;

  flex-grow: 1;

}

.flex-qty {

  margin: 0;

  font-size: 28pt;

}

.flex-qty-label {

  margin: 0 5pt 5pt 5pt;

}

.flex-center {

  background-color: royalblue;

  align-self: center;

  font-size: 15pt;

  flex-grow: 3;

}
<div class="flex-container card-panel teal lighten-2">
  <div>
    <div class="flex-sub-container">
      <p class="flex-item-left-corner">Left Top</p>
      <p class="flex-item-right-corner">Right Top</p>
    </div>
  </div>
  <div>
    <div class="flex-sub-container2">
      <div class="flex-sub-item">
        <p class="flex-qty">7</p>
        <p class="flex-qty-label">Label</p>
      </div>
      <p class="flex-center">This is my very long center text.</p>
    </div>
  </div>
  <p class="flex-bottom-middle">Bottom Middle</p>
</div>

<div class="flex-container card-panel teal lighten-2">
  <div>
    <div class="flex-sub-container">
      <p class="flex-item-left-corner">Left Top</p>
      <p class="flex-item-right-corner">Right Top</p>
    </div>
  </div>
  <div>
    <div class="flex-sub-container2">
      <div class="flex-sub-item">
        <p class="flex-qty">7</p>
        <p class="flex-qty-label">Label</p>
      </div>
      <p class="flex-center">This is my very long center text.</p>
    </div>
  </div>
  <p class="flex-bottom-middle">Bottom Middle</p>
</div>

Answer №1

Looking to create some space between the green box and the blue box. Attempted to use "space-around" but it doesn't achieve the desired effect.

Resolved by adding margin between them. The space options only function if there is actual space available...meaning the combined widths of the children are less than the parent width.

Desiring the containers to be of equal size but unsure how to make them responsive.

Want the text in the center to be aligned at the center of itself as well, not just within the "p" tag.

Addressed by enclosing the p in a div to ensure equal height of the two divs and then centering the text as usual.

Interested in having multiple containers fit on a single line on larger screens. Debating whether wrapping another flex container around it is necessary.

Indeed, a flex-container (with wrapping) can be added, but relevant media queries will also be needed.

Requesting spacing between the root containers.

Simply added margin-bottom.

.flex-container {
  display: flex;
  flex-direction: column;
  background-color: grey;
  padding: 0;
  margin-bottom: 1em;
}
.flex-sub-container {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}
.flex-item-left-corner {
  /*background-color: red;*/
  margin: 0;
  padding: 0;
}
.flex-item-right-corner {
  /*background-color: red;*/
  margin: 0;
  padding: 0;
  justify-content: flex-end;
}
.flex-bottom-middle {
  /*background-color: red;*/
  align-self: center;
  margin: 0;
}
.flex-sub-container2 {
  /*background-color: yellow;*/
  display: flex;
  justify-content: center;
}
.flex-sub-item {
  background-color: green;
  display: flex;
  flex-direction: column;
  align-items: center;
  flex-grow: 1;
}
.flex-qty {
  margin: 0;
  font-size: 28pt;
}
.flex-qty-label {
  margin: 0 5pt 5pt 5pt;
}
.flex-center {
  background-color: royalblue;
  font-size: 15pt;
  flex: 3;
  margin-left: 2em;
  text-align: center;
}
<div class="flex-container card-panel teal lighten-2">
  <div>
    <div class="flex-sub-container">
      <p class="flex-item-left-corner">Left Top</p>
      <p class="flex-item-right-corner">Right Top</p>
    </div>
  </div>
  <div>
    <div class="flex-sub-container2">
      <div class="flex-sub-item">
        <p class="flex-qty">7</p>
        <p class="flex-qty-label">Label</p>
      </div>
      <div class="flex-center">
        <p>This is my very long center text.</p>
      </div>
    </div>
  </div>
  <p class="flex-bottom-middle">Bottom Middle</p>
</div>

<div class="flex-container card-panel teal lighten-2">
  <div>
    <div class="flex-sub-container">
      <p class="flex-item-left-corner">Left Top</p>
      <p class="flex-item-right-corner">Right Top</p>
    </div>
  </div>
  <div>

    <div class="flex-sub-container2">

      <div class="flex-sub-item">
        <p class="flex-qty">7</p>
        <p class="flex-qty-label">Label</p>
      </div>
      <div class="flex-center">
        <p>This is my very long center text.</p>
      </div>
    </div>

  </div>
  <p class="flex-bottom-middle">Bottom Middle</p>
</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

Having difficulty swapping out images on an HTML website template for Internet Explorer

Recently, I obtained an HTML website template that I am currently working on customizing. One of the tasks I have been tackling is replacing some of the existing images with my own. After resizing the new images to match the dimensions of the originals, ...

Is there a way to position a pseudoelement behind its parent yet still in front of its grandparent using absolute positioning?

Imagine the following scenario: <div id="one"/> <div id="two"> <div id="three"/> </div> <div id="four"/> I am trying to apply a pseudoelement to three that appears behind it but in front of two (and any other ancestors if ...

Removing the gridlines in a Linechart using Apexcharts

I am experiencing issues with the grid and Nodata options on my Apexchart line chart. noData: { text: none , align: 'center', verticalAlign: 'middle', offsetX: 0, offsetY: 0, style: { color: undefined, fontSize: &apo ...

changing the css transformation into zoom and positioning

My goal is to incorporate pinch zoom and panning using hardware-accelerated CSS properties like transform: translate3d() scale3d(). After completing the gesture, I reset the transform and switch to CSS zoom along with absolute positioning. This method ensu ...

"Learn how to create a scrolling div using a combination of CSS and JavaScript with absolute and relative

After relying solely on "pre-made" components like Mui or TailWind, I decided to create a component using only CSS and maybe JavaScript. However, I encountered some difficulties when attempting to position a div inside an image using relative and absolute ...

Is there a way to lower just one border?

I need assistance with moving the .container div downward on my webpage. I attempted using (margin-top: 30px) and it worked, but unfortunately, it also shifted down the border of the (.container) along with the outline of .all. My goal is to have only the ...

Problem with displaying Django input fieldsSolution for missing Django input

Currently, I am in the process of constructing a website using Django. One of my tasks involves transferring input from index.html to about.html through view.py. However, despite successfully passing the input as seen in the URL in the browser's addre ...

Validating radio buttons in Ionic framework

I am currently working on implementing a form in my application using Ionic and AngularJS. I have added validation to all the fields, but I am facing an issue with the radio button validation. The error message is being displayed correctly when no radio bu ...

Encase a group of div elements with identical styling

Looking for a solution on how to programmatically wrap a set of divs with a parent div based on a certain class match. Any suggestions or ideas on how to achieve this? ...

Using ngFor in Angular 6 to create a table with rowspan functionality

Check out this functional code snippet Desire for the table layout: <table class="table table-bordered "> <thead> <tr id="first"> <th id="table-header"> <font color="white">No.</font> </th> <th id="table-hea ...

changing web pages into PDF format

I need to convert HTML to PDF on a Linux system and integrate this functionality into a web application. Can you suggest any tools that are capable of doing this? Are there any other tools I should consider for this task? So far, I have attempted the foll ...

The challenges with implementing makeStyles in React Material UI

const useStyles = makeStyles((theme) => ({ toolbarMargin: { ...theme.mixins.toolbar, marginBottom: "3em", }, logo: { height: "7em", }, tabContainer: { marginLeft: "auto", }, tab: { ...theme ...

The collapsible menu is not expanding properly after a click event due to the small size of

My website has a menu placed in the header with a background color. When resizing the page (max width 1044 px), it switches to a mobile menu, but the div holding the menu and anchors is too small compared to the header. Is there a solution to this issue? ...

Altering the backdrop upon hovering over an element

As a beginner in Javascript and Jquery, I am working on creating an interactive feature where hovering over one element changes the background image in another column. I have managed to write the function, but now I want to add an animation to the image tr ...

Having trouble changing font color using AngularJS - what am I missing?

I am currently working on a straightforward program where the user inputs text into a textbox, and that text is displayed below. However, I also want the font color to change based on what the user types in the "color" field. Why isn't this functional ...

Tips for effectively displaying HTML data from a database on a web browser

Storing html data in a database is a common practice for many web developers. The html data generated by a wysiwyg editor is typically simple and straightforward. Prior to storing the html data in the database, it is essential to run it through HTMLPurif ...

Dynamic Cell Class Assignment in Ag Grid

My Div's dimensions can change based on user interaction, with the div containing an ag-grid component. Initially, the div/grid loads in a compressed size, so I've applied specific classes (like small font-size, height, padding, etc.) to eliminat ...

A guide on updating CSS content dynamically within ExtJS

In my resource folder, I have a CSS file that contains some values which I need to change or add based on certain conditions. However, I'm struggling to figure out how to achieve this. Here is an example of the code: triggers: { clear: { ...

Using the calc function to define input width may not yield the expected results

I am facing an issue where I have an input element with width: calc(100% - 100px); and no padding/margin/border. Next to this input, I want to place a div with position: inline-block; and width: 100px;. The problem is that the div is going to the next lin ...

A new URL must be refreshed for the link to properly bind the data received from the API call with the subscribe method

In my Angular application, I have a URL link that fetches data from the backend API using the subscribe method in a component. The expected behavior is that when this URL is accessed in a browser, the data should be displayed in the corresponding HTML comp ...