简体   繁体   English

自动为绝对孩子的父div设置高度

[英]Automatically set height for parent div of absolute children

I've got a situation, where i have a few divs with absolute position (for 3d flipping effect ). 我有一种情况,我有几个具有绝对位置的div(用于3d翻转效果)。 As a result, parent div's height is very small and absolute content overflows on top of lower content. 因此,父div的高度非常小,绝对内容会在较低的内容之上溢出。 Can't really set a fixed height for responsive reasons. 出于响应原因,无法真正设置固定高度。

I'm planning to give children divs relative position, and then gather height with a help of jquery and set the height based on it. 我打算给孩子divs相对位置,然后借助jquery收集高度并根据其设置高度。 But there really is a lot of elements and vars for responsive. 但是确实有很多元素和变量可以响应。 Is there any other, more elegant way to make children fit the parent? 还有其他更优雅的方法来使孩子适合父母吗?

http://jsfiddle.net/xyjrLa2p/ http://jsfiddle.net/xyjrLa2p/

html html

<div class="parent">
<div class="title">Title</div>
<div class="absolute">
    <p>Absolute</p>
    <p>Absolute</p>
    <p>Absolute</p>
</div>
<div class="clearboth"></div>
</div>

css 的CSS

.parent {
position:relative;
background-color:yellow;
}
.absolute {
position:absolute;
}
.absolute p {
padding:5px;
}
.clearboth {
clear:both;
}

If you don't mind .absolute posistion, try to make it relative : 如果您不介意.absolute ,请尝试使其relative

.absolute {
    position:relative;
}

Working example -- http://jsfiddle.net/xyjrLa2p/3/ 工作示例-http://jsfiddle.net/xyjrLa2p/3/

OK, as I expected, this is another of those " 99 out of 100 answers ". 好,正如我所料,这是“ 100个答案中的99个 ”中的另一个。 As a rule of thumb, remember that if you're thinking of responsive design , chances of using absolute position are really slim, close to 0, because it's the absolute opposite to responsiveness , unless you're planning to deal with LOTS of media queries. 根据经验,请记住,如果您正在考虑响应式设计 ,则使用绝对位置的机会确实很小,接近于0,因为它与响应性绝对相反,除非您打算处理很多媒体查询。

So, in your case and your sample, here you have: 因此,根据您的情况和您的样本,这里有:

body {
    background: #ccc;
}
.flip {
    -webkit-perspective: 800;
    width: 20vw;
    height: 20vh;
    position: relative;
    margin: 50px auto;
    display:inline-block;
}
.flip .card.flipped {
    -webkit-transform: rotatex(-180deg);
}
.flip .card {
    width: 100%;
    height: 100%;
    -webkit-transform-style: preserve-3d;
    -webkit-transition: 0.5s;
}
.flip .card .face {
    width: 100%;
    height: 100%;
    position: absolute;
    -webkit-backface-visibility: hidden;
    z-index: 2;
    font-family: Georgia;
    font-size: 3em;
    text-align: center;
    line-height: 200px;
}
.flip .card .front {
    position: absolute;
    z-index: 1;
    background: black;
    color: white;
    cursor: pointer;
}
.flip .card .back {
    -webkit-transform: rotatex(-180deg);
    background: blue;
    background: white;
    color: black;
    cursor: pointer;
}

You can add some min-width or max-width in pixels or add a media-query to change the vh/vw behavior at some point, but there you go, fully responsive behavior with (obviously) no absolute position. 您可以添加一些min-widthmax-width以像素为单位),或者添加media-query以更改vh/vw行为,但是到了那儿,您将获得完全响应的行为,(显然)没有绝对位置。

Always remember: if you want to use position:absolute , it should be inside a container with position:relative . 永远记住:如果要使用position:absolute ,则应将其放置在具有position:relative的容器内。 While there are exceptions to this, it's an easy rule that will save you a lot of trouble at the time of positioning elements. 尽管有一些例外情况,但这是一条简单的规则,可以在定位元素时省去很多麻烦。

See fiddle here 在这里看到小提琴

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

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