What is the best way to design a flexible two-column layout using divs instead of tables?

Looking to create a dynamic two-column layout with divs instead of tables?

In the left div, I plan to include text and a call to action. As for the right div, I'm debating between an image or having a transparent background with a PNG.

I aim for both divs to be aligned and adaptable. They should also stack on top of each other at various screen sizes.

Here's my progress so far, although it's not flawless. Any tips on improving this code to avoid compatibility issues across different browsers would be greatly appreciated.

Thank you.

<style type="text/css">*{
 box-sizing: border-box;
 padding: 10px;
}

 .column {
 float: left;
 width: 300px;
  }

 .center {
 padding: 50px 0;
  }

 .row:after {
   content: "";
   display: table;
   clear: both;
  }

 @media screen and (max-width: 900px) {
   .column  {
    width: 50%;
    }
  }


@media screen and (max-width: 600px) {
  .column  {
  width: 100%;
  }
</style>

<div class="row">
<div class="column" style="background-color:#e0e620;">
<div class="center">
<p style="font-size:18px; ">The Information is now available as an audiobook.<br />
<a class="link-button-green" href="" title="Info guide">Listen now</a></p>
</div>
</div>

<div class="column" style="background-color:#E5E5E5;">
<img src="https://www.w3schools.com/css/img_forest.jpg">
</div>

Answer №1

To display the columns next to each other, consider using flex instead of float. For smaller screens, you can omit flex in your @media query to stack the columns vertically.

Ensure that the image width spans 100% of the flex column and avoid inline CSS styling.

Sample HTML code:

<div class="row">
  <div class="column left">
    <div>The Information is now available as an audiobook.<br />
      <a class="link-button-green" href="" title="Info guide">Listen now</a>
    </div>
  </div>

  <div class="column right" >
    <img src="https://www.w3schools.com/css/img_forest.jpg">
  </div>
</div>

Corresponding CSS snippet:

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
}

.row {
  display: flex;
}

.column {
  flex:1;
}

.left {
  background-color:#e0e620;
  padding:20px;
}

.right {
  background-color:#E5E5E5;
}

img {
  width:100%;
}

@media screen and (max-width: 400px) {
  .row {
    width: 100vw;
    display:block;
  }
}

Explore this example on jsfiddle: https://jsfiddle.net/tLubao5d/1/

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 method used by Chrome dev tools to create CSS selectors?

When setting a new style rule for an element, Chrome generates a selector that includes the entire hierarchy of elements instead of just the class name. For instance: <body> <div class="container"> <span class="helper"> ...

Unable to hide HTML using PHP conditional - It's malfunctioning

I'm a PHP newbie and attempting to execute some PHP code within an HTML document. The goal is to show additional fields when users select a specific item from a drop down menu. I am trying to make it based on the value of that particular drop down it ...

jQuery - Navbar with both horizontal and vertical orientation

I'm new to JavaScript and jQuery, and I'm looking to create a vertical nav/slider that follows the cursor when hovering over horizontal navbars links to reveal sub-links, essentially creating a dropdown menu effect. While I don't expect any ...

Adjust the default margins and padding of email header images specifically for Outlook emails

I am currently working on coding an email, and I have encountered an issue with the alignment of the hero image, specifically in Outlook. https://i.sstatic.net/ap7SG.png Despite trying various solutions from previous answers to this problem (such as sett ...

Is it possible to utilize Apache FOP for the conversion of any HTML document to a PDF file?

I attempted to utilize Apache FOP for the purpose of converting HTML to PDF, going through the steps of HTML --> XHTML --> XSL-FO --> PDF. For the conversion from xhtml to XSL-FO, I made use of the xhtml2fo.xsl tool from Antenna House. While this ...

What methods can I employ to dynamically style SVG elements that have been generated programmatically?

Utilizing a js library called mermaid to create svg on a webpage, I am in need of dynamically applying styling to specific parts of the svg when users activate different commands through keyboard shortcuts. Specifically, I aim to highlight the element in t ...

