简体   繁体   English

为什么宽度:100%不在div {display:table-cell}上工作?

[英]Why is width: 100% not working on div {display: table-cell}?

I'm trying to vertically and horizontally center some content overlaying an image slide (flexslider). 我试图垂直和水平地居中覆盖图像幻灯片(flexslider)的一些内容。 There were some similar questions to this one, but I couldn't find a satisfactory solution that applied directly to my specific problem. 有一些类似的问题,但我找不到一个直接适用于我的具体问题的令人满意的解决方案。 Because of the limitations of FlexSlider, I cannot use position: absolute; 由于FlexSlider的限制,我不能使用position: absolute; on the img tag in my implementation. 在我的实现中的img标签上。

I almost have workaround below working. 我几乎有下面的工作方法。 The only problem is I cannot get the width & height declarations to work on inner-wrapper div with the display: table-cell property. 唯一的问题是我无法使用display: table-cell属性获取宽度和高度声明来处理inner-wrapper div。

Is this standard behavior, or am I missing something with my code? 这是标准行为,还是我错过了我的代码? If it's standard behavior, what's the best solution to my problem? 如果这是标准行为,那么我的问题的最佳解决方案是什么?

HTML HTML

<ul>
    <li>
        <img src="#">
        <div class="outer-wrapper">
            <div class="inner-wrapper">
                <h1>My Title</h1>
                <h5>Subtitle</h5>
            </div>
        </div>
    </li>
</ul>

CSS CSS

html, body {
    margin: 0; padding: 0;
    width: 100%; height: 100%;
}

ul {
    background: #CCC;
    height: 100%;
    width: 100%;
    list-style-position: outside;
    margin: 0; padding: 0;
}

li {
    width: 100%;
    display: table;
}

img {
    width: 100%;
    height: 410px;
}

.outer-wrapper {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    margin: 0; padding: 0;
}

.inner-wrapper {
    display: table-cell;
    vertical-align: middle;
    text-align: center;
    width: 100%;
    height: 100%;
}

Note: the centered content will be more than 1 element, so I can't use the line-height trick. 注意:居中的内容将超过1个元素,因此我不能使用行高技巧。

jsFiddle . jsFiddle

Putting display:table; 放置display:table; inside .outer-wrapper seemed to work... 里面.outer-wrapper似乎工作......

JSFiddle Link JSFiddle链接


EDIT: Two Wrappers Using Display Table Cell 编辑:两个使用显示表格单元格的包装

