This design refuses to work:

<style> .myblock{ width:150px; height:150px; background-color:rgba(255,0,0,0.7); margin-right:-150px; } </style> <div class="myblock"></div> 

Yes, you can use margin-left , but it works fine with all values, both with and without a sign. What is my problem?

  • for display:inline-block code will work. Maybe just not as you expect. Can you show the expected behavior for a negative margin-right? - Grundy
  • margin-left: 150px; - MaximPro pm
  • then use margin-left. In general, for positioning it is better to use special tools. - Grundy
  • if you're talking about position: relative and the subsequent use of top bottom right left, then it's somewhat more problematic because it physically stayed in place and visually moved out - MaximPro
  • Positioning options can be many, and the choice of a particular depends on the specific markup, and the specific type that needs to be obtained - Grundy

1 answer 1

My opinion, why is this happening:

  1. Because the browser counts from right to left relative to me

therefore margin-left and works

 *{ padding: 0; margin: 0; } .myblock { width: 150px; height: 150px; background-color: rgba(255, 0, 0, 0.7); margin-left: 150px; } 
 <div class="myblock"></div> 

  1. In order for the margin-right work it is necessary to press it to the right edge.

 * { padding: 0; margin: 0; } .myblock { width: 150px; height: 150px; background-color: rgba(255, 0, 0, 0.7); float: right; margin-right: 150px; } 
 <div class="myblock"></div> 

  • I used to think about 3-4 years ago that my construction worked - MaximPro
  • logically, it should work as margin-left: 150px; only margin-right: -150px; , and you have a different result - MaximPro
  • if the block width 100% margin-right will also work - soledar10
  • parent block width? or current? - MaximPro