The margin-left negative is not functioning when setting the left side at 50%

I'm having trouble getting this specific CSS to work on an element:

.element {
  position: absolute;
  left: 50%;
  margin-left: -285px // the element has width: 500px;
}

This element is contained within another div that also has an absolute position.

For some reason, the browser is not applying the margin-left as expected, and I can't figure out why!

To better illustrate my issue, I created a CODE PEN for reference -> http://codepen.io/anon/pen/KpKrmN

Any assistance would be greatly appreciated. Thank you!

Answer №1

Uncertain of your actions, however, I propose a possible remedy. Consider implementing the following code snippet:

padding-right: calc(50%-285px);

Answer №2

Not completely following, but you might find this codepen helpful: http://codepen.io/anon/pen/jPOQLM

The main change is the use of "block". I transformed your p into a div and positioned it to the right.

<div class="main">
  <div class="interaction">
    Some text
    <br/>
    <span>Other text</span>
  </div> 
</div>

.main {
  position: absolute;
  width: 100%;
  height: 150px;
  top: 180px;
  text-align: center;
  border: 1px solid red;
}

.interaction {
  position: absolute;
  right: 0;
  left: 50%;
  margin-left: -285px;
}

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 method for invoking a class method in Typescript from another method within the same class acting as an event handler?

I came across this TypeScript code that I need help with: class MyClass { constructor() { $("#MyButton").on("click", this.MyCallback); this.MyMethod(); } MyCallback = () => { $.ajax("http://MyAjaxUrl") ...

Ways to confirm if a video has sound

There is a video that has an audio track, yet the volume is not zero or muted, resulting in the video being silent. I am looking for a way to determine if a video is audible so that I can perform specific functions accordingly. I came across a method to c ...

developing a multimedia player using HTML5

I'm attempting to create a dynamic video "collage" that showcases videos with different aspect ratios in a flexible grid layout. Each row should have videos of the same height, while filling up the horizontal space within a container. With known widt ...

Step-by-step guide on integrating a specific location into Google Maps using React.js

I'm in the process of revamping my website using Reactjs. I want to incorporate a specific Google location with reviews on the map, similar to how it appears on this example (My current website is built on Wordpress). As of now, all I've been ab ...

Leveraging webpack alongside url-loader for embedding images within React applications

When styling with an inline style and passing in the URL of an image file, I typically do it as shown below: let url = `url(${this.state.logo})` var cardStyle = { backgroundImage: url, backgroundSize: '200px 50px' ...

Tips for creating a horizontally expandable container that adjusts after overflowing

I am struggling to create a container that initially fills with text vertically, and then expands horizontally if needed. You can see what I mean in this diagram here: container_diagram Just like shown in the image - the container has a fixed height that ...

An error with code -4058 occurred when I tried to execute the command npm run start-rewired

When I start my React application using npm with the command npm run start-rewired, I encountered an error that looks like this: > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="265654494c434552661608170816">[email p ...

Utilize the Magento extension to seamlessly integrate JavaScript code into the head section of your website

I am attempting to incorporate JavaScript code into all or some pages of my website using an extension. I require a dynamic version (hosted in a .phtml file) of the following script: <default> <reference name="head"> & ...

Using jQuery to create a script that will transition random images simultaneously within a div

I am currently working on a JavaScript script that will animate 10 images flying into a div, appearing side by side. The goal is to have each image fly in randomly with a unique transition. At the moment, my script is almost complete as I've successf ...

Optimal code structuring for a javascript plugin

Hey there, I came across some intriguing posts on this topic, but I believe it's a very personal question that requires a tailored answer. So, I'm reaching out to ask you: what is the most effective way to organize my code for a JavaScript plugin ...

utilizing the data provided by a user in the "prompt" window within my HTML code

Looking to utilize user input from the prompt window. This is my custom JavaScript form: var answer; function load() { answer=prompt("what is your name?", "Write your name here"); return answer; } function hideNsee() { var HNS = docume ...

What is the best way to incorporate a fixed-position button that spans the full width of the screen?

After trying numerous solutions on stackoverflow, I have yet to find one that works for me. a.buttongc { border-radius: 5px; background: #f5b220; color: #fff; font-size: 17px; height: 44px; line-height: 42px; color: #fff; text-decoration ...

Sending an array using PHP through AJAX in jQuery to a separate PHP script

I am struggling with sending a PHP array to another PHP file using AJAX and jQuery. When I send the PHP array via jQuery post using a hidden input type value, it gets converted into a string. Therefore, the receiving PHP file does not receive it as an arra ...

Tips on aligning text link to the right within li using CSS

I am looking to adjust the position of a text link that is currently aligned to the left inside an li element, by moving it slightly to the right. Is there a way to achieve this effect without changing the overall layout? Page link: CSS: #secondnav li ...

Angular4 is throwing an error stating that the center element in the HTML is an undefined function

Is there an alternative tag that can be used instead of <center> <\center> in angular4, as it indicates that center is not a recognized function? ...

Having an issue where my form is automatically submitted before the data from the database is fetched to display after scanning a product barcode in a field

Having trouble with PHP - when I scan a product barcode in a field, the form is automatically submitted before fetching data from the database to display in the fields. You can see a screenshot of the form here: https://i.stack.imgur.com/2D7yg.png <f ...

Associate the right-click action with Angular Material

I'm currently working with Angular Material. My goal is to develop a directive that allows me to trigger a right-click event on an element. This is what I have attempted so far: JavaScript: app.directive('rightClick', ["$parse", function ...

Tips on ensuring a div containing an image element has equal height as the image

Here is the HTML code I am working with: <div> <img src="http://placehold.it/319x300" alt=""> <div> And the corresponding CSS: *{margin: 0; padding: 0;} div{ background: red; width: 319px; height: auto; } img{ width ...

Switching Styles in React applications

I am currently developing a React application and I have multiple stylesheets to restyle the same elements. For the purpose of this query, let's assume that I have two stylesheets with identical elements but different styles. Presently, in my app, I i ...

Massive React State Array containing hundreds of Input elements, sluggish state updates triggered by onChange events

I am working on a React form that consists of multiple complex inputs, each with its own complex state. To manage the state of all these inputs, I have a parent component where each input is wrapped in a child component. The parent component holds a state ...