I would comment on your answer but i have too little rep :( anyways... 我会评论你的答案,但我的代表太少了:(反正......

Going off your answer , seems like all you need to do is add display:table; 离开你的答案 ,似乎你需要做的就是添加display:table; inside .outer-wrapper (Dejavu?), and you can get rid of table-wrapper whole-heartedly. .outer-wrapper (Dejavu?)中,你可以全心全意地摆脱table-wrapper

JSFiddle 的jsfiddle

But yeah, the position:absolute lets you place the div over the img , I read too quickly and thought that you couldn't use position:absolute at all, but seems like you figured it out already. 但是,是的, position:absolute让你把div放在img ,我读得太快,并且认为你根本不能使用position:absolute ,但看起来你已经弄明白了。 Props! 道具!

I'm not going to post the source code, after all its 99% timshutes's work, so please refer to his answer, or just use my jsfiddle link 我不会发布源代码,毕竟它的99%timshutes的工作,所以请参考他的答案,或只是使用我的jsfiddle链接

Update: One Wrapper Using Flexbox 更新:使用Flexbox的一个包装器

It's been a while, and all the cool kids are using flexbox: 已经有一段时间了,所有很酷的孩子都在使用flexbox:

<div style="display: flex; flex-direction: column; justify-content: center; align-items: center;">
    stuff to be centered
</div>

Full JSFiddle Solution 完整的JSFiddle解决方案

Browser Support ( source ): IE 11+, FireFox 42+, Chrome 46+, Safari 8+, iOS 8.4+ ( -webkit- prefix), Android 4.1+ ( -webkit- prefix) 浏览器支持( 来源 ):IE 11 +,FireFox 42 +,Chrome 46 +,Safari 8 +,iOS 8.4+(- -webkit-前缀),Android 4.1+(- -webkit-前缀)

CSS Tricks: a Guide to Flexbox CSS技巧:Flexbox指南

How to Center in CSS : input how you want your content to be centered, and it outputs how to do it in html and css. 如何在CSS中居 :输入您希望内容如何居中,并输出如何在html和CSS中执行此操作。 The future is here! 未来就在这里!

I figured this one out. 我想出了这一个。 I know this will help someone someday. 我知道有一天这会帮助别人。

How to Vertically & Horizontally Center a Div Over a Relatively Positioned Image 如何在相对定位的图像上垂直和水平居中Div

The key was a 3rd wrapper. 关键是第三个包装。 I would vote up any answer that uses less wrappers. 我会投票给任何使用较少包装的答案。

HTML HTML

<div class="wrapper">
    <img src="my-slide.jpg">
    <div class="outer-wrapper">
        <div class="table-wrapper">
            <div class="table-cell-wrapper">
                <h1>My Title</h1>
                <p>Subtitle</p>
            </div>
        </div>
    </div>
</div>

CSS CSS

html, body {
margin: 0; padding: 0;
width: 100%; height: 100%;
}

ul {
    width: 100%;
    height: 100%;
    list-style-position: outside;
    margin: 0; padding: 0;
}

li {
    width: 100%;
    display: table;
}

img {
    width: 100%;
    height: 100%;
}

.outer-wrapper {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  margin: 0; padding: 0;
}

.table-wrapper {
  width: 100%;
  height: 100%;
  display: table;
  vertical-align: middle;
  text-align: center;
}

.table-cell-wrapper {
  width: 100%;
  height: 100%;  
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}

You can see the working jsFiddle here. 你可以在这里看到工作的jsFiddle

I discovered that the higher the value of 'width' is, the smaller the box width is made and vice versa. 我发现'width'的值越大,盒子宽度越小,反之亦然。 I found this out because I had the same problem earlier. 我发现了这个,因为我之前遇到了同样的问题。 So: 所以:

.inner-wrapper {
    width: 1%;
}

solves the problem. 解决了这个问题。

Welcome to 2017 these days will using vW and vH do the trick 欢迎来到2017这几天将使用vWvH做的伎俩

 html, body { margin: 0; padding: 0; width: 100%; height: 100%; } ul { background: #CCC; height: 100%; width: 100%; list-style-position: outside; margin: 0; padding: 0; } li { width: 100%; display: table; } img { width: 100%; height: 410px; } .outer-wrapper { position: absolute; width: 100%; height: 100%; top: 0; margin: 0; padding: 0; } .inner-wrapper { display: table-cell; vertical-align: middle; text-align: center; width: 100vw; /* only change is here "%" to "vw" ! */ height: 100vh; /* only change is here "%" to "vh" ! */ } 
 <ul> <li> <img src="#"> <div class="outer-wrapper"> <div class="inner-wrapper"> <h1>My Title</h1> <h5>Subtitle</h5> </div> </div> </li> </ul> 

Your 100% means 100% of the viewport, you can fix that using the vw unit besides the % unit at the width. 你的100%意味着100%的视口,你可以使用除了宽度上的%单位之外的vw单位来修复它。 The problem is that 100vw is related to the viewport, besides % is related to parent tag. 问题是100vw与视口相关,除了%与父标签相关。 Do like that: 这样做:

.table-cell-wrapper {
    width: 100vw;
    height: 100%;  
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}

How about this? 这个怎么样? (jsFiddle link) (jsFiddle链接)

CSS CSS

ul {
    background: #CCC;
    height: 1000%;
    width: 100%;
    list-style-position: outside;
    margin: 0; padding: 0;
    position: absolute;
}
li {
 background-color: #EBEBEB;
    border-bottom: 1px solid #CCCCCC;
    border-right: 1px solid #CCCCCC;
    display: table;
    height: 180px;
    overflow: hidden;
    width: 200px;
}
.divone{
    display: table-cell;
    margin: 0 auto;
    text-align: center;
    vertical-align: middle;
    width: 100%; 

}
img {
    width: 100%;
    height: 410px;
}
.wrapper {
  position: absolute;
}

Just add min-height:100% and min-width:100% and it will work. 只需添加min-height:100%和min-width:100%即可。 I had the same problem. 我有同样的问题。 You don't need a 3th wrapper 你不需要第3个包装器

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

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