【问题标题】:Why does the element disappear when I set the width and height to 100%? [duplicate]为什么当我将宽度和高度设置为 100% 时元素会消失? [复制]
【发布时间】:2018-07-19 01:50:28
【问题描述】:

我有一个圆圈,悬停在它上面会翻转。 face-container widthheight210px

我希望宽度和高度是动态的,因此我将两者都设置为 100%,但随后元素消失了。

我认为这是因为内容。

.section {
    position: relative;
    padding: 80px 0;
}
.card-container {
    position: relative;
    margin: 0 auto;
}    
.card-container:after {
    clear: both;
}
.item-circled {
    background-color: transparent;
    text-align: center;
    position: relative;
    width: 100%;
    display: inline-block;
}
.face-container {
    position: relative;
    width: 210px;
    height: 210px;
    z-index: 1;
    -webkit-perspective: 1000px;
    perspective: 1000px;
}
.face-card {
    width: 100%;
    height: 100%;
    -webkit-transform-style: preserve-3d;
    transform-style: preserve-3d;
    -webkit-border-radius: 50%;
    border-radius: 50%;
    -webkit-transition: all .5s linear;
    transition: all .5s linear;
}
.face-container:hover .face-card {
    -webkit-transform: rotateY(180deg);
    transform: rotateY(180deg);
    -webkit-border-radius: 50%;
    border-radius: 50%;
}
.face-1 {
    position: absolute;
    width: 100%;
    height: 100%;
    backface-visibility: hidden;
    overflow: hidden;
    -webkit-border-radius: 50%;
    border-radius: 50%;
} 
.face-1.back {
    display: block;
    box-sizing: border-box;
    color: white;
    font-size: 13px;
    text-align: center;
    background-color: #aaa;
    -webkit-transform: rotateY(180deg);
    transform: rotateY(180deg);
    -webkit-border-radius: 50%;
    border-radius: 50%;
} 
.centered{
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
}
<div class="container-fluid">
    <div class="section">
        <section class="card-container">
            <div class="row center-block">  
                <div class="col-xs-push-2 col-xs-8 text-center">    
                    <div class="item-circled col-xs-4">
                        <div class="face-container">
                            <div class="face-card">
                                <div class="face-1">
                                    <div class="text-center" style="background-color: #f7eebe;font-size: 300%;width: 100%;height: 100%">
                                        <span class="centered">front</span>
                                    </div>
                                </div>
                                <div class="face-1 back">
                                    <p class="centered">back</p>
                                </div>
                            </div> <!-- face-card -->
                        </div> <!-- face-container -->
                    </div> <!-- col-xs-4 -->
                </div> <!-- col-xs-8 -->
            </div> <!-- row -->
        </section> <!-- card-conteiner -->
   </div> <!-- section -->
</div> <!-- container-fluid -->

我应该怎么做才能解决这个问题?

这是一个小提琴: http://jsfiddle.net/cmvz978x/19/

【问题讨论】:

  • 使用基于百分比的高度依赖于在父元素上设置的显式高度值:因为人脸容器是父元素,所以不能设置为 100%
  • 当你说它应该是响应式的,那么设置 100% 宽度/高度,100% 基于什么? ...浏览器,或图像,或....
  • 顺便说一句,使用百分比的动态宽度效果很好,height: 100% 不行,一旦你将它设置为 100%,它的所有祖先,一直到html/body,也需要设置高度,除非其中一个具有使用百分比以外的单位定义的高度。
  • @LGSon,我正在使用 Bootstrap,并且该元素需要 4 列,所以我希望它采用那个宽度
  • 好吧,width: 100% 不是问题,它会占用其父母的宽度,height: 100% 是,我在评论中对此进行了解释,并在欺骗链接中进行了解释。这很重要,无论您是否使用 Bootstrap。如果您使用 Bootstrap 4,请查看第二个重复链接,它谈到了 Bootstrap 4 所基于的 Flexbox。

标签: html twitter-bootstrap css


【解决方案1】:

正如@Aravind S 在 cmets 中提到的,您不能将容器设置为 height: 100%。该元素将不再定义大小,因此将是“100% 没有”。这就是你的元素消失的原因,因为 0 的 100% 只是 0。

如果您想让这个元素具有响应性,我的一个建议是实现 CSS 断点。它们看起来像:

/* Extra small devices (phones, 600px and down) */
@media only screen and (max-width: 600px) {...} 

/* Small devices (portrait tablets and large phones, 600px and up) */
@media only screen and (min-width: 600px) {...} 

/* Medium devices (landscape tablets, 768px and up) */
@media only screen and (min-width: 768px) {...} 

/* Large devices (laptops/desktops, 992px and up) */
@media only screen and (min-width: 992px) {...} 

/* Extra large devices (large laptops and desktops, 1200px and up) */
@media only screen and (min-width: 1200px) {...}

这个 sn-p 是从 W3 复制而来的。 通过使用这些断点,您可以为不同的视口设置不同的大小。

例如:

/* Small devices (portrait tablets and large phones, 600px and up) */
@media only screen and (min-width: 600px) {
    .face-container {
        width: 210px;
        height: 210px;
    }
} 

/* Medium devices (landscape tablets, 768px and up) */
@media only screen and (min-width: 768px) {
    .face-container {
        width: 310px;
        height: 310px;
    }
}

/* Large devices (laptops/desktops, 992px and up) */
@media only screen and (min-width: 992px) {
    .face-container {
        width: 410px;
        height: 410px;
    }
}

这将允许您的元素随网页缩放。您也可以根据需要省略断点,页面将选择最适用的断点来定义其样式。

【讨论】:

【解决方案2】:

解释如下:为了使百分比值适用于身高,必须确定父母的身高。更多here

指定百分比高度。百分比计算如下 相对于生成框的包含块的高度。如果 包含块的高度没有明确指定(即,它 取决于内容高度),并且这个元素不是绝对的 定位时,该值计算为“自动”。百分比高度 根元素是相对于初始包含块的。

如果您希望宽度和高度为动态响应,您可以使用媒体查询来定位特定分辨率。

【讨论】:

  • 那为什么这里没有发生:jsfiddle.net/1ryvajex/3 ?
  • @Dan 什么在那里不起作用?如果将100% 高度设置为元素,则其父级应具有静态高度或宽度。请记住一件事 span 您使用的是内联元素。
  • span 现在是块jsfiddle.net/1ryvajex/7,它占用了大约 8 列的整个父宽度
  • 它的高度是字母A..你必须在父级设置所需的高度。如果你想将100%设置为.face-container然后,将210px设置为父级.face-container.card-container 或任何你喜欢的。
  • 我希望它像在提到的示例中那样适合内容,而不是特定的像素值
【解决方案3】:

您可以通过对 .face-container 元素的宽度和高度使用垂直高度 (vh) 属性来实现您想要的效果。 (我还包括了 min-width 和 min-height 以确保圆圈不会变得小于文本。)如果您使用此方法,则必须对 CSS 进行其他调整以正确对齐容器。

.face-container {
    position: relative;
    min-width: 200px;
    min-height: 200px;
    width: 80vh;
    height: 80vh;
    z-index: 1;
    -webkit-perspective: 1000px;
    perspective: 1000px;
}

【讨论】:

猜你喜欢
  • 2023-01-13
  • 2020-02-08
  • 1970-01-01
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
  • 2014-05-26
  • 2012-03-15
  • 2016-03-17
相关资源
最近更新 更多