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

Having difficulty accessing a function within the SignalR connection function

I am currently facing an issue while trying to access a specific function within a SignalR connection function. Below is the code snippet showcasing the entire script for better understanding: $(function() { var chat = $.connection.chatHub; chat.c ...

Tips for effectively using the question mark as a separator in a webservice URL

In my nodejs / express project, I am attempting to create a webservice where I separate the URL from parameters using '?' and use '&' as parameter separators. When using this method, it works perfectly fine: app.get("/tableref/:event/ ...

I am unable to retrieve the values from a manually created JavaScript list using querySelectorAll()

const myList = document.createElement("div"); myList.setAttribute('id', 'name'); const list1 = document.createElement("ul"); const item1 = document.createElement("li"); let value1 = document.createTe ...

What is the most effective approach for managing exceptions at a global level in Node.js using Express 4?

Is there an Exception filter available in node.js with express 4, similar to the one in asp.net MVC? I have searched through various articles but haven't found a solution that meets my requirements. I also attempted the following in app.js: process ...

CSS: What's with the weird gray element appearing in Safari?

Does anyone have a solution for removing the grey layer (cf the image) in Safari? This issue does not occur in Chrome. The structure is as follows: <div class="class1"> <div class="class2"> <select> ... </selec ...

Is there a way to send a preexisting JSON object to an OPTION in jQuery or JavaScript?

What is the best way to pass a preexisting JSON as a data value in an HTML option tag? I am aware that I can manually pass individual variables like this: <option data-value='{"name":"rajiv","age":"40"}'>a</option> However, if I ha ...

Display icon within ng-bind-html when the value is not true

Is there a way to integrate a fontawesome icon into ng-bind-html on ui-select based on a boolean value? <span ng-bind-html="project.IsActive == false && <i class="fa fa-ban" aria-hidden="true"></i> | highlight: $select.search">& ...

Issue with Ionic toggles not updating the correct local storage entry

Encountering an issue with ionic toggles where changing any toggle affects only the last if statement below in local storage. Here is the list of toggles... $scope.settingsList = [ { text: "GBP", checked: gbpON }, { text: "USD", checked: usdON } ...

Exploring the World with the vue-i18n Plugin

Recently, I have integrated translation into my app and now I am looking to implement a button event that allows users to change the language on the home page. For this task, I referred to two tutorials: Localization with Vue and Localization with Vue Tut ...

What is the best way to vertically align my content in the center?

Is there a way to vertically center all of the text content, especially focusing on the drop-down button and block, here? Despite my efforts to research and find solutions online, I have not been able to achieve the desired visual results. I am restricted ...

Guide to creating HTML file with SSI tags

Currently, my website uses Nginx SSI tags to dynamically include template files such as the header, footer, and sidebar. However, I need to provide the final HTML output to clients instead of running it on their browsers. The issue I'm facing is that ...

Working with promises and Async/Await in Express/node can sometimes feel ineffective

Currently, I am delving into the world of node and express with the aim to create a function that can extract data from a CSV file uploaded by the user. My challenge lies in the fact that the data is being outputted as an empty array before it goes through ...

Utilizing two imports within the map() method

I have been considering the idea of incorporating two imports within map() in my React code. This is how my code looks: {this.state.dataExample.map(item => ( <ItemsSection nameSection={item.name} /> item.dat ...

Tips for aligning a text box field in the center using Bootstrap

I'm having trouble centering a text field on my screen. No matter what I try, it stays aligned to the left. How can I fix this and get it centered? <div class="form-row"> <div class="col-md-4 col-md-offset-4"> ...

exploring numerous boxes in an engaging and educational tutorial

Explaining this in the title was a bit tricky, but what I want to create is an interactive tutorial. In this tutorial, the user will click on an area or product they want to clean or use for cleaning. After clicking, another box with relevant information w ...

I encountered an error message that said: "Cannot assign '12': 'Cart.product' must be a 'Product' instance. Can someone please assist me in resolving this problem?"

[Included in the image of my code is the add-to-cart function where the error can be found on line 5] def add_to_cart(request): user = request.user product_id = request.GET.get('prod_id') product = Product.objects.get(id=product_id) ...

Avoiding double tapping to zoom on Chrome Android while using the desktop version of a website

Is there a way to disable zooming when double clicking in Google Chrome with the desktop site enabled? I attempted to use <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> without success. I al ...

Exploring Facebook Graph API response with Angular 2 Mapping

I am using HTTP calls to access the Graph API in order to retrieve posts from a Facebook page. Here is the code snippet that fetches an array of posts: let url = 'https://graph.facebook.com/15087023444/posts?fields=likes.limit(0).summary(true),comme ...

centering headers using tailwind styles

I am facing a challenge with positioning my main title, logo, and subtitle. I want the subtitle to be centered regardless of the logo's width. Currently, the position of the sub title changes based on the logo's width, resulting in it not aligni ...

What could be the reason for the absence of definition for 'res'?

As I work on coding a bot using discord.js, I am facing an issue while trying to set up a system where the bot can send a message as long as it is not blacklisted. However, every time I attempt to run the function, I encounter this error message: Reference ...