Boostrap: A guide to positioning a button on top of an image while resizing

Need help figuring out how to position and resize a button over an image on your website? Currently, the button is moving when you resize the browser window. You've tried using percentages in the CSS without success. What should you do next?

<div id="discover" class="container-fluid">
<div class="row-fluid"> 
  <div class="col-lg-12 col-sm-12 col-xs-12 col-md-12 withimg">
    <img id="discoveryour" src="img/x.png" class="img-responsive">
  </div>  
</div>
<div class="row-fluid"> 
  <div id="bttnimg" class="col-lg-12 col-sm-12 col-xs-12 col-md-12">
<form id="start" method="post" action="x.php">
<button class="btn-primary">text</button>
</form> 
</div>
</div>
</div> 

Css:

  .withimg {
  width: 100%;
  overflow:hidden;
  padding: 0px;
  margin: 0px;
 }

  #discover{
  position: relative;  
 }

#bttnimg{
float: left;
position: absolute;
left: 62%;
top: 25%;
max-width: 750px;

 }

Answer №1

Ah, the classic dilemma of overlaying elements on a responsive image challenge.

It may seem daunting at first, but fear not; there's a simple solution to ensure the vertical positioning of your overlaid elements adjusts along with the image size changes.

Here is one straightforward approach:

https://i.sstatic.net/kB6OT.png

HTML:

<div class="img-wrapper">
  <img class="img-responsive"
       src="http://lorempixel.com/output/people-q-c-1200-400-4.jpg">
  <div class="img-overlay">
    <button class="btn btn-md btn-success">Button</button>
  </div>
</div>

CSS:

.img-wrapper {
  position: relative;
}

.img-responsive {
  width: 100%;
  height: auto;
}

.img-overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  text-align: center;
}

.img-overlay:before {
  content: ' ';
  display: block;
  /* adjust 'height' to position overlay content vertically */
  height: 50%;
}

The img-overlay:before pseudo-class takes care of the vertical alignment by adjusting the position of the overlay relative to the top of the image. In this example, the button will always be centered at 50% down from the top of the image (modify the height: 50% property for desired positioning).

jsfiddle

To ensure that the button remains responsive in size based on the window width, you can define a new class specifically for the button. Let's name it btn-responsive (replacing btn-md in the provided example). Utilize @media queries to adjust the styling of the btn-responsive based on varying window widths. Here's an example:

.btn-responsive {
  /* equivalent to 'btn-md' */
  padding: 10px 16px;
  font-size: 18px;
  line-height: 1.3333333;
  border-radius: 6px;
}

@media (max-width:760px) {
  /* similar to 'btn-xs' */
  .btn-responsive {
    padding: 1px 5px;
    font-size: 12px;
    line-height: 1.5;
    border-radius: 3px;
  }
}

Repeat this process for other screen widths as needed.

Answer №2

For those curious about implementing this feature with Bootstrap 5, there are new classes available to assist. To position a button floating on the top right corner of an image, you can use the following code (you can customize the button with any text or icon of your choice):

<div class="card">
    <img class="card-img-top img-thumbnail" src="someimage.png" alt="alt text">
    <div class="card-img-overlay">
        <a href="#" class="btn btn-outline-warning btn-sm float-end"
           data-bs-toggle="popover" data-bs-content="Edit image" data-bs-trigger="hover focus">
            <i class="far fa-edit"></i>
        </a>
    </div>

    <div class="card-body">
        <h5 class="card-title">Some title</h5>
        <p class="card-text">Some text</p>      
    </div>
</div>

Refer to the official Bootstrap documentation for further details.

Answer №3

Is it feasible to substitute the form tag with another DIV? This way, you could apply position: absolute to the button. I've put together a fiddle as a demonstration: https://jsfiddle.net/1x1pjwk7/

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

What's the reason behind this file not opening?

When I try to insert this code into files index.html, style.css, and app.js, the page refuses to open. The browser constantly displays a message saying "The webpage was reloaded because a problem occurred." I am using a MacBook Air with macOS Big Sur and a ...

Tips on aligning border-radius with parent border-radius

Seeking to create a text box with a sleek line on the left side for design flair. The challenge arises when attempting to match the thickness of the line with the border radius of 10px. After various unsuccessful attempts, here is the current solution: ...

I find it frustrating that Angular consistently redirects me to the login page every time I attempt to navigate to a different page

