Designing a responsive two-column table layout

My current challenge involves a small table structured as a bullet list with two columns. To enhance mobile display, I am seeking a way to collapse it down to a single column:

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

The current implementation includes the following code snippet:

<style>
@media (min-width: 600px)
{
  ul > li > span
  {
    display: inline-block;
    width:   250px;
  }
}

@media (max-width: 599px)
{
  ul > li > span:first-child
  {
    display: block;
  }
}
</style>

<ul>
  <li><span>E-mail:</span> <span><a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e1808382a199989bcf9284">[email protected]</a>"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f8999a9bb8808182d68b9d">[email protected]</a></a></span>
  <li><span>Telefon:</span> <span><a href="tel:+46701234567">070-123 45 67</a></span>
  <li><span>Adress:</span> <span>Föreningen XYZ</span><br>
  <span></span> <span>Storgatan 1</span><br>
  <span></span> <span>123 45 Småstad</span>
  <li><span>Swish:</span> <span><a href="tel://1234567890">123-456 78 90</a></span>
  <li><span>Bankgiro:</span> <span><a href="tel://1234–5678">1234-5678</a></span>
</ul>

However, I am not satisfied with the fixed column widths and would prefer them to adjust based on content. Additionally, another table I have utilizes text-align: right for the second column but needs left alignment when collapsed. My current solution lacks elegance, and I aim to avoid media queries if possible.

I have explored using Flexbox as an alternative but have experienced challenges achieving my desired layout.

Answer №1

Utilize HTML to structure it as a description list, then transform it into a flexible grid layout for larger screens.

Test the responsiveness by running this code snippet and using the full page option while adjusting your browser width accordingly.

/* Following a mobile-first approach with base styles focusing on the mobile layout */

dt::before {
  content: '• ';
}

dd {
  margin: 0 0 1em 1em;
}

/* Apply additional rules for wider screens in ascending order of width */

@media (min-width: 500px) {
  dl {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 1em;
  }

  dd {
    margin: 0;
  }
}
<dl>
  <dt>E-mail:</dt>
  <dd><a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a0c1c2c3e0d8d9da8ed3c5">[email protected]</a>"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b1d0d3d2f1c9c8cb9fc2d4">[email protected]</a></a></dd>

  <dt>Telefon:</dt>
  <dd><a href="tel:+46701234567">070-123 45 67</a></dd>

  <dt>Adress:</dt>
  <dd>Föreningen XYZ<br>Storgatan 1<br>123 45 Småstad</dd>

  <dt>Swish:</dt>
  <dd><a href="tel://1234567890">123-456 78 90</a></dd>

  <dt>Bankgiro:</dt>
  <dd><a href="tel://1234–5678">1234-5678</a></dd>
</dl>

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

NuxtJS - Google Auth: ExpiredAuthSessionError: The authentication session has expired because both the token and refresh token have expired. Please try your request

I've been using nuxtjs for some time now. Everything related to authentication was working smoothly until my last update. However, after updating the nuxt.config.js file and moving the axios plugin from the plugins array to the auth config object, an ...

jQuery Mobile elements remain resident in the memory

I'm currently working on a jQuery mobile app that includes a chart created with Highcharts, which can be exported using CanVG and canvas2ImagePlugin. However, I've noticed that the chart object remains in memory. As a result, if the page is opene ...

Utilize the bootstrap grid to align content into 4 columns evenly

I have the following code snippet: <div class="panel-body"> <div class="col-sm-6"> <label class="head6">Business Name : </label><span class="head9">'.$name.'</span> </div> <div ...

JavaScript function failing to validate password

While engaging in an online game where the objective is to uncover a password by inspecting the page source or element, I encountered a puzzling line: if(el.value == ""+CodeCode+""). My assumption is that el.value represents my guess, and it indicates that ...

What is the best way to delete spaces between span elements while preserving the spaces within each individual span tag?

Is there a way to eliminate unexpected spaces that appear between spans? One attempt was made by setting font-size: 0 within the wrapper <div>, which successfully removed spaces not only between <span> tags but also within each <span> ta ...

