Alternative to using table-row for generating table breakpoints in CSS, consider using a

Currently, my table layout looks like this:

<div style="display:table">
  <div style="display:table-row">
    <div style="display:table-cell">Content 1</div>
    <div style="display:table-cell">Content 2</div>
    <div style="display:table-cell">Content 3</div>
    <div style="display:table-cell">Content 4</div>
  </div>
</div>

With the above code, I am able to successfully display all 4 columns in a single row.

Now, I am looking to create a media query that will modify the table so that it displays only 2 columns per row.

Despite the need for formatting consistency, I prefer to stick with using a table approach rather than switching to flexbox or a bootstrap grid.

Are there any alternate methods available to introduce breakpoints between cells and achieve the desired results purely through CSS?

Answer №1

To adjust the display to a different value, use a media query

element {
  display: inline-block;
}
@media (max-width: 600px) {
  element {
    display: block;
  }
}

Here's a snippet from your code with an added var() function to maintain your inline style...

:root {
  --parent: table;
  --row: table-row;
  --cell: table-cell;
}

@media (max-width:800px) {
   :root {
    --row: grid;
    --cell: block;
  }
  [style="display:var(--row)"] {
    grid-template-columns: repeat(2, auto)
  }
}

body>* {
  margin: auto;
  border: solid 1px;
}

body> div>div>div, td {
  border: solid 1px;
  padding: 3px;
  margin: 3px;
}
<div style="display:var(--parent);">
  <div style="display:var(--row)">
    <div style="display:var(--cell)">Content 1</div>
    <div style="display:var(--cell)">Content 2</div>
    <div style="display:var(--cell)">Content 3</div>
    <div style="display:var(--cell)">Content 4 ---</div>
  </div>
</div>
Display:reset works for any HTML element, even if its an HTML table..
<table style="display:var(--parent);">
  <tr style="display:var(--row)">
    <td style="display:var(--cell)">Content 1</td>
    <td style="display:var(--cell)">Content 2</td>
    <td style="display:var(--cell)">Content 3</td>
    <td style="display:var(--cell)">Content 4 ---</td>
  </tr>
</table>

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

Toggle the jQuery class off when a different element is chosen

I've created a function that toggles a class to change the style of an element when it is selected. However, I'm facing an issue where the new style remains even when another element is selected. Is there a way to make the styling revert back to ...

Using jQuery.dataTables to choose all rows except the final column

I'm utilizing a table with jQuery.dataTables. I have a requirement to input dates in the last column. However, when I click to input a date, the row becomes deselected. The following code is meant for selecting all rows: Displayed below is the DataT ...

Stop the border from curving along with the container's border radius

Currently, I am working on a tabs component that features a container with border radius. When a tab item is selected, it should display a bottom border. However, I'm facing an issue where the border curves along with the border radius instead of rem ...

Arranged in individual rows, the items are placed from left to right

I am looking to display multiple rows of data, with each row potentially having a spacer element that shifts the text on that row to the right. The spacer needs to be contained in its own div element for styling purposes such as adding borders or backgroun ...

Arranging components in a horizontal layout on Jquery mobile

I'm completely new to Jquery mobile and I'm having trouble getting these elements aligned. Here's my code, but I need the two icons to line up horizontally with the caption "Heading". <h3>Heading</h3> <img src="../re ...

Utilize custom fonts from your local directory within a Vite Vue3 project

Inside my main.scss file, I am loading local fonts from the assets/styles/fonts folder: @font-face { font-family: 'Opensans-Bold'; font-style: normal; src: local('Opensans-Bold'), url(./fonts/OpenSans-Bold.ttf) format('truety ...

Challenges Arising from the Featured Image Dimensions in WordPress

I'm currently experimenting with the featured image on this website powered by WordPress. The issue I'm facing is that the image is using a custom size instead of the size I intended. Additionally, it seems to be inheriting the characteristics of ...

Redefining the colors of an SVG when it is a CSS background image in code

I currently have an SVG icon encoded within my CSS file. I'm looking to change the color of this icon when a user hovers over it without creating a duplicate icon in a different color. Here's a snippet from my CSS file: background-image: url("d ...

Tips for adjusting text color in a paragraph based on its content

I have a paragraph or h1 with phrases like "the color of this item is Magenta" or "this output is Red". These color-specific words (red, black, cyan, magenta or yellow) may appear within the paragraph or h1 tag. Here is my current code snippet: HTML < ...

What is preventing the textbox from resizing to the full div in IE? Everything looks good in Chrome

Having trouble with IE Resizing? The following code seems simple but it's causing issues: A div containing a text-box that should take up 100% of the DIV while still showing the red border. Unfortunately, setting height and width to 100% causes the bo ...

What is causing the navigation bar items to shift with the logo when attempting to position it to the left?

Whenever I attempt to shift .logo to the left, it also drags all the other navbar items (Home, Features, Pricing, Disabled) along with it. All I want is for .logo to move to the left without affecting the positioning of the other navbar items. I should me ...

Ensure that the contents of the upper and lower divs are appropriately aligned, while maintaining the centered positioning of the

As a complete beginner in the world of HTML and CSS, I am facing a challenge with my code. Here's the HTML snippet: <section class="instructions"> <div class="row"> <div class="instructions-col" ...

The Bootstrap carousel comes with a brilliant feature! Conceal those 'previous' and 'next' buttons when you reach the first and last slide. However, it seems like the 'slide' event is not functioning as

I have implemented a standard Bootstrap carousel on my website: <div id="myCarousel" class="carousel slide" data-ride="carousel"> <!-- Indicators --> <ol class="carousel-indicators"> <li data-target="#myCarousel" data- ...

Hide the navigation bar elements specifically on Safari browser when using Bootstrap v4.5.3

I'm having an issue with my Bootstrap v4.5.3 navbar not displaying correctly on Safari browser. All I see is the colored line, no text or hamburger icon. Can anyone help me with this? Here is my Navbar Code: <nav class="navbar navbar-expand-l ...

CSS with a "true" space for a fixed element (ensuring the previous element does not overlap with the current fixed element)

I am facing an issue with the layout of my webpage. I have two horizontal columns - the left one is fixed, along with a bottom footer that is below the second content column. When I add a border to the content column, it extends all the way down to the fo ...

Safari and Chrome browsers (using the Webkit engine) do not support hiding the vertical scrollbar on if

I currently have an iframe embedded on www.example.com that is linked to support.example.com (which is a CNAME to a foreign domain). To ensure the contained webpage fits perfectly without needing any scrollbars, I automatically adjust the height of my ifr ...

Limit the y-axis on CanvasJS visualization

Is it possible to adjust the minimum and maximum values on the y-axis of the graph below: new CanvasJS.Chart(this.chartDivId, { zoomEnabled: true, //Interactive viewing: false for better performance animatio ...

After clicking, an created modal is displayed, then disappears on its own

After creating 2 Modals, I encountered an issue where the second modal with the class "category-modal" reverts back to display none when I click the "+" sign on the form. The first modal, with the class "modal," displays correctly when the add button is cl ...

Android Webkit does not properly respect z-index when it comes to absolutely positioned elements

This tricky bug is causing quite the headache. Check out Android ticket 6721 for a demonstration of how the Android browser fails to adhere to z-index rules with absolutely positioned elements overlapping <a> or <input> tags. I desperately nee ...

What is the best way to ensure a string of words in HTML/CSS wraps to the next line seamlessly?

As I work on creating my portfolio website using Bootstrap and custom CSS, I am running into an issue with the game titles breaking in the middle when displayed. Despite my limited proficiency in English, I tried searching for a solution without success. ...