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

Looking to extract data from a Json object and add it into a table

<!DOCTYPE html> <html> <head> <script type="text/javascript"> function displayJsonData() { var jsonData = { "cars": [ '{"model":"Sentra", "doors":4, "features":["hi"," ...

Divide information in the chart

<table id="tab"> <tr><td class="sub">mmmmm</td><td class="sub">jjjjj</td></tr> <tr><td class="sub">lllll</td><td class="sub">wwwww&l ...

Clearing selections from a multiple-choice input field

I am seeking to implement a delete feature in a multi-select element similar to the functionality seen here on stackoverflow when selecting multiple tags for a question. Once an item is selected, I would like to display a close icon next to it so that user ...

The many potentials of looping through Sass maps

My sass map contains a variety of color shades, and the full code can be found on Codepen: $pbcolors: ( pbcyan : ( 50: #E5F5FC, 100: #CCEBF9, 200: #99D7F2, 300: #66C3EC, 400: #33AFE5, 500: #009DBF, 600: #008CAB, 700: #007C98, 800: #006D85, 900: #005D72 ...

What is the best way to target an input element with a specific ID using JQuery Mobile?

For instance, <input type="button" id="one" value="o" data-role="button" data-inline="true" class="game"/> I aim to utilize this button in a manner where I can access its value using the ID, like for updating the value. Is there a way to reference ...

Empower the user with the ability to interact through touch on dynamically

I am trying to make a div touchable and draggable. I have dynamically created 1 to 10 divs within another div. Currently, these divs can only be dragged using the mouse or cursor. However, I want to enable dragging through touch as well. Can anyone provi ...

Implementing automatic activation of a tab upon page load using Angular

I am currently working with a set of Bootstrap nav pills in my navigation bar. The 'ng-init' code in my Angular script sets the 'dateState' to 'past' on page load. However, I have noticed that the 'Past Events' nav p ...

Discovering a CSS value using jQueryUncovering the CSS value with jQuery

Currently, I have a script that allows me to change the color of an event in a calendar to red when clicked. eventClick: function(calEvent, jsEvent, view) { $(this).css('border-color', 'red'); } Now, I want to enhance this featur ...

Creating line breaks for ToolTip titles in Material-UI can be achieved by using the appropriate syntax and

I am currently working with the ToolTip component and I have a query regarding displaying two lines for the title text - one line for each language rather than having them displayed on a single line. Is it possible to achieve this and how can I style the c ...

Update the content of the widget

Currently, I am utilizing the following script to display NBA standings: <script type="text/javascript" src="https://widgets.sports-reference.com/wg.fcgi?script=bbr_standings&amp;params=bbr_standings_conf:E,bbr_standings_css:1&amp"></sc ...

Add your logo and personalized graphic to the top header section of your website using the Divi Wordpress theme

Can a logo and custom graphic be placed in the "top-header" section (i.e. to the left of the phone number) within the well-known ElegentThemes Divi template? I experimented with absolute positioning and adjusting the container/body to relative. The code c ...

How to Align Video Alongside Image in HTML without Using CSS

Is there a way to place a video on the left and an image on the right of the same line in HTML without relying on CSS? ...

Padding will not completely fill the <li> and <div> elements

Looking to create a sleek menu bar that's 40px tall and fills up 80% of the browser width. The challenge arises when trying to center the text within the menu items. Despite adjusting padding for alignment, there's still a small gap because of & ...

What is the best way to position a button at the bottom of an image?

I am facing an issue with the following code snippet: * { box-sizing: border-box; } .wrapper { position: relative; /* border:1px solid blue; */ width: 250px; } .text { position: absolute; border:1px solid red; bottom: 0px; ...

Combining Various Template Variables into a Single Variable in Django

I am facing a challenge in assigning multiple values from a dictionary to a variable within a Django template. For example: fruit.supplier = tesco fruit.color = blue {% with test=fruit.supplier_fruit.color %} {{ test }} {% endwith %} The expected ...

What is the method for including a TabIndex property in a JSON file?

I discussed the integration of HTML fields within a JSON file and highlighted how to utilize the TabIndex property effectively in JSON files. ...

How to customize JavaFx context menu appearance using CSS to achieve a full-width background color

I am trying to customize my context menu, but I'm having trouble with the white space between the menu item and the context menu border. Here is an example: view the rendered context menu with CSS styles What I want is for the color to fill the entir ...

Leveraging the :checked state in CSS to trigger click actions

Is there a way to revert back to the unchecked or normal state when clicking elsewhere in the window, after using the :checked state to define the action for the clicked event? ...

Aptana Studio 3 fails to detect PHP code embedded within an .html file

When writing code, such as: <!DOCTYPE html> <html> <body> <?php echo "My first PHP script!"; ?> </body> </html> After <?php, I am encountering a red X icon error indicating that a ">" is missing at the end of the ...

What strategies can be implemented to enhance the performance of this jQuery slider?

I have successfully created an image carousel. Encountered Challenges: The last slide is displayed, but the counter shows the first slide. How can I resolve this issue? When clicking play, it continues to advance the count. Is there a way to make it ...