Tips on narrowing the left margin of a div when the size of the contained element becomes too large

I am facing a challenge in formatting text on the web that contains equations embedded as SVG images. To address this issue, I have replaced the SVG elements with fixed-sized div in the example provided below. The SVG element is nested within a div specifically designed to house the SVG.

The equations come with a small margin (3em in the example) on the left side. This has been achieved by adjusting the margin property of the encompassing div.

In cases where the equations are large and exceed the page width, they need to be scaled down. I handle this by setting the max-width property of the svg to 100%. However, scaling down the equation while maintaining a relatively large left margin can appear awkward. Hence, I am exploring ways to reduce the margin size when the size of the SVG exceeds the allocated 100% width.

I believe there must be a simple solution to achieve this objective, but so far my search has been unsuccessful.

In the example below, the equation labeled as #equation2 is too wide and being scaled down. How can I adjust the margin of the containing div accordingly? (I am open to reconsidering the HTML structure if required.)

html {
  background-color: red;
}
body {
  background-color: white;
  margin: 1em;
  width: 20em;
}
div.math {
  margin-left: 3em;
}
div.math div {
  max-width: 100%;
}

#equation1 {
  height: 1em;
  width: 8em;
  background-color: gray;
}
#equation2 {
  height: 1em;
  width: 30em;
  background-color: gray;
}
<html><body>
<p>Lorem ipsum...</p>
<div class='math'>
  <div id='equation1'></div>
</div>
<p>Lorem ipsum...</p>
<div class='math'>
  <div id='equation2'></div>
</div>
<p>Lorem ipsum...</p>
</body></html>

Answer №1

To achieve the desired layout, utilize flexbox and create a pseudo element to mimic margins. By incorporating the shrink effect, you can manipulate the sizing effectively:

html {
  background-color: red;
}
body {
  background-color: white;
  margin: 1em;
  width: 20em;
}
div.math {
  display:flex;
}
div.math::before {
  content:"";
  flex-basis:3em;
  flex-shrink:3; /* control the shrink factor*/
  min-width:5px; /* define a min value*/
}

div.math > div {
  height: 1em;
  width: 8em;
  background-color: gray;
}
<p>Lorem ipsum...</p>
<div class='math'>
  <div id='equation1'></div>
</div>
<p>Lorem ipsum...</p>
<div class='math'>
  <div id='equation2' style="width:20em"></div>
</div>
<p>Lorem ipsum...</p>
<div class='math'>
  <div id='equation2' style="width:23em"></div>
</div>
<p>Lorem ipsum...</p>
<div class='math'>
  <div id='equation2' style="width:25em"></div>
</div>
<p>Lorem ipsum...</p>
<div class='math'>
  <div id='equation2' style="width:30em"></div>
</div>
<p>Lorem ipsum...</p>

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 is the best way for me to collect messages submitted through a form on my website?

After completing my website using HTML, CSS, and JavaScript, I added a form with inputs for name, email, and message at the bottom of the page. Now, I am trying to figure out how to receive the information submitted by visitors in my email. ...

Encountering a blank webpage displaying a warning that reads, "The use of 'event.returnValue' is outdated

