简体   繁体   English

jQuery append()与CSS的怪异互动display:inline-block

[英]jquery append() weird interaction with css display:inline-block

I am trying to create a webpage with two div sections displayed next to each other horizontally. 我正在尝试创建一个网页,其中两个div部分水平并排显示。 In one of them I want to append content that will change over time. 我想在其中之一中添加随时间变化的内容。 I noticed that if I use css display:inline-block to align the two div sections and then I use append() to insert a paragraph in the first section, the second one is pushed down to align with the paragraph block. 我注意到,如果我使用css display:inline-block对齐两个div部分,然后使用append()在第一部分中插入一个段落,则将第二部分向下推以与该段落块对齐。 I know I can fix this problem by using float:left instead, but I still don't understand why inline-block behaves that way. 我知道我可以通过使用float:left来解决此问题,但是我仍然不明白为什么inline-block的行为方式如此。 I wonder if there is a way to make inline-block work in case I really need to use that instead of float. 我想知道是否有一种方法可以使内联块工作,以防万一我真的需要使用它而不是float。 Here is JsFiddle: Link 这是JsFiddle: 链接

<div id="left">left</div>
<div id="right">right</div>

#left, #right{
    background-color:#ff0;
    width: 100px;height: 100px;
    display:inline-block;}

<script>
    $("#left").append("<p>Paragraph</p>");
</script>

when using display: inline-block you have to add vertical-align: top if you want the elements to display at the top: 使用display: inline-block ,如果要让元素显示在vertical-align: top则必须添加vertical-align: top top:

JSFIDDLE JSFIDDLE

The reason being inline-block elements are set to baseline by default 默认将inline-block元素设置为baseline的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM