Tips for positioning elements in a three-column layout, ensuring they remain aligned regardless of screen size

Looking for a solution: I'm facing an issue with my three columns where the text and buttons are not aligned properly due to resizing. The columns are not of equal width, and the content jumps around as the text size changes. How can I fix this problem?

HTML:

<section class="sub-offer">
      <div class="offer-container">
        <div class="offer-up">
          <h2>Lorem, ipsum.</h2>
          <img src="https://via.placeholder.com/150x150.png/09f/fff" alt="" />
        </div>
        <div class="offer-down">
          <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minus a
            rerum sapiente odit porro obcaecati fugit, maxime modi veritatis
            quis!
          </p>
        </div>
        <div class="more-btn">
          <a href="#">Lorem, ipsum.</a>
        </div>
      </div>

      <!-- Repeat above code block two more times -->

    </section>

CSS:

.sub-offer {
  text-align: center;
  display: flex;
  position: relative;

  .offer-container {
    background: linear-gradient(to bottom right, #2e2e3b, #0f1519);
  }

  h2 {
    font-family: 'Playfair Display', serif;
    font-size: 15px;
    position: absolute;
    margin-left: auto;
    margin-right: auto;
    margin-top: 4.3em;
    width: 33%;
    text-align: center;
  }

  p {
    font-size: 12px;
    margin-top: 2em;
    padding: 0 0.5em 0 0.5em;
    font-family: 'Montserrat', sans-serif;
  }

  a {
    display: inline-block;
    font-family: 'Montserrat', sans-serif;
    font-size: 12px;
    background: white;
    margin: 3em 0 2.5em 0;
    padding: 0.5em;
    text-decoration: none;
    color: black;

    &:hover {
      background: #e5e5e5;
    }
  }
}

Visual Representation:

Link to Image

Answer №2

My problem is now resolved. I was able to ensure that all three columns have an equal width by following the advice given by @tacoshy in the comments section:

section.sub-offer { display: flex; } .offer-container { width: calc(100% / 3); }

In order to align the three buttons while still keeping them responsive, I enclosed each button within a div and applied the following CSS:

.more-btn {
    display: flex;
    align-self: center;
    margin-top: auto;
}

Answer №3

I may not be the most experienced developer, but I decided to give it a shot. When I need things to align perfectly, I typically use grid, so I attempted to add it to .offer-container using this code:

.offer-container {display: grid; grid-template-rows: 1fr 1fr 1fr;}
. Below is a functional example. However, since I'm new to assisting people on StackOverflow, or even using StackOverflow in general, I may have made mistakes such as providing a solution to the wrong problem or fixing issues that were not requested. For a summary of the changes I made, you can refer to the following: added } for sub-offer,
removed extra closing bracket on a{}
,
separated &:hover to a:hover{
, made <p> text white. I hope this resolves the issue you are facing unless it has already been resolved.

.sub-offer {
    text-align: center;
    display: flex;
    position: relative;
    width:100px
}

.offer-container {
    background: linear-gradient(to bottom right, #2e2e3b, #0f1519);
    display: grid;
    grid-template-rows: 1fr 1fr 1fr;
}

h2 {
    font-family: 'Playfair Display', serif;
    font-size: 15px;
    position: absolute;
    margin-left: auto;
    margin-right: auto;
    margin-top: 4.3em;
    width: 33%;
    text-align: center;
}

p {
    font-size: 12px;
    margin-top: 2em;
    padding: 0 0.5em 0 0.5em;
    font-family: 'Montserrat', sans-serif;
    color: white;
}

a {
    display: inline-block;
    font-family: 'Montserrat', sans-serif;
    font-size: 12px;
    background: white;
    margin: 3em 0 2.5em 0;
    padding: 0.5em;
    text-decoration: none;
    color: black;
}

a:hover {
    background: #e5e5e5;
}
<section class="sub-offer">
  <div class="offer-container">
    <div class="offer-up">
      <h2>Lorem, ipsum.</h2>
      <img src="https://via.placeholder.com/150x150.png/09f/fff" alt="" />
    </div>
    <div class="offer-down">
      <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minus a
        rerum sapiente odit porro obcaecati fugit, maxime modi veritatis
        quis!
      </p>
    </div>
    <div class="more-btn">
      <a href="#">Lorem, ipsum.</a>
    </div>
  </div>

  <div class="offer-container">
    <div class="offer-up">
      <h2>Lorem, ipsum.</h2>
      <img src="https://via.placeholder.com/150x150.png/09f/fff" alt="" />
    </div>
    <div class="offer-down">
      <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Excepturi
        perspiciatis voluptas qui iste, voluptatem atque ab rerum illum quia
        incidunt odio?
      </p>
    </div>
    <div class="more-btn">
      <a href="#">Lorem, ipsum.</a>
    </div>
  </div>

  <div class="offer-container">
    <div class="offer-up">
      <h2>Lorem, ipsum.</h2>
      <img src="https://via.placeholder.com/150x150.png/09f/fff" alt="" />
    </div>
    <div class="offer-down">
      <p>
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Beatae
        vitae aut fugit dicta repellendus dolorem, quam, accusamus hic nemo
        ullam quod porro atque error?
      </p>
    </div>
    <div class="more-btn">
      <a href="#">Lorem, ipsum.</a>
    </div>
  </div>
</section>

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

Ways to extract a variable from an HTML source using JavaScript and Ajax queries

I'm currently working with Zend Framework 2 and I have a question regarding accessing data defined in HTML within my JavaScript code. Here is the HTML snippet: <tr class="MyClass" data-MyData="<?php echo json_encode($array);?>"> And her ...

Two-toned diagonal design featuring a lone character

I'm searching for a unique design featuring the letter "d" in two colors, arranged diagonally without any gradient effects. Here is the CSS code I am currently using: .gradient_text_class { font-size: 72px; background-image: linear-gradient(50de ...

How to access specific values from a deserialized JSON string using Javascript

Within the nested for loops, I have the following row: document.getElementById("sm" + i + "b" + j).innerHTML = jsonval.values.sm1b1; ^^^^^^ In order to change the va ...

Would you prefer to generate fresh HTML using JavaScript or dynamically load an existing HTML layout using AJAX?

I have a project where I need to generate a large amount of HTML that isn't currently on the page. Up until now, I've been using jQuery to construct the page piece by piece with JavaScript, adding divs and adjusting layouts as needed. Lately, I ...

Issues with the linear-gradient feature in Ionic3 are preventing it from properly functioning

I'm currently experimenting with creating a unique gradient color in my Ionic3 variable.sass file. $colors: ( primary: #4471C1, secondary: #32db64, danger: #f53d3d, light: #f4f4f4, dark: #222, newcolor: linear-gradient( ...

attempting to utilize staggerTo function in GSAP

Struggling to animate my images with greensock so they start from bottom: 0, have opacity 1, and stagger in. My code seems valid but I must be missing something after looking at it for so long. I suspect the find() function is not returning what I expect ...

Tips for effectively utilizing the display:none property to conceal elements and permanently eliminate them from the DOM

While working on my website, I utilized CSS media queries to hide certain elements by using display: none. Even though this effectively hides the element from view, it still lingers in the DOM. Is there a way to completely eliminate the element from the ...

Transition one background image into another using background positioning

Snippet of code: https://jsfiddle.net/foy4m43j/ HTML: <div id="background"></div> CSS: #background { background-image: url("http://i.imgur.com/hH9IWA0.png"); background-position: 0 0; background-repeat: repea ...

Define the content and appearance of the TD element located at the x (column) and y (row) coordinates within a table

In my database, I have stored the row and column as X and Y coordinates. Is there a straightforward way to write code that can update the text in a specific td element within a table? Here is what I attempted: $('#sTab tr:eq('racks[i].punkt.y&a ...

Is it possible to load fancybox content within a div element?

I am attempting to modify the functionality of fancybox 2 by placing the content inside a hidden div (rather than floating it in the center of the screen) that will expand to fit its contents. When the close button is clicked, the div should shrink and hid ...

Updating ng-repeat in a different controller with AngularJS

My coding task is quite straightforward. I am trying to refresh an ng-repeat in my AngularJS application. Here is the HTML code snippet: <div class="quick-actions" ng-controller="DashboardController"> <div class="ui accordion segment"> ...

Using PHP, show a specific table row when clicked by matching the ID

I am currently developing an application for a school project that manages tests. This app allows employees to log in, select a client, register clients, and conduct tests with them, all while storing the data in a database. I have successfully implemente ...

Manipulate component properties using CSS hover in React with Material-UI

I am attempting to modify the noWrap property of a Typography element when hovering over it. I have defined a custom CSS class for the parent element and the hover functionality is working correctly. However, I am unsure how to adjust the noWrap property u ...

How can I enable the "edit" function for a button within a list item?

I am currently working on a feature that allows users to edit list rows by clicking a button. However, I am facing an issue where the "edit" button only edits the first row instead of the matched line. This functionality is part of a program I am developi ...

CSS for two columns with a width of 50% each

Below is a table for reference: <table width="100%"> <tr> <td id="td1"></td> <td id="td2"></td> <td id="td3"></td> <td id="td4"></td> </tr> </table> Is there a wa ...

Learn how to customize the background color of the DropdownToggle when a DropdownItem in Bootstrap 4 is clicked

Is it possible to modify the background color of DropdownToggle when selecting DropdownItem? Additionally, is there a way to adjust the top and bottom blue borders when clicking DropdownToggle? Picture 1: The image depicts the initial appearance before se ...

AJAX does not support functioning links

I've been experimenting with AJAX on my website, and encountered an issue. When I dynamically add content to the page using container.insertAdjacentHTML('beforeend', thing); everything looks fine. However, when I try to click on the links wi ...

How can I create a responsive design for my div elements?

I am facing an issue with responsiveness in my div container. Inside the square-shaped frame, I have a h2 tag that does not adjust properly when viewed on different devices such as mobile and browsers. Despite setting up media queries to make it responsive ...

Solving the Size Problem with Bootstrap 5 Off Canvas

I am utilizing the bootstrap 5 off-canvas feature for collapsing, while also having an announcement bar that appears as a dismissible alert on top of my navbar. The issue I'm facing is that the top size of my off-canvas is not adjusting properly whe ...

Preventing navbar resizing on websites

I'm trying to have a section on the navbar dedicated for text or alerts that appear based on certain events. However, even when the alert is hidden, it still affects the height of the navbar. How can I resolve this issue? It may be difficult to see i ...