Utilizing CSS Flexbox for both desktop and mobile layouts

As I venture into the world of FLEXBOX, I find myself pondering a layout challenge that involves both Desktop and Mobile views.

The content I need to display consists of labels and corresponding data (not part of a form).

For Desktop view, my vision is for it to look like this:

label-------label-------label------label

Data--------Data--------Data-------Data

On Desktop, the width of label and data will be based on percentages depending on the number of elements present.

However, when switching to mobile, I envision the layout to transform into this:

label------ Data-------

Label------ Data-------

label------ data-------

Label------ Data-------

label------ data-------

When viewed on a mobile device, I aim for the label and data segments to each occupy 25% and 75% respectively.

I'm unsure whether the HTML setup should follow this structure:

<div class="container">
   <div class="label">Label</div>
   <div class="data">Data</a>
   <div class="label">Label</div>
   <div class="data">Data</a>
   <div class="label">Label</div>
   <div class="data">Data</a>
</div>

OR

<div class="container">
   <div class="label">label</div>
   <div class="label">label</div>
   <div class="label">label</div>
</div>
<div class="container">
   <div class="data">data</div>
   <div class="data">data</div>
   <div class="data">data</div>
</div>

Any advice or pointers on how to approach this would be highly appreciated! Thank you!

Answer №1

It seems like you are open to different HTML structures.
My suggestion is to organize data pairs using parent elements to create columns and rows effectively.
By utilizing two nested flexboxes, you have the flexibility to adjust the flex-direction as needed.

/* DEMO PURPOSES ONLY */
var $body = $('body');
$('button').on('click', function() {
  $body.toggleClass('small');
});
.container {
  display: flex;
  flex-wrap: wrap;
  background-color: lightgreen;
}

.group {
  display: flex;
  flex-direction: column;
}

.item {
  padding: .25em .5em;
}

@media (min-width: 700px) {
  .container {
    background-color: lightblue;
    flex-direction: column;
  }
  .group {
    flex-direction: row;
  }
}




/* DEMO PURPOSES */

