The padding bottom workaround doesn't seem to be effective for mobile devices

Lately, I've been experimenting with a padding-bottom technique to prevent content reflow and it's working flawlessly on desktop.

One of the divs in my project has something like this;

<div style="--imageWidth: 300; --imageHeight: 200;"></div>

This is how it looks in CSS;

div {
  border: 3px dashed magenta;
  padding-bottom: calc(var(--imageHeight) / var(--imageWidth) * 100%)}

However, when viewed on mobile devices, the image appears distorted with a significant gap between the image and text below.. You can view the issue here

Answer №1

Consider eliminating the inline style and focusing solely on CSS styles. Utilizing a media query can help define parameters for mobile view, allowing you to create 2 versions of CSS - one for desktop (your traditional CSS) and another for mobile devices (using a media query within your CSS).

The code snippet below showcases how to have a blue background on desktop and green on mobile:

/*CSS*/
body {
  background-color: blue;
}

@media only screen and (max-width: 600px) {
  body {
    background-color: green;
  }
}

Apply this same principle when writing specific code for your <DIV>. Assign an id to it and reference that in your CSS for desktop styling, then utilize @media to target that id and adjust it for mobile devices.

By resizing your browser, you can test the responsiveness of your design. Additionally, exploring flexbox can greatly assist with layout challenges, as it allows for flexible design options both inside and outside the media query for size-specific adjustments.

We hope this information addresses your inquiry. Feel free to share any discoveries or further questions!

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

My experience with jquery addClass and removeClass functions has not been as smooth as I had hoped

I have a series of tables each separated by div tags. Whenever a user clicks on a letter, I want to display only the relevant div tag contents. This can be achieved using the following jQuery code: $(".expand_button").on("click", function() { $(th ...

Error: The symbol $ is not recognized

This is a snippet of code that I've written to automate address filling and extract latitude/longitude/address information. <html> <head> <LINK rel="stylesheet" type="text/css" href="style.css"> <script type="text/javascript" sr ...

Menu in Bootstrap 5 experiencing a jittery effect when hovered over

I am facing an issue with shaking when hovering over the link.nav where I have added a border-bottom to display on hover. <section id="main_header"> <div class="container-fluid mainbar-bg py-1"> <div class="conta ...

I'm having trouble getting the code to work properly after the "else" statement using both jQuery and Javascript. My head is

Being a newcomer to javascript and jquery, debugging and viewing log files seem like a challenge compared to php. Any help from experienced individuals would be greatly appreciated. Although my code mostly works fine, I'm having trouble with the if/e ...

Adjust the point color of landmarks in Face-api.js

I need to customize the pointColor and lineColor in my code, but I'm not sure how to do it. I am using the fast-api.js library and haven't come across any examples of customized Landmarks Styles. export const drawResults = async (image, canvas, r ...

Customize the order of elements in Bootstrap for mobile devices

How can I create a responsive Bootstrap layout with 2 columns and a nested row in the right column? The desired layout is as follows: Desktop Version: --------- | 2 || 1 | | || | | |------- | || 3 | | || | | ...

The art of filtering data using jQuery's data-filter feature

I'm struggling to create a jQuery filter function that will display only the items belonging to a specific category when that category is clicked on. Although I have written some functions, I can't seem to get it to work properly. My goal is to ...

Ensuring the validity of an HTML form by including onClick functionalities for additional form elements

I am working on a form that I put together using various pieces of code that I found online. My knowledge of HTML and JavaScript is very basic. The form includes a button that, when clicked, will add another set of the same form fields. Initially, I added ...

stop automatic resizing of windows

My CSS is written using percentages. I need to maintain the percentages intact. Is there a way to stop the layout from changing when the browser window is resized? I want the percentages to remain unaffected, even when the browser window is being resized ...

Ways to Achieve the Following with JavaScript, Cascading Style Sheets, and Hypertext

Can someone help me convert this image into HTML/CSS code? I'm completely lost on how to do this and don't even know where to start searching for answers. Any assistance would be greatly appreciated. Thank you in advance. ...

What is the best way to limit the top margin for a fixed element?

Recently, I came across this code snippet: jQuery(document).ready(function($){ $( window ).scroll(function() { var current_top = $(document).scrollTop(); var heights = window.innerHeight; document.getElementById("sHome").styl ...

Unexpected behavior with the background property in CSS

Visit the live webpage Searching for a solution to showcase code snippets on my site, I'm faced with a challenge as a CSS beginner. My struggle lies in creating a dark background for displaying these code snippets. Upon trying to implement a class n ...

Changing a form into a static display format

Currently, I am working on designing a profile page layout. During the editing process of their profiles, I aim to showcase a form similar to the one provided in the Bootstrap library: https://getbootstrap.com/docs/4.4/components/forms/#form-row. Once use ...

Halt the execution of a function upon clicking a div element

I'm currently working on a function that needs to be stopped when a div with the class "ego" is clicked. This function toggles the visibility of the header based on the scroll position and should only run by default. Below is the code snippet: $("#e ...

The modal div remains hidden when embedded inside a PHP file

When I include an agenda.php file that contains a div with the Bootstrap modal class inside another div in a PHP file, it does not display properly. agenda.php <style type="text/css"> .block a:hover{ color: silver; } ...

Having trouble pinpointing the CSS/jQuery conflict and resolving it

Take a look at this page: At the top right of the website, you'll find a search box. However, it appears to be poorly designed. On this page, you can see the normal and desired behavior: (click on 'advertenties' to view the jquery effect). ...

What is the best way to apply a border-radius to the top of bars in @nivo/bar?

Is it possible to apply a border radius to the tops of each stack in a stacked responsive bar created from the Nivo library, without affecting the bottom? Currently, the responsive bar's border radius applies to both the top and bottom. Thank you! ...

Is it possible to use columns in the Bootstrap 4 grid system to create text that wraps around an image?

I have been searching for a solution all day without success. Can anyone help me figure out how to wrap text around an image using d-flex or flex-wrap in Bootstrap 4's grid layout with columns and rows? Below, I have shared my code and included two im ...

How can I resolve the issue of React not identifying the prop on a DOM element?

Currently, I am learning React and facing an issue with a warning message: "react-dom.development.js:86 Warning: React does not recognize the isSelected prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell i ...

Transfer dynamically generated table data to the following page

Seeking guidance on a common issue I'm facing. I am creating a table using data from Firebase upon page load, and I want users to click on a row to view specific details of that item. It may sound confusing, but it will make more sense with the code p ...