Is using -100px or minus however many pixels considered as bad coding practise, should it be avoided? Do employers look down upon this?

Example Use:

<div style="margin-left:500px; margin-top:-85px;"> 
<p>Home | About | Contact</p>
</div>

Dani AI

Generated

Short answer: using negative pixel offsets is not automatically "bad," but it is often a brittle hack for layout. asked whether it should be avoided — the practical concern is not the property itself, but the long-term maintainability, responsiveness, and accessibility of the layout. and are right that negative spacing works in CSS, but modern workflows favor layout primitives that scale reliably.

Prefer these patterns instead of hard-coded negative pixels:

  • Use Flexbox or Grid for structure, reserving offsets only for intentional visual overlap.
  • For motion or small shifts, use transform (GPU-friendly) rather than animating margins.
  • Use relative units (%, em, rem, vw) or CSS variables so shifts adapt when fonts or viewport change.

A small example showing a safer shift:

.element {
  transform: translateY(-10px); /* avoids layout reflow when animating */
}

If negative margins are required, document the reason in a CSS comment, keep values as small and relative as possible, and test across breakpoints and browsers. Teams and employers tend to care about readable, predictable code and responsive behavior more than whether a specific margin is negative. For details on browser behavior and layout alternatives see the MDN margin reference and practical guides on negative margins and modern layout on CSS-Tricks:

Recommended Answers

All 2 Replies

I recently faced a similar issue when I was learning about page layout using CSS.

As I understand it, and I may not be understanding it fully or correctly, when you are using relative positioning, you can use negative margins. That is because the margin anchor (top left) will be that of the base element's position. If you wanted an element to be higher than or to the left of the base element, then you would use negative margin values.

In very rare cases I use negative margins. My employer does not mind it. He tells as long as the output is same in all the browsers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.