Flexbox problem - uneven heights

I have set up a flex box layout and you can view the codepen link here:- https://codepen.io/scottYg55/pen/zYYWbJG.

My goal is to make the pink backgrounds in this flexbox 50% of the height, along with the background image so it resembles more of a grid system.

However, I am facing an issue where the pink background is not expanding its width regardless of what I attempt. I simply want all images and pink backgrounds to be the exact same height. Would using CSS grid be a better solution?

If anyone could provide assistance, I would greatly appreciate it!

HTML

<div class="blog-contl row">

    <div class="col">
        <div class="blog-half">
            <div class="blog-half-img" style="background-image:url('http://www.project-progress.co.uk/cloudhouse/site/wp-content/uploads/blog6.jpg')">
            </div>
            <div class="blog-half-cont">
                <h4>Test Post 12</h4>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</p>
                <div class="main-button">
                    <a href="http://www.project-progress.co.uk/cloudhouse/site/test-post-12/" title="Read More" class="btn-default">Read More</a>
                </div>
            </div>
        </div>
    </div>

    <!-- Repeat for other columns -->

</div>

CSS

.row {display:flex;}

.blog-contl img {width:auto;}

.blog-contl .row {
    display: flex;
    flex-wrap: wrap;
}

/* Styles for blog-half elements */

.blog-contl .col {padding:0;}

.blog-half-img {
    background-size: cover;
    background-repeat: no-repeat;
    width: 100%;
    padding-bottom: 50%;
}

