【问题标题】:An alternative to using negative margin?使用负边距的替代方法?
【发布时间】:2013-12-30 20:56:27
【问题描述】:

我使用负边距将我的图像放在背景上,但是当我放大很多时,它会移动并严重扭曲图像。由于负边距,右侧的框在缩放时会移到顶部。

请在下面找到我正在使用的代码:-

  <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;
    }
}

编辑:

当我因为负边距放大太多时会发生这种情况。

【问题讨论】:

  • 你想完成什么?你能做一个jsfiddle,你只显示重现问题所需的代码吗?很多时候,在为一个问题做一个 jsfiddle 时,我可以自己解决问题
  • 如前所述,jsfiddle 会有很大帮助。另外,您为什么将内联样式与 CSS 一起使用?
  • 您在哪个浏览器上见证了这一点,您使用的窗口大小是多少?小提琴对您的图片的链接特别有用。

标签: html css margin


【解决方案1】:

免责声明:此答案基于我认为您要问的内容。对于您的问题的更具体的解决方案,请更具体地说明您想要实现的目标。

您似乎正在使用负边距和填充来补偿您的图像相对定位的事实(默认情况下)。为避免破坏您的布局,您可以使用以下两种方法之一来实现相同的效果:

方法 1(不理想):将背景图像移出其当前容器并进入更广泛的文档上下文。然后绝对定位您的图像,使其不会影响布局的其余部分:

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;
}

方法 2(更好):只需将背景图像作为 background 应用到主体(或者最好是最大高度/宽度包装器)。

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;
}

此外,您可以简单地使用position: fixed 样式(您已经定义),而不是使用边距来定位您的.platform-row-box,但您需要定义top/right/bottom/left 值。

.platform-row-box {
    position: fixed;
    top: 40px;
    right: 20%;
}

【讨论】:

  • 您好,我尝试了第二种方法,仍然可以在编辑问题中看到顶部的空间。
  • “放大”是什么意思?
  • 因此,如果用户在 ipad 或 iphone 等小屏幕上查看它,我会在我的编辑中看到上面给出的屏幕
【解决方案2】:

为对侧边距赋予正值和浮动(意味着如果您想向左移动 -20px,则将浮动向右赋予该 div 并为右边距赋予正值

【讨论】:

    【解决方案3】:

    对于那些正在寻找一个简单、好的替代负边距的人:

    不好,导致文档流混乱:

    margin-top: -2px; 
    

    好,改为:

    position: relative;
    top: -2px;
    

    【讨论】:

      猜你喜欢
      • 2022-06-12
      • 2011-08-10
      • 2012-06-27
      • 2013-08-10
      • 2010-12-22
      • 2014-05-24
      • 2011-12-11
      • 2013-02-22
      • 2014-01-14
      相关资源
      最近更新 更多