I have a specific resolution in mind where I want to run jQuery code during window scrolling

I have a jQuery code that I want to run only when the resolution is greater than 950px. If not, I risk losing the responsive logo. My goal is to create a navigation bar similar to the one on the Lenovo homepage. $(document).ready(function(){ //left ...

Animating Font Awesome content through CSS transitions

I am looking to animate a font awesome pseudo element on my website. Below is the HTML code I am working with: <li class="has-submenu"> <a onclick="$(this).toggleClass('open').closest('.has-submenu').find('.submenu&a ...

Utilizing AJAX to dynamically update a div's content by extracting a specific div from the retrieved data

Although I believe my code is correct, I am not very familiar with AJAX and have been struggling for hours to get it right. I've tried various approaches, including using filters, but nothing seems to work. The issue I'm facing is that the chat m ...

Discrepancies in Span Element Behavior Between Firefox and Chrome

Seeking assistance with a tooltip feature that reveals extra information when a user hovers over a span. The issue arises when the span is split across two lines, causing the extra information to appear in different positions on Firefox and Chrome browsers ...

Problem with HTML relative paths when linking script sources

Having trouble with a website I constructed using Angular. The problem lies in the references to javascript files in index.html. The issue is that the paths in the HTML are relative to the file, but the browser is searching for the files in the root direct ...

Leveraging the Bootstrap 3 audio player, although displaying a standard HTML5 output

When trying to use the Bootstrap 3 player to display an audio player, I'm encountering an issue where the page only shows a default black HTML5 audio player output. Here is my current directory structure: miuse/ application/ bootstrap/ ...

Conflicting events arising between the onMouseUp and onClick functions

I have a scrollbar on my page that I want to scroll by 40px when a button is clicked. Additionally, I want the scrolling to be continuous while holding down the same button. To achieve this functionality, I implemented an onClick event for a single 40px s ...

Enhancing list item appearance by replacing bullets with Font Awesome icons

I need to create a list similar to this example https://i.sstatic.net/w84xS.png Although I successfully replaced the bullet with a fontawesome icon, the result is not quite right as shown here https://i.sstatic.net/yqnbJ.png You may notice a difference ...

Ensure two CSS components occupy the entirety of their container, positioned next to each other, with margin spaces between

I am looking to make two elements take up a precise percentage of the parent's width, while also including margins between them. Here is my current setup: <div class='wrap'> <div class='element'>HELLO</div>< ...

There appears to be an inexplicable issue hindering the functionality of smoothscroll in

Continuing to improve my website: I have implemented a button for scrolling down the page, but I would like to add smooth scrolling functionality for a better user experience. After some research and experimentation, I came across this compact script tha ...

The function has exceeded the time limit of 60000 milliseconds. Please make sure that the callback is completed

Exploring the capabilities of Cucumber with Protractor has been an intriguing journey for me. As I delved into creating a feature in Gherkin language, outlining the steps and scenarios necessary for my end-to-end tests, a new world of possibilities opened ...

Ways to streamline a form containing several duplicate rows and make it more user-friendly

Looking for some advice on implementing a "working hours" module. Just need something simple like: Monday: 10:00 - 18:00 ... Saturday: 11:00-16:00 with some additional info. I'm currently attempting to... <form id="working_hours"> <s ...

Make sure all of the inner tags are properly contained within their respective containers

In my explanatory bubble, I aim to include text within the explain-header div as a container for my explanations: /*Explain Bubble*/ .explain-container { position: relative; overflow: hidden; max-width: 70vw; max-height: 50vh; background-c ...

What is the best way to import my json information into an HTML table and customize the rows as radio buttons using only Javascript?

I am facing an issue with processing a JSON response: var jsondata = dojo.fromJson(response); There is also a string that I am working with: var jsonString = jsondata.items; The variable jsonString represents the JSON data in the file: jsonString="[ ...

Prioritize the timepicker over the use of a modal window

Having an issue with my time picker in Angular being blocked by a modal window. Component.ts open() { const amazingTimePicker = this.atp.open(); amazingTimePicker.afterClose().subscribe(time => { console.log(time); }); } // T ...