如何删除使用CSS相对定位元素后出现的空格

Tsc*_*cka 35 css layout whitespace

我一直在寻找这个问题,并最终在谷歌第10页的一些不起眼的论坛上找到了解决方案.解决方案就在答案中

出现的问题如下:在用CSS相对定位一个元素后,我得到了一个空格元素所在的空间......我不想要空格!

    .thetext 
    {
        width:400px;
        background:yellow;
        border: 1px dashed red;
        margin:50px;
        padding:5px;
        font-weight:bold;
    }
    .whiteblob
    {
        position:relative;
        top:-140px;
        left:70px;
        width:200px;
        height:50px;
        border: 4px solid green;
        background:white;
        font-size:2.5em;
        color:red;
        
    }
    .footerallowedwhitespaceinblue
    {
        height:10px;
        background-color:blue;
    }
    .footer
    {
        background-color:grey;
        height:200px;
    }
Run Code Online (Sandbox Code Playgroud)
<div class="thetext"><script type="text/javascript">for(c=0;c<50;c++){document.write("Lorem ipsum dolor est, ");}</script>
    </div>
    <div class="whiteblob">
        &nbsp;buy this!
    </div>
    <div class="footerallowedwhitespaceinblue">
    </div>
    <div class="footer">
        The whitespace above is way to big! The buy this still takes up space whilst it is moved.
    </div>
Run Code Online (Sandbox Code Playgroud)

JSFiddle:http://jsfiddle.net/qqXQn/

正如你在例子中看到的那样,我想要的唯一空格是由文本块引起的空格,边距为50px; 并且由footerallowedwhitespaceinblue的间距(蓝色使其可见).问题是......空白现在太大了,因为"买这个"div在相对定位之后仍占用空间.

我该如何解决这个问题?

Tsc*_*cka 45

您可以通过应用等于元素宽度或高度的负边距来解决此问题.

对于位于顶部的100px高度的元素,您将应用margin-bottom:-100px;

对于位于底部的100px高度的元素,您将应用margin-top:-100px;

对于位于左侧的100px宽度的元素,您将应用margin-right:-100px;

对于位于右侧的100px宽度的元素,您将应用margin-left:-100px;

剪切和粘贴css片段:

.top 
    {
    postion:relative;
    top:-100px;
    height:25px;
    margin-bottom:-25px;
    }
.bottom
    {
    postion:relative;
    top:100px;
    height:25px;
    margin-top:-25px;
    }
.left
    {
    postion:relative;
    left:-100px;
    width:25px;
    margin-right:-25px;
    }
.right
    {
    postion:relative;
    left:100px;
    width:25px;
    margin-left:-25px;
    }
Run Code Online (Sandbox Code Playgroud)

然后重新编写的示例代码变为:

.thetext 
{
    width:400px;
    background:yellow;
    border: 1px dashed red;
    margin:50px;
    padding:5px;
    font-weight:bold;
}
.whiteblob
{
    position:relative;
    top:-140px;
    left:70px;
    width:200px;
    height:50px;
    margin-bottom:-50px;
    border: 4px solid green;
    background:white;
    font-size:2.5em;
    color:red;
    
}
.footerallowedwhitespaceinblue
{
    height:10px;
    background-color:blue;
}
.footer
{
    background-color:grey;
    height:200px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="thetext"><script type="text/javascript">for(c=0;c<50;c++){document.write("Lorem ipsum dolor est, ");}</script>
</div>
<div class="whiteblob">
    &nbsp;buy this!
</div>
<div class="footerallowedwhitespaceinblue">
</div>
<div class="footer">
</div>
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/qqXQn/1/

  • 这非常有帮助,这正是我消除 div 下面的空白/填充所需要的(位置:相对) (2认同)

use*_*063 5

这是一个例子。在这种情况下,对象被向右移动,然后使用负的最高值向上移动。消除其尾随保证金空间需要添加一个相等的负保证金值。

 position:relative;
 width:310px;
 height:260px;
 top:-332px;
 margin-bottom:-332px;
 left:538px;
Run Code Online (Sandbox Code Playgroud)