Keep a consistent square proportion within a horizontal adaptable design

I am currently working on creating a responsive material list view that consists of cards with images, text details, and user actions. The challenge I'm facing is that the height of the cards can vary due to the different textual details, and the width also needs to be responsive.

My main requirement is to ensure that the image within each card remains a square shape. While I have found solutions for achieving this in a vertical layout, I am struggling to implement it in a horizontal layout.

Below is a concept design using Flex to showcase the box layout. However, I am open to alternative solutions that do not involve Flex:

Sample code:

.card {
  display: flex;
  flex-flow: row wrap;
  width: 100%;
  margin-bottom: 15px;
}

.card .title {
  line-height: 3rem;
  font-size: 1.5rem;
  font-weight: 300;
}

.card .action {
  background: grey;
  order: 3;
  flex-basis: 100%;
}

.card .content {
  background: red;
  order: 2;
  flex-grow: 1;
}

.card .image {
  background: gold;
  order: 1;
  flex-grow: 1;
}
<div class="row">
  <div class="medium-6 columns end">
    <div class="card">
      <div class="content">
        <span class="title">
          Just a Title
        </span>
      </div>
      <div class="action">
        <p>this is an action</p>
      </div>
      <div class="image">
        <p>Image goes here</p>
      </div>
    </div>
    <div class="card">
      <div class="content">
        <span class="title">
          Title with some text
        </span>
        <P>This is some content under the title</P>
      </div>
      <div class="action">
        <p>this is an action</p>
      </div>
      <div class="image">
        <p>Image goes here</p>
      </div>
    </div>
    <div class="card">
      <div class="content">
        <span class="title">
          Title with more text
        </span>
        <P>This is some content under the title</P>
        <P>Second line of text</P>
      </div>
      <div class="action">
        <p>this is an action</p>
      </div>
      <div class="image">
        <p>Image goes here</p>
      </div>
    </div>
  </div>
</div>

Answer №1

Consider inserting the following script towards the end of your project:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.js"></script>
<script>
    var cw = $('.image').width();
    $('.image').css({
    'height': cw + 'px'
    });
</script>

MODIFICATION

If you want the resizing to happen dynamically when the window is resized, this alternative might work better:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.js"></script>
<script>
    var cw = $('.image').width();
    $('.image').css({
    'height': cw + 'px'
    });
window.onresize = function(event) {
    var cw = $('.image').width();
    $('.image').css({
    'height': cw + 'px'
    });
};
</script>

Answer №2

If your images are square, you can achieve this layout:

.card {
  display: flex;
  flex-flow: row wrap;
  width: 100%;
  margin-bottom: 15px;
}

.card .title {
  line-height: 3rem;
  font-size: 1.5rem;
  font-weight: 300;
}

.card .action {
  background: grey;
  order: 3;
  flex-basis: 100%;
}

.card .content {
  background: red;
  order: 2;
  flex-grow: 1;
}

.card .image {
  background: gold;
  order: 1;
  flex: 1;
  max-width: 30vw; /* scale img block with page */
}

.card .image img {
  display: block;
  width: 100%;
}
<div class="row">
  <div class="medium-6 columns end">
    <div class="card">
      <div class="content">
        <span class="title">
          Just a Title
        </span>
      </div>
      <div class="action">
        <p>this is an action</p>
      </div>
      <div class="image">
        <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=img%20goes%20here&w=300&h=300">
      </div>
    </div>
    <div class="card">
      <div class="content">
        <span class="title">
          Title with some text
        </span>
        <P>This is some content under the title</P>
      </div>
      <div class="action">
        <p>this is an action</p>
      </div>
      <div class="image">
        <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=img%20goes%20here&w=300&h=300">
      </div>
    </div>
    <div class="card">
      <div class="content">
        <span class="title">
          Title with more text
        </span>
        <P>This is some content under the title</P>
        <P>Second line of text</P>
      </div>
      <div class="action">
        <p>this is an action</p>
      </div>
      <div class="image">
        <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=img%20goes%20here&w=300&h=300">
      </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

Is there a way to control the visibility of two divs depending on which one a user hovers on?

Having two stacked div elements containing images, I want the div with id = "features3_HDS_image" to become visible when a user hovers over the div with id = "HDS_Blurb", and the div with id = "features3_FH_image" to be displayed when hovering over the div ...

Steps to import shared CSS using Styled-components

In my project using react, I've implemented styled-components for styling. One requirement is to have a shared loading background. Here is the code snippet of how I defined it: const loadingAnimation = keyframes` 0% { background-position: 95% 95%; } ...

Issue encountered while attempting to attach an event listener to an element within a class

