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

标签 css layout whitespace

出现的问题如下: 在使用 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;
    }
<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>

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

正如您在示例中看到的,我想要的唯一空白是由 50px 边距的文本 block 引起的空白;和 footerallowedwhitespaceinblue 的间距(蓝色所以可见)。 问题是......现在空白太大了,因为“买这个”div 在相对定位后仍然占用空间。

我该如何解决?

最佳答案

您可以简单地通过应用等于元素宽度或高度的负边距来解决这个问题。

对于位于顶部的 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;
    }

修改后的示例代码变成了:

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

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

关于css - 如何删除使用 CSS 相对定位元素后出现的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13722095/

相关文章:

c++ - 组织 C++ 代码

javascript - 共享多个 HTML 文件布局的最佳方式是什么?

html - pre 标签中的空格不会被忽略

jquery - .closest .prev jquery 无法专注于上面的 A 标签

jquery - 调整 div 背景图片的大小

html - 如何在旋转木马图像更改时保持不变的旋转木马容器顶部添加按钮?

android - android中的屏幕兼容性问题

Mysql查询哪一列是空格?

sql - 使用 PostgreSQL 修剪尾随空格

html - 我的 wrapper 和书籍容器没有对齐,我该如何使用 flexbox 解决这个问题?