I currently have a website with both normal user-accessible pages and an admin dashboard accessible only by admins through ngx-admin. To restrict users from accessing the admin dashboard, I've implemented an auth guard that redirects them to the logi ...

What is the best method for choosing the parent elements?

I am attempting to create a sidebar that saves the last clicked link in local storage and still displays the collapsed links after the page is reloaded. $(".clickedLink").parent().parent().css('background-color', 'green'); Ca ...

Whenever I create a new JavaScript application, the CSS files do not seem to reflect the most recent changes

My Next.js App is running smoothly on my client when I do npm run build and test it with node server js. However, the issue arises when I move the app to a production server. After executing npm run build on the server, everything seems to work fine except ...

Rotation using CSS in Internet Explorer 8

Can anyone help me with a CSS solution for rotating elements in IE8? I've tried some solutions that claim to work in IE8, but they're not working for me. What am I doing wrong? Here's what I've attempted: <!DOCTYPE html> <htm ...

Exploring GitHub's sleek popup: in search of CSS styling!

Exciting news from Github: they have introduced a new feature called maps. This feature is developed using Leaflet, and it has some unique custom CSS for the pop-up design: I'm eager to implement something similar on my site, but I'm having trou ...

"After completing the survey form, the User Details form is displayed upon clicking the submit button

In my Quiz, each question loads on a separate page with clickable options. Some questions may have multiple answers, including an "Others" option. At the end of the quiz, users need to fill out a form. Although I've created a survey form, I'm fa ...

The motion does not adhere to the delay in transition

How come the first animation doesn't have a delay when hovering in and out? It just disappears instantly. I'm attempting to achieve a delay similar to the hue-rotate effect when hovering in and out. https://jsfiddle.net/u7m1Ldq6/15/ <div id ...

Steps for transferring data between two components

I'm looking for a way to transfer an object's id from one component to another. <ng-container matColumnDef="actions"> <mat-header-cell *matHeaderCellDef></mat-header-cell> <mat-cell *matCellDef="let user"> ...

Unable to display HTML content on a page using the foreach callback in JavaScript

I have a function that iterates through each element of an array and generates HTML content on the page while updating some properties using elements from the array. I am utilizing forEach to iterate over the elements of the array and innerHTML to display ...

The CSS hover effect is not being implemented correctly. Can you identify the specific issue that is causing this problem?

I am working on creating a dynamic Search List for the Search Bar that changes color when hovered over and gets selected when clicked from the displayed item list. searchlist.jsx: import React from 'react' import { FaSearch } from 'react-ic ...

Is there a way for me to detect if the user has manipulated the CSS or JavaScript in order to alter the look of my website?

Is there a way to determine if someone is utilizing script or CSS extension tools? For instance, if they have adjusted alignments, applied display: none; to multiple elements, or utilized jQuery's .hasClass to manipulate divs. ...

Is there a way to retain the dropdown values when the page is reloaded?

In my ASP.NET project, I have created a website where users can add articles to a table displaying different products. The website contains tables showing warehouse items and materials needed for product manufacturing. Users can select a product from a dr ...

What is the best way to position a row of divs above their parent div?

Need guidance on placing a row of divs (each with 3 columns of images) on top of a parent div (image) using Bootstrap 5. Previously used position: absolute, but encountering overflow issues. Attempted overflow: visible as well. Provided below is the code s ...

Choosing a CSS Grid layout

New to web development, I have a design dilemma. Working on a game that teaches students how to multiply, I am unsure about the best way to display the multiplication process. This picture illustrates my issue: https://i.sstatic.net/F8ZGs.png Given that I ...

steps for making a dropdown search bar

I am currently working on integrating a search form into my navigation bar. My goal is to have a small magnifying glass icon in the navigation bar, which, when clicked, will reveal a dropdown search form (similar to how it's done on this site - ). I&a ...

Does an event fire after the onclick event completes?

After an onclick event, I need a particular code to be executed. This works well on mobile devices using touchstart and touchend events. However, is there an equivalent event for computers? This is how my current code looks like: document.getElementById( ...

Can you place 4 table cells in the first row and 3 table cells in the second row?

Exploring the world of HTML and CSS designs for the first time. Here's a code snippet I've been working on: <html> <body> <table width="100%"> <tr> <td width="25%">&#160;</td> ...

What is the best way to present a specific row layout using HTML and CSS?

Can someone assist me in creating rows of links in html+css with a specific layout? The desired format should follow this structure: (Psuedocode) <body class="class_with_minmaxwidth_set_in_css"> <div class="container"> <div class ...