Tips for maintaining the responsiveness of both images and text within a div container

I'm completely new to HTML and CSS. Everything looked perfectly aligned until I added text inside the three divs and now my images are unbalanced, even though I've set the same width and height:

Here is the code snippet:

#wrapper{
 display:flex;
 width: 100%;
 justify-content: space-around;
 }
 
 .party{
 display:inline-flex;
 flex-direction: column;
 align-items: center;
 justify-content: space-around;
 padding: 2%;
 
 }
 
 .party img{
 border: solid black 2px;
 padding: 0.3%;
 margin: 2%;
 max-width: 100%;
 max-height: 100%;
 }
 input[type = button] {
 background-color: black;
 padding: 10px 20px;
 color: white;
 margin: 2px 1px;
 cursor: pointer;
 }
<div id="wrapper">
<div class = "party">
<img src = "Images/birthday_1.jpg">
<h3 style = "font-size: 2vw;">Party Events</h3>
<p style = "font-size: 1.5vw;"> Want flower decoration for your birthday, ceremonies, baby showers and any party events? Consult to our florist now!</p>
<input type = "button" value = " Shop Now "/>
</div>
<div class ="party">
<img src = "Images/wedding_1.jpg">
<h3 style = "font-size: 2vw;"> Wedding Events </h3>
<p style = "font-size: 1.5vw;">We offer free consultation for a bride-to-be. Call our store to make an appointment on <b>+64 85459 3943</b></p>
<input type = "button" value = " Shop Now "/>
</div>
<div class = "party">
<img src = "Images/sympathy.jpg">
<h3 style = "font-size: 2vw;">Sympathy</h3>
<center><p style = "font-size: 1.5vw;"> Fresh flowers that express heartfelt thoughts and sincere condolences. Talk to our dedicated team by phone or come in to meet with our flourist in a private consultation.</p></center>
<input type = "button" value = " Shop Now "/>
</div>
</div>

This is what I want it to look like:

** Any suggestions on how to achieve this ideal result?**

Answer №1

#wrapper{
    display:flex;
    width: 100%;
    justify-content: space-around;
}
.party{
    display:inline-flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-around;
    padding: 2%;
    width: 33.33%;
}
.party-image-wrapper {
    position: relative;
    width: 100%;
    height: 0;
    border: solid black 2px;
    padding: 0.3%;
    padding-top: 100%;
    margin: 2%;
    max-width: 100%;
    max-height: 100%;
}
.party img{
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: block;
    object-fit: cover;
}
input[type = button] {
    background-color: black;
    padding: 10px 20px;
    color: white;
    margin: 2px 1px;
    cursor: pointer;
}
<div id="wrapper">
    <div class = "party">
        <div class="party-image-wrapper">
            <img src = "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg">
        </div>
        <h3 style = "font-size: 2vw;">Party Events</h3>
        <p style = "font-size: 1.5vw;"> Want flower decoration for your birthday, ceremonies, baby showers and any party events? Consult to our florist now!</p>
        <input type = "button" value = " Shop Now "/>
    </div>
    <div class ="party">
        <div class="party-image-wrapper">
            <img src = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4d/Cat_November_2010-1a.jpg/1200px-Cat_November_2010-1a.jpg">
        </div>
        <h3 style = "font-size: 2vw;"> Wedding Events </h3>
        <p style = "font-size: 1.5vw;">We offer free consultation for a bride-to-be. Call our store to make an appointment on <b>+64 85459 3943</b></p>
        <input type = "button" value = " Shop Now "/>
    </div>
    <div class = "party">
        <div class="party-image-wrapper">
            <img src = "https://i.guim.co.uk/img/media/26392d05302e02f7bf4eb143bb84c8097d09144b/446_167_3683_2210/master/3683.jpg?width=1200&height=1200&quality=85&auto=format&fit=crop&s=49ed3252c0b2ffb49cf8b508892e452d">
        </div>
        <h3 style = "font-size: 2vw;">Sympathy</h3>
        <center>
            <p style = "font-size: 1.5vw;"> Fresh flowers that express heartfelt thoughts and sincere condolences. Talk to our dedicated team by phone or come in to meet with our flourist in a private consultation.</p>
        </center>
        <input type = "button" value = " Shop Now "/>
    </div>
</div>

When using the flex property to align elements in a row, it's important to specify the widths of each element to prevent them from adapting to their content's width. I divided the width into 33.33% for each of the 3 elements to achieve this.

To create responsive square images, you can place the image within a container with a fixed height set to 0 and a percentage-based padding, resulting in a perfect square shape regardless of the image's original size or ratio. This allows you to use absolute positioning and object-fit: cover to fit the image perfectly within the square container.

While these concepts may seem complex at first, don't worry if you're still learning. Feel free to ask any questions in the comments section for further clarification.

Answer №2

Initially, the reason for the dynamic resizing of the boxes is due to their inclusion in a flexbox layout where they inherently adjust based on their content. Although there are ways to modify this behavior, it seems like you're aiming for all 3 boxes to maintain an equal width.

If your design is limited to just 3 party boxes, avoid relying solely on flexbox for controlling widths and implement the following CSS:

.party {
    display:inline-flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-around;
    padding: 2%;
    width: 33.33%;
    box-sizing: border-box;
}

Alternatively, if you prefer to retain the flexbox approach, you can utilize flex-basis: 33.33% instead of width: 33.33%.

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

