如果您对Safari中的CSSvw和高度100%和css设置div高度为浏览器高度感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解Safari中的CSSvw和高度100%的各种细节,并对css设
如果您对Safari中的CSS vw和高度100%和css设置div高度为浏览器高度感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解Safari中的CSS vw和高度100%的各种细节,并对css设置div高度为浏览器高度进行深入的分析,此外还有关于Chrome / Safari无法填充Flex父级的100%高度、css – Chrome/Safari中的图像插值模式?、css – Chrome/Safari无法填充100%高度的父级、css – DIV – 强制高度100%= / =页面高度?的实用技巧。
本文目录一览:- Safari中的CSS vw和高度100%(css设置div高度为浏览器高度)
- Chrome / Safari无法填充Flex父级的100%高度
- css – Chrome/Safari中的图像插值模式?
- css – Chrome/Safari无法填充100%高度的父级
- css – DIV – 强制高度100%= / =页面高度?
Safari中的CSS vw和高度100%(css设置div高度为浏览器高度)
高度为100%的元素和固定高度为vw(视口宽度)的父元素在Safari上实际上并不是100%高(Mac Mavericks,不确定Windows).它是0.
我准备了pen with example来证明我的问题.使用Chrome时,内部元素的正确高度为100%.使用Safari(v7.0.2(9537.74.9)),内部元素的高度为0.
编辑:在iOS 7.1的iPhone上发生同样的错误
解决方法
#parent { width:100%; height:50vw; background: red; } #child { width: 100%; height: inherit; background: lime; }
updated pen
在Safari 8中,此错误已得到修复,百分比高度/宽度应该可以正常工作.
Chrome / Safari无法填充Flex父级的100%高度
如何解决Chrome / Safari无法填充Flex父级的100%高度?
解
使用嵌套的伸缩容器。
摆脱百分比高度。摆脱表属性。摆脱vertical-align
。避免绝对定位。 一直坚持使用flexBox。
应用于display: flex
flex项目(.item
),使其成为flex容器。这会自动设置align-items:
stretch
,告诉孩子(.item-inner
)扩大父母的整个身高。
重要提示:从弹性项目中删除指定的高度,此方法才能起作用。 如果孩子有指定的身高(例如height: 100%
),那么它将忽略align-items: stretch
来自父母的身高。为了使stretch
默认设置生效,孩子的身高必须计算为auto
。
试试看(HTML不变):
.container {
display: flex;
flex-direction: column;
height: 20em;
border: 5px solid black
}
.item {
display: flex; /* new; nested flex container */
flex: 1;
border-bottom: 1px solid white;
}
.item-inner {
display: flex; /* new; nested flex container */
flex: 1; /* new */
/* height: 100%; <-- remove; unnecessary */
/* width: 100%; <-- remove; unnecessary */
/* display: table; <-- remove; unnecessary */
}
a {
display: flex; /* new; nested flex container */
flex: 1; /* new */
align-items: center; /* new; vertically center text */
background: orange;
/* display: table-cell; <-- remove; unnecessary */
/* vertical-align: middle; <-- remove; unnecessary */
}
<div>
<div>
<div>
<a>Button</a>
</div>
</div>
<div>
<div>
<a>Button</a>
</div>
</div>
<div>
<div>
<a>Button</a>
</div>
</div>
</div>
说明
我的问题是
.item-inner { height: 100% }
无法在webkit(Chrome)中使用。
它不起作用,因为您使用的百分比高度与规范的传统实现方式不一致。
指定百分比高度。相对于生成的盒子的包含块的高度计算百分比。如果未明确指定包含块的高度,并且此元素未绝对定位,则该值将计算为
auto
。高度取决于其他属性的值。
换句话说,要使百分比高度适用于流入儿童,父级 必须 具有设定的高度。
在您的代码中,顶级容器具有定义的高度: .container { height: 20em; }
第三级容器具有定义的高度: .item-inner { height: 100%; }
但是在它们之间,二级容器.item
– 没有 定义的高度。Webkit将其视为缺少的链接。
.item-inner
告诉Chrome: 给我height: 100%
。Chrome浏览器会向父级(.item
)供参考并做出响应:100%是什么? 我什么也没看到(忽略flex: 1
那里的规则)。结果,height: auto
根据规范适用(内容高度)。
另一方面,Firefox现在接受父级的flex高度作为孩子的百分比高度的参考。IE11和Edge也接受弯曲高度。
此外, 如果与 (任何数字值(不会),包括在内) 结合使用,Chrome会接受flex-grow
作为适当的父级参考。但是,在撰写本文时,此解决方案在Safari中失败。 flex-basis
auto
flex-basis: 0
#outer {
display: flex;
flex-direction: column;
height: 300px;
background-color: white;
border: 1px solid red;
}
#middle {
flex-grow: 1;
flex-basis: 1px;
background-color: yellow;
}
#inner {
height: 100%;
background-color: lightgreen;
}
<div id="outer">
<div id="middle">
<div id="inner">
INNER
</div>
</div>
</div>
四种解决方案
可靠的跨浏览器解决方案是在所有父元素上指定高度。这样可以防止丢失链接,基于Webkit的浏览器会认为这些链接违反了规范。
请注意,min-height
和max-height
是不可接受的。它必须是height
属性。
此处有更多详细信息: 使用CSSheight
属性和百分比值
适用position: relative
于父母和position: absolute
孩子。
尺寸与孩子height: 100%
和width: 100%
,或使用污损特性:top: 0
,right: 0
,bottom:
0
,left: 0
。
通过绝对定位,百分比高度可以在父级上没有指定高度的情况下工作。
是否需要两个容器button
?为什么不删除.item
或.item-
inner
,或两者都不删除?尽管button
元素有时无法作为flex容器使用,但它们可以是flex项目。考虑让或成为button
孩子,并删除不必要的标记。.container``.item
这是一个例子:
.container {
height: 20em;
display: flex;
flex-direction: column;
border: 5px solid black
}
a {
flex: 1;
background: orange;
border-bottom: 1px solid white;
display: flex; /* nested flex container (for aligning text) */
align-items: center; /* center text vertically */
justify-content: center; /* center text horizontally */
}
<div>
<a>Button</a>
<a>Button</a>
<a>Button</a>
</div>
摆脱百分比高度。摆脱表属性。摆脱vertical-align
。避免绝对定位。 一直坚持使用flexBox。
应用于display: flex
flex项目(.item
),使其成为flex容器。这会自动设置align-items:
stretch
,告诉孩子(.item-inner
)扩大父母的整个身高。
重要提示:从弹性项目中删除指定的高度,此方法才能起作用。 如果孩子有指定的身高(例如height: 100%
),那么它将忽略align-
items: stretch
来自父母的身高。为了使stretch
默认设置生效,孩子的身高必须计算为auto
。
试试看(HTML不变):
.container {
display: flex;
flex-direction: column;
height: 20em;
border: 5px solid black
}
.item {
display: flex; /* new; nested flex container */
flex: 1;
border-bottom: 1px solid white;
}
.item-inner {
display: flex; /* new; nested flex container */
flex: 1; /* new */
/* height: 100%; <-- remove; unnecessary */
/* width: 100%; <-- remove; unnecessary */
/* display: table; <-- remove; unnecessary */
}
a {
display: flex; /* new; nested flex container */
flex: 1; /* new */
align-items: center; /* new; vertically center text */
background: orange;
/* display: table-cell; <-- remove; unnecessary */
/* vertical-align: middle; <-- remove; unnecessary */
}
<div>
<div>
<div>
<a>Button</a>
</div>
</div>
<div>
<div>
<a>Button</a>
</div>
</div>
<div>
<div>
<a>Button</a>
</div>
</div>
</div>
解决方法
我想要一个具有特定高度的垂直菜单。
每个孩子都必须填充父母的身高,并且中间对齐文本。
孩子的数量是随机的,因此我必须使用动态值。
Div .container
包含随机数量的子代(.item
),这些子代始终必须填充父代的高度。为此,我使用了flexbox。
为了使文本中间对齐,我使用了display: table-cell
技巧。但是使用表格显示需要使用高度100%。
我的问题是.item-inner { height: 100% }
无法在webkit(Chrome)中使用。
- 有解决此问题的方法吗?
-
还是有一种不同的技术可以使所有
.item
文本的高度都垂直于中间对齐而填满父级的高度?.container {
height: 20em;
display: flex;
flex-direction: column;
border: 5px solid black
}
.item {
flex: 1;
border-bottom: 1px solid white;
}
.item-inner {
height: 100%;
width: 100%;
display: table;
}
a {
background: orange;
display: table-cell;
vertical-align: middle;
}
<div> <a>Button</a> </div>
<div> <a>Button</a> </div>
<div> <a>Button</a> </div>
css – Chrome/Safari中的图像插值模式?
ms-interpolation-mode: nearest-neighbor; image-rendering: -moz-crisp-edges;
这可以在IE和Firefox中使用,但不适用于Chrome和Safari。是否有任何webkit替代方案或任何其他方式来实现这一效果?
解决方法
https://developer.mozilla.org/en-US/docs/CSS/image-rendering#Examples
这在当前版本的Chrome中无效,以下是一些有用的链接:
> http://vaughnroyko.com/state-of-nearest-neighbor-interpolation-in-canvas/
> Image scaling by CSS: is there a webkit alternative for -moz-crisp-edges?
我没想到有办法。
而一些Google搜索器却很快就证实了这一点。 webkit image interpolation的顶级Google结果是:
http://code.google.com/p/chromium/issues/detail?id=1502
2008年9月报道,仍未解决。
还有:https://bugs.webkit.org/show_bug.cgi?id=40881
如果我需要这个,我可能会编写一个PHP脚本来使用最近邻居来动态地放大图像。
这将在每个浏览器中工作,但是您有所有额外的处理和传输开销。
css – Chrome/Safari无法填充100%高度的父级
每个孩子必须填充父级的高度,并且具有中间对齐的文本。
孩子的数量是随机的,所以我必须使用动态值。
Div .container包含一个随机数的子元素(.item),总是必须填充父元素的高度。为了实现我使用flexBox。
为了使文本对齐到中间的链接我使用display:table-cell技术。但是使用表格显示需要使用100%的高度。
我的问题是.item-inner {height:100%}在webkit(Chrome)中不工作。
>有这个问题的修复吗?
>或者有一个不同的技术,使所有.item填充父的高度与文本垂直对齐到中间?
Example here jsFiddle,should be viewed in Firefox and Chrome
.container { height: 20em; display: flex; flex-direction: column; border: 5px solid black } .item { flex: 1; border-bottom: 1px solid white; } .item-inner { height: 100%; width: 100%; display: table; } a { background: orange; display: table-cell; vertical-align: middle; }
<div> <div> <div> <a>Button</a> </div> </div> <div> <div> <a>Button</a> </div> </div> <div> <div> <a>Button</a> </div> </div> </div>
解决方法
My problem is that
.item-inner { height: 100% }
is not working in
webkit (Chrome).
它不工作,因为你使用百分比高度不符合规范的传统实现的方式。
07000
percentage
Specifies a percentage height. The percentage is calculated with respect to the height of the generated Box’s
containing block. If the height of the containing block is not
specified explicitly and this element is not absolutely positioned,the value computes toauto
.auto
The height depends on the values of other properties.
换句话说,对于在流内子级上工作的百分比高度,父级必须具有设置的高度。
在你的代码中,顶层容器有一个定义的高度:.container {height:20em; }}
第三级容器具有定义的高度:.item-inner {height:100%; }}
但是在它们之间,二级容器 – .item – 没有定义的高度。 Webkit将其视为缺少链接。
.item-inner告诉Chrome:给我高度:100%。 Chrome查找父(.item)以供参考,并做出回应:100%的什么?我没有看到任何东西(忽略flex:1规则是那里)。因此,它应用height:auto(content height),根据规范。
另一方面,Firefox现在接受父级的flex高度作为孩子的百分比高度的参考。 IE11和Edge也接受弯曲高度。
此外,如果与flex-basis(任何值工作,包括flex-basis:0)结合使用,Chrome将接受flex-grow作为足够的父引用。然而,在写这篇文章的时候,这个解决方案在Safari中失败了。
#outer { display: flex; flex-direction: column; height: 300px; background-color: white; border: 1px solid red; } #middle { flex-grow: 1; flex-basis: 1px; background-color: yellow; } #inner { height: 100%; background-color: lightgreen; }
<div id="outer"> <div id="middle"> <div id="inner"> INNER </div> </div> </div>
解决方案
1.在所有父元素上指定高度
可靠的跨浏览器解决方案是在所有父元素上指定高度。这可以防止缺少链接,基于Webkit的浏览器会认为违反了规范。
请注意,min-height和max-height是不可接受的。它必须是height属性。
CSS相对和绝对定位
应用位置:相对于父级和位置:绝对到子级。
大小绝对定位的孩子与高度:100%和宽度:100%,或使用偏移属性:顶部:0,右:0,底部:0,左:0。
使用绝对定位,百分比高度在父级上没有指定高度。
3.删除不必要的HTML容器(推荐)
是否需要两个容器周围按钮?为什么不删除.item或.item-inner,或两者?虽然button
elements sometimes fail as flex containers,它们可以是flex项目。考虑使按钮成为.container或.item的子对象,并删除无偿标记。
这里有一个例子:
.container { height: 20em; display: flex; flex-direction: column; border: 5px solid black } a { flex: 1; background: orange; border-bottom: 1px solid white; display: flex; /* nested flex container (for aligning text) */ align-items: center; /* center text vertically */ justify-content: center; /* center text horizontally */ }
<div> <a>Button</a> <a>Button</a> <a>Button</a> </div>
4.嵌套Flex容器(推荐)
去除百分比高度。删除表属性。摆脱vertical-align。避免绝对定位。只要坚持flexBox一路通过。
将display:flex应用于flex项(.item),使其成为flex容器。这会自动设置align-items:stretch,它告诉孩子(.item-inner)展开父节点的整个高度。
尝试此操作(无需更改HTML):
.container { display: flex; flex-direction: column; height: 20em; border: 5px solid black } .item { display: flex; /* new; nested flex container */ flex: 1; border-bottom: 1px solid white; } .item-inner { display: flex; /* new; nested flex container */ flex: 1; /* new */ /* height: 100%; <-- remove; unnecessary */ /* width: 100%; <-- remove; unnecessary */ /* display: table; <-- remove; unnecessary */ } a { display: flex; /* new; nested flex container */ flex: 1; /* new */ align-items: center; /* new; vertically center text */ background: orange; /* display: table-cell; <-- remove; unnecessary */ /* vertical-align: middle; <-- remove; unnecessary */ }
<div> <div> <div> <a>Button</a> </div> </div> <div> <div> <a>Button</a> </div> </div> <div> <div> <a>Button</a> </div> </div> </div>
jsFiddle
更多信息
在CSS中通常如何工作的百分比:
> Working with the CSS height
property and percentage values
使用父级的flex高度作为子级百分比高度的参考的Firefox和IE示例:
> Chrome ignoring flex-basis in column layout
> Height is not correct in flexbox items in Chrome
> Flexbox in Chrome–How to limit size of nested elements?
> min-height inside a flex item is being ignored in chrome
css – DIV – 强制高度100%= / =页面高度?
http://throwbackhero.com/index1.php
问题是我将身体设置为高度:100%;在样式表和#wrapper div中也是如此.高度偏离当前页面高度,并没有考虑到其他div可能导致溢出.
我想,即使浏览器的垂直大小发生变化,空白页面也是浏览器的大小,内容/包装器div将缩小以适应.
可以这样做吗?
编辑
好的,我原来的问题非常混乱.这是一张照片;
所以,在图1(左)是问题.高度100%;在包装器和内容div上,它正在创造那个坏孩子.我希望它看起来像图片,白色/灰色区域根据浏览器的大小增长/缩小…
解决方法
height: 100vh;
其中’vh’代表浏览器窗口的垂直高度.响应调整浏览器和移动设备的大小.
今天关于Safari中的CSS vw和高度100%和css设置div高度为浏览器高度的讲解已经结束,谢谢您的阅读,如果想了解更多关于Chrome / Safari无法填充Flex父级的100%高度、css – Chrome/Safari中的图像插值模式?、css – Chrome/Safari无法填充100%高度的父级、css – DIV – 强制高度100%= / =页面高度?的相关知识,请在本站搜索。
本文标签: