Exploring optimal practices for CSS components and responsive design integration

If I were to develop a reusable component like a table that will be used across various pages, the content and number of columns may differ from one page to another, but the overall design should remain consistent.

.my-table {
    background-color: ... etc
    color: ... etc
}

When it comes to making this component responsive, should each page have its own set of media queries to adjust the appearance of the component, or should the component itself handle the queries?

For instance, you might want to hide specific columns when viewing on mobile on one page, while on another page with fewer columns, hiding those particular ones wouldn't make sense. How should the responsive behavior be managed in these scenarios? Should it be separate from the component styles?

Another scenario often encountered is how CSS guides suggest structuring layout classes such as:

.layout-columns-2 {
     float: left;
     width: 50%;
}

.layout-columns-4 {
     float: left;
     width: 25%;
}

These classes can be reused for creating side-by-side column layouts, but difficulties arise once responsive rules need to be applied.

Consider a situation where on one page, you wish to shift from floated columns to stacked columns at 600px width, while on another section of the same page, the transition should occur at 800px width. What would be the most effective way to approach this dilemma?

Answer №1

If you're looking to improve the structure of your tables, consider creating a Sass partial that contains table mixins. These mixins can then be applied selectively to different tables, allowing for better organization and easier styling adjustments.

To accommodate varying breakpoints per table, it's recommended to keep the default table styles as "mobile-first" and customize them individually as needed.

_tables.scss:

table {
  // Define global mobile-first styles here
}

@mixin columns-2() {
  td {
    float: left;
    width: 50%;
  }
}

@mixin columns-4() {
  td {
    float: left;
    width: 25%;
  }
}

// Function to calculate max breakpoint (1px less)
@function max-break($size) {
  @return $size - .0625em;
}

By utilizing this partial file, you can easily incorporate specific mixins for each individual table...

special-page.scss:

@import 'tables';

.my-table {
  // Hide columns 2/4 below 800px
  @media (max-width: max-break(50em)) {
    td:nth-child(2),
    td:nth-child(4) {
      display: none;
    }
  }

  // Implement 2 columns at 600px
  @media (min-width: 37.5em) {
    @include columns-2();
  }

  // Utilize 4 columns at 600px    
  @media (min-width: 50em) {
    @include columns-4();
  }
}

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

Struggling with aligning items vertically in Bootstrap 4?

Despite my experience in programming, I am struggling with CSS and HTML. I am attempting to create a simple "Login Page" for my personal web application. GitHub: https://github.com/n-winspear/cashflow-webapp After trying various solutions suggested in ot ...

Utilizing CSS to align all HTML form elements at the center

Is it possible to centrally align all form labels and input fields by using CSS? If so, what would be the approach for a form where the label text is displayed vertically above the input field? #fieldset label { display: block; } #fieldset input ...

The list countdown for loop only appears in the initial iteration

Hey there, I'm currently facing an issue with duplicating my JavaScript countdowns and displaying images of cards on each loop iteration. Strangely, the countdown only appears in the first instance even though it's within the loop. I'm seeki ...

Ways to update the div's appearance depending on the current website's domain

There is a piece of code that is shared between two websites, referred to as www.firstsite.com and www.secondsite.com The goal is to conceal a specific div only when the user is on secondsite. The access to the HTML is limited, but there is an option to ...

Centered div's overflow remains visible

Yes, the positioning of the parent container is set to relative. I am attempting to achieve a ripple effect by expanding multiple circles from a central point in all directions until they stretch beyond the viewport. I have created circular divs that are ...

Displaying items in the shopping cart across two separate columns

I have a website located at Within that page, I have included the Cart table using a woocommerce shortcode. Currently, the cart table is positioned below the billing details and payment options section. My goal is to move this cart table to be situated ...

Minimize the gap between col-lg elements in responsive mode within Bootstrap

My website is built using Bootstrap 4 and the following is the HTML code: <div class="container"> <div class="row text-center "> <div class="col-lg-6">A</div> <div class="col-lg-6">B</div> </div> < ...

Managing Positioning of PHP Print Statement

Recently, I developed a PHP script for my site that facilitates user registration and login specifically for membership purposes. If a user successfully logs in, the script redirects them to a landing page for members. However, if the login fails, the scri ...

Exploring the world of jQuery animation and background colors with Animate()

I'm currently attempting to implement a basic pulse effect by utilizing JQuery to modify the background color. However, I am facing issues with animating the backgroundColor property. function show_user(dnid) { /* dnid represents the HTML ID of a ...

How to change the file name of an HTML page in a website template?

After downloading a free dashboard website template from the internet, I've been trying to incorporate it into my own website. My main focus now is on creating a navbar that opens the relevant page upon clicking. To do this, I duplicated the html fi ...

Clicking on the title link will open the content in an iframe, but the image

Having trouble with a previous post but I've created a codepen to illustrate the issue. Hoping someone can help me out! Check out the codepen here: "https://codepen.io/Lossmann/pen/GRrXyQY" ...

"Maximizing the slider with jCarousel: A guide to enhancing your carousel experience

I recently added jcarousel to my website and things are looking good so far. Take a peek at how my site is currently set up: check it out! What I'm aiming for now is a feature where if a user clicks on an image to enlarge, it will open in a new tab ...

Determine the starting position of a div's CSS bottom using jQuery before the hover animation begins

Currently, I am in search of the CSS value 'bottom' for each div that belongs to the 'shelf-info-text' class. These particular divs can be found inside a parent div called 'shelf-item'. The bottom value is determined automati ...

Dynamic Mat-select-trigger that automatically adjusts its size to fit the content

Currently, I am experimenting with an Angular Mat-Select that allows multiple selections. To display the selected values in the value field, I have implemented a custom Mat-Select-Trigger. My goal is to enable automatic resizing of the value field (similar ...

Tips for customizing the background color of hyperlinks

Hello, I am trying to figure out how to create a hyperlink with alternating background colors. Can anyone guide me on achieving this? <div class="left_column"> <div class="category"> <p>Category</p> </div> <ul ...

Adjust Sidebar Height to Match Document Height (using React Pro Sidebar)

Having an issue with the height of a sidebar component in Next.js using React Pro Sidebar. It seems to be a JavaScript, HTML, and CSS related problem. I've tried several suggested solutions from Stack Overflow, but none of them seem to work. Surprisin ...

How can you position text to the right of an image using CSS?

Trying to right-align text next to an image; CSS .p1 { position: absolute; width: 100px; height: 50px; top: 40%; left: 70%; } HTML <img src=../images/diagram1.png alt="Diagram"/> <span class="p1">This is a tes ...

Instability detected in the image when hovering over and off

Here is a snippet of code from my HTML file: <img id="id1" src="photo1" usemap="#ir" > <img src="photo2" id="id2"/> <span> <map name="ir" id="ir"> <area alt="" title="my title" href="my link" shape="poly" coords="13,15,29 ...

Guidelines for utilizing the php mail() function

It seems that there is an issue with this code when running it on a local host versus a live server. I am attempting to use this code for a mail function, which works on another site but not on this one. I know the code will produce an error on localhost ...

Exploring Django's view field for efficient data rendering

Is it possible to display multiple views for a single page/template? For instance, imagine a website where users can submit movie reviews and read others' reviews as well. Now, I need to add an edit button to the review pages but only want the button ...