How can I guarantee that the <div> is of equal size and that the text adjusts to fit without requiring overflow hidden

Looking for a simple platform to sell used cars? This is the perfect solution for dealers who prefer a straightforward listing of vehicles and prices presented in a visual format reminiscent of a print magazine.


                body {
                    font-family: Helvetica, sans-serif;
                }

                header,
                footer {
                    background-color: #7690AC;
                    color: #FFFFFF;
                    padding: 20px;
                    padding-bottom: 20px;
                }

                .content {
                    padding: 10px;
                    margin-bottom: 10px;
                    margin-left: 20px;
                }

                .grid {
                    display: grid;
                    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
                    grid-gap: 20px;
                    align-items: center;
                }

                .grid > article {
                    border: 1px solid #333;
                    box-shadow: 2px 2px 6px 0px rgba(0, 0, 0, 0.3);
                    min-height: 25em;
                    top: -10px;
                }

                .grid > article img {
                    max-width: 100%;
                    margin-bottom: 12px;
                }

                .text {
                    font-size: 1em;
                    transform: scaleX(1.2) scaleY(1);
                    margin-left: 25px;
                    width: 270px;
                }

                .text {
                    padding: 0 10px 20px;
                }

                .text > button {
                    background: gray;
                    border: 0;
                    color: white;
                    padding: 10px;
                    width: 100%;
                }

                h3.price {
                    text-align: center;
                }

                .cards {
                    grid-template-columns: repeat(2, 1fr);
                    border-collapse: collapse;
                    max-width: 1200px;
                    margin: 0 auto;
                    display: grid;
                    grid-gap: 1rem;
                }

                .card {
                    background-color: dodgerblue;
                    color: white;
                    padding: 1rem;
                    height: 4rem;
                }

                p.address,
                p.phoneno {
                    font-size: 18px;
                    font-weight: 900;
                    line-height: 12px;
                }

                ☎:before {
                    font-size: 14px;
                }
            
        

                <header>
                    <h1>YOURMOTORHOMES</h1>
                    <h2>Axtley Road, Penderton XX1 1XA</h2>
                    <h2>01634 00000</h2>
                </header>
                <div class="content">
                    <p>Welcome to YourMotorhomes!</p>
                </div>
                    
                <main class="grid">
                    <article>
                        <img src="https://i.imgur.com/PApYyjj.jpg" alt="Elddis Autoquest">
                        <div class="text">
                            <p><b>2020 70 ELDDIS AUTOQUEST 194 2.2 HDi 140 LOW PROFILE MOTORHOME PEUGEOT BOXER CHASSIS</b>
                                4 berth, end washroom, white, 100 litre fresh water, 70 litre waste water, 3 ring hob, OG cooker, 155 litre fridge, 600 miles, almost as new, 
                                <b class="price">£44895</b>
                            </p>
                        </div>
                    </article>
                      
                    <!-- Additional articles will go here -->
                     
                </main>

                <footer>
                    content
                </footer>
            
        

If you need any assistance with fine-tuning this template or making it production-ready, feel free to reach out. We're constantly working on enhancements to ensure that your sales platform is efficient and visually appealing.

Answer №1

    Here is an alternative suggestion - 
1. Delete the transform property from the .text class and 
2. Eliminate align-items from the .grid class. 

    .text {
        font-size: 1em;
        /* transform: scaleX(1.2) scaleY(1); */ // Make a note to remove this line. 
        margin-left: 25px;
        width: 270px;
    }
    .grid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
        grid-gap: 20px;
        /* align-items: center; */ // Keep in mind to delete this line. 
    }

Answer №2

Here are some necessary adjustments to the code that arranges the articles in a grid:

  1. Ensure each article has the same height
  2. Make sure each image within an article has the same height
  3. Adjust the font size of the text so it always fits within the article size

Let's address each requirement one by one:

  1. To set the height of each article, consider what would be appropriate for accommodating varying amounts of text while maintaining readability. The snippet below suggests a height of 30em.

  2. To maintain consistent image heights within articles, utilize a fixed dimension div with the image as a background. This ensures the entire image is visible without distortion or cropping.

  3. For dynamically adjusting font size based on text div height, JavaScript may be needed. Check the snippet below which iterates through each article and adjusts font size until it fits appropriately.

const articles = document.querySelectorAll('.grid > article');
    articles.forEach(article => {
      const articleH = article.offsetHeight;
      let h = article.querySelector('.text').offsetHeight;
      var fontSize = 0.9;
      while (h > articleH/2) {//NOTE assumes the img is 50% alter if it is something else
        article.querySelector('.text').style.fontSize = fontSize + 'em';
        h = article.querySelector('.text').offsetHeight;
        if (fontSize <= 0.2) break;
        fontSize = fontSize - 0.05;
      }
    });
body {
    font-family: Helvetica, sans-serif;
    width: 100vw;
    }

    // Other CSS styles mentioned in the original code...
<header>
    <h1>YOURMOTORHOMES</h1>
    <h2>Axtley Road, Penderton XX1 1XA</h2>
   // More HTML content from the original code...
    </main>
    <footer>
    content
    </footer>

An alternative method for handling image sizes could be using "cover" instead of "contain". This approach prioritizes centralizing the main subject of the image but may risk some parts being cut off.

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

