简体   繁体   English

将绝对div沿相对位置放置

[英]Positioning an absolute div along side a relative one

I'm trying to position an absolute object beside a relative object. 我试图在相对对象旁边放置一个绝对对象。 The initial object causes the relative div to wrap to the next line. 初始对象使相对div包装到下一行。

Everything is working as it should be, with the exception of the logo forcing the title to move to a new "line". 一切正常,除了徽标会强制标题移至新的“行”以外,其他所有内容均应正常工作。 If I change the #logo div to be position:absolute I can fix the positioning problem, but then my logo hover ceases to function. 如果将#logo div更改为position:absolute ,则可以解决定位问题,但是徽标悬停会停止工作。

Edit: Here's a live demo: http://vaer.ca/warm-forest-8234/ 编辑:这是一个实时演示: http : //vaer.ca/warm-forest-8234/

Here is my HTML: 这是我的HTML:

<div id="container">

        <a href="index.html" id="logo"></a>

        <div id="header">

            <h2><a href="index.html">Collectif</a></h2>

        </div>  

        <div id="nav">
            <ul>
                <li><a href="#">About</a></li>
                <li><a href="#">Services</a></li>
                <li><a href="#">Work</a></li>
            </ul>
        </div>

And here is my CSS: 这是我的CSS:

#container {
    width: 980px;
    margin: 0 auto;
    position: relative;
    }

#header {
    height: 75px;
    text-align: right;
    position: relative;
    }

#header h2 {
    font-size: 2.5em;
    font-weight: 400;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    padding-top: 15px;
    }

#header a {
    text-decoration: none;
    color: #000000;
    -o-transition:.5s;
    -ms-transition:.5s;
    -moz-transition:.5s;
    -webkit-transition:.5s;
    transition:.5s;
    }

#header a:hover {
    color: #7acfdd;
    }

#logo {
    position: relative;
    display: block;
    margin-top: 10px;
    margin-left: 10px;
    background: url('../img/logo.png') no-repeat;
    width: 60px;
    height: 60px;
    -o-transition:.5s;
    -ms-transition:.5s;
    -moz-transition:.5s;
    -webkit-transition:.5s;
    transition:.5s;
    }

#logo:hover {
    position: relative;
    background: url('../img/logo-hover.png') no-repeat;
}

Try: 尝试:

#logo {
    float: left;
}

I would say : 我会说 :

#logo {
    float: left;
    position: relative;
}

#header {
    height: 75px;
    text-align: right;
    position: relative;
    clear:both;
}

And its just fine to use a float: left; 使用浮点数就好了:left; and position: relative; 和位置:相对; (I'm always using it when i need to float a div on top of an other because when you use a position: relative; you can apply an z-index) (当我需要将div浮动到另一个顶部时,我总是使用它,因为当您使用position:relative时,可以应用z-index)

You have set you #logo link to display:block forces the <a> element to take up that whole space. 您已将#logo链接设置为display:block强制<a>元素占用整个空间。 See it by using the developer tools. 使用开发人员工具查看。 You can do the following to fix it all and along with that, make it more semantic. 您可以执行以下操作来修复所有问题,并使其更具语义。

#logo{
display:inline-block;
}
#header{
display:inline-block;
float:right
}

These changes will get you your desired results. 这些更改将为您提供所需的结果。

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

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