What is the best way to utilize flexbox to create a table-like appearance with vertically centered columns and alternating row shading?

In my quest for vertically aligned columns using flexbox, I stumbled upon this solution:

jsFiddle

.container {
  display: flex;
  flex-direction: row;
}
.column {
  display: flex;
  flex-direction: column;
  margin: 0.3em;
}
.column > div:first-child {
  font-weight: bold;
}
<div class='container'>
  <div class='column'>
    <div>food</div>
    <div>beans</div>
    <div>macademia nuts salted and coated with caramel</div>        
  </div>
  <div class='column'>
    <div>comments</div>
    <div>beans</div>
    <div>excellent nutrition</div>                
  </div>
  <div class='column'>
    <div>price</div>
    <div>3$</div>
    <div>20$</div>                
  </div>            
</div>

Even though the above method was effective, a new challenge arose when I attempted to incorporate zebra shading into the mix, resulting in visible margins between colors as evidenced in this jsfiddle.

My dilemma now is how to leverage flexbox to create a tabular appearance with aligned columns and seamless zebra shading.

Answer №1

Swap out margin for padding.

Use padding specifically on inner columns:

/* UPDATED RULE */
.column + .column > div {
  padding-left: 2em;
}

/* ORIGINAL CODE */
.container {
  display: flex;
  flex-direction: row;
}
.column {
  display: flex;
  flex-direction: column;
}
.column > div:first-child {
  font-weight: bold;
}
.column > div:nth-child(odd) {
  background: #dddddd;
}
<div class='container'>
  <div class='column'>
    <div>food</div>
    <div>beans</div>
    <div>macademia nuts salted and coated with caramel</div>
  </div>
  <div class='column'>
    <div>comments</div>
    <div>beans</div>
    <div>excellent nutrition</div>
  </div>
  <div class='column'>
    <div>price</div>
    <div>3$</div>
    <div>20$</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 causing my nested class to be treated as a sibling rather than a child in HTML code

I have been searching for a clear answer to my query, but haven't found one yet. In my HTML script, I have nested divs structured like this: <ul id="navbar"> <li><a href='#' class='dropdown'>Rules</a> ...

Can you guide me on implementing CSS Houdini in Next.js?

Exploring the world of CSS Houdini in my Next.js project has been quite an adventure. After successfully installing the necessary CSS Houdini package and css-paint-polyfill using yarn, I decided to follow the webpack guidelines provided at . Below is a sn ...

What is the most dependable method for referencing a CSS class through programming?

Within my Sharepoint project, I am dynamically generating controls in the overridden CreateChildControls() method. I am uncertain which approach is more preferable when it comes to referencing the .css file: protected override void CreateChildControls() { ...

Minimize the screen dimensions and adjust the margins accordingly

Having a problem with responsive margins? Take a look at this site as an example: When I shrink the screen size, the margins decrease and smoothly transition from 4 to 2 columns. In my case, it does something similar but the issue is that the margin does ...

The sub-navigation element goes beyond the traditional navigation bar and causes misalignment

I'm currently in the process of developing sub navigation bars that expand upon hover. However, I've encountered an issue where hovering over "Manage" triggers the regular box to expand. This is happening because of the position: relative within ...

The default values for CSS filters

I have a keen interest in various CSS filters: blur brightness contrast grayscale hue-rotate invert opacity saturate sepia Could someone provide the default values for each filter (preferably as a % value, where applicable)? The MDN documentation does no ...

How can I ensure that my content stays fixed at the bottom of a scrolling div in Material UI React?

I have developed a React JS component using Material UI that includes a chat input feature. However, I am facing an issue where the width of the chat input is always half the screen size on desktop or mobile devices. When I try to change the width to 100%, ...

Navigate back to the previous HTML page while maintaining the existing data without causing a refresh

I have implemented an onLoad function in my HTML page to retrieve data from the server and update table rows using AngularJS ng-repeat. However, I am facing an issue where every time I navigate to another HTML page and return, the onLoad function execute ...

Encountering an unanticipated reference error while attempting to paste the data

// Modify the layout function document.addEventListener('DOMContentLoaded', function() { function modifyLayout(productId) { // Referencing the original card and detailed card const originalCard = document.querySelector(&ap ...

Generate a standard Wordpress menu containing all pages without the need to manually add each page in the menu editor

I'm struggling to figure out how to display all pages in WordPress without manually adding them to a menu. Instead of creating a new menu and selecting specific pages, I want to automatically show all existing pages in the menu. For example, if I ha ...

Ways to perform a jQuery selection beginning with a pre-existing jQuery object

When working with Vanilla JavaScript, you can select an element from the DOM using the following method: let element = document.querySelector('[css selector]'); Once you have the element selected, you can further target elements within it using ...

Solutions for altering the appearance of a combobox using CSS

Currently, I am experimenting with javafx 2 and trying to modify the background color of the menu/list through CSS. Despite attempting the code snippet below, nothing seems to be working as expected. Do you have any suggestions or insights on how to achi ...

How can I temporarily change an image when clicking on it using JavaScript?

I am working on an image section where I want to change the image for a few seconds when clicked and then revert back to the original image. Here is the code I have tried: <img src="contacticons.jpg" usemap="#image-map" id="imgName"> <a onclick ...

Using clearfix on every div element is essential to avoid container collapsing

Would it be beneficial to include clearfix in every div element to avoid container collapse issues? Or is it more practical to apply it only to specific classes, like so? <div class="container"> Additional div elements... </div> .containe ...

How can I assign a true or false value to an input variable in HTML using AngularTS?

I copied the following HTML code: <tag-input fullWidth id="inputDir" formControlName="inputDir" [modelAsStrings]="true" [placeholder]="'choice feature'" ...

What is the best way to adjust text to a specific width on an HTML canvas?

Is there a way to adjust the width of a single-line text string precisely on an HTML5 canvas? My current method involves initially setting a font size, measuring the text's width using measureText(my_text).width, and then determining a new font size b ...

Guide to using Ajax to send a form and receive text in response

Check out my code on Fiddle: $("form.signupform").submit(function(e) { e.preventDefault(); var data = $(this).serialize(); var url = $(this).attr("action"); var form = $(this); // Added this line for reference $.post(url, data, function(data) { $(for ...

Reset the queue for the slideDown animation using jQuery

I am experiencing an issue with my code where multiple animation effects are running simultaneously. Specifically, when I hover over navbarli1 and then move to another li element with the same navbarli1 class, the slide-down effect is not working as expect ...

The selection choices in the HTML <select> dropdown appear oversized on Chrome browsers

There has been a recent change in Chrome versions, likely in June 2017, that is causing options in a <select> input to appear much larger than in other browsers or older versions of Chrome. For instance, on some machines, the dropdown on this w3scho ...

How to implement a scrollbar for tables using Angular

How can I implement a vertical scroll bar only for the table body in my Angular code? I want the scroll bar to exclude the header rows. Since all rows are generated by ng-repeat, I am unsure how to add overflow style specifically for the table body. Here ...