The function of style.marginRight differs from that of style.marginLeft

One function aligns content to the left while the other doesn't align it to the right

function openNavLeft() {
  document.getElementById("mySidenavLeft").style.width = "100vw";
  document.getElementById("main").style.marginLeft = "100vw";
}

function openNavRight() {
  document.getElementById("mySidenavRight").style.width = "100vw";
  document.getElementById("main").style.marginRight = "100vw";
}

I'm wondering why these functions are not working as expected. What should I change?

Answer №1

Imagine you have a webpage and you decide to add a margin to the left of an element such as a div.

As per usual, without any margin, the div will be aligned closely to the left due to the language's left-to-right nature.

However, if you introduce a left margin, it shifts the div towards the right side since the space on the left is now occupied.

On the contrary, placing a margin on the right merely adds extra space after the div without affecting its positioning.

If there are other elements taking up space before the div, a right margin won't force them to move aside.

To enhance my explanation, I included a simple diagram below:

----------------------
|       WEBPAGE      |
|                    |
|[div]               |
|                    |
|[   Left Margin    ]|[div]
|                    |
|[div][ Right Margin |    ]
|                    |
|[img][div][   Right | Margin ]
|                    |
----------------------

Hope that clarifies things! 🙂

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

Tips for closing the "editor" window while utilizing custom code for create, edit, and delete buttons and updating the table row in the Avant Template

I have been using the PHP template provided by Avant to build an Editable Table feature. The table allows users to select a row and then click an Edit button, which triggers a popup window containing the fields from that row. I am in the process of adding ...

Tips for customizing the appearance of the react-stripe-checkout form

Could someone please provide guidance on applying custom styles to the react-stripe-checkout form component, such as changing the background color? Visit this link for more information ...

Having trouble bringing my custom-built Angular module into my Angular application

Currently considering the utilization of this Yeoman generator as a starting point for a small project that will contain several reusable form components to be published. The generator constructs a module and an example component, directive, pipe, and serv ...

What coding techniques can be used to create dynamic animation of a log stream in javascript/css

When I have a series of events in my browser, I want to display them as a list with a smooth animation effect. As each new event comes in, I'd like the existing items to slide down gracefully, making room for the new event to fade in at the top of the ...

Displaying XML data using d3js

Currently, I am working on a d3.js assignment for my school project. While looking at some examples to load an XML file, I seem to be encountering difficulties in figuring out what's going wrong. The XML file is loading correctly, but as I am still le ...

Calling Number() on a string will result in returning a value of NaN

Currently, I am working on the following code snippet: app.put("/transaction/:value/:id1/:id2", async(req,res) => { try { const {value,id1,id2} = req.params; const bal1 = await pool.query("Select balance from balance where id=$1",[i ...

Problems with ng-repeat in kenwheeler's AngularJS implementation

Hey there! I'm currently using the slick carousel by ken wheeler, but when I implement ng-repeat on the div, it only shows 'listed images'. I'm not sure what I'm doing wrong here. Any ideas? <section class="regular slider" sty ...

What is the best way to divide my value sets using jQuery?

Within a given string such as 28_34/42/34,23_21/67/12,63_5/6/56, I am seeking to split or eliminate sets based on the ID provided. For example, if the ID is 23, then the set 23_21/67/12 should be removed. To achieve this, I am checking the value after the ...

In my React JS class component, I am looking to have the image display switch each time the button is clicked

How can I change the image when clicking a button in a class component? I am familiar with function components, but unsure how to achieve this. class CoffeeImages extends React.Component{ constructor(props){ super(props) ...

Intersection Observer API is not compatible with the functionality of the navigation bar

Having trouble with the Intersection Observer API. Attempting to use it to show a nav-bar with a white background fixed to the viewport once it scrolls out of view. Initially tested using console.log('visible') successfully to determine visibili ...

How to efficiently retrieve a form's data from multiple forms with identical ids in JavaScript

I am facing a challenge with multiple forms on the html page that have the same ID. I need to send each form's information to the server based on user selection, but I haven't been able to find a relevant solution. For my Authorization polic ...

strange occurrences in localToWorld transformation

Hello there! Currently, I'm working on a project where I'm generating a TextMesh using font geometry and placing it within an empty pivot object. My goal is to obtain the world coordinates of each vertex in the TextMesh so that I can manipulate ...

What might be causing the invisibility of a particular div element?

Here is the layout in the div element: <div class=" wrap clear"> <div class="block pink float"></div> <div class="block blue float"></div> <div class="block orange float"& ...

The resource was identified as a document but was sent using the MIME type application/json

I'm having an issue with posting to a jQuery modal's Action and receiving JSON in return. Every time I try, I encounter the following error: Resource interpreted as Document but transferred with MIME type application/json Instead of staying on ...

Is it possible to apply CSS to the alert and confirm functions in Angular?

Currently, I am facing an issue with implementing CSS on elements that are nested inside a function. I am unsure of how to access them properly. For example: updateUser() { this.usersService.UpdateUser(this.user.id, this.user) .subscribe(() =& ...

The communication feature using jQuery and PHP is not saving information to the database

I'm currently in the process of setting up a chat page using jQuery and PHP. However, I've encountered an issue where although the Added echo statement appears after submitting data on my simple form, the information is not actually being added t ...

Is there a way to convert a button into clickable text that performs the same function as the original button?

Seeking a solution to transform a button into clickable text with the same functionality. Explored resources like Google, StackOverFlow, and Youtube for answers. This is the current code snippet representing the button: <button onclick="mainApp.l ...

Tips for fixing the issue of "module ./response not found" in Node.js Express

Whenever I execute the command $ npm start this error message appears > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8feefcfce6e8e1e2eae1fbbccfbea1bfa1bf">[email protected]</a> start > nodemon server.js ...

What is the best way to eliminate the "1 empty item" from a JSON object using JavaScript?

This is an example json object that has been modified to remove certain values. { name: { first: 'Robert', middle: '', last: 'Smith' }, age: 25, DOB: '-', hobbies: [ 'running', 'coding', & ...

Creating a DIV with vertical scroll-bars exclusively for lengthy paragraphs in HTML: What's the best approach?

I need to display terms and conditions on my website without using a text field or taking up the entire page. Instead, I want to show the text in a specific area with only a vertical scroll bar for users to navigate through the content. Here is the curren ...