Basic HTML code that displays either one or two columns based on the width of the screen

My monitoring website displays dynamic rrd graphs in png format with fixed dimensions.

I am looking to adjust the layout based on browser window size - showing the graphs in 1 column if the width is below a certain threshold, and in 2 columns if it exceeds that limit.

While I understand basic HTML concepts like tables and JavaScript properties such as innerWidth, I am unsure how to effectively use these to implement intelligent column width handling.

This is an excerpt of my current code:


<table style="width:100%">
    <tr>
        <td>
            <body><center>
                <h2>CO2: unfiltered values </h2>
                <p><a href="./index_CO2unf.html"><img src="./ramdisk/htdocs/CO2unf_1h.png" alt="CO2unf_1h.png"/></a></p><p><br></p>
                <h2>CO2: filtered values </h2>
                <p><a href="./index_CO2fil.html"><img src="./ramdisk/htdocs/CO2fil_1h.png" alt="CO2fil_1h.png"/></a></p><p><br></p>
                <h2>O2: partial pressure O2 values</h2>
                <p><a href="./index_O2pp.html"><img src="./ramdisk/htdocs/O2pp_1h.png" alt="O2pp_1h.png"/></a></p><p><br></p>
            </td>                                                                                                                
            <td> 
                <h2>O2: percentage O2 values</h2>
                <p><a href="./index_O2pct.html"><img src="./ramdisk/htdocs/O2pct_1h.png" alt="O2pct_1h.png"/></a></p><p><br></p>
                <h2>O2: Temperature values</h2>                                                                                 
                <p><a href="./index_O2temp.html"><img src="./ramdisk/htdocs/O2temp_1h.png" alt="O2temp_1h.png"/></a></p><p><br></p>
                <h2>O2: Pressure values</h2>                                                                                       
                <p><a href="./index_O2pres.html"><img src="./ramdisk/htdocs/O2pres_1h.png" alt="O2pres_1h.png"/></a></p><p><br></p>
            </body>                                                                                                                    
        </td>  
    </tr>  

The central </td><td> section needs to be removed when the browser width falls below the defined threshold. How can I achieve this functionality?

Answer №1

If you want to simplify your layout, consider replacing your table with two <div> elements and utilize @media queries like demonstrated in this example: https://jsfiddle.net/CLEAN_SMITH/qwertyuiop/4/

body{margin: 0;}
div{width: 50%; height: 600px; float: left; text-align: center;}
.div-1{background-color: green;}
.div-2{background-color: yellow;}
@media (max-width: 700px){
    div{width: 100%;}
}

Answer №2

Check out this sample markup code:

.dual-columns {
  float: left;
  margin-right: 15px;
}
.dual-columns:nth-child(2n + 1) {
  clear: left;
}
.clear-fix {
  clear: both;
}
<div class="dual-columns">
  <h3>Title</h3>
  <p><img src="http://lorempixel.com/400/600/technics/1"></p>
</div>
<div class="dual-columns">
  <h3>Title</h3>
  <p><img src="http://lorempixel.com/400/600/technics/2"></p>
</div>
<div class="dual-columns">
  <h3>Title</h3>
  <p><img src="http://lorempixel.com/400/600/technics/3"></p>
</div>
<div class="dual-columns">
  <h3>Title</h3>
  <p><img src="http://lorempixel.com/400/600/technics/4"></p>
</div>
<div class="dual-columns">
  <h3>Title</h3>
  <p><img src="http://lorempixel.com/400/600/technics/5"></p>
</div>
<div class="dual-columns">
  <h3>Title</h3>
  <p><img src="http://lorempixel.com/400/600/technics/6"></p>
</div>
<!-- Clear fix for floated elements -->
<div class="clear-fix"></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 the best way to inject a service instance into the implementation of an abstract method?