.small .container {
  background-color: lightblue;
  flex-direction: column;
}
.small .group {
  flex-direction: row;
}
button {
  position: absolute;
  top: 100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<div class="container">
  <div class="group">
    <div class="item label">Label 1</div>
    <div class="item data">Data 1</div>
  </div>
  <div class="group">
    <div class="item label">Label 2</div>
    <div class="item data">Data 2</div>
  </div>
  <div class="group">
    <div class="item label">Label 3</div>
    <div class="item data">Data 3</div>
  </div>
</div>


<button>Simulate Size Change</button>

To showcase multiple rows of data in a demo:

/* DEMO PURPOSES */
var $body = $('body');
$('button').on('click', function() {
  $body.toggleClass('small');
});
.container {
  display: flex;
  flex-wrap: wrap;
  background-color: lightgreen;
}
.group {
  display: flex;
  flex-direction: column;
}
.item {
  padding: .25em .5em;
}
.label {
  font-weight:bold;
}
.flag {
  color:red;
}

@media (min-width: 700px) {
  .container {
    background-color: lightblue;
    flex-direction: column;
  }
  .group {
    flex-direction: row;
  }
}


/* DEMO PURPOSES */

.small .container {
  background-color: lightblue;
  flex-direction: column;
}
.small .group {
  flex-direction: row;
}
button {
  position: absolute;
  top: 130px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="container">
  <div class="group">
    <div class="item label">Label 1</div>
    <div class="item data">Data 1a</div>
    <div class="item data flag">Data 1b</div>
    <div class="item data">Data 1c</div>
  </div>
  <div class="group">
    <div class="item label">Label 2</div>
    <div class="item data">Data 2a</div>
  </div>
  <div class="group">
    <div class="item label">Label 3</div>
    <div class="item data">Data 3a</div>
    <div class="item data">Data 3b</div>
  </div>
</div>


<button>Simulate Size Change</button>


If you're looking for more tips, I recommend exploring Accessible, Simple, Responsive Tables @ CSS-Tricks for valuable insights and resources on building responsive tables.

Answer №2

I prefer the first option for ordering. For desktop view, I would implement a flex flow of column wrap, specify a height for the container, and set each flex-item to be 50% in height.

When it comes to mobile view, I would switch to flex-flow: row wrap, define the container's width, and adjust each child to take up 25%/75% depending on the specific child.

Here's the HTML structure:

<div class="container">
   <div class="label">Label</div>
   <div class="data">Data</a>
   <div class="label">Label</div>
   <div class="data">Data</a>
   <div class="label">Label</div>
   <div class="data">Data</a>
</div>

And here's the corresponding CSS:

.container {
  display: flex;
  flex-flow: row wrap;
  justify-content: space-evenly;
  align-items: center;
  height: 500px; // Set your preferred height here
}

.label, .data {
  height: 45%; // Slightly less than 50% to account for margins
}

@media only screen and (max-width: 900px) { // Define 'mobile' based on desired width
  .container {
    flex-flow: row wrap;
    width: unset;
    height: 500px; // Adjust as needed
  }

  .data {
    width: 70%;
  }

  .label {
    width: 22%;
  }
}

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 make an input text the focus when an item on a dropdown menu is clicked?

I have a website with a dropdown menu and an input box. I want the user experience to be more seamless by automatically focusing the mouse cursor inside the input box when they click on an option in the dropdown menu. This way, users can start typing right ...

Creating a toggle label using just CSS: a step-by-step guide

Recently, I was trying to create a toggle label using HTML, CSS, and JavaScript. Here is the code snippet that I experimented with: function genre_itemclick(item){ if(item.classList.contains('light_blue_border_button')) { ...

Conflicting CSS media queries causing style inconsistencies

Why does the order of media queries affect which style is applied? @media screen and (min-width:660px){ body{ background:darkgreen; } } @media screen and (min-width:480px){ body{ background:navy; } } When the code is written in this order, ...

Difficulty loading CSS in PugJS

I can't seem to get my dashboard.pug file to load both dashboard.css and dashboard.js, even though they are all in the same folder. I have already set up express.static correctly for the public folder. const publicpath=path.join(__dirname,'../p ...

Accordion Widget in Bootstrap fails to initiate with proper icon

I recently designed a webpage showcasing different parts of a privacy policy. Using Bootstrap and its accordion feature, I successfully displayed the information with standard icons. However, I encountered an issue where the accordion initially loads with ...

Discover the secret to halting a CSS animation precisely on the final nth-child in CSS3

I have encountered some difficulty in stopping a crossfade animation on the last nth-child element. Although I am aware of using animation-fill-mode: forwards, implementing it in different places such as in the initial .crossfade declaration did not yield ...

Create the design shown below without using the <pre> tag

Creating this pattern using <pre> is straightforward, but I am interested in achieving the same result with <div> Is it possible to do this with margins? I attempted to use margins and was able to achieve success, although some patterns prove ...

Scroll upwards within a nested div

Is there a way to smoothly animate the content of the inner div upward without requiring any button clicks? Also, is there a method to trigger an event once the animation has completed? ...

Eliminate any vacant areas within a container and shift the container, along with its contents, on the webpage

I want to eliminate the empty space within the container while maintaining some spacing around the black box, and ensure responsiveness. Additionally, how can I shift the container with its content downwards and to the right of the webpage, while still ke ...

Div with Navigation Bar

Struggling to create a Nav Bar using Divs and aligning the links side by side? Looking for some guidance on how to achieve this design? Lorem Ipsum placeholder text added here. Lorem Ipsum ad usu quem delectus, sea stet sale id. Nulla nostrud appetere ea s ...

Embed a webpage into a PowerPoint presentation

This question may be unconventional, but I am in search of a PowerPoint add-in that allows me to insert webpages for free. I have previously used LiveWeb, however, the browser does not support CSS3 and HTML5, so I need one that does. Can you suggest any a ...

CSS Only Sidebar Dropdown (without using Bootstrap)

I want to create a sidebar with collapsible dropdown options and smooth animations using only pure css/js/html/jquery, without relying on bootstrap like I did in the past. Here is an example of what I created previously with bootstrap: Now, as a challenge ...

Is it possible to assign the margin-bottom property of one element to be equal to the dynamic height of a sibling element?

I am in the process of creating a website that features a fixed (non-sticky) footer placed behind the main content using the z-index property. The footer only becomes visible when the user scrolls to the bottom of the page, achieved by assigning a margin-b ...

Mastering Instagram Automation: Techniques for Navigating the Lightbox with Selenium

I am in the process of developing a Python script that assists users in Instagram engagement groups by efficiently liking everyone's photo during each round. I am facing an issue where Selenium is unable to click on the first photo when I reach a user ...

Tips for creating a div element that closes on the second click:

On the PC version, I have three blocks that open and close perfectly when clicked. However, on the mobile version, when I click on one block, it opens but does not close unless I click on another block. Additionally, if I click again on the same block th ...

Exploring different ways to customize the grid style in Angular 6

Here is the style I am working with: .main-panel{ height: 100%; display: grid; grid-template-rows: 4rem auto; grid-template-columns: 14rem auto; grid-template-areas: 'header header''sidebar body'; } I want to know how to cha ...

Display the list of articles in a three-column format

Is there a way to create CSS rules to display articles in three columns, like the example shown in the image below? The preferred HTML markup is: <div>1</div> <div>2</div> <div>3</div> ... <div>6</div> The ...

Maintain the alignment of content in the center while expanding background images

I am looking to create a website with a split background effect, similar to the design on this site . I want to maintain a content container width of 980px while achieving this look. I attempted to accomplish this by adding height and background color pro ...

Would it be feasible to incorporate an additional icon next to the arrow in a bootstrap accordion?

I'm currently working with a bootstrap 4 accordion and I have been attempting to include an additional icon alongside the default arrow up/down icon. Although I tried nesting both icons inside a row, I still can't seem to figure out how to align ...

When an image is a child of the parent content and another element has opacity set, the CSS stack order will be altered

Recently, I came across an interesting issue while working with CSS, particularly with negative margins to stack elements on top of each other. I am aware of the natural stack order in which elements overlap, where the later element always appears on top ...