我今天正在考虑CSS定位模式,并意识到position:relative除了position:absolute在子元素上工作之外,我从不使用 任何东西.
我更像是一个"开发人员"而不是"设计师",但多年来我已经做了很多基于CSS的布局.我已经使用了表格,浮点数,边距(正面和负面),位置:绝对位置,甚至位置:固定,但我认为我从未有过使用位置的场合:相对于实际定位元件.
是否有一些伟大的CSS-guru技术依赖于位置:相对(并用于实际设计)?我错过了吗?
这是一个使用绝对定位和jQuery动画Div的简单测试用例.
<html>
<head>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js'></script>
<script type='text/javascript'>
function slide(){
$('#box').animate({'left': 0},3000);
}
</script>
</head>
<body>
<button onclick="slide()">slide</button>
<div id="box" style="position: absolute; width: 120px; height: 100px; background: #ff0000; left: 500px"></div>
</body>
Run Code Online (Sandbox Code Playgroud)
在Firefox 4(在Mac上)动画"泪流满面"并且非常恶劣.在Safari和Chrome中它更好,但仍然有明显的抖动.
将问题简化为上述测试用例后,我不确定下一步该怎么做.这是一个jQuery错误吗?我是否遗漏了导致浏览器重绘负载的绝对定位?如果有些人可以尝试上面的代码并进行思考,那将是非常感激....即使它只是让我放心,我不会生气:)
这是一个完整的测试用例:
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<style type="text/css">
html, body, ul, li, div, span {
padding: 0;
margin: 0;
}
ul.container {
display: table;
list-style-type: none;
margin-right: 24px;
position: relative;
}
ul.container li {
display: table-cell;
position: relative;
}
ul.container div, ul.container span {
border: 1px dotted #000;
}
ul.container div {
width: 40px;
height: 40px;
position: absolute;
left: 0;
top: 40px;
background-color: #008000;
}
ul.container span {
display: block;
width: 40px;
height: 40px;
background-color: #9acd32;
}
</style>
</head>
<body> …Run Code Online (Sandbox Code Playgroud) 我一直在试图让我的网站成为crossbrowser ..在IE6中我有一个相对定位的容器,里面有一个绝对的div,它有负的顶部和左边,但不是在相对的顶部div,正在下面看起来像这样:
++++++++++++++++++++++
+ container +
+++++++++++++er +
+llo +er +
+rld +er +
+++++++++++++er +
+ container +
+ container +
++++++++++++++++++++++
Run Code Online (Sandbox Code Playgroud)
注意:
++++++++++++++++++++++
+ container +
+++++++++++++++++++er +
+ hello +er +
+ world +er +
+++++++++++++++++++er +
+ container +
+ container +
++++++++++++++++++++++
Run Code Online (Sandbox Code Playgroud)
在所有其他浏览器中我只使用静态定位容器和hello world div的绝对位置,并且工作得很好,但在ie6绝对div是奇怪的定位,无论我怎么顶或左我没有移动,所以我想为IE6做这个,但我有上面描述的问题.
我遇到以下html布局的问题.
我知道有关于绝对定位和粘性页脚的其他问题,我尝试了很多解决方案,但我没有做到这一点,所以我试图发布整个布局HTML代码,看看是否有人可以找到解决方案.
我使用了这个 StickyFooter解决方案.
问题是正确的吧.
它应该保持固定在距离页脚25px的位置,但是,正如您所看到的,如果条形内容增长,内容将在页脚下方和条形底部边框之外下降.
显然我会将内容留在栏内,导致栏杆长大并将页脚向下推.
<!doctype html>
<head>
<style type="text/css">
/* Sticky Footer */
{ margin: 0; }
html, body, form { height: 100%; }
.wrapper {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -50px; /* the bottom margin is the negative value of the footer's height */
}
#footerPage, #divPush
{
height: 50px; /* .push must be the same height as .footer */
}
/* End / Sticky Footer */
body { background-color:#000; …Run Code Online (Sandbox Code Playgroud) 我希望并排使用块元素.我不想使用left, right, top,或任何类似的东西.
<div class="iLB">LOLOLOL</div>
<div class="iLB">
This is content.<br/>
This is content.<br/>
This is content.
</div>
<div class="iLB">This, too?</div>
Run Code Online (Sandbox Code Playgroud)
.iLB {
display: inline-block;
}
Run Code Online (Sandbox Code Playgroud)
现场演示:jsFiddle
我一直在寻找一段时间,我似乎无法找到解决这个问题的方法.当输入字段在移动safari中获得焦点时(尚未检查其他浏览器),由于safari将该元素放入视图(更靠近中心),固定元素将跳转.该错误仅在用户滚动时发生,如果用户仍在页面顶部,则不会发生任何错误.
截图前后


有没有人知道如何解决这个问题?我遇到的唯一解决方案是在聚焦时滚动回到顶部,然后在模糊滚动回到位置......这看起来像是一个非常草率的解决方案.
这是屏幕截图中的示例网站,非常基本,没有任何规范.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, scale=1"/>
<title>MySite</title>
<style>
body{
height:100%;
width:100%;
padding:0;
margin:0;
font-size:16px;
font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
}
header, footer {
background-color:#333;
padding:10px;
color:#FFF;
width:100%;
}
header {
position:fixed;
top:0;
left:0;
}
input[type=search] {
position:relative;
margin-left:20px;
width:160px;
font-size:16px;
height:26px;
}
section{
padding:10px;
}
</style>
</head>
<body>
<header>
<b>MySite</b>
<input type='search' placeholder='Search'>
</header>
<section>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, …Run Code Online (Sandbox Code Playgroud) 我偶然发现IE10和IE9的奇怪之处,不影响IE8:当设置其父级的border-radius,overflow和position时,隐藏"position:fixed"子元素(参见jsfiddle示例).如果禁用其中一个属性,则会显示固定元素.
我在http://jsfiddle.net/arkhi/7Nydz/上放了一个实例.
<div style="position:relative; border-radius:5px; overflow:hidden;">
<a style="position:fixed">fixed child</a>
</div>
Run Code Online (Sandbox Code Playgroud)
我想知道是否有人有任何解释或链接解释这个错误(如果这是一个错误,而不是我没有看到明显的错误).
非常感谢您对此的任何反馈!
css css-position css3 internet-explorer-9 internet-explorer-10
我有三个div标签,我想在div标签之间垂直留出空间,也希望将我的第一个div标签作为固定的。
当我将第一个div位置设置为非固定位置时,我可以留出垂直空间。
<html>
<head>
<title>div</title>
</head>
<body style="margin: 0; padding: 0;">
<div style="width:100%; background-color:Lime;">
<div style="width:100%; height:10%; background-color:Blue;">
a
</div>
<div style="width:70%; height:100%; background-color:Gray; margin: auto; margin-top: 2em; margin-bottom: 2em;">
b
</div>
<div style="width:100%; height:5%; background-color:Aqua;">
c
</div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
当我将“ div a”的位置更改为固定值时,“ div a”和“ div b”都从空白处下降:2em。
<html>
<head>
<title>div</title>
</head>
<body style="margin: 0; padding: 0;">
<div style="width:100%; background-color:Lime;">
<div style="width:100%; height:10%; background-color:Blue; position: fixed;">
a
</div>
<div style="width:70%; …Run Code Online (Sandbox Code Playgroud) 当你尝试绝对定位元素时tbody,tr甚至td你发现它在大多数浏览器中都不起作用.它在Firefox中按预期工作.不在IE,Edge和Chrome中.
position: relative在tbody,tr甚至td被忽略.然后第一个父母position: relative用作绝对放置的"锚".
BTW:position: relative当你设置做作品tbody来display: block.但是你可能会遇到表行宽度的麻烦.通常,子元素不再像表元素那样精确地表现.列已经消失..但它不是这个问题的一部分.
我的问题是:
为什么是position: relative忽略的tbody,tr td?
这种行为有什么理由吗?
这是一个应该修复的错误吗?
.example {
border: 1px solid #ccc;
position: relative;
background: #eee;
margin: 2em;
padding: 2em;
width: 50%;
}
.abs {
display: block;
position: absolute;
left: 100%;
top: 0;
}
table {
//border: 5px solid rgba(255,200,0,0.2);
border-collapse: collapse;
}
tbody {
border: …Run Code Online (Sandbox Code Playgroud)css-position ×10
css ×9
html ×4
css3 ×2
alignment ×1
animation ×1
css-tables ×1
header ×1
html5 ×1
javascript ×1
jquery ×1