简体   繁体   English

使用float:left和float:right在父div内对齐2个子div时出现问题

[英]Trouble aligning 2 child divs inside a parent div using float:left and float:right

I know the answer is simple, but I can't seem to figure it out. 我知道答案很简单,但我似乎无法弄清楚。

<div id="header">
<div id="30">
    <img src="../images/logo.png" width="97" height="97" />
</div>
<div id="company">
    <img src="../images/company.png" width="370" height="97" />
</div>
<div style="clear: both;"></div>
</div>

#header {
width:960px;
background-color:#e9e9e9;
margin-left:auto;
margin-right:auto;
padding-bottom:15px;
overflow:hidden;
}
#30 {
float:left;
}
#company {
float:right;
}

I couldn't figure out how to post the code from jsfiddle. 我不知道如何从jsfiddle发布代码。 The result is the "30" div aligned to the left side of the parent and the "company" div aligned to the right, but it's dropped down a line. 结果是将“ 30” div对齐到父级的左侧,将“公司” div对齐到父级的左侧,但是将其放在一行中。

Your first problem is that #30 isn't a valid ID — CSS doesn't like it when the first character is a number. 您的第一个问题是#30不是有效的ID-当第一个字符是数字时,CSS不喜欢它。 Try #thirty . 尝试#thirty

Here's a JSBin showing it working, albeit with JSBin logos. 这是一个JSBin,显示了它的工作原理,尽管带有JSBin徽标。 http://jsbin.com/IJaseKa/1 http://jsbin.com/IJaseKa/1

CSS has trouble with IDs beginning with a number. CSS在以数字开头的ID时遇到问题。 Use something with letters instead. 请改用带字母的东西。

Here is the working code 这是工作代码

http://jsfiddle.net/3bSVw/ http://jsfiddle.net/3bSVw/

<div id="header">
    <div id="other">
        <img src="../images/logo.png" width="97" height="97" />
    </div>
    <div id="company">
        <img src="../images/company.png" width="370" height="97" />
    </div>
    <div style="clear: both;"></div>
</div>




#header {
    width:960px;
    background-color:#e9e9e9;
    margin-left:auto;
    margin-right:auto;
    padding-bottom:15px;
    overflow:hidden;
}
#other {
    float:left;    
}
#company {
    float:right;
}

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

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