The text inside a paragraph tag is causing other elements to shift when the window size is changed

With three divs displayed inline, each containing an image, heading, paragraph, and anchor tags, a design issue arises. The inconsistency in text lengths under the images causes visual discrepancies when resizing the window. For example, while the first two divs display text in 4 rows, the third div's longer text extends to 5 rows, disrupting the alignment of elements. This problem persists at various window sizes due to the direct impact of text length on element positioning. Any suggestions for resolution?

One attempted solution involved wrapping the paragraph tag within another div and setting a specific height through CSS. However, this approach proved ineffective in resolving the issue. The 'span-1-of-3' class mentioned is part of an external grid system sourced online.

<div class="span-1-of-3 box">
                <h3>New Products</h3>
                    <img src="img/girlleather.jpeg" alt="Woman in Leather" class="what-img">
                    <div class="text-box"><p>TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST</p></div>
                    <a href="#" class="btn">Check out</a>       
            </div>
            <div class="span-1-of-3 box">
                <h3>Fashion Show</h3>
                    <img src="img/fashionshow.jpeg" alt="Fashion Show" class="what-img">
                    <div class="text-box"><p>TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST</p></div>
                    <a href="#" class="btn">Check out</a>       
            </div>
            <div class="span-1-of-3 box">
                <h3>New: Watches</h3>
                    <img src="img/watches.jpg" alt="Watches" class="what-img">
                    <div class="text-box"><p>TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST</p></div>
                    <a href="#" class="btn">Check out</a>       
            </div>

.what-img {
    width: 94%;
    border-radius: 10px;
}

.new {
    text-align: center;
}

.box {
    display: inline-block;
    margin-top: -20px;
}

.box h3 {
    padding: 15px 0px 15px 0px;
    letter-spacing: 4px;
    font-size: 150%;
}

.box p {
    padding: 15px 10px 15px 10px;
    font-size: 17px;
    word-break: break-word;
}

.text-box {
   position: relative;
}

.box a {
   text-decoration: none;
   color: #fff;
   padding: 10px 20px 10px 20px;
   background-color: #923d0a;
   border: 2px solid #000;
   border-radius: 20px;
   display: inline-block;
   margin-top: 20px;
   letter-spacing: 2px;
}

Answer №1

Consider using the vertical-align attribute to align elements:

.box {
    display: inline-block;
    vertical-align: top;
}

.what-img {
    width: 94%;
    border-radius: 10px;
}

.new {
    text-align: center;
}

.box {
    display: inline-block;
    margin-top: -20px;
    width: 20%;
    vertical-align: top;
}

.box h3 {
    padding: 15px 0px 15px 0px;
    letter-spacing: 4px;
    font-size: 150%;
}

.box p {
    padding: 15px 10px 15px 10px;
    font-size: 17px;
    word-break: break-word;
}

.text-box {
   position: relative;
}

.box a {
   text-decoration: none;
   color: #fff;
   padding: 10px 20px 10px 20px;
   background-color: #923d0a;
   border: 2px solid #000;
   border-radius: 20px;
   display: inline-block;
   margin-top: 20px;
   letter-spacing: 2px;
}
<div class="span-1-of-3 box">
  <h3>New Products</h3>
  <img src="img/girlleather.jpeg" alt="Woman in Leather" class="what-img">
  <div class="text-box"><p>TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST</p></div>
  <a href="#" class="btn">Check out</a>       
</div>
<div class="span-1-of-3 box">
  <h3>Fashion Show</h3>
  <img src="img/fashionshow.jpeg" alt="Fashion Show" class="what-img">
  <div class="text-box"><p>TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST</p></div>
  <a href="#" class="btn">Check out</a>       
</div>
<div class="span-1-of-3 box">
  <h3>New: Watches</h3>
  <img src="img/watches.jpg" alt="Watches" class="what-img">
  <div class="text-box"><p>TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST</p></div>
  <a href="#" class="btn">Check out</a>       
</div>

Answer №2

To organize the content, I enclosed everything within a div using flex in a row format and assigned a specific fixed width (such as 1000px).

