Position the center button evenly between two images, aligned vertically using percentages

I'm attempting to achieve a specific layout that includes images scaling across Bootstrap breakpoints and a static button position. The challenge is maintaining the layout as depicted in the mockup, regardless of image size.

  • The images are set to img-fluid for responsiveness.
  • The button does not need to scale and should remain fixed in its position.

Despite trying various methods like using absolute positioning for the button within the first column, I couldn't achieve the desired outcome.

I am open to alternative approaches as long as the content remains within a single row. This is just one of my attempts.


Here are some of my attempted solutions:

Initial layout without the button:

<div class="row py-3">
    <div class="col">
        <img src="https://fakeimg.pl/693x678" class="img-fluid">
    </div>
    <div class="col">
        <img src="https://fakeimg.pl/693x678" class="img-fluid">
    </div>
</div>

Attempt 1:

<div class="row py-3">
    <div style="position: absolute" class="text-center h-auto" style="position: absolute; left: 0; right: 0;">
        <a class="btn btn-success text-white px-5">Hi guys</a>
    </div>
    <div class="col">
        <div class="w-100">
            <img src="https://fakeimg.pl/693x678" class="img-fluid mr-3">
            <img src="https://fakeimg.pl/693x678" class="img-fluid ml-3">
        </div>
    </div>
</div>

Attempt 2:

<div class="row py-3">
    <div class="col">
        <a class="btn btn-success text-white px-5" style="position: absolute; left: 0; right: 0;">Hi guys</a>
        <img src="https://fakeimg.pl/693x678" class="img-fluid">
    </div>
    <div class="col">
        <img src="https://fakeimg.pl/693x678" class="img-fluid">
    </div>
</div>

Various other attempts were made...

Answer №1

Looks something like this

.container {
  position: relative;
}

