绝对子的相对高度,相对父级

时间:2012-12-15 12:41:42

标签: html css absolute css-position

我目前正在完成第一年的作业(考试作业)。我有一个绝对定位的文章的问题,这是一个相对定位的文章。为了实现这一点,它似乎必须绝对定位,但是当内容变大时,它不会自动扩展自身的高度。任何人都可以帮助我获得灵活的高度,而不会破坏其余的布局吗?

内容是相对的 content-1等是绝对的。

test page

html

 <div class="content">
        <div class="content-1">
            <article id="info_about"></article>
            <article id="google_map"></article>
            <article id="misc_info" class="clearfix"></article>
        </div>
        <div class="content-2">

        </div>
        <div class="content-3">

        </div>
        <div class="content-4">

        </div>
    </div>

CSS:

.tabs {
    position: relative;
    margin: 40px auto;
    width: 100%;
}

.tabs input {
    position: absolute;
    z-index: 999;
    width: 120px;
    height: 40px;
    left: 0px;
    top: 0px;
    opacity: 0;
    -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
    filter: alpha(opacity=0);
    cursor: pointer;
}
.tabs input#tab-2{
    left: 120px;
}
.tabs input#tab-3{
    left: 240px;
}
.tabs input#tab-4{
    left: 360px;
}

.tabs label {
    background: rgba(30, 30, 30, 0.8);
    font-size: 10pt;
    line-height: 40px;
    height: 40px;
    position: relative;
    padding: 0 20px;
    float: left;
    display: block;
    width: 80px;
    color: #ffffff;
    letter-spacing: 1px;

    text-align: center;
text-shadow: 1px 1px 1px rgba(255,255,255,0.3);

    box-shadow: 2px 0 2px rgba(0,0,0,0.1), -2px 0 2px rgba(0,0,0,0.1);

    /* CSS3 TRANSITION */
    -webkit-transition-property: background;
    -webkit-transition-duration: 0.2s;
    -moz-transition-property: background;
    -moz-transition-duration: 0.2s;
    -o-transition-property: background;
    -o-transition-duration: 0.2s;
    transition-property: background;
    transition-duration: 0.2s;
}

.tabs label:after {
    content: '';
    position: absolute;
    bottom: -2px;
    left: 0;
    width: 100%;
    height: 2px;
    display: block;
}

.tabs input:hover + label {
    background: rgba(40, 40, 40, 0.8);
}

.tabs label:first-of-type {
    z-index: 4;
    box-shadow: 2px 0 2px rgba(0,0,0,0.1);
}

.tab-label-2 {
    z-index: 3;
}

.tab-label-3 {
    z-index: 2;
}

.tab-label-4 {
    z-index: 1;
}

.tabs input:checked + label {
    background: rgba(0, 0, 0, 0.8);
    z-index: 6;
}

.clear-shadow {
    clear: both;
}

.content {
    position: relative;
    width: 100%;
    background-color: red;
    z-index: 5;
    min-height: 480px;
}

.content div {
    position: absolute;
    height: 100%;
    top: 0;
    left: 0;
    z-index: 1;
    opacity: 0;

    -webkit-transition: opacity linear 0.1s;
    -moz-transition: opacity linear 0.1s;
    -o-transition: opacity linear 0.1s;
    -ms-transition: opacity linear 0.1s;
    transition: opacity linear 0.1s;
}

.tabs input.tab-selector-1:checked ~ .content .content-1,
.tabs input.tab-selector-2:checked ~ .content .content-2,
.tabs input.tab-selector-3:checked ~ .content .content-3,
.tabs input.tab-selector-4:checked ~ .content .content-4 {
    z-index: 100;
    -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
    filter: alpha(opacity=100);
    opacity: 1;

    -webkit-transition: opacity ease-out 0.2s 0.1s;
    -moz-transition: opacity ease-out 0.2s 0.1s;
    -o-transition: opacity ease-out 0.2s 0.1s;
    -ms-transition: opacity ease-out 0.2s 0.1s;
    transition: opacity ease-out 0.2s 0.1s;
}
.content div h2,
.content div h3{
    color: #398080;
}
.content div p {
    font-size: 14px;
    line-height: 22px;
    font-style: italic;
    text-align: left;
    margin: 0;
    color: #777;
    padding-left: 15px;
    font-family: Cambria, Georgia, serif;
    border-left: 8px solid rgba(63,148,148, 0.1);
}


/* CONTENT PAGES*/

/* om os */

article#info_about{
    width: 530px;
    background: rgba(0, 0, 0, 0.8);
    height: 480px;
    margin-right: 20px;
    float: left;
}

article#google_map{
    width: 350px;
    height: 350px;
    background: rgba(0, 0, 0, 0.8);
    margin-bottom: 20px;
    float: left;
}

article#misc_info{
    width: 350px;
    height: 110px;
    background: rgba(0, 0, 0, 0.8);
    float: left;
}

1 个答案:

答案 0 :(得分:0)

我不确定为什么要使用单选按钮进行导航。我建议使用简单的jQuery选项卡解决方案http://css-tricks.com/examples/OrganicTabs/重新开始并使用类似的东西,但是我能够通过以下更改让内容与内容一起增长:

<强> HTML 在content-1到content-4中添加一类clearfix。

示例:<div class="content-1 clearfix">

<强> CSS

.content div {
opacity: 0;
-webkit-transition: opacity linear 0.1s;
    -moz-transition: opacity linear 0.1s;
    -o-transition: opacity linear 0.1s;
    -ms-transition: opacity linear 0.1s;
    transition: opacity linear 0.1s;
}

.content-2, .content-3, .content-4{
position: absolute;
top: 0;
}


article#info_about {
width: 530px;
min-height:480px;
background: rgba(0, 0, 0, 0.8);
margin-right: 20px;
float: left;
}