What is the best way to apply a left margin to an image only when it is displayed on its own line on small screens using Bootstrap 4.0?

Here is some code I have to display 2 images next to each other:

<img style="padding-left:25%;margin-top:15px;margin-right:20%" src="~/img/1.png" /><img  style="margin-top:15px" src="~/img/2.png" />

As shown below:

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

However, on smaller screens, the 2 images will be displayed underneath each other (which is okay), but they won't be vertically aligned as seen in the image below:

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

How can I correct this? In simple terms, how do I add a left margin to the second image only when it appears on a separate line?

Answer №1

To achieve responsive design, you must incorporate media queries:

img {
    margin-top:15px;
}
@media screen and (min-width: 768px) {
    img {
        padding-left:25%;
        margin-right:20%;
    }
}

The specific value of 768px used in the media query is flexible and can be adjusted to meet your requirements. Another option is to utilize min-resolution instead of min-width.

Answer №2

An alternative approach is to utilize the display property along with a media query.

<div class="display">
 <img style="margin-right: 20px;" src="~/img/1.png" />
 <img src="~/img/2.png" />
</div>
.display {
  display: flex;
}
@media(min-width: 320px) and (max-width: 767px) {
 .display {
   display: block;
 }
}

Answer №3

If you want to handle this with javascript, one way would be to listen for the window resize event and then calculate if the total width of all images exceeds the window width. Based on that, you can adjust margin or padding as needed.

window.addEventListener('resize', function(event){ //listen to window resize
  let img1w = document.getElementById('image1').offsetWidth;
  let img2w = document.getElementById('image2').offsetWidth;
  if (img1w + img2w > window.innerWidth) {
    document.getElementById('img1w').style.marginLeft = '200px' //just a placeholder. do what you want here
  }
});

An alternative approach is to use bootstrap media queries, but keep in mind that there are only a limited number of predefined break points. If you need more control, creating your own custom media query may be the better option.

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

Use JavaScript to overlay a rectangle on top of an HTML element

Although I found a similar question here, it lacks any code in both the question and the answer. My goal is to convert this functionality into a 100% JavaScript solution. Currently, I can draw a rectangle on top of HTML content using PHP. I scrape a webs ...

When a container is styled with 'overflow-y: auto', it will display additional horizontal and vertical scrolling bars

I have two files, one HTML and one CSS. The HTML file contains the following code: <div class="app"> <div class="header"></div> <div class="main"> <div class="container_1"> ...

Tips for aligning a Bootstrap container in the middle of the viewport vertically

I'm struggling to center a Bootstrap container vertically in the viewport. You can view my code at http://www.bootply.com/C8vhHTifcE All of my attempts at centering have been unsuccessful. Does anyone have a solution? Just to clarify: I want the co ...

After being redrawn, the line on the canvas vanishes

After updating the angle value, the line is quickly redrawn correctly but then disappears. It seems like the input value may be getting refreshed and set to undefined again. How can I ensure that the line retains the correct angle? <script language=" ...

Tips for resolving an overlap issue with the navbar dropdown on the right side of a navbar in react js using app.css

I am facing an issue with the app.css file in React JS where my navigation dropdown is overlapping on the right side. I have attached an image for reference. Currently, I am using React JS and Laravel for development but haven't been able to find a so ...

What is the correct way to maintain focus on a nested scrolling div over another scrolling div with jQuery?

I have encountered an issue with a modal view that contains content extending beyond its boundaries. To remedy this, I applied the overflow-y: scroll property. However, the problem arises when the modal view overlaps with the body, which also has scrolling ...

Form validation using JQuery within a Bootstrap 4 modal incorporating tabs is preventing submission

Encountering a challenge with JQuery Validation within a modal that contains tabs. When I'm on the Sign-in Tab and click the Login button, the validation errors display correctly: https://i.sstatic.net/caReK.jpg ISSUE 1 However, on the New Account ...

Implementing dynamic tab switching using Ajax and jQuery to add active classes

I am currently working on creating ajax-based tabs by loading specific divs within each tab. In order to achieve this, I need to dynamically add an active class to identify which tab is open at any given time. Unlike bootstrap tabs, the difference here lie ...

Steps to directly reference a particular shape within an SVG illustration

I have a webpage with a large SVG image embedded in it. The image is too big to fit in the standard viewport, so there are horizontal and vertical scroll bars on the browser window. Within the SVG image, there are multiple rectangles. My goal is to create ...

I'm looking for help on creating a three-column table using javascript/jquery. The table should display Product, Price, and Discount (which is calculated at 20%). Can anyone

Check out this code snippet. var items = ["Laptop", "Tablet", "Smartphone", "Headphones", "Camera"]; var costs = [599.99, 299.99, 799.99, 149.99, 499.99]; displayItems = ""; totalCost = 0; for (var j = 0; j < items.length; j++) { displayItems += " ...

Creating a Personalized Dropdown Menu Within the <div> Tag

My current task involves obtaining an element in the following format: https://i.sstatic.net/uhHkL.png Inside the nav-item, there is a dropdown with a top-centered triangle on it, and the dropdown is positioned at the center. Below is what I have achiev ...

CSS Buttons with a rounded design on one edge

I need to create a button that looks like this: https://i.sstatic.net/jWwHm.png Although I thought it would be simple, I'm having difficulty with creating the rounded edges and adding additional color. Currently, my attempt looks like this (but the ...

Tips for wrapping the content of a <span> element within a <td> element

Can someone help me figure out how to wrap the content of a <span> tag that is inside a <td>? The style I have applied doesn't seem to be working. Here's the code snippet: <td style="white-space:nowrap;border:1px solid black"> ...

There is an unnecessary gap between the parent div and the image contained within it

Snippet of HTML code showcased below: <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="css/style.css" rel="style ...

implementing css class in javascript

I've been attempting to utilize a plugin for select functionality, but I'm running into issues with the CSS not applying when I insert the class within JavaScript: The plugin I want to use via JavaScript: $('.search-test').SumoSelect ...

Instead of shifting the floated div, opt to have the window cut off instead

Currently, I am facing an issue with a section on my website that has absolute text overlaying an image. While it looks good on larger screens, I want to implement a media query to adjust it at a specific window size. Here is what's currently happeni ...

Styling can be compromised by Angular due to selector element interference

I have a question regarding a bootstrap button group. The buttons within it are Angular components structured like this: <div class="btn-group float-right" role="group" aria-label="Basic example"> <app-action [actionType]="'inv ...

Add a DropDown menu to the RadToolBarDropDown component

I am currently utilizing Telerik's GUI system and am interested in inserting a dropdown menu inside another one within the interface. However, I am unsure of how to go about accomplishing this task. Below is what I have managed to create thus far: & ...

What steps can I take to ensure that the original field does not regain focus once the dialog is closed?

My users prefer not to switch from the keyboard to mouse while filling out an input form. To address this, I am using the onFocus event to display a JQuery UI Dialog. However, when I use the dialog's close(); function, the dialog reopens as the origin ...

How to customize the appearance of an element within a shadow DOM

I am currently implementing a custom CSS style over an application without the ability to modify its code. Initially, the DOM structure looked like this: <div class="foo"> <div class="bar"></div> </div> and I ...