Aligning text at the center of the page, stacked on top of each other

I am struggling with creating a responsive main page for a website. I am new to this and feeling lost on how to proceed. My goal is to stack text on top of each other in a readable way while also ensuring it looks good on mobile devices. However, my curren ...

Show a PDF document on the webpage by making an ajax request in a C# MVC application

Using ItextSharp, I am creating a Pdf from html and trying to show a preview of the Pdf on the screen for the user to interact with. Although the Pdf generation is correct, I am struggling to display it on the screen. Here is the Ajax call: function Pre ...

Rearrange the characters within the variable before inserting them after

I'm currently dealing with an external system that generates outdated code, however, I do have access to css and javascript. The specific issue at hand involves working with a table that displays each item on a separate row, sometimes resulting in up ...

JQuery's hover functionality is causing problems

I am experiencing an issue with the hover function. Here is an example: <a href="http://jsfiddle.net/sushanth009/YeBMA/1/">Click here </a> $('.curtain').hover(function() { var $container = $(this).closest('div.container&apo ...

Update the value of the second dropdown based on the choice made in the first dropdown menu

Within my code, I have defined two drop-down menus as shown below: <label for="select-choice-1" class="select">Customer Product Name</label> <select name="select-choice-1" id="select-choice-1" onchange="selectTransport(this.valu ...

Displaying random divs and subsequently animating them downwards using JavaScript

I am in the process of creating a random appearing div that then falls down. Here is the code I have so far: $(document).ready(function(){ $('#test').animate({top: 80 + '%'},900); }); <div id="test" style="background:#98bf21;heigh ...

What is preventing this DIV from stretching to the full width of its children?

Why isn't my container DIV expanding to the width of the large table? <html> <head> <link rel="stylesheet" href="http://www.blueprintcss.org/blueprint/screen.css" type="text/css" media="screen, projection" /> <link ...

Function for jQuery Slide Toggle is not being invoked

I'm struggling with jQuery Slide Toggle. $(document).ready(function() { $("a").click(function() { $(this).parent().parent().children("p").Toggle(400); return false; }); }); I have multiple posts displayed on the page fetched from ...

Odd behavior observed in the Android browser while choosing input fields

When stacking modal elements in the Android Webkit browser (tested on versions 2.2, 2.3, and 3.0), strange behavior seems to occur. Take this example: Here, I have a jQuery UI date picker with z-index 200, a gray overlay div covering the entire document w ...

Radio buttons with multiple levels

Looking to implement a unique two-level radio button feature for a specific option only. Currently, I have written a logic that will display additional radio buttons under the 'Spring' option. However, the issue is that when it's selected, t ...

Manipulating element height in AngularJS

My goal is to utilize the bootstrap style for a fixed navigation bar at the top of the page. By using navbar-fixed-top, I can fix an element to the top. The visibility of #subnav will be determined by the value of showsubnav. <div id="backnav"> ...

Ensure there is a gap between each object when they are arranged in a

Is there a way to customize the layout of elements in the ratings view so that there is automatic spacing between them? I considered using text (white spaces) for this purpose, but it seems like an inefficient solution. Are there any other alternatives to ...

Is there a CSS alternative to using <br clear=all> on divs that do not have set heights?

Back in the day, I used to rely on <br clear=all> at the end of a div, without specifying a height value in CSS, just to control its height. These divs would expand and contract based on the content within. Is there a more elegant approach to make ...

Is it possible to modify the inner text of the body element without changing the surrounding HTML tags?

In my script, I have the following line : var ReplacingScope = document.querySelector( "body" ); let Regexp = new RegExp("fdp", 'gi'); ReplacingScope.innerText = ReplacingScope.innerText.replace( Regexp,"pdf"); While this successfully replaces ...

Can you explain the functionality of icon maps (or charts)?

As I was exploring the source code of various websites, I noticed a common pattern - all the icons were stored in one image map. Upon further investigation, I discovered that each icon is referenced individually when needed. I came across a helpful article ...

Incorporate row numbering feature into jQuery DataTables

Is there a way to have jQuery datatables generate a row number column in the first column, similar to a datagrid in VB? This is what I'm aiming for: If you have any insights on how this can be achieved, please share! ...

Having difficulty retrieving the value from a database column as it consistently displays both the column name and value

I am currently working on setting up a dynamic site title that will be controlled by my database. This way, I can easily update the database with a new title and have it reflect on my website. Below is an example of the controller setup: TestController: ...

Is it possible to create a webpage layout with CSS using the flex or block properties?

This code is automatically generated HTML from a CMS and the items will be dynamic inside a ul tag. However, I require a layout similar to the image below: https://i.sstatic.net/1xnow.png * { box-sizing: border-box; } ul { list-style: none; padd ...

Text that disappears upon clicking on show/hide按钮

Could someone please help me figure out how to prevent the "See more" text from disappearing when clicked in the example below? I want it to stay visible. Thank you! ...

How to use Jquery to fetch the value of a td element nested inside another

After consulting the manual, I am trying to find a way to extract the value in the second column of a table when a specific span is clicked. However, the challenge is that the click event is currently being triggered by the first span cell. Here is the jQu ...