Examples of merging responsive design and equal height columns in CSS:

As a beginner in CSS, I am facing challenges in creating a responsive layout that adjusts based on the screen it is viewed on and achieving equal heights in columns. While I have been able to address these issues individually, combining them has proven to be difficult for me. Specifically, my struggles include attempting to use a responsive layout with fixed column heights, which results in text overflow, and trying to create equal-height columns using flex properties without media queries.

In my design, I have three columns and my objectives are as follows:

  • For large devices: display a single row of three equal-width columns
  • For medium devices: show two columns in one row each taking up 50% width, followed by a single column taking up the full width in the next row
  • For small devices: list three rows with each column occupying 100% width

My current code on JSFiddle outlines my attempts so far: View Here

I am encountering two main problems:

  • The background color height of the columns is not consistent across all three columns, despite the border being uniform
  • The columns fail to expand over multiple lines, especially in the context of smaller devices

Snippet of the accompanying CSS code is provided below for reference:

*{
   box-sizing: border-box;
   font-family: "Book Antiqua";
 }

 h1 {
   margin-bottom: 15px;
   text-align: center;
 }

 .row {
   width: 100%;
   display: flex;
   overflow: auto;
 }

 .box {
   flex: 1;
   background-color: #AAAAAA;
   color: white;
   text-align: justify;
   padding: 10px;
   margin-left: 20px;
   margin-right: 20px;
 }

 h2 {
   position: relative;
   background-color: #222222;
   color: white;
   border: 1px solid white;
   text-align: center;
   width: 40%;
   margin-inline-start: 60%;
 }
 ...
 ... (Remaining CSS styles)
 ... 

Your insights and suggestions are greatly appreciated to tackle this challenge effectively!

Answer №1

First and foremost, there seems to be a mix-up between "float" and "flex".

When utilizing a Flex layout, the use of floating elements becomes unnecessary.

One key thing to note is:

display:flex;

This property applies directly to the immediate children and not any further down the hierarchy. This explains why the outer border ends up with the same height as the div with the class "col-xs-12", for instance. To ensure that your "box" element attains the desired height as well, I have added a height: 100%; which is compatible with flexbox in this scenario.

In the HTML section, I have assigned a "column" class to all columns; it would be advisable to define consistent styles here such as padding and borders.

By implementing these styles, you will achieve the desired heights and layout. To maintain responsiveness, you have already set some styles to 50%/50%/100% which appears sufficient. However, as you may have noticed, the outcome is actually 25%/25%/50% due to the default behavior of flex being to place all children on a single line and calculate their widths collectively to reach 100% (divided by 2 in the initial example).

To craft the layout you envision, include:

flex-wrap: wrap;

This adjustment ensures that your layout appears responsive as intended.

You can view the updated fiddle here: https://jsfiddle.net/smynzw8t/

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

Identify the fields in the form that have been edited for easier reference

Seeking guidance on creating a form with interactive input box fields. When a user inputs something, the field highlights in orange. Moving to another field removes the highlight and focuses the new box. Clicking "save" stores the form data. Pressing "mo ...

Modify information on the user interface without the need to refresh the page

Is there a way to update data on the UI without having to refresh the screen in a web application built with Node.js? I'm looking to make only specific changes on the screen. Additionally, how can I ensure that the data displayed on the screen is upda ...

I am having trouble getting any value back from my POST request using HTML, Node.js, and Express

I've been attempting to make a simple post request from an HTML page, but when I check the console after the post, it doesn't return anything. I'm confident that I have correctly set up the body parser on the server side. Could there be some ...

What are the steps to installing and utilizing the Chart.js package on your local machine?

I thought installing chart.js on my Raspberry Pi would be a simple task, but I seem to be struggling with it. Due to the nature of my project, I need to have it installed locally rather than relying on an online version. Following the usual steps, I navig ...

What is the best way to retrieve the current URL with a hashtag symbol using JavaScript?