Error 410: The Webpage has Disappeared

Unfortunately, my website fell victim to a hacking attack that resulted in the addition of numerous unwanted pages. These pages were quickly indexed by Google, causing a significant drop in traffic. The names of these pages all follow a similar pattern, s ...

jQuery width property does not seem to be functional on menus that do not contain any dropdown

I am currently working on creating a menu that displays arrows underneath the items when hovered over or when the .active class is added to the menu. Everything is working fine, except for the fact that it only works on menus with drop-downs and the child ...

What is the best way to display the current time (generated through JavaScript) within a designated div element?

During my JavaScript studies, I stumbled upon the following code snippet: function printTime() { var d = new Date(); var hours = d.getHours(); var mins = d.getMinutes(); var secs = d.getSeconds(); document.body.innerHTML = hours+":"+mins+":"+sec ...

Error Alert: JQuery Pop Up Issue

Struggling with getting my JQuery Pop-Up Box to work properly. Any guidance for a newbie like me on how to fix it would be greatly appreciated. Here's the code I've been working on: <!-- POP UP HTML --> <div class="infocontainer"> ...

Opting for PHP over JSON for the instant search script output

Is there a way to modify my Google Instant style search script, written in jQuery, to retrieve results from a PHP script and output PHP-generated HTML instead of JSON? Below is the current code: $(document).ready(function(){ $("#search").keyup(functi ...

Animating a div using a changing scope variable in AngularJS

As a newcomer to Angular, I am looking to add animation to a div element using ng-click within an ng-repeat loop. Here is what I have attempted: app.js var app = angular.module( 'app', [] ); app.controller('appController', function($ ...

Tips on ensuring the first column of an HTML table remains fixed in responsive design

I am trying to create a responsive HTML table where the first column remains fixed. I have a select box on my PHP page and I am using AJAX to display data from a database in the HTML table. However, when selecting a value in the select box in a responsiv ...

event handler in JavaScript that triggers upon a click

Encountering an issue with my code <tr> <td id="<?php echo; $id ?>" onclick="addrow(?php echo $id; ?>)">...</td> </tr> <tr id="<?php echo $id; ?>" class="ndisplay">...</tr> <tr id="<?php echo $i ...

Wordpress isn't loading CSS as expected (I believe...)

Recently, a wordpress based website I am working on suddenly stopped pulling the CSS files...or at least that's what it seems like. I can't post a screenshot of the wordpress admin dashboard because I don't own the site and my boss might no ...

Adjust the background shade of specific characters within an input text field

When a user inputs a string into a standard HTML input field (type="text"), such as "2013/13/29", and validation reveals that the number 13 is invalid in this context, I would like to visually highlight it by changing its background color to red while tu ...

The filtering functionality appears to be malfunctioning

There's a table that I need help with: https://i.sstatic.net/ywTgQ.png Underneath the header, there is an "Apply Filter" button which should filter the data in the table based on user input. The issue is that when I click the button, nothing happen ...

In the latest version of Bootstrap, the carousel has been redesigned to smoothly transition across the entire page, providing a

I am currently in the process of learning Bootstrap 5 and decided to create a simple carousel by referring to the official documentation on Bootstrap 5 Carousel. Following their instructions, I copied their code and integrated it into my template. Howeve ...

Do AngularJS routes allow the use of special characters in URLs?

Issue at hand: Every time I enter http://localhost:53379 in the browser, it redirects me to http://localhost:53379/#/. Why is the /#/ being added? angular .module('app', ['ngRoute', 'ngStorage']) .config([&apo ...

Combine one CSS property in Emotion

Having some trouble creating reusable animations with Emotion. I've defined a fadeUp animation that works well: export const animatedFadeUp = css` opacity: 1; transform: translateY(0); `; export const fadeUp = css` opacity: 0; transform: tran ...