In my Angular application, I have a service that extends an abstract class and implements an abstract method. @Injectable({ providedIn: 'root', }) export class ClassB extends ClassA { constructor( private service : ExampleService) { s ...

Rephrase the ajax call and the data retrieved

I'm struggling to find a solution for writing this code snippet without using async: false,. var imageX; var groupX; $.ajax({ type:'GET', url:'php/myphp.php', dataType:'json', async: false, success: ...

Customize your Outlook emails with HTML and CSS to right-align content for a polished look

My current project involves generating a report to be sent via email, with Outlook being the mandatory application in our system. The report's content is HTML-based and utilizes a table layout. Part of the content needs to appear right-aligned within ...

Directly within Angular, map the JSON data to an object for easy assignment

I came across this information on https://angular.io/guide/http. According to the source, in order to access properties defined in an interface, it is necessary to convert the plain object obtained from JSON into the required response type. To access pr ...

What exactly does the term "entry point" refer to within the context of npm init?

Starting a new project can be overwhelming, especially when faced with a list of questions during the npm init process. One question that often stumps developers is "entry point." The options provided, like name, version, and description, may seem straig ...

I have difficulty generating appropriate pseudonyms

Struggling to create aliases in my react project (CRA 3.1.1) has been a challenge for me. Despite attempting various methods, I have not been successful in achieving it. The only success I've had so far is aliasing the simple "src" folder based on som ...

GWT Validation - Enhance Your Application with a Third Party Library

Is there a reliable JavaScript library available for validating GWT fields such as Email, Text Length, Phone Number, Date & SSN, etc.? I am unable to use the GWT Validation Framework or GWT Errai in my application because I receive responses as JSON r ...

Prevent the event from bubbling up on active elements

Having trouble with stopping event propagation? Picture this situation: <table id="test"> <tbody> <tr> <td class="row"> </td> <td class="row"> </td> <td class="ro ...

Utilizing Ruby interpolation to dynamically select elements in Capybara's CSS selectors

Having trouble with invalid selector errors when attempting to locate an element using capybara This method is successful: page.find("#widget_amount_Standard") But I encounter issues when trying any of the following: credit_type = "Standard" page.find( ...

The initial res.body object in NodeJS is enclosed in quotation marks

I've hit a roadblock while working on my social media website project using the MERN stack. Despite scouring the internet, I haven't been able to find a solution to the issue at hand. While following an online course, I encountered a problem tha ...

What is the best way to incorporate a text box along with a submit button that triggers a callback to a javascript function when clicked, utilizing either plain javascript or jQuery?

As a beginner in the world of JavaScript, I'm struggling to find the solution to this problem. Any assistance would be greatly welcomed. ...

I am experiencing an issue where my React application is not loading the fonts when utilizing `type: 'asset/resource'` to load fonts within a CSS file. Can anyone provide guidance on

I'm encountering an issue with my webpack setup where fonts are not being loaded when using type: 'asset/resource'. Actually, there are several problems arising from the use of asset/resource, but let's focus on this particular issue fo ...

Unable to access the 'fn' property of undefined in Handlebars causing a TypeError

I encountered an issue with my custom handlebars helper. When I fail to define values for the parameters, I receive the following error message. module.exports = function(src, color, classatr, options) { if (typeof src === 'undefined') src ...

Unable to transform Table layout into DIVs

My form for data input consists of labels with inputs, but the labels are localized so I am not sure about the length of text for different languages. To tackle this issue, I initially used a table layout: <table> <tr> <td> ...

Problem with BehaviorSubject: next() method is not functioning as expected

My goal is to utilize an angular service for storing and communicating data via observables. Below is the implementation of my service: public _currentLegal = new BehaviorSubject({ name: 'init', _id: 'init', country: 'init& ...

Is there a way to automate the uploading of captcha images to this solving service using Python?

Currently, I am working on a Python program with the goal of solving captchas on a website. My plan is to integrate 2captcha into the solution. Using Selenium, I have managed to write a Python script that fulfills all required tasks except for solving the ...

Issue with hook not updating when invoked inside useEffect

I'm encountering an issue with updating the state after fetching data from my API. The API response seems to be correct, but for some reason, my weatherData-hook is not getting updated and it returns undefined. Can anyone point out what mistake I migh ...

Display JSON data using Vue.js

Trying to display JSON file results using Vue.js, with the goal of showing the result in a value. Here is the code snippet: data () { return { fetchData: function () { var self = this; self.$http.get("/api/casetotalactivation", functio ...

Error: The function `map` cannot be applied to `cardsData`

I'm encountering an issue where I need to store user queries in cardsData and then map through the data within cardsData. However, when I run the code on my terminal, it throws an error. As a beginner, I've researched various forums that mention ...

Examining whether an ajax call was not initiated within an Angular application

Is there a way to verify that an ajax request has not been made using Angular's $httpBackend? I attempted to use verifyNoOutstandingRequest() but it doesn't seem to be triggering test failures in version 1.1.5. Here is more information about the ...