In the CSS for .box, I included a max-width property with a set maximum width of 333px (calculating from dividing the container's width by 3: 1000 / 3 = 333).

With this fixed width approach, resizing the window won't disrupt the layout and maintain the spacing without causing any clutter.

.inline {
  display: flex;
  flex-direction: row;
  width: 1000px
}

.what-img {
  width: 94%;
  border-radius: 10px;
}

.new {
  text-align: center;
}

.box {
  display: inline-block;
  margin-top: -20px;
  max-width: 333px;
}

.box h3 {
  padding: 15px 0px 15px 0px;
  letter-spacing: 4px;
  font-size: 150%;
}

.box p {
  padding: 15px 10px 15px 10px;
  font-size: 17px;
  word-break: break-word;
}

.text-box {
  position: relative;
}

.box a {
  text-decoration: none;
  color: #fff;
  padding: 10px 20px 10px 20px;
  background-color: #923d0a;
  border: 2px solid #000;
  border-radius: 20px;
  display: inline-block;
  margin-top: 20px;
  letter-spacing: 2px;
}
<div class="inline">
  <div class="span-1-of-3 box">
    <h3>New Products</h3>
    <img src="img/girlleather.jpeg" alt="Woman in Leather" class="what-img">
    <div class="text-box">
      <p>TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST</p>
    </div>
    <a href="#" class="btn">Check out</a>
  </div>
  <div class="span-1-of-3 box">
    <h3>Fashion Show</h3>
    <img src="img/fashionshow.jpeg" alt="Fashion Show" class="what-img">
    <div class="text-box">
      <p>TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST</p>
    </div>
    <a href="#" class="btn">Check out</a>
  </div>
  <div class="span-1-of-3 box">
    <h3>New: Watches</h3>
    <img src="img/watches.jpg" alt="Watches" class="what-img">
    <div class="text-box">
      <p>TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST</p>
    </div>
    <a href="#" class="btn">Check out</a>
  </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

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 ...

Is there a way to determine if the tab has been muted by the

Chrome enables users to easily mute tabs by right-clicking on them and selecting Mute Tab. I am curious about whether there is a method to detect when a user has muted the tab. In other words, I am interested in knowing if it's possible for a website ...

Nuxt: Issue with Head function affecting title and meta tags

I need to customize the titles and meta descriptions of different pages on my Nuxt website. However, I am struggling to make it work using the head() method as indicated in the documentation. For example, in my contact.vue file: export default class Cont ...

Creating a div that is subtly askew

When it comes to positioning a div 15 pixels off-center horizontally from the page, traditional methods like setting margins to auto or using position:absolute may not work in this situation. The challenge lies in ensuring that the div scales correctly a ...

Tips for creating a clickable phone number on a WordPress classified website

Despite trying numerous solutions to make the phone number clickable, it still didn't work as expected <?php global $redux_demo;?> <?php $phoneon= $redux_demo['phoneon']; ?> <?php if($phoneon == 1){?> <?php $po ...

What is the purpose of the c() function in JavaScript?

I recently stumbled upon some interesting JavaScript code in the source of a web page: function fsb329142055() { var b=new Array(57,50,102,50,52,99,50,53,52,56,102,98,102,98,101,102,101,49,53,61,101,99,110,57,111,78,109,54,114,111,56,48,102,38,1 ...

How can I customize the color of the Title Component in Ant Design using Styled Components?

I am currently integrating Ant Design with styled components in my project. One of the components in my application is a NavBar that includes an H1 Component. H1.tsx import React from 'react'; import { Typography } from 'antd'; impor ...

Using node appendChild() in HTML for animating elements

As someone who is brand new to the world of web development, I am trying my hand at creating a webpage that expands as a button is clicked. Recently, I stumbled upon this helpful link which includes some code: The HTML code snippet is: <ul id="myList" ...

How to fetch an image from a web API using JavaScript

I have recently entered the world of web development and I am facing an issue where I am trying to retrieve a Bitmap Value from a web api in order to display it on an HTML page using JavaScript. However, despite my efforts, the image is not getting display ...

Steps to bold a specific word within a div on react native

Hey there! I'm working with the code below: getDescription = () => { return( <div className="Description">{this.props.mytext + ': ' + this.name} </div> ) } Is it possible to bold only the specific te ...

Combining Two DIVS Side by Side

Hey there, I am working on a timeline using two divs within a table cell. My goal is to have these divs overlap instead of appearing one below the other. The position attribute for all the DIVs inside the cell must remain unchanged due to the drag/drop fu ...

Mastering the Art of Manipulating Z-Index Stacking Context in CSS Using Transforms

I'm struggling to position the red square on top of the table. After reading articles about Z-index Stacking Context and browsing through this stack overflow page about overriding CSS Z-Index Stacking Context, I still can't seem to figure it out. ...

There seems to be an issue with the zebra striping in the rich:datatable component when using the rowClasses

I'm attempting to give my table a "zebra" color pattern for the rows, but it seems like the rowClasses attribute isn't functioning properly (edit: I don't see any colors applied to the table at all). Here is how I defined the styles: <s ...

Unable to retrieve the sum total of all product items and display it in the designated text element

My product items are dynamically generated in a list. I have used the calculateItemTotal() method to determine the total for each item. Now, I need to sum up all these item totals and display the result in the total text field. However, instead of showing ...

What is the process of creating a parent-child rowspan HTML Table in a Blazor Server Application?

I am currently facing a challenge in my Blazor Server application where I am attempting to present parent-child data with rowspan in an HTML table. The table is being rendered, however, it is not displaying correctly. Here is the JSON data I am working wi ...

How can I utilize PHP to generate several tables within a single database?

I need some guidance on creating multiple tables within the "new" database. I'm curious if it's possible to include PHP code inside the query to generate table names dynamically. Any help would be greatly appreciated. Thank you in advance. <? ...

Emphasize a passage by clicking on a different section of text

Seeking Assistance I am currently customizing this template which incorporates the MixItUp plugin. My query pertains to highlighting the "filter tab" upon clicking on the corresponding text when hovering over each image, a task I find challenging as a new ...

Tips for customizing CSS hover effects using HTML and jQuery scripts

Just a heads up before I ask my question - the code I'm about to share is sourced from this video on YouTube, so all credit goes to the creator. JS $(".product-colors span").click(function(){ $(".product-colors span"). ...

Having trouble getting smooth scrolling with easing to function properly

I'm facing an issue with a JQuery function that is supposed to enable smooth scrolling using JQuery easing, but for some reason, it's not functioning correctly and I'm unable to pinpoint the error. Here is the code for the function: $(func ...

What is the best way to invoke an external JavaScript source using another JavaScript source?

I am trying to connect 2 different files together. file1.php and document.html file1.php has the following code in it: document.writeln('< script src="https://www.googletagservices.com/tag/js/gpt.js"> googletag.pubads().definePassback ...