Unlocking the Mysterious Realm of HTML

I recently stumbled upon a mysterious combination of HTML attributes: <div vanisheffect="true" blinkcolor="red" fadeopacity="0.8"> Have you ever encountered something like this? And more importantly, does it have any impact on specific browsers? Is ...

Trouble with toggling class "collapsed" in Bootstrap v3 Navbar-toggle when using multiple data-targets

When using the data-target attribute with multiple CSS selectors, the behavior of toggling the "collapsed" class in navbar-toggle does not work as expected. If only one selector is used, the class is toggled properly, but when multiple selectors are specif ...

Executing a JavaScript function across multiple elements: A step-by-step guide

I recently came across this code snippet on w3schools.com: <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> * {box-sizing: border-box} body {font-family: Verdana, ...

Applying the CSS property overflow-x: hidden will prevent horizontal overflow and only display

When I set overflow-x: hidden on an element that overflows both horizontally and vertically, it displays a vertical scroll bar along with hiding the horizontal overflow. I attempted adding overflow-y: visible and just overflow: visible, but it had no effec ...

CSS - starting fresh with animations

I am facing an issue with a CSS animation that I created. Everything seems to be working correctly, but I need to complete it by setting the input type to reset the animation. Below is the CSS code snippet that should reset the animation: $('button& ...

Steps for incorporating HTML templates into a Next.js 13 project

Looking for advice on converting an HTML template using Bootstrap or Tailwind CSS into a Next.js 13.4 project. I have experience with Next.js 12 but understand that the project structure has changed, removing files like _app.js and _document.js. Any tips o ...

Ways to insert text inside a border

I'm struggling to figure out how to insert text within a border. Can someone guide me on how to accomplish this? Is there a way to place text in the center of a border? I have included a screenshot for reference. ...

Tips for effectively passing a parameter in @url.Action

I stumbled upon this piece of code: <button type="button" class="btn btn-inverse btn-danger" onclick="@("window.location.href='" + @Url.Action("ActionName", "ControllerName") +"'");"> Link </button> for redirecting- it works gre ...

Tips for Excluding Content Retrieved Through file_get_contents?

Currently, I am storing the source code of a webpage in a variable called $html using the following line: $html = file_get_contents('http://www.google.com'); When I use <textarea><?php echo htmlentities($html); ?></textarea> ...

Unable to get the Gtranslate function to function properly within the drop-down menu

Currently, I am using Gtranslate.io languages on my website with flags displayed without a drop-down menu. Everything is running smoothly but now I am looking to enhance the user experience by placing the flags inside a drop-down list. I want English to ...

Access the file and execute JavaScript code concurrently

Can I simultaneously link to a file as noticias.php and call a JavaScript function? <a href="javascript:toggleDiv('novidades');" class="linktriangulo"></a> The toggleDiv function in my noticias.php file: function toggleDiv(divId) { ...

Deduct a digit from a variable

My goal is to decrease the value of revlength each time the loop runs. For example, if there are 2 posts by 'A Google user', then subtract 2 from revlength. This is my attempted solution: var revlength = place.reviews.length; if (place.reviews[ ...

The problem arises from misinterpreting MIME types when serving SVG files, causing the resource to be seen as an image but transferred with

Having some issues targeting SVG images with CSS to use as backgrounds on certain elements. When I try to access the image directly here, it works perfectly fine. However, when using it in CSS, I encounter the following error: Resource interpreted as Imag ...

CSS translation animation fails to execute successfully if the parent element is visible

Inquiries similar to this and this have been explored, but do not provide a solution for this particular scenario. The objective is to smoothly slide a menu onto the screen using CSS translation when its parent is displayed. However, the current issue is ...

Encountering some difficulties while setting up my development tool (specifically webpack)

I have embarked on a journey to learn modern JavaScript with the help of Webpack and Babel. As a beginner in this field, my instructor is guiding me through the principles of modern JavaScript by building an app called Forkify - a recipe application. While ...

Extract hidden form variables using a function in PHP

Here is the JavaScript function that I have written: function GetCellValues() { var rows = document.getElementsByTagName('tr'); var str = ''; for (var c = 1 ; c < rows.length ; c++) { str += '\n&apo ...

The issue of video tags not displaying previews on iPhone across all browsers is also accompanied by problems with controls not functioning correctly

As I delve into the world of HTML5 video tags, I encountered an issue where the video wouldn't show a preview frame initially. Instead, only the Resume button would appear. Furthermore, after playing the video with the Resume button, it wouldn't ...

Encountering issues with the functionality of my Bootstrap navbar

After diving into the world of bootstrap, I encountered some challenges with the navigation bar in mobile view. Despite adding a navbar and a toggle button, it doesn't seem to be functioning correctly. Following tutorials and examples meticulously, bu ...

Transforming JQuery code into pure Javascript: Attaching an event listener to dynamically generated elements

After exhausting all available resources on stack overflow, I am still unable to find a solution to my problem. I am currently trying to convert the JQuery function below into Vanilla JavaScript as part of my mission to make web pages free of JQuery. How ...

When the child element's "aria-expanded" attribute is set to true, a CSS class should be dynamically

Is there a way to apply a grey background to the first div when the dropdown is open? I've managed to add CSS based on the child elements' aria-expanding attribute, but I'm unsure how to do it for a parent element. Since I am using Vue, I pr ...