简体   繁体   English

CSS 相对于固定/绝对定位

[英]CSS positioning relative over fixed/absolute

Apologies if I appear quite "noobish" with CSS.抱歉,如果我对 CSS 显得相当“笨拙”。 I've been trying to set the following...我一直在尝试设置以下...

#0 {
 width: 100%;
 height: y;
 border: 1px solid black;
}   
#a {
 position: fixed;
 float left;
 width: x;
 height: y;
 border: 1px solid black;
}
#b {
 position: relative;
 float: left;
 width: x;
 height: y;
 border-right: 1px solid black;
}

/* HTML */
<div id="0">Some div...</div>
<div id="a">Another div</div>
<div id="b">Final div...</div>

For some reason, if I attempt to position #b below #a, #b will appear ontop of #a unless I declare its position as either static or absolute, but will then be required to manually position the top/left properties, and this doesn't display properly for all browsers either. For some reason, if I attempt to position #b below #a, #b will appear ontop of #a unless I declare its position as either static or absolute, but will then be required to manually position the top/left properties, and this对于所有浏览器也不能正确显示。 Any help would be greatly appreciated!任何帮助将不胜感激!

Have got it fixed now, (had to declare html: body:( width; 95%;)) Thanks for all the advice guys!现在已经修好了,(必须声明 html: body:(width; 95%;))谢谢大家的建议!

First of all, a couple of fixes: #0 needs to be named something else, because IDs cannot start with a number.首先,修复几个问题:#0 需要命名为其他名称,因为 ID 不能以数字开头。 You're also missing a: in the float property for #a.您还缺少 a: 在 #a 的 float 属性中。 Not sure what's with the 'x' and 'y' for the height/width, either - I assume those are just examples?也不确定高度/宽度的“x”和“y”是什么 - 我认为这些只是示例?

Fixed and Absolute elements are out of the document flow. Fixed 和 Absolute 元素不在文档流中。 That is, they don't take up space as far as normal positioned elements.也就是说,它们不会像正常定位的元素那样占用空间。

Therefore, the fixed element will have the relative one over it in your example, as you found because they can inhabit the same xy space.因此,在您的示例中,固定元素将具有相对元素,正如您所发现的,因为它们可以位于相同的 xy 空间中。 If you had it as absolute, top:0;如果你认为它是绝对的,top:0; left: 0, the same thing would happen.左:0,同样的事情会发生。

Next, you had one as a float (almost), so let's consider that float changes all of that positioning.接下来,你有一个作为浮动(几乎),所以让我们考虑浮动改变了所有的定位。 Floats are 'special' in the way they are laid out.花车的布局方式是“特殊的”。 They are in the flow, but will be as far up and in the float direction as possible.它们在流动中,但会尽可能地向上并在浮动方向上。 If they're too wide to fit next to other floated content on that line, they go to the next line.如果它们太宽而无法与该行上的其他浮动内容相邻,则它们 go 到下一行。

You could do你可以做

<style>
 #a
  {
  float:left;
  height:100px;
  width:150px;
  background-color:black;
  }
  #b
  {
  clear:left;
  height:100px;
  width:150px;
  background-color:green;
  }
 </style>
 <div id='a'>aaaaa aaa</div>
 <div id='b'>bbb bbb</div>

'Clear' means an element will appear beneath preceding elements that are floated. “清除”意味着一个元素将出现在浮动的前面元素的下方。 #b will be on the next line, underneath #a. #b 将在下一行,在#a 下方。 You could also make #a have a big margin on the right, or be wide enough to fill whatever container and not leave room for #b, to make #b be beneath #a instead of next to it.您还可以使#a 在右侧有一个很大的边距,或者足够宽以填充任何容器并且不为#b 留出空间,以使#b 在#a 下方而不是在它旁边。

I don't believe your width: x and height: y syntax is correct, and your #a float property is missing a colon.我不相信您的width: xheight: y语法是正确的,并且您的#a float 属性缺少冒号。 Also, IDs can't start with numbers, so #0 should be changed to something else.此外,ID 不能以数字开头,因此应将#0更改为其他内容。 Here is the code that makes everything work right.这是使一切正常的代码。 You had to get rid of your fixed and relative positioning.你必须摆脱你的固定和相对定位。 CSS: CSS:

#zero {
 width: 100%;
 border: 1px solid black;
}   
#a {
 float: left;
 border: 1px solid black;
}
#b {
 float: left;
 border-right: 1px solid black;
}

And the HTML:和 HTML:

<div id="zero">Some div...</div>
<div id="a">Another div</div>
<div id="b">Final div...</div>

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

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