.blog-half-cont {
    background: #da55c9;
    margin: 0;
    padding: 40px;
    box-sizing: border-box;
    text-align: center;
    color: #fff;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.blog-half {display:flex;flex-wrap:wrap;}

.col:nth-of-type(2) .blog-half .blog-half-cont {
  order:1;
}

.col:nth-of-type(2) .blog-half .blog-half-img {
  order:2;
}

Any help will be highly appreciated! Thank you!

Answer №1

It is recommended to utilize grid over flex for better alignment of elements. Below is the code snippet that demonstrates the usage of grid layout.

.row { display: grid; grid: auto /auto auto auto; }
        .blog-half { display: grid; grid-auto-rows: 1fr; }
        .blog-contl img {width:auto;}
        .blog-half-img {
            background-size: cover;
            background-repeat: no-repeat;
            width: 100%;
            padding-bottom: 50%;
        }
        .blog-half-cont {
            background: #da55c9;
            margin: 0;
            padding: 40px;
            box-sizing: border-box;
            text-align: center;
            color: #fff;
            display: flex;
            flex-direction: column;
            justify-content: center;
        }
        .col:nth-of-type(2) .blog-half .blog-half-cont {
          order:1;
        }

        .col:nth-of-type(2) .blog-half .blog-half-img {
          order:2;
        }
<div class="blog-contl row">
        <div class="col">
            <div class="blog-half">
                <div class="blog-half-img" style="background-image:url('http://www.project-progress.co.uk/cloudhouse/site/wp-content/uploads/blog6.jpg')">
                </div>
                <div class="blog-half-cont">
                    <h4>Test Post 12</h4>
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</p>
                    <div class="main-button">
                        <a href="http://www.project-progress.co.uk/cloudhouse/site/test-post-12/" title="Read More" class="btn-default">Read More</a>
                    </div>
                </div>
            </div>
        </div>
        <div class="col">
            <div class="blog-half">
                <div class="blog-half-img" style="background-image:url('http://www.project-progress.co.uk/cloudhouse/site/wp-content/uploads/blog3.jpg')">
                </div>
                <div class="blog-half-cont">
                    <h4>Test Post 9</h4>
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</p>
                    <div class="main-button">
                        <a href="http://www.project-progress.co.uk/cloudhouse/site/test-post-9/" title="Read More" class="btn-default">Read More</a>
                    </div>
                </div>
            </div>
        </div>
        <div class="col ">
            <div class="blog-half">
                <div class="blog-half-img" style="background-image:url('http://www.project-progress.co.uk/cloudhouse/site/wp-content/uploads/blog6.jpg')">
                </div>
                <div class="blog-half-cont">
                    <h4>Test Post 6</h4>
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</p>
                    <div class="main-button">
                        <a href="http://www.project-progress.co.uk/cloudhouse/site/test-post-6/" title="Read More" class="btn-default">Read More</a>
                    </div>
                </div>
            </div>
        </div>
    </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 snapping feature of the jQuery Mobile slider?

I've been diving into various sources for the past couple of hours, attempting to implement code snippets claiming to enable snapping functionality on a slider component. I'm facing doubts about the feasibility of achieving this with the jQuery M ...

Getting an input value dynamically in a React variable when there is a change in the input field

I am having an issue with my search text box. I need to extract the value onchange and send a request to an API, but when I try using the normal event.target method, it shows an error. How can I fix this? The problem is that onchange, I need to call a func ...

Is there a way to address this scrollbar issue without relying on HTML?

Currently, I am working on a website where the only customization allowed is related to CSS. While I've been able to make progress, I've encountered an issue with the scrollbar placement. The scrollbar is extending beyond the boundaries of the di ...

Is there an issue with my Ajax request?

I've been troubleshooting an issue where my function consistently returns an error condition when trying to make a simple Ajax call to a local JSON file. I've ruled out the possibility that it's a file system call by uploading the file, but ...

Error code 2 in Phonegap 2.2.0 iOS file transfer issue

saricsarda2[326:15b03] Executing multiple tasks -> Device: YES, App: YES saricsarda2[326:15b03] -[CDVFileTransfer download:] [Line 313] File Transfer is currently downloading... saricsarda2[326:15b03] File Transfer has been completed with response code ...

Converting MS Access databases

My latest project involves the conversion of an existing MS Access 2007 application into a web-based application. Currently, all data is stored in a SQL Server 2005 database. Given my limited web development experience, I am approaching this task with caut ...

Tips for closing a popup without exiting the entire webpage

I'm facing an issue while trying to create a video playlist using two HTML files (index.html and video.html). In my video.html file, I have set up a popup for playing videos. However, whenever I close the popup or click anywhere on the page, it redire ...

Several queries for directions on Google Maps are resulting in the same response being returned for all requests

I've been experimenting with the Google Maps Directions service to retrieve three different travel methods for the same route (driving, walking, and bicycling). Despite successfully iterating through the array of methods and setting them in the respec ...

Is it possible to delete a section of the URL within an anchor tag prior to the #anchor

My webpage contains a link that looks like this: <li class="jump"><a href="http://example.com/#about">About Me</a></li> I am interested in using jQuery to eliminate the 'http://example.com/' section of any URL found with ...

The Bing map control fails to adhere to the div element

After visiting , I successfully generated a map using the following HTML code: <div class="fluid-row" style="height:300px;"> <div class="span12"> <div id="prdmap" class="map"> </div> </div> Here is the accompanying J ...

Executing a function with the initial click

Is there a way to run a function only on the first click, without having it run every time? I already have another function running on window.onload, so I can't use that. Both functions work fine independently, but not together. Right now, I'm ca ...

jQuery's error function triggered even after a successful AJAX request

I need assistance with my WordPress site contact form, as I want it to submit without refreshing the page. Currently, the form sends the email successfully, but I added an error function in case it fails. However, now the error function is being called eve ...

Adjustable height within embedded object tag

I've been struggling to adjust the height of the content on my site automatically. I have attempted using iframes as well, but nothing seems to work despite trying numerous code examples from various sources including CSS and JS scripts. Any help wou ...

Tips for showing nested JSON data in a PrimeNG table within Angular version 7

I am struggling to display nested json data in a PrimeNG table. When I retrieve the data using an HTTP service, it appears as [object][object] when displayed directly in the table. What I want is to show the nested json data with keys and values separated ...

What is the best way to incorporate quotes within inline CSS code?

Can anyone help me with inserting the following CSS into HTML? Here is the CSS snippet that needs to be converted to HTML: .group .circular-progress::before { ... content: "10%"; ... } This is what I have attempted so far: <div class="group"&g ...

Guide to dynamically add tr element using CSS to a table

While using the $.each function in jQuery, I am retrieving data and adding it to a table in the following manner: $.each(segment, function (index, innerSegment) { var tr; tr = $('<tr/>'); tr.append("<td>" + segment[i].Airline.Air ...

Creating a Burger Navigation Menu with CSS and HTML using Image Overlapping

I'm encountering an issue with the new website I created for my local Church. I followed a tutorial on building a burger menu using just HTML and CSS, which you can view here. The problem arises when I attempt to place an image below the navigation m ...

Using Flask to Render Templates with Repeated HTML Components

I am currently working on integrating a Flask app with JS. My app.py file has the following code: import urllib import requests import time from es import book from flask import Flask, render_template, request, jsonify app = Flask(__name__) @app.route( ...

How to precisely navigate to a particular row in a GWT FlexTable

Is it possible to scroll a flextable to a specific selected row? I am attempting to implement functionality where I have two panes, LeftView and RightView. When an item is selected on the RightView, the FlexTable in the LeftView should automatically scrol ...

Transform your Image into Base64 and easily upload it using AJAX

Is there a way to convert an image to Base64 and send it via AJAX for processing without relying on any external library? Just using basic HTML <input type="file" id="image" accept="image/*"> Via AJAX $(document).on(&a ...