我有一个固定在屏幕顶部的元素,z-index 为 100。当我在 Chrome 中向上滚动页面时(OS X 上为 V. 21.0.1180.82,Windows 上为 V. 21.0.1180.83 m)我注意到一个渲染问题,看起来像这样:

更新:
我注意到每次<section>...</section>在向上滚动期间固定元素传递一个新元素时都会出现渲染问题。Chrome 似乎有一些与 HTML5 标签相关的问题,例如<section>...</section>或<nav>...</nav>。更改这些以<div>...</div>解决渲染问题,无论如何我仍然想继续使用 HTML5 标签。
有人知道解决这个问题的“黑客”吗?
在此之前,我将尝试创建一个可重现的代码示例并将错误提交给chromium。
我有一个 DIV 容器,.floatingPage它绝对位于另外两个 DIV 容器.top和.bottom. 我的问题是.floatingPage元素的内容是动态的,有时包含大量数据,这些数据延伸.floatingPage到屏幕的可见部分下方。
.top {
height:350px;
background:'images/background.png';
position:relative;
}
.bottom {
height:350px;
background:#F1F1F1;
}
.floatingPage {
position:absolute;
top:50px;
}Run Code Online (Sandbox Code Playgroud)
<div class="top">
<div class="floatingPage">
// blah blah blah (lots of data)
</div>
</div>Run Code Online (Sandbox Code Playgroud)
现在,因为我知道我的应用程序的用户使用 JS(公司控制面板),我一直在计算.floatingPage元素的高度并扩展 的高度.bottom以适应新内容。到目前为止,这一直没问题,并开始引起问题。另外,我不想为其他用户没有 JS 的项目做这个 hack。
我需要这个floatingPage元素漂浮在其他两个元素的顶部——这就是网站设计的重点。在没有 JS 的情况下,如何保持这种影响但防止它离开页面?
我有一个已知高度的容器(高度有时比屏幕长)。
这个容器有一个垂直和水平居中的元素(via flex)。
我希望这个元素总是在屏幕上垂直居中在容器可见部分的块中。
我试过的:
position:sticky; top:50% - 然而,这只会让它在容器的下半部分居中。position:sticky; bottom:50% - 然而这只会让它以上半部分为中心position:sticky; top: 50%; bottom:50%;- 它似乎top覆盖bottom所以这就像我第一次尝试top:50%只这是一个演示:
.container {
height: 1200px;
background-color: rgba(0, 0, 0, .7);
color: white;
display: flex;
justify-content: center;
align-items: center;
}
.center-piece {
background-color: green;
position: sticky;
top: 50%;
}
.center-piece2 {
background-color: steelblue;
position: sticky;
bottom: 50%;
}Run Code Online (Sandbox Code Playgroud)
<div class="container">
<div class="center-piece">
#1
</div>
<div class="center-piece2">
#2
</div>
</div>Run Code Online (Sandbox Code Playgroud)
无论如何要保持它完全居中,同时“始终在屏幕上”,在容器的可见部分,顶部和底部?
这是我的应用程序的截屏视频:https : //www.youtube.com/watch?v=CwYaBgolNHU …
假设一个简单的结构是这样的:
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
</head>
<body>
<div style="position: absolute; top: 0; bottom: 0; left: 0; right: 0;">Text</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
在大多数移动浏览器中,这会显示绝对定位的文本,而无需任何移动元素的方法。但是在 Safari / iOS 9 中,您可以垂直拖动元素,类似于溢出滚动。
我知道position: fixed;没有这个问题,但在我的用例中,我绝对必须使用position: absolute;.
有什么办法可以防止绝对定位元素在 Safari 中出现这样的行为?
在我的项目中,有一些图像。我的代码看起来像:
<div className="col-sm-6">
<img src="xxx">
</div>
<div className="col-sm-6">
<img src="xxx">
</div>
Run Code Online (Sandbox Code Playgroud)
的宽度为img的 100% div,即整个屏幕的 50%。如果我调整浏览器的大小,图像的宽度就会改变。在这种情况下,如何保持高度仍然与宽度相同?
我想将我的 textarea 的占位符文本定位到 textarea 的底部。这是我的代码:
<p class="text">
<textarea name="text" class="feedback-input" id="comment" placeholder="Vos remarques"></textarea>
</p>
Run Code Online (Sandbox Code Playgroud)
CSS:
textarea {
width: 100%;
height: 150px;
line-height: 150%;
resize:vertical;
}
Run Code Online (Sandbox Code Playgroud)
这是一个完整的代码笔。我想将 textareas 和 input 的占位符设置为靠近 input 和 textarea 的底部。目前,它们位于文本区域和输入的顶部。有什么方法可以选择 textarea 和 input 的占位符以及靠近文本区域底部、靠近边框的位置吗?
我有一个父母和一个孩子的div。父级只有一张图像。子 div 有另一个图像。子图像需要出现在父图像的左上角,我想将子图像absolute的位置设置为将父图像的位置设置为某个非静态位置,例如relativeor absolute。(我也设置了left,top但让我们暂时忘记它。)
这是因为absolute子元素的设置仅在其第一个祖先以非静态方式定位时才起作用,因为根据 W3Schoolsabsolute意味着该元素相对于其第一个定位(非静态)祖先元素定位。
但是我注意到即使我没有设置父级的位置(即默认情况下它的位置将是“静态”),事件然后子图像仍然很好地定位在左上角。
有人可以帮助我理解为什么我不必将父级的位置设置为某个非静态值以使该absolute属性正常工作吗?还是我理解absolute错误?
<div class="parent">
<img src="https://lh4.ggpht.com/wKrDLLmmxjfRG2-E-k5L5BUuHWpCOe4lWRF7oVs1Gzdn5e5yvr8fj-ORTlBF43U47yI=w300">
<div class="child">
<img src="http://xiostorage.com/wp-content/uploads/2015/10/test.png" width="200">
</div>
</div>
/*.parent {
position:relative;
}*/
.child {
display:none;
position:absolute;
left:0;
}
.parent:hover .child {
display:initial;
}
Run Code Online (Sandbox Code Playgroud)
更新: Connexo帮助更新了代码。
尝试取消position对父母的注释以解开谜团。接受的答案有更多信息。
<h1>
Positioning test
</h1>
<div class="parent">
<img src="https://lh4.ggpht.com/wKrDLLmmxjfRG2-E-k5L5BUuHWpCOe4lWRF7oVs1Gzdn5e5yvr8fj-ORTlBF43U47yI=w300">
<div class="child">
<img src="http://xiostorage.com/wp-content/uploads/2015/10/test.png" width="200">
</div>
/*
.parent …Run Code Online (Sandbox Code Playgroud) 是否可以使定位元素相对于当前不是其父元素的另一个元素?例子:
<div class="want-to-be-parent">
<div class="current-parent">
<div class="element-to-position"><!---content---></div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
所以在这种情况下,我想将“元素到位置”绝对定位在“想要成为父母”的左上角,但它目前相对定位在“当前父母”内。
编辑 - 假设所有这三个元素都具有“位置:相对”并且我无法控制它。我想绝对定位“元素到位置”,并让它相对于“想要成为父母”而不是“当前父母”进行绝对定位。我必须动态执行此操作,因为没有其他方法。
我现在对绝对定位有点困惑。我一直认为,如果我绝对定位一个元素,它将相对于它的父元素定位(与相对于它的通常位置(如相对定位)相反)。在做作业时,我现在遇到了这种情况,我很困惑:
<body>
<div> <!-- This is colored red in my example -->
...
</div>
<div style="position: absolute;"> <!-- This is colored green in my example -->
...
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
当然,当我设置的实际位置与left/ right/ top/bottom我得到什么,我会从一个绝对定位的元素的期望。那么是否position: absolute只是设置为在没有position: absolute另外指定的情况下采用它所在的确切位置?
https://jsfiddle.net/d3yns9b6/演示了如何max-width将其设置为大于包含元素的内容时不起作用。
由于它的位置绝对正确,因此它应该可以延伸到包含元素之外。如果我使用width它设置一个精确值,但是示例中的两段文本都恰好就是该宽度。
我希望它们都占用尽可能少的宽度,最多达到我设置的最大数量(即使它超过了父容器)。
.out {
position: relative;
width: 200px;
height: 400px;
background-color: blue;
}
.in {
position: absolute;
max-width: 600px;
background-color: yellow;
}Run Code Online (Sandbox Code Playgroud)
<div class="out">
<div class="in">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laboriosam commodi saepe, magnam aliquid quisquam cum ex corrupti sequi aut eius harum animi vitae, exercitationem eaque tempore culpa at itaque explicabo.
</div>
<div class="in" style="margin-top:300px">
Lorem
</div>
</div>Run Code Online (Sandbox Code Playgroud)
css ×10
css-position ×10
html ×8
css3 ×1
flexbox ×1
image ×1
ios ×1
javascript ×1
jquery ×1
placeholder ×1
textarea ×1