a {
  position: absolute;
  bottom: 15%;
  left: 40%;
  transform: translateX(-50%);
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
  <div class="row">
      <img src="https://fakeimg.pl/693x678" class="img-fluid">
  </div>
  <div class="row">
    <img src="https://fakeimg.pl/693x678" class="img-fluid">
  </div>
  <a class="btn btn-primary text-white px-5">Hello everyone</a>
</div>

Answer №2

Here is an example of HTML and CSS code:

<div class="row another-row">
  <div class="col">...</div>
  <div class="col">...</div>
  <a class="button another-button"></a>
</div>

The corresponding CSS styling:

.another-row {
  position: relative;
}

.another-button {
  position: absolute;
  bottom: 30px;
  margin: 0 auto;
}

Answer №3

When placing a button within the first column, you can easily center it between two columns by using the formula parentWidth - 0.5 * buttonWidth. By combining the properties left and transform, you can achieve this effect:

For example:

document.querySelector(".btn").addEventListener("click", function () {
  [].forEach.call(document.querySelectorAll("img"), function (img) {
    img.src = "https://placehold.it/693x500";
  });
});
body {
  overflow-x: hidden;
}

.btn {
  position: absolute;
  left: 100%;
  transform: translateX(-50%);
  z-index: 2;
  bottom: 15%;
}
<link type = "text/css" rel = "stylesheet" href = "https://cutt.ly/Twe8un2"/>

<div class = "row">
  <div class = "col">
    <img src = "https://placehold.it/693x300" class = "img-fluid">
    <a class = "btn btn-danger text-white">Custom Button</a>
  </div>
  <div class = "col">
    <img src = "https://placehold.it/693x300" class = "img-fluid">
  </div>
</div>

Answer №4

Check it out here - I created a pen using a grid layout to mimic your mockup design.

I opted for Grid because it simplifies stacking elements and structuring a 2D layout.

.grid-container {
    display: grid;
    grid-template-rows : min-content 150px 20%;
    grid-template-columns : 45% 5% 5% 45%;
    justify-items: center;
    align-items: center;
    width: max-content;
    height: max-content;
 }

The images occupy only one row each, but to align the button as needed, I allocated 3 rows. The 150px row specifically serves as space for the button. To position the images, they span across all 3 rows.
In terms of columns, since I wanted the button centered above the images, I created 4 columns. Each image spans the outer 2 columns, while the button occupies the inner 2 columns.

 .img1 {
   grid-row: 1/4;
   grid-column: 1/3;
 }

 .img2 {
   grid-row: 1/4;
   grid-column: 3/5;
 }

 .button1 {
   grid-row: 2/3;
   grid-column: 2/4;
 }

You have the flexibility to adjust the percentages and sizes to better suit your requirements.

Note : In this example, the row you referred to would represent the grid container.

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

Learn how to showcase real-time stock information from Yahoo Finance on an Android device. See the JSON format provided below for guidance

finance_charts_json_callback( { "meta" : { "uri" :"/instrument/1.0/PTC/chartdata;type=quote;range=1d/json/", "ticker" : "ptc", "Company-Name" : "PTC Inc.", "Exchange-Name" : "NMS", "unit" : "MIN", "timezone" : "EDT", "curr ...

Passing a variable to the render method in a PDF document

I'm currently working on a project with Django where I need to convert an HTML monthly report into a PDF. The data for the report is retrieved from a database based on the selected month. I am facing an issue in sending the selected month from the HTM ...

Can you explain the significance of the '#' symbol within the input tag?

I was reading an article about Angular 2 and came across a code snippet that uses <input type='text' #hobby>. This "#" symbol is being used to extract the value typed into the textbox without using ngModal. I am confused about what exactly ...

Unable to transmit HTML emails

I've been encountering an issue while trying to send HTML emails using Gmail SMTP from CI. It seems that my emails are being rejected if they contain any tables. Interestingly, there is no error message provided; the emails simply do not show up in my ...

Effortlessly retrieving the id attribute from an HTML tag using jQuery

Currently, I am encountering an issue with a code snippet that is designed to extract the value from an HTML tag. While it successfully retrieves a single word like 'desk', it fails when attempting to do so for an ID consisting of two or more wor ...

The issue is that only the first checkbox within ngFor is being toggled on and off each time a different checkbox is clicked

When I have checkboxes inside a ngFor loop and click on any one of them, only the first one gets checked and unchecked, updating only the status of the first checkbox. Here is the HTML template: <div *ngFor="let num of count" class="m-b-20"> & ...

Automatically generating new lines of grid-template-columns with CSS grid

I am new to using grid in CSS and there are still some concepts that I don't quite understand. Below is the container I am working with: <section class="css-grid"> <div class="css-item"><img src="1.jpg" / ...

How can I apply borders to individual columns within a Bootstrap table without affecting the rows?

My attempted solution involved adding th and tfoot. However, I faced issues with applying borders to only the td elements - border was not being applied at the bottom of the table, and the thickness and color of the borders were inconsistent between column ...

Using Bootstrap to handle opening a link when a dropdown menu button is clicked

Utilizing Bootstrap in my main menu was essential for navigating through the numerous pages, subpages, and sub-subpages within my project. The dropdownmenu feature proved to be very helpful in this regard. However, my client now has a specific request - t ...

Pull in content or data from a separate page by leveraging AJAX

Hello, I'm just starting out in programming and I could really use some help with this issue. index.html <button id="press">Click here</button> <div id="show_image"> </div> <br> <div id="appended_area"></div&g ...

Unable to Utilize Custom Colors in Tailwind CSS within NextJS

I am currently experimenting with NextJs and Tailwinds CSS for a new project. However, I keep encountering an error when attempting to apply a custom background color: Failed to compile ./styles/globals.css:7:12 Syntax error: /Users/anishkunapareddy/Deskto ...

The feature of Google street view is not supported within the tabs on WordPress

I'm experiencing some issues with displaying Google maps and street view using the Wp Google Map WordPress plugin within tabs. The map displays perfectly on the first tab where I placed the short code for the map, but on the second tab where I placed ...

Counting Numbers with jQuery Animation from Zero to Percentage Value

I am currently working on a project where I aim to incorporate a jQuery Animated Number Counter, starting from zero and ending at a specified percentage value. The values of the counter are dynamically retrieved from a database. function timer() { if ...

The issue of the highlighted row not disappearing persists even after clicking on the adjacent table row

When selecting the gear icon for the menu option in a table row, I would like the background to turn yellow. I attempted the following code to highlight the table row: var view = Core.view.Menu.create({ model: model, menuContext: { ibmm: ibmm }, ...

"Exclusive Mui sx styles will be applied only when a specific breakpoint

As I update my old mui styling to utilize the sx attribute, I've noticed the ability to specify styles for different breakpoints using sx = {{ someCssProp: { xs: ..., md: ... and so on. Prior to this, I had been using theme.breakpoints.only for some ...

Drawing the canvas is taking place prior to the image being fully loaded in JavaScript

Currently, I am in the process of creating a collage using images that are all sized at 174x174 pixels. The images are stored on my server, and while my program can locate them successfully, it requires me to click the "save image" button twice for them to ...

I need help figuring out how to get a horizontal scrollbar positioned at the top of a div to function properly once the content has been loaded using ajax

HTML CODE <div id="scrollidout" style="overflow: auto; overflow-y: hidden; "> <div id="scrollidin" style="padding-top: 1px; height: 20px; width: 1000px">&nbsp;</div> </div> <div class="resultcontent" style="overflow ...

Avoiding leaps through the use of dynamic pictures?

Currently, I am implementing the picture element along with srcset to download the same image but in varying resolutions depending on the screen size of the device. The image has a style of max-width: 100%, causing it to shift the content below when downl ...

Remove HTML tags from a table cell containing a combination of radio buttons and labels

Javascript Function: My JavaScript function posted below is designed to iterate through the column indexes specified in the 2nd parameter and also iterate through the element ids provided in the 3rd parameter. It will then populate the textbox, radiobutto ...

The React modal refuses to disappear, stubbornly clinging to the original component it sprang from

Hey there, I'm a beginner in learning React and I'm encountering an issue with my popup modal component. Even after showing the modal, I can still interact with and click on the main UI elements. Can anyone assist me with this problem? https://i ...