What steps can I take to ensure the image remains fixed in its position when the browser width is reduced?

Recently, I encountered a challenge with my media query where I wanted to align a button next to another one when the browser width was reduced to 991px. However, instead of staying in place, the button started sliding underneath the other button as I continued to decrease the browser width.

I attempted to resolve this issue by setting the button's position to absolute, but unfortunately, that didn't produce the desired results. Despite trying various other solutions, I couldn't find a suitable fix for the problem.

If anyone has any suggestions on how I can ensure the button stays in place even when the browser width is less than 991px, please let me know. My apologies if my question appears vague; I'm open to any assistance or recommendations.

@media screen and (max-width: 991px) {
 a.myImg {
      position: absolute;
      margin-left: 11em;
      margin-top: -7.7em;
  }
}

Below is the HTML code snippet:

<div class="container-fluid">
    <div class="col-md-6">
        <a href="#" class="myImg col-md-6 col-sm-5 col-xs-5">
            <img src="picture/path"/>
        </a>
    </div>
</div>

Answer №1

@greyskies It seems like you're looking for some guidance on how to achieve a specific layout with buttons. I have provided two examples on CodePen that might help you out.

Example 1: If you want both buttons in the same 'box', I have included some CSS adjustments to make them align horizontally and adjust their size accordingly.

Example 2: In case you prefer the buttons in separate boxes next to each other, I made changes to the classes of the boxes to achieve this layout.

If these examples don't match your requirements, please consider providing a screenshot of the desired outcome for better assistance.

Here is the HTML code:

<div class="container-fluid v1">
  <h1>version 1</h1>
  <div class="row">
    <div class="col-md-6">
      <a href="#" class="myImg">
            <img src="https://via.placeholder.com/150x60?text=button-1"/>
        </a>
      <a href="#" class="myImg">
            <img src="https://via.placeholder.com/150x60?text=button-2"/>
        </a>
    </div>
  </div>
</div>
<div class="container-fluid v2">
  <h1>version 2</h1>
  <div class="row">
    <div class="col-6">
      <a href="#" class="myImg">
            <img src="https://via.placeholder.com/150x60?text=button-1"/>
        </a>
    </div>
    <div class="col-6">
      <a href="#" class="myImg">
            <img src="https://via.placeholder.com/150x60?text=button-2"/>
        </a>
    </div>
  </div>
</div>

And here is the compiled CSS:

.myImg img {
  display: block;
  width: 100%;
  height: auto;
}

.v1 .myImg {
  width: 48%;
  margin: 0 1%;
  float: left;
}

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

The functionality of jQuery field validation is impaired when interacting with Chrome's autofill feature

Despite adding the autocomplete="off" attribute to both the form and inputs using html5, I'm still unable to prevent Chrome autofill from interfering with the form submission process. In addition to this issue, I have implemented a basic jQuery field ...

Ways to implement div on hover in my table's td cells

Here is the HTML table code I have: <table border='1px'> <tr> <td id='td'>first</td> <td>last</td> <tr> <td id='td'>newFirst</td> <td>newSecond</td> </t ...

One div takes a backseat to the other div

I recently delved into learning Bootstrap, but I'm baffled as to why one div is appearing behind another. <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Fa ...

Tips for using jQuery dropdown menus

I am currently in the process of creating a prototype for a quick dropdown menu using jQuery. However, I have reached the limits of my knowledge on jQuery and could use some assistance in solving my issue. My objective is to have a dropdown animate down w ...

Linking all styles with Angular 2

Is it possible to apply a style when the exact nature of that style is unknown? Consider a scenario where I have a model containing string variables defining styles, as shown below: myStyle1:string="margin-left:10px"; myStyle2:string="margin ...

Concealing objects identified by a specific id

I have two separate div elements var promptContent = $('<div id="errorr">').addClass("formErrorContent").html(promptText).appendTo(prompt); var arrow = $('<div id="errorrarrow">').addClass("formErrorArrow"); I need to refe ...

Enabling hover effects to scroll divs only when interacted with

I am aiming to achieve two separate scrolling divs and I'm uncertain about the exact approach. Experimenting with various overflow properties has only resulted in one scrolling independently. .profile { width: 100%; display: flex; ...

Navigating Your Way Through the Center (ASP.NET MVC)

My navbar has elements that I want to center within it, but using margin hasn't been successful. Do you have any suggestions on how I can achieve this alignment using CSS? This is the code snippet: <div class="navbar "> <div class="cont ...

What is the best way to use regex to target only the content within a particular set of tags?

While there have been numerous similar inquiries posed before, mine pertains to a specific selection within the tags. I am not interested in extracting the entire content between <a href and </a>, but rather just targeting the "> within th ...

Styling Vue JS Components with CSS Binding

I've been trying to apply CSS styling to vuejs tags without success. I'm struggling to get it to work with both regular styling and conditional binding. I've exhausted all the solutions on stackoverflow, but nothing seems to be working for m ...

Browsersync and Gulp Continuously Refreshing CSS

My Gulp and Browsersync setup is causing the CSS in the Style.css file to reset every time I run npm start. While the changes to index.html persist and I can successfully push the initial CSS changes to Github, I am puzzled why the CSS additions keep reset ...

Help me find a way to refine div elements in the HTML response obtained via AJAX

Currently, I am working on creating a dynamic fields form that includes settings such as Label, Name, Id, Min, Max, and Value. Whenever I click on a button, a JavaScript function is triggered to return the definition of a text-box within two separate div ...

CSS - Combining Selectors and Pseudo Classes to Boost Styling

When it comes to grouping selectors in CSS, there is an easy way to do it like this: .class1 #id1, .class2 #id2, .class3 #id3 { } Applying pseudo classes on these grouped selectors can be a bit repetitive. Is there a method to group multiple selectors a ...

Issue with Table Content Being Truncated After Conversion from HTML to MS Word 2003

I am experiencing an issue where the table content is being cut off on the right side of the page when converting from an HTML page into MS Word 2003. Below is a sample HTML code (where placeholder $CLOB_DATA will be replaced by large CLOB data): <html ...

The seamless integration of React.js with HTML

My friend and I are beginners in the world of programming, with a solid grasp of html, CSS, and JavaScript. We're currently collaborating on a small project where we aim to create a chat system. While researching resources for our project, we came acr ...

Why do style assignments lose their motion when executed right after being made?

If you take a look at this specific fiddle in Webkit, you will see exactly what I am referring to. Is there a way to define the style of an element when it is first specified, and then its final state? I would like to be able to fully define a single-ste ...

``There seems to be a problem with the radio buttons where the selection disappears when

I'm facing an issue with a radio button group I created. It's functioning properly, but the selection disappears when clicked outside of the radio button. Can someone assist me in resolving this problem? Here is the link to the jsfiddle for refer ...

Improving access for disabled individuals in HTML through tab-index usage

Hey there, I'm currently dealing with some challenges regarding tab-index for disabled elements. It seems that we are unable to focus on the elements, as screen reader tools are not announcing them and are skipping over them directly. For example: I ...

Options presented in a horizontal line for convenient and quick

I need help with making my menu items responsive so they stay in one line without overlapping as the screen size decreases. Here is the code snippet I am currently using: <!doctype html> <html lang="en"> <head> <meta charset="utf-8 ...

What are some solutions for repairing unresponsive buttons on a webpage?

My task is to troubleshoot this webpage as the buttons are not functioning correctly. Here’s a snippet of the source code: <!DOCTYPE html> <html lang="en"> <head> ... </head> <body> <div id="container" ...