css - 我在菜单中的元素似乎没有宽度?

标签 css

看这个codepen演示

编辑 当然,硬编码元素的宽度不是解决方案。它应该只是实际标题的大小。

HTML

<br/>

<nav class="navigation">
     <div class="navfake"></div>
            <div class="singleelement">
    <div class="container">
        <div class="title">Test title 2</div>
        <div class="titlepicture">some picture</div>
    </div>
</div>
            <div class="singleelement">
    <div class="container">
        <div class="title">Test title newsfeed super long 1</div>
        <div class="titlepicture">some picture</div>
    </div>
</div>
    </nav>

CSS

/** the newsfeed and footer */
nav {
    width: 100%;
    height: 20px;
    position: relative;
}

.navfake {
    width: 100%;
    height: 100%;
    background: green;
    z-index: 10;
    position: relative;
}

.singleelement {
    display: inline-block;
    height: 60px;
    text-align: center;
}

.container {
    position: absolute;
    top: 0px;
    -webkit-transition: all .8s ease;
    -moz-transition: all .8s ease;
    -ms-transition: all .8s ease;
    -o-transition: all .8s ease;
    transition: all .8s ease;
    height: 200px;
}

.titlepicture {
    position: absolute;
    width: 100%;
    background-color: red;
    z-index: 10;
    height: 100px;
    overflow: hidden;
}

.container:hover {
    top: -80px;
}

.container:hover .titlepicture {
    height: 100px;
    z-index: 10;

}

.title {
    z-index: 555;
    position: relative;
    height: 20px;
    width: 100%;
}

.footer {
    position: relative;
    z-index: 1000;
    background-color: white;
}

最佳答案

The trick was to replace position absolute with position relative

HTML

<nav class="navigation">
     <div class="navfake"></div>
            <div class="singleelement">
    <div class="container">
        <div class="title">Test title 2</div>
        <div class="titlepicture">some picture</div>
    </div>
</div>
            <div class="singleelement">
    <div class="container">
        <div class="title">Test title newsfeed super long 1</div>
        <div class="titlepicture">some picture</div>
    </div>
</div>
    </nav>
     <div class="footer">some text blablablablablablabla
          <br/>some text blablablablablablabla
          <br/>some text
          <br/>some text
          <br/>some text
          <br/>some text
          <br/>some text
          <br/>xxx
      </div>

CSS

/** the newsfeed and footer */
nav {
    width: 100%;
    height: 20px;
}

.navfake {
    width: 100%;
    height: 100%;
    background: green;
    z-index: 10;
    position: relative;
}

.singleelement {
    display: inline-block;
    height: 60px;
    text-align: center;
    position: relative;
}

.container {
    top: -20px;
    position: relative;                 /**changed*/
    -webkit-transition: all .8s ease;
    -moz-transition: all .8s ease;
    -ms-transition: all .8s ease;
    -o-transition: all .8s ease;
    transition: all .8s ease;
    height: 200px;
}

.titlepicture {
    position: absolute;
    background-color: red;
    z-index: 10;
    height: 100px;
    overflow: hidden;
}

.container:hover {
    top: -80px;
}

.container:hover .titlepicture {
    height: 100px;
    z-index: 10;

}

.title {
    z-index: 555;
    position: relative;
    height: 20px;
    width: 100%;
}

.footer {
    position: relative;
    z-index: 1000;
    background-color: white;
}

关于css - 我在菜单中的元素似乎没有宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22976371/

相关文章:

html - XSLT:组织用于打印的 html 表格

webkit - 如何使用CSS3制作透明纹理图案?

css - 如何让两个 div 标签与 CSS 水平对齐?

javascript - 动态添加 css 类会在之后立即产生不希望的结果

html - Border-Box 100% 宽度计算 - 文本不适合

jquery - 如何仅通过单击下一个和上一个按钮来更改轮播的内容?

javascript - 使用 D3 为 svg 填充背景图像

javascript - 动画左侧的 JQuery 轮播延迟

html - 我怎样才能使用 flexboxes 让我的元素一起移动?

我的 spring MVC 应用程序中没有加载 CSS 文件