css - 负上边距不适用于子图像

标签 css parent-child margin

这就是我希望小鸡盒的样子:![] 1 出于某种原因,我不能使用负边距来让推特图片位于框的中心。我的亲子关系有问题吗?

我的 css 在外部工作表中,但在这里:

<style type="text/css">

#chicklet_container {
    margin:20px auto 0px auto;
    width:540px;
    height:215px;
}

#chicklet_box {
    margin:0px 0px 10px 0px;
    width:190px;
    height:160px;
    border-style:solid;
    border-width:33px 5px 5px 5px;
    border-color:#45BA88;
    position:relative;
}

#chicklet_box2 {
    margin:-30px 0px 10px 0px;
    width:190px;
    height:160px;
    border-style:solid;
    border-width:0px 0px 30px 0px;
    border-color:#3f4040;
}

#chicklet_text {
    text-align:center;
    margin:-196px 0px 0px 0px;
    color:#FFF;
    width:190px;
    font-family:"Verdana, Geneva, sans-serif";
    font-size:27px;
    line-height:20px;
}

#chicklet_text2 {
    text-align:center;
    margin:139px 0px 0px 0px;
    color:#FFF;
    width:190px;
    font-family:"Proxima, Nova, Ultralight";
    font-size:26px;
    line-height:20px;
}

#chicklet_box img {
    margin:-250px 0px auto 5px;
}
</style>

这是 html:

<div id="chicklet_container">
    <div id="chicklet_box">
        <div id="chicklet_box2">
        </div>
            <div id="chicklet_text">Follow Me</div>
            <div id="chicklet_text2">@soandsoandso</div>
            <img src="images/twitter.jpg" alt="">
    </div>
</div>

最佳答案

为什么要使用这么多边距来对齐 Twitter 鸟图像或文本。使用尽可能低的边距。相反,请尝试使用该图像 position:absolute; 和 top ,left 属性。会更干净。但是要记住一件事,如果您对元素使用绝对位置,请检查其外部元素或父元素是否已定位,如果未定位,则情况可能会变得更糟,该子元素可能会移至其他位置。

关于css - 负上边距不适用于子图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15795505/

相关文章:

javascript - jquery css 选择器使用输入类型错误

html - 如何使 div 占据所有可用的剩余水平空间而不指定其宽度?

html - 添加带有 CSS 边框的缩略图

Java多态性-从父映射获取子命令

sql - 基于多个 child 配对的 parent 计数

javascript - Vue.js - Prop 同步不是即时的

html - 如何在不向下移动的情况下缩小我的文本边距?

html - 在调整浏览器窗口的宽度和/或高度大小时,如何防止 div 重叠/移动/使我的页面看起来难看

html - 使用有效和无效通知和符号进行表单验证

html - 事件选项卡边框底部位于 div 下方,而不是位于 div 中