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

Equal gutters are present between CSS floats

After conducting various experiments, I have devised a method to incorporate fixed and equal gutters between floats. My approach involves using positive and negative margins as well as multiple wrappers, making the solution somewhat cumbersome. However, I ...

Insert a div element into the JavaScript file

I have a JavaScript code snippet that needs to be inserted after the "Artwork" section. Here is the code: <div class="image-upload"> <label for="files"> <img src="ohtupload.jpg"> </label> </di ...

Troubleshooting a Vue.js formatting problem in Visual Studio 2019

Encountering an issue with VS2019 while attempting to format this code section. <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="milestone.ascx.cs" Inherits="uc.dms.milestone" %> <section class="content-header"> <h1> ...

Is there a way to align my anchor tag so that my link appears perfectly centered both vertically and horizontally on the page?

Despite my efforts to find a solution, I am stuck with this issue. Here is the code I have been working on: <a href='/Customers' class='centre'>Start</a> I attempted to wrap it in a div tag and also wanted to add a small gr ...

Ways to determine if a variable's value is changing while scrolling?

Currently working on a jQuery project where I have a variable that changes values based on scrolling. I need to determine when this value is increasing or decreasing as the user scrolls. Any suggestions on how to accomplish this? Thanks! ...

The Fancybox iFrame is not appearing on the screen

I am facing an issue with the html and javascript code I have. The html looks like this: <ul> <a class="iframe" href="/posting/form?id=8"><li>Publish</li></a> </ul> and I am using the following javascript: <scr ...

Error: You forgot to close the parenthesis after the argument list / there are duplicate items

I have already tried to review a similar question asked before by checking out this post, but unfortunately, I still couldn't find a solution for my problem. Even after attempting to add a backslash (\) before the quotation mark ("), specificall ...

Prevent jQuery validation from passing with radio buttons

I am currently encountering an issue with a particular field in my form that is being validated by jquery. Here is the troublesome field: <label>Select an Account Type<</label><br> <input type="radio" name="accountType" value="1" ...

PHP MySQL Table with a Date Range Filter

As someone who is new to PHP, I have a question. How can I set up a date range filter? I've looked at tutorials, but they haven't worked for me. I do have some code, but it doesn't include any functions. I want to implement this in a CRUD t ...

How can we control the timing of elements displaying on a web page in Javascript?

I've been attempting to implement a scrolling feature on my webpage using JavaScript, but whenever I run it, the content appears all at once instead of scrolling. The $("#Menu").html('') function doesn't seem to clear the screen properl ...

Displaying individual attributes of objects through v-for loop

I have created a table-making component to streamline the process of creating multiple tables. Each table consists of three key elements: The object (an array of objects with one row per object) Headers specific to each table The object properties that n ...

Executing a function in JavaScript using square brackets

While I was reading through the jQuery source code, I came across this interesting line: jQuery(this)[ state ? "show" : "hide" ](); Is there any particular benefit to using this syntax over the more traditional approach shown below? state ? jQuery(this) ...

What is the most effective method for integrating a hosted React application into a website that is not built using React?

I currently have a complete React application hosted on AWS that I want to embed into another non-React site. Right now, I have it embedded using an iframe and it's working well. <!doctype html> <html lang="en> <head> <meta c ...

Guide on implementing two submission options in an HTML form using JavaScript

Currently, I am working on a form that includes two buttons for saving inputted data to different locations. However, I am facing an issue with the functionality of the form when it comes to submitting the data. Since only one submit function can be activa ...

send a json response from my webpage

I have recently developed an HTML code to construct a user form where individuals can input information into various text fields. Additionally, I have included a button on the page that is meant to gather all the entered data and transform it into a json f ...

Tips for accessing a website and logging in to extract important data for scraping purposes

Currently facing an issue with scraping data from a website that requires logging in. I've been attempting to use the node request package for the login process, but so far have been unsuccessful. The code snippet I'm currently using is: var j = ...

Creating a NgFor loop in Angular 8 to display a dropdown menu styled using Material

I'm currently facing an issue with incorporating a Materialize dropdown within a dynamically generated table using *ngFor. The dropdown does not display when placed inside the table, however, it works perfectly fine when placed outside. <p>User ...

Crafting a responsible table design with the help of grid layout techniques

Is there a way to create a table that can adjust its rows and columns based on the container width or when resized? I attempted to use grid-gap in the background, but it resulted in empty red squares when there weren't enough elements in a row. http ...

Learn how to connect a formArray from the parent component to the child component in Angular with reactive forms, allowing you to easily modify the values within the formArray

In my parent component, there is a reactive form with controls and a form group. When the user selects a playerType from a dropdown menu, I dynamically add a formArray to the formGroup. This form array will contain either 2 or 3 form groups based on the p ...

Utilize jQuery to encase the final word of a paragraph within span tags

Greetings! Consider the following HTML snippet: <p>Phasellus sit amet auctor velit, ac egestas augue.</p> I am interested in enclosing the last word within span tags to achieve the following result: <p>Phasellus sit amet auctor velit, ...