I am attempting to display the current URL after the question mark with a hash symbol using PHP, but unfortunately, it is not achievable. Therefore, I need to utilize JavaScript for this task, even though I have limited knowledge of it. This is the specifi ...

Not all properties are affected by the CSS stylesheet

Even though my CSS stylesheet successfully changes the background color of the page, I am facing issues with other properties that I have set on my HTML tags. {% block body %} <div id='title'> <h1> VISUALIZER2D ...

Struggling with CSS Flexbox when it comes to organizing rows and combining Table and Table Cells

I've been struggling to fix a CSS flex problem for hours. My goal is to make sure that the three boxes in each row have the same height. While I'm familiar with using flex, I suspect that floats may be the root of the issue. Even after clearing t ...

Displaying a variable in HTML using PHP before it's actually declared

I have a webpage named search.php My goal is to customize the title/tab tag of the page to display the number of items found along with "Mysearch". Here's what I want it to look like: <head> <title><?php echo $number_count; ?> it ...

What is the specific character code assigned to the "Enter" key on a

Check out this code snippet: http://jsfiddle.net/YttKb/ In my javascript, I am trying to add a new line of text using utf-8 coding. Which character should I use to make a new line? It seems like \n only creates a line break, not a new line of text. ...

Guide to displaying a loading image when selecting an item from a dropdown menu using JavaScript

When using three drop-down lists and performing an AJAX call on each dropdown list's onclick function, I encountered a delay in loading the data. To address this issue, I attempted to display a loading image while processing the AJAX call. <form ...

Swapping out the current div with the outcome from jQuery

I'm attempting to retrieve the most recent data for a div and replace it. The ajax query is fetching the entire HTML page, from which I am locating the right div. Now I want to update the current right div with the new content. $.ajax( { ...

Is it possible to run quirks mode in one frame while running standards mode in another?

Back in the IE6 days, I developed an old application that relies on frames instead of iframes and runs in quirks mode. Now, I'm wondering if it's possible to have one frame remain in quirks mode while another operates in standards mode when using ...

How can I locate a div element using multiple class names?

<div class="value test" /> I am trying to locate this specific element on the web page which has two classes assigned to it. Since className does not accept space-separated values, I am exploring alternative ways to achieve this. Any suggestions? ...

Shades of Grey in Visual Studio Code

Whenever I use VSC, I notice these odd grey boxes that appear in my editor. They seem to be dynamic and really bother me. Interestingly, switching to a light theme makes them disappear. However, I prefer using a dark theme and haven't been able to fin ...

Support for Chrome in Angular 8

Can someone please advise on the minimum version of Google Chrome that is supported by Angular 8? Additionally, I am looking for a way to prompt users to update their Chrome browser if it doesn't meet the required version. My Angular application seems ...

Can floated divs be prevented from collapsing without taking up any width?

Let's discuss an issue that is not related to block elements collapsing when their children are floated, and has nothing to do with clearing at all. Imagine having a set of floated divs like: <div class="column">Column 1</div> <div cl ...

Spinning divs using JavaScript when the mouse hovers over them, and then returning them to their original position when the mouse moves

I am looking to create a functionality where a div rotates when the mouse enters it and returns to its original position when the mouse leaves. Everything works fine when interacting with one div, but as soon as I try hovering over other divs, it starts gl ...

Getting the ng-model value from a Directive's template and passing it to a different HTML file

When working with my name directive, I am unable to retrieve the value of ng-model from the template-url. team-two.html <form name="userForm" novalidate> <div name-directive></div> </form> <pre>{{userForm.firstname}}< ...

Link insertion column

How can I insert a hyperlink into this particular column? <div class="col-md-6" style="height: 125px;"> <div style="background: #ffffff;"></div> <div class="borde ...

changing size when hovered over with the mouse is not consistent between entering and exiting

Hi there, I'm currently utilizing the transform: scale(); property on a website and could use some assistance with a particular issue I haven't been able to find an answer for online. Here is the code snippet I'm working with: HTML: <d ...