Seeking to establish uniform column heights in Bootstrap without utilizing a table

I am working on making my columns in Bootstrap 3 have equal heights without using 'display: table;' as it affects the overall layout. Here is a snippet of my code:

<article>

 <div id="post-<?php the_ID(); ?>"> <!-- getting post's id --> 

   <div class="row">

    <div class="col-md-8 indx-img" style="background-image:url('...');">
    </div>

    <div class="col-md-4 text-cell">
        <h1>title</h1>
        <h3>category</h3> 
    </div>

   </div><!-- /#row -->

 </div><!-- /#post -->

</article>

I have content on the right and a column with a background image on the left that needs a specified height for the image to show properly. I want this height to be applied to the column with the text too.

To make the height responsive, I have used the following CSS so far:

CSS

indx-img {
 padding: 16% 0;
}

The issue is that this height does not affect the column with the text. My ultimate goal is to ensure both columns have equal heights and the text is vertically centered in the 'text-cell' column.

Answer №1

If you're facing challenges with CSS solutions that might disrupt your layout, utilizing JavaScript can be a viable alternative.

In this scenario, we've created a function called resize to iterate through the posts.
If the page exceeds a certain breakpoint, the post container's height is adjusted to match the image's height. Subsequently, the text container's height is set to 100% once.

If the page falls below the breakpoint, we check if the height has been defined. If so, we remove the height setting to allow for natural expansion and contraction.

The resize function is initially triggered upon page load and then linked to the window resize handler.

(Demo)

<script type="text/javascript>
    window.onload = function() {
        (function () {
            "use strict";
            var resize = function () {
                var posts = document.querySelectorAll('[id^="post"] .row'), post;
                for (var i = 0; post = posts[i]; i++) {
                    if (window.innerWidth > 768) {
                        post.style.height = post.firstElementChild.offsetHeight + 'px';
                        if(post.lastElementChild.style.height !== '100%') {
                            post.lastElementChild.style.height = '100%';
                        }
                    } else {
                        if(post.style.height !== '')
                            post.style.height = '';
                    }
                }
            };
            window.onresize = resize;
            resize();
        })();
    }
</script>

Answer №2

When dealing with this type of issue, I always rely on the matchHeight plugin, which seamlessly integrates with Bootstrap.

To implement this solution, simply install the plugin and assign a class of matchHeight to your col-md-8 and col-md-4 columns:


  <div class="matchHeight col-md-8 indx-img" style="background-image:url('...');">
</div>

<div class="matchHeight col-md-4 text-cell">
    <h1>title</h1>
    <h3>category</h3> 
</div>

Next, include the following script in your JavaScript file:

$('.matchHeight').matchHeight();

This fix can be applied across your entire website by following these steps, even multiple times on the same page (the plugin automatically scopes within the current "row" element to ensure that only elements within the same row are matched for size).

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

Custom directives are unable to interact with inbuilt directives