Although this issue has been discussed before and was previously considered a bug, I am currently using jQuery v1.11.0, which should have resolved it. The problem I am encountering is that when my page loads, there is supposed to be a slide-in effect (as a ...

Best practices for selecting checkboxes and transferring values to another page in PHP/HTML

Apologies for my lack of experience in PHP. I am in the process of creating a project without any knowledge of PHP. I have set up a database with a list of users and can display users based on specific information through a search. Each search query has a ...

The vertical shift in one of the inline table cell DIVs is being caused by two of them

I've been searching for a solution to my issue, but have come up empty-handed. I apologize if this has already been discussed before. My HTML page contains a section that appears as follows: <div id=wrap> <div id=lb> Content ...

Fine-tuning the page design

I'm working on a registration page using Bootstrap-5 for the layout. Is there a way to place all elements of the second row (starting with %MP1) on the same row, including the last field that is currently on the third row? See image below: https://i. ...

Ways to increase the size of a div to match the maximum height of its parent container

My current project involves using an angular dialog layout where the API to open and manage the dialog comes from a separate library. The dialog's parent container has a max-height attribute, meaning the dialog's height is determined by its conte ...

The tooltip is being truncated

https://i.sstatic.net/o41Qz.png Struggling with a tooltip that keeps getting cut off? I've tried everything and still can't get it right. <th class="fd-table--header-cell" scope="col"> <p class=&q ...

Implement pop-up functionality on additional buttons. Modify existing code to accommodate multiple buttons

I have a feature that allows me to click on a button and trigger a pop-up window with the desired content. The issue I am facing is how to duplicate this functionality for multiple buttons, each triggering a different pop-up content. I attempted to duplic ...

Incorporating a dropdown menu into an HTML table through jQuery proves to be a

I loaded my tabular data from the server using ajax with json. I aimed to generate HTML table rows dynamically using jQuery, with each row containing elements like input type='text' and select dropdowns. While I could create textboxes in columns ...

When the screen size decreases, display the title on two lines

Currently, I am in the process of developing a react web application with the assistance of redux. My main objective is to ensure that the page is highly responsive. At this point, there is a title positioned at the top displaying: Actions to do: Past due ...

The POST method is failing to collect data from the form

Currently in the process of learning Web Design with HTML, PHP, and Javascript, I have taken on the challenge of creating my own website called MySite.html. Within this site, there is a sign-in screen that utilizes the POST method to send data to a file na ...

Implement a callback function to dynamically generate variables and display them within an HTML element

Apologies in advance for any errors I may make as I am a beginner when it comes to javascript, jquery, and json. I have a script that retrieves data from a json file and displays it on a webpage using javascript, jquery, ajax (I believe), and json. When ...

A solitary block positioned at the heart of a grid - jquery mobile

Using grid in jQuery Mobile, I have designed a page and now looking to place a block exactly centralized on it. In the first grid, there are 2 blocks equally positioned. In the next grid, only one block is present and I want it to be centered. JSFiddle L ...

The overflow hidden property does not seem to be effective when used in conjunction with parallax

The issue arises when I attempt to use the overflow hidden property in combination with parallax scrolling. Although everything seems to be working correctly with JavaScript for parallax scrolling, setting the overflow to hidden does not contain the image ...

Misunderstandings between HTML and CSS

Can someone please clarify how the functionality of the active-slide class? Code: <div class="slider"> <div class="slide active-slide">..</div> <div class = "slide slide-feature">..</div> <div class = "slide">..</di ...

Implementing the CSS link tag in the header of a NextJS 13 application for enhanced styling

In my Next 13 application, I'm looking to link directly to a basic CSS file. Unfortunately, adding it to the Head component hasn't yielded any results for me. I've also attempted to create a custom _document within both the app and pages, bu ...

Setting !important to every property's value at once

One interesting approach I am using is applying !important to all of the css properties' values like this: .someclass{ color: #f00 !important; background-color: #ff0 !important; margin: 0 !important; padding: 0 !important; width: 100% ! ...

Trouble encountered while attempting to save canvas as PNG

In our TShirt design portal, we utilize fabric.js for creating designs. I have encountered an issue when trying to save the canvas as a PNG using canvas.toDataURL('image/png'). This action throws a "DOM exception 18" error on Google Chrome, while ...

Struggling to retrieve the dynamic select list information from HTML and pass it to PHP. Any suggestions or guidance would be greatly

<form method="POST" name="mailform" action="sendmail.php"> <fieldset> <?php require_once("mysql_connect.php"); $sql = mysql_query( " SELECT NAME FROM TABLE WHERE ID = ( SELECT ID FROM TABLE2 WHERE COLUMN = '$USERNAME') ORD ...

Trigger the fire event on .click() except when text is being selected

I am currently working on a chat box project and I want the input field within the message area to automatically get focused when the user clicks anywhere in the chat box. However, I don't want this functionality to interfere with text selection for c ...