Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An alternative to using negative margin?

Tags:

html

css

margin

I am using negative margin's to place my image on the background however when i zoom to much it shifts and distorts the image to a great deal. On zooming the box on the right goes towards the top because of the negative margin.

Please find below the code i am using:-

  <div class="platform-row" style="float: right; margin-right: 145px; padding: 2px;">
            <span><a href="download/index.html">
                <img src="assets/Box.png" border="0" /></a></span><br>
            <div class="platform-row-box">
                SOME TEXT GOES HERE...................
            </div>

            <a href="download/index.html">
                <div class="getmxit">Get ABC Now</div>
            </a>
            <div style="background: black; margin-top: 3px; width: 181px; opacity: .8; padding: 2px">

                <span><a class="platform-icon apple" href="download/ios/index.html"></a></span>
                <span><a class="platform-icon android" href="download/android/index.html"></a></span>

            </div>
        </div>

CSS:

    .platform-row {
    -webkit-transform: translateZ(0);
    -moz-transform: translateZ(0);
    -ms-transform: translateZ(0);
    -o-transform: translateZ(0);
    transform: translateZ(0);
    margin-top: -530px;
    margin-left: 700px;
}

    .platform-row .platform-row-box {
        color: white;
        font-size: 14px;
        margin: 0 auto;
        width: 181px;
        opacity: .8;
        margin-top: -170px;
        position: fixed;
    }

@media screen and (max-width: 640px) {
    .platform-row {
        padding-right: 55%;
    }
}

@media screen and (max-width: 479px) {
    .platform-row {
        margin-top: 40px;
        padding-right: 35%;
    }
}

.platform-icon {
    -webkit-transition: opacity 0.2s;
    -moz-transition: opacity 0.2s;
    transition: opacity 0.2s;
    /**background-image: url("platform_icons-14a66f5cbf10a328f7b38e6070c26e62.png");**/
    background-image: url("Home_Get.png");
    display: inline-block;
    height: 25px;
    width: 25px;
    margin-right: 0px;
    opacity: 1;
}

@media only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min--moz-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2 / 1), only screen and (min-device-pixel-ratio: 2), only screen and (min-resolution: 192dpi), only screen and (min-resolution: 2dppx) {
    .platform-icon {
        background-image: url("platform_icons%402x-dfed2cc115fa8b344e08c9072408095a.png");
        background-size: 454px 88px;
        -webkit-background-size: 454px 88px;
    }
}

enter image description here

EDIT:

This is what happens when i zoom in too much because of the negative margin.

enter image description here

like image 694
vini Avatar asked Dec 12 '13 12:12

vini


People also ask

Should you use negative margin?

Because negative margins bring elements closer together, it is perfect for creating overlapping elements.

What is a negative margin?

What is a negative profit margin? A negative profit margin is when your production costs are more than your total revenue for a specific period. This means that you're spending more money than you're making, which is not a sustainable business model.

Is negative margin valid?

Negative margins are valid in css and understanding their (compliant) behaviour is mainly based on the box model and margin collapsing. While certain scenarios are more complex, a lot of common mistakes can be avoided after studying the spec.

Can padding or margin be negative?

No. Padding only takes positive values. Negatives are ignored or treated as 0, which would have the same effect: none. Margins can have negative values, as can other position related properties, but not padding.

When to use negative margins in HTML?

This is by far the most common use case for negative margins. You give a container a padding so that its contents have some breathing space. However, you want the header to span the entire container, ignoring the padding. Negative margins are the way to go. Anecdotally, I find negative margins fairly intuitive.

What is the difference between relative positioning and negative margin?

Using a negative margin is more appropriate than applying relative positioning since you only have to apply it to the first of the new columns instead of to each <li> tag. Cool, huh?

How do static elements react to negative margins?

There are 2 types of scenarios where negative margins take center stage. A static element is an element with no float applied. The image below illustrates how static elements react to negative margins. When a static element is given a negative margin on the top/left, it pulls the element in that specified direction.

How do you use margin top in HTML?

By adding margin-top:-2.6em (twice the line-height of <li>) to .top, all elements move up in perfect alignment. Using a negative margin is more appropriate than applying relative positioning since you only have to apply it to the first of the new columns instead of to each <li> tag.


1 Answers

Disclaimer: This answer is based on what I think you're asking. For a more specific solution to your problem, please be more specific with regard to what you're trying to achieve.

It looks like you're using negative margins and padding to compensate for the fact that your image is relatively positioned (by default). To avoid breaking your layout you can achieve the same thing with one of two approaches:

Method 1 (not ideal): Move your background image outside of its current container and into the broader document context. Then position your image absolutely so that it doesn't effect the rest of your layout:

HTML
<img class="background" src="somedir/background.png">
<div class="platform-row">....</div>

CSS
.background {
    position: absolute;
    top: 0;  /* defining the top/left/bottom/right declarations are important! */
    left: 0;
    /* bottom: 0; apply these two if you want your image to stretch and fill the entire viewport */
       /*right: 0; */
    height: 100%;
    width: auto;
}

Method 2 (better): Just apply the background image as a background to the body (or preferably a max height/width wrapper).

HTML
<div class="page-wrapper">
    <div class="platform-row">....</div>
    <!-- other page content -->
</div> <!-- end of page-wrapper -->



CSS
.page-wrapper {
    background: transparent url('/images/background.png') 0 0 no-repeat;
    /* fix the image in place (not supported in older browsers) */
    background-position: fixed;
}

Also, instead of using margins to position your .platform-row-box, you can simply use the position: fixed style (which you've already defined), but you'll need to define the top/right/bottom/left values.

.platform-row-box {
    position: fixed;
    top: 40px;
    right: 20%;
}
like image 161
monners Avatar answered Sep 30 '22 20:09

monners