【问题标题】:DIV moves when I put text in it [closed]当我将文本放入其中时 DIV 移动[关闭]
【发布时间】:2013-02-13 13:31:37
【问题描述】:

我花了一些时间从this site 上的 div 层创建了一个谱系,我得到了很好的对齐。

但是当我将文本放入其中时,无论是在 div 还是在 p 标签中,它都会将 div 层显着向下移动。

它似乎没有添加任何边距或填充或任何其他我在检查元素时可以看到的东西,而且它似乎没有影响孙 div 层。

JSFiddle

HTML:

<div id="pedigree">
    <div id="parentwrap">
        <div class="parent">test</div>
    </div>
    <div id="childwrap">
        <div class="child">
            <p>Am. Ch. Kenai's Aldebaran</p>
        </div>
        <div class="child">
            <p>pAm. Ch. Santa Clara Del Viento</p>
        </div>
    </div>
    <div id="grandchildwrap">
        <div class="grandchild">Am. Can. Ch. Ryzann's Eclipse at Kenai</div>
        <div class="grandchild">Am. Ch. Timber Ridge's Abi of Kenai</div>
        <div class="grandchild">Am. Ch. Sky Run Gavril Virtual Zip JC</div>
        <div class="grandchild">Am. Can. Ch. Tazeb's Zena</div>
    </div>
</div>

CSS:

#pedigree {
    position: relative;
    width: 584px;
    height: 204px;
    margin: 0 auto;
    margin-top: 15px;
    padding-bottom: 15px;
    border-bottom: 1px dotted black;
}
#parentwrap {
    position: relative;
    display: inline-block;
    margin: 0;
    width:auto;
    height: 205px;
}
.parent {
    position: relative;
    width: 190px;
    height: 202px;
    margin: 0px;
    padding: 0px;
    border: 1px solid black;
}
#childwrap {
    position: relative;
    display: inline-block;
    margin: 0;
    width: auto;
    height: 205px;
}
.child {
    position: relative;
    width: 190px;
    height: 95px;
    margin: 0px;
    padding: 0px;
    border: 1px solid black;
    margin-bottom: 10px;
}
#grandchildwrap {
    position: relative;
    display: inline-block;
    width: auto;
    height: 205px;
}
.grandchild {
    position: relative;
    width: 190px;
    height: 46px;
    margin: 0px;
    padding: 0px;
    border: 1px solid black;
    margin-bottom: 4px;
}
.parent, .child, .grandchild {
    -moz-border-radius: 35px;
    border-radius: 35px;

【问题讨论】:

  • 不相关,但您网站上的副本充满了拼写/语法错误。记得也要解决这个问题!
  • 点将添加代码
  • 如果从 div #pedigree 中删除 height 会改变什么吗?
  • 将这两个尽可能小的标记/css 等隔离开来。通常这会启发你,你可以用更集中的集合来学习。顺便说一句,根据我有限的经验,它很可能是在比容器更大的东西里面。

标签: css html text


【解决方案1】:

添加文本会为 #parentwrap div 创建基线,因此 div 会与之对齐。没有文本,就没有基线,因此 div 采用后备布局模式。

要修复,请设置添加#parentwrap { vertical-align:top; }

【讨论】:

  • 我确实想知道这是否可能是对齐问题,但我不明白为什么会这样(因此询问而不是猜测 - 更好地理解为什么)我不确定它为什么会在默认情况下创建基线,似乎没什么用,但这确实解决了我的问题,所以非常感谢。有这么棒的人社区真的很棒。
  • @Alohci 你能解释一下吗,或者给我一个解释“基线”的链接。
  • @Nix - 确实如此。与基线对齐的精确规则,这是默认设置:w3.org/TR/CSS21/visudet.html#propdef-vertical-align。特别注意它说“基线:将框的基线与父框的基线对齐。如果框没有基线,则将底部边距边缘与父框的基线对齐。
  • 谢谢。我认为它可以帮助我们这些有抱负的设计师的开发人员;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-29
  • 1970-01-01
  • 2012-12-11
  • 2019-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多