Upon running the code, I encountered an error message that reads: Uncaught TypeError: this.startButton.addEventListener is not a function and I'm unsure of how to resolve it. Although I can successfully console.log the button inside the class, adding ...

Tips for retrieving JSON data using ajax with jPut

Recently, I stumbled upon a handy jQuery plugin called Jput that allows for easy appending of JSON data to HTML content. You can check it out here. However, I am curious about the process of sending and retrieving JSON data via AJAX. Any insights or guida ...

Here is a unique version: "A guide on centering a carousel item in jquery upon being clicked."

Does anyone know how to center the item I click in a carousel? I've searched high and low for a solution but couldn't find a clear answer. Can someone please assist me with this? This is what I have tried so far: http://jsfiddle.net/sp9Jv/ Here ...

Displaying photos locally in HTML is not supported

I've encountered an issue with my HTML code. I'm trying to load images from my local drive, and the path to the folder seems correct because I can open the images by ctrl + clicking on them. However, when I open my page using a live server or hos ...

Checking for Internet Connectivity in Mobile HTML5

Is it possible to check for internet connectivity on a mobile device? Can websockets be utilized to ping a server and determine if the connection is available? I am feeling frustrated as I believed there was a ping function in websockets for client-side u ...

How can you ensure an element's height matches its width within an Ionic framework?

I am currently creating an application using the Ionic framework, and I am looking to set a square image as the background. The challenge is to ensure that the image scales appropriately for different screen resolutions; ideally, it should occupy the full ...

Node.js is able to read an HTML file, but it seems to have trouble locating and loading

Struggling to load a jQuery file or any file in a node.js read HTML document. After spending considerable time on this issue, I realized that the Google-hosted library file works fine but my local file does not. The problem seems to be related to directing ...

What is causing this element to unexpectedly elongate when I hover on it, despite having a 3D rotation effect applied?

After implementing a rotational hover effect on an element using the following code: transform: perspective(600px) rotateY(-25deg); I encountered a major issue when hovering over it. The element sometimes rotates abruptly and stretches excessively. If y ...

What is the best way to create a cornered button using CSS?

Is there a way to create a button using only CSS? https://i.stack.imgur.com/7mjVV.png This is the code I've come up with so far: .customButton { width: 100px; height: 100px; background: #6d1a3e; position: relative; transform: rotate(-90 ...

I am experiencing issues with Google Fonts not functioning properly when I insert the bootstrap CDN

I have encountered an issue while working on my landing page. I am using Google fonts for the design and trying to integrate Bootstrap as well. However, whenever I add the Bootstrap link, all the font styles stop working. I have checked the order of the CS ...

Problems with aligning elements to both the left and right sides of the same line

Currently, I have two divs that I would like to float on the left and right sides. However, they seem to be sticking together and I can't seem to separate them.. HTML: <nav> <div id="nav_content"> <div id="home_ico ...

What makes the nav class different from the navbar class in Bootstrap?

Recently, I delved into learning about bootstrap and web development. To my surprise, I stumbled upon the nav and navbar classes in bootstrap 4. I'm curious to know what sets them apart from each other and when it's best to use one over the other ...

Is the row border camouflaged by the background?

feed_page: { margin: 'auto' }, feed_list: { margin: 'auto' }, feed_item: { textAlign: 'center', backgroundColor: '#fff', borderBottom: '1px solid #e0e0e0', margin: '10 ...

Is it possible to adjust the button's position based on the location of the cursor?

I am just starting to learn JavaScript, so I am not yet familiar with all the commands involved. I have a code that includes a button which scrolls back to the top of the page, but I was wondering if there is a way to change the position of that button bas ...

Best Practices for Integrating PHP Code into an HTML File for Contact Form Placement

My current setup involves an html file named index.html, which contains the code for my contact form fields such as name and email. Is there a way to include PHP code in this file without converting it to index.php? ...

Slideshow plugin glitch on Safari and Opera caused by jQuery implementation

My current project involves utilizing a fantastic plugin called Sequence.js for creating animations and transitions with CSS3 based on jQuery. I have set up a simple responsive fadeIn-fadeOut slideshow using this plugin. While everything works smoothly an ...

Animated the height of a rectangle during initial loading and when the mouse hovers over or leaves

Being new to Vue.js, I am looking to create a simple animation on component load for the first time. You can find my initial code here: <template> <div id="app"> <div class="rect" /> </div> </template ...

Improving the way text entered in an input box is displayed using HTML table cells

Seeking advice on how to display the last two input boxes in a dynamic HTML table so that users can easily view the data they have entered. The issue arises when the length of the data in these columns is large, making it difficult for users to see all the ...