var app = angular.module("myDiscuss", []); app.controller("TabController", function() { this.tab = 0; this.subTab = 0; this.like = 0; this.selectLike = function(setTab) { this.like= setTab; }; this.selectTab = function(setTab) { this. ...

JavaScript code to generate diamond-shaped numbers

Greetings everyone! I am facing an issue with my code that is supposed to generate diamond-shaped numbers using javascript. The code involves two javascript functions triggered by a single onclick event, but unfortunately, one of the functions does not see ...

How to toggle the visibility of a div with multiple checkboxes using the iCheck plugin for jQuery

I customized my checkboxes using the icheck plugin to work with both single and multiple checkboxes, including a "Check all" option. Here is an example of how it looks in HTML: HTML : <div>Using Check all function</div> <div id="action" c ...

How to customize the font size in three.js CSS3DRenderer

I am trying to use threejs's CSS3DRenderer to display text in my 3D view. However, I am facing issues with controlling the font size. Despite setting font-size: 1px in CSS, the text remains large. I also attempted to adjust the scale of the css3dobjec ...

Guide on retrieving information from the World Bank API using jQuery/AJAX despite receiving a success status code of 200, but encountering an error

I am trying to retrieve data in JSON format using the code provided at this link: . Despite specifying dataType: jsonp, the request is returning a status 200 and the data is received but the success handler is not being triggered, instead the error block ...

Troubleshooting issue with Rails 5.2.3 AJAX request: JSON key-value pairs containing the plus sign are not being received

In my Rails 5.2.3 project, I am attempting to send an AJAX request that includes a JSON representation of a callflow object (the specifics of which are not relevant). This JSON representation is located within a textarea with the id "newCallflowJsonDisplay ...

Having trouble with document.getElementById.innerHTML not displaying the correct content?

document.getElementById works in getting the element, but for some reason, the content disappears as soon as it is written in it. There are no errors on the console to indicate what's causing this behavior. I have been unable to identify the reason b ...

Guide to dynamically setting SCSS $variables in JavaScript after retrieving them from local storage in a React application

In my current situation, I am retrieving color combinations in hash values from the database through an API call and then saving them in localStorage for future use. However, I am facing a challenge when trying to access this data from localStorage and uti ...

Discover the secret to opening two JPG files on YouTube using just one URL!

I was curious about the way YouTube thumbnails function. Here is an example thumbnail from a random YouTube video. There is an hqdefault.jpg in the URL, followed by some variables that indicate the size. If we remove or change these variables, a larger im ...

Fixed Div Columns on the Far Left

My goal is to create a left-floating div that remains fixed in place while allowing another div to float next to it, creating two columns that stay intact when the page is scrolled. After researching, it seems like fixed position and floating elements don ...

Preventing Javascript array elements from being overwritten: Best practices

My challenge lies with the addToClients() function, which is designed to add a new value to the clients array whenever a button is pressed. The issue I am facing is that each time I press submit, the previous element in the array gets replaced by the new o ...

Ensure that the HTTP GET request completes before initializing Angular using the ngOnInit lifecycle hook

Overview I am working on an Angular application that consists of three components: root, child1 (tabs.component.ts), and child2 (io.component.ts). Additionally, there is a service in place that communicates with a Tomcat server by sending get and post req ...

Tips for sending information back to the previous screen in React Native Navigation version 5

Recently, I upgraded to react native navigation version 5 and now I am facing an issue with sending data back to the previous screen when making a goBack() call. To navigate to the next view, I use: const onSelectCountry = item => { console.log(it ...

Establish a jQuery cookie to store language preferences

On the website I oversee, I want to implement a way to set a cookie for the selected language. The only information available is that users choose their preferred language by clicking on a dropdown menu with distinct classes assigned to each language - on ...

Elements with inline-block display not aligning horizontally next to each other

I am puzzled as to why the two sections, section.structure and section.specificity, are not aligning properly. It seems that when I reduce the width, they line up correctly at a certain point which I haven't identified yet. My goal is to have the wid ...

Using React to pass a state value as a parameter in a method

Looking for a way to increase the reusability of my class. Dealing with a large form and don't want to create a unique method for each input. How can I pass the state value as a parameter to the method? I attempted the following approach: state = ...

What is the best way for me to comprehend these two sections of code?

const [counter, setCounter] = useState(0); function sortPeople() { const sortedPeople = [...peopleList]; let sortCount = counter; if (sortCount === 2) { sortCount = 1; setCounter(1); } else { sortCount += 1; setC ...

Enhancing visual appeal with Carousel Slider images

I am facing an issue with the Carousel Slider. I added an image to index.php, but when I tried adding a second image, it appeared just below the first one in the carousel. Click here for the first image Click here for the second image Slider.php < ...

Click on the button to automatically scroll to a specific component inside the dialog

In my JSF/Primefaces application, I have a page with a long content that can be scrolled, along with a dialog also containing lengthy content that requires scrolling. The dialog includes a button and I am looking to scroll the dialog content to a specific ...

Is there a way to automatically fill number fields by clicking on radio buttons using a JavaScript function?

Currently, I am facing an issue with a task that involves three elements: 1. Two Number fields (from standard class) that need to be populated dynamically. 2. A Javascript function designed to extract coordinates using geolocator. 3. A radio button which, ...