CSS 定位边距问题
我不确定如何使用 css 定位元素,因为当我使用如下方法时,每当我调整浏览器大小时,元素都会保留在同一位置,而不是我希望它们位于调整大小的文档上的位置。请你告诉我我做错了什么吗?
.logo {
left: 20px;
top: 20px;
position: absolute;
}
#header h1 {
margin-top: 20px;
margin-left: 500px;
color: rgb(127, 127, 126);
line-height: 0px;
}
请拉一把小提琴 - http://jsfiddle.net/hHGRc/
I am unsure how to position elements using css, as when I use methods like the following, whenever I resize the browser, the elements stay in the same place instead of where I would like them to be on the resized document. Please can you tell me what I am doing wrong?!
.logo {
left: 20px;
top: 20px;
position: absolute;
}
#header h1 {
margin-top: 20px;
margin-left: 500px;
color: rgb(127, 127, 126);
line-height: 0px;
}
Please, have a fiddle - http://jsfiddle.net/hHGRc/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 (X)HTML DOM 中,CSS 识别四种定位类型。默认情况下,HTML 中的每个元素都是静态定位的。这意味着它根据它在正常流程中出现的位置进行定位。
相对定位
当对象相对定位时,意味着它根据原点修改位置,即它在正常流(静态)中的定位位置。然而,Relative 还做了一些其他特殊的事情,并告诉浏览器其所有子元素将根据此元素定位,无论使用相对还是绝对。
绝对定位
当对象被
绝对
定位时,它会根据其最近的非静态
定位祖先的位置进行放置。如果没有,则它使用来确定其位置。如果它的同级或祖先没有
绝对
定位,这有可能破坏文档流。如果从最外层顶部节点到当前节点的所有节点都被绝对定位,那么它将保持流程。固定定位
这会将元素从流中取出并根据 Window 对象定位该对象。这意味着无论文档的滚动状态、大小或任何其他属性如何,它都将始终出现在该位置。 (这就是获得随你滚动的对象的方法)。
针对您的问题提供多种解决方案
首先,正如其他人所描述的,您可以将
position:relative
添加到#header
中。正如上面所解释的,它将使您的标头成为最近的非静态
祖先,并将使用它和确定位置的基础。这对您来说可能并不理想,因为您是一名公认的新手,而这个absolute
可能很容易破坏足够的流程,使您可能会在处理同级元素时遇到困难。作为替代方案,您可以将徽标从
position:absolute
更改为position:relative
。这将使您的徽标保留在流程中,但根据徽标在文档流程中自然出现的位置移动徽标。除非您在 #header 中使用浮动,否则这可能就是您想要的,因为它 a) 保持流程,b) 允许使用子元素floats
而不丢失流程,c)实现您的理想定位,d) 保留父元素的继承(当它很重要时)。另一种选择是将
#header
更改为position:absolute
。但是,这可能会改变一切交互的方式,除非您将所有父元素和同级元素更改为position:absolute
。此外,您可能无法访问祖先定义的宽度和高度,因为它们只有在同一流中时才会被继承。这是对您来说第二好的情况,因为您可以简单地添加规则body * {position:absolute; }
并且一切都将与您保持一致。然而,它忽略了真正教你需要学习的关于定位的知识,而只是一个拐杖。希望这有帮助,
模糊逻辑
Within the (X)HTML DOM, CSS recognizes four types of positioning. By default, every element in HTML is positioned Statically. This means that it is positioned according to the place that it appears in the normal flow.
Relative Positioning
When an object is positioned
relative
, it means that it modifies the position based on the origin, which is where it would have been positioned in the normal flow (static). Relative also does something else special, however, and tells the browser that all of its children will be positioned according to this element, whether using relative or absolute.Absolute Positioning
When an object is positioned
absolute
, it is placed according to the position of its nearest non-static
positioned ancestor. If there is not one, then it uses the<body>
to determine its position. This has the potential to break document flow, if its siblings or ancestors are not positionedabsolute
. If all are positionedabsolute
from the outer most top node to current node, then it will maintain the flow.Fixed Positioning
This takes the element out of the flow and positions the object according to the Window object. This means that no matter the scroll state of the document, its size or any other property, it will always appear in that location. (This is how you get objects that scroll with you).
Multiple solutions to your issue
First, as described by others, you may add
position:relative
to the#header
. It will, like explained above, make your header the nearest non-static
ancestor and will use it and the basis for determining position. This is probably not ideal for you because you are an admitted novice and this oneabsolute
could easily break enough flow that you may struggle with sibling elements.As an alternative, you may change the logo from
position:absolute
toposition:relative
. This will keep your logo in the flow, but move the logo according to where it appears naturally in your document flow. Chances are that unless you are using floats in your #header, this is probably the one you want, as it a) keeps flow, b) allows for use of child elementfloats
without losing flow, c) achieves your ideal positioning, d) keeps inheritance from parent elements (when it is important).Another choice is to change the
#header
toposition:absolute
. This may alter the way everything interacts, however, unless you change all of your parent and sibling elements toposition:absolute
. Additionally, you may lose access to ancestor defined widths and heights, as they are only inherited if they are in the same flow. This is the 2nd best situation for you as you can simply add the rulebody * { position:absolute; }
and all will remain in the flow with you. However, it neglects to really teach you the things you need to learn about positioning and will simply be a crutch.Hope this helps,
FuzzicalLogic
在CSS中定义position:absolute会将相关元素从文档流中剔除。
将其视为层:最底层是文档(尽管并不总是如此,具体取决于 z-index!),最顶层是您定义为绝对定位的元素。
通过设置position:absolute,您已经告诉浏览器您将负责相对于文档(屏幕)的左上角定位元素。在上面,您已告诉浏览器将 #logo 放置在距离文档左侧 20 像素和距离顶部 20 像素的位置。当您调整浏览器视口的大小时,该元素将保留在该位置。
我认为您想要的是将元素定位在文档流中,而不使用绝对定位。这可以通过浮动、边距和填充的组合来实现。
Defining position: absolute in CSS takes the element in question out of the flow of the document.
Think of this as layers: the bottom most layer is the document (though not always, depending on z-index!), and the top most layer is your element which you have defined as absolutely positioned.
By setting position: absolute, you have told the browser that you will be responsible for positioning the element relative to the top left corner of the document (screen). Above, you have told the browser to position #logo 20px from the left and 20px from the top of the document. When you resize your browser viewport, that element will remain in that position.
I think what you want is to position your element within the document flow, without using absolute positioning. This can be achieved with a combination of floats, margins, and padding.
CSS 定位可能很难正确理解,但一旦理解了,您就会发现它非常有用。
试试这个: http://www.barelyfitz.com/screencast/html-training /css/positioning/
基本上,要定位需要锁定到父元素或容器元素的任何内容,父元素或容器元素本身也需要定位(绝对或相对,并不重要)这就是所谓的定位 语境。如果绝对定位的元素找不到定位自身的父元素或容器,它将使用“body”作为定位上下文。
因此,在您的示例中,如果我要在没有看到您的 HTML 和更多 CSS 的情况下进行猜测,
则将
position:relative
添加到#header
将允许.logo
将其自身绝对定位在#header
元素的左上角。同样重要的是要记住,绝对定位会使元素脱离文档的正常流程。
CSS positioning can be tricky to understand correctly, but once you do, you'll find it very useful.
Try this: http://www.barelyfitz.com/screencast/html-training/css/positioning/
Basically, to position anything that needs to be locked to a parent or a container element, the parent or container element itself needs to be positioned as well (absolute, or relative, doesn't matter) this is called positioning context. If an absolutely positioned element cannot find a parent or container that is positioning itself, it will then use the `body as the positioning context.
So in your example, if i were to to guess without seeing your HTML and more of your CSS,
adding
position:relative
to#header
would then allow.logo
to position itself absolutely from the top left of the#header
element.Also important to remember that absolute positioning takes the element out of the normal flow of the document.
我将进行大胆的猜测,并说您的徽标位于标题部分内,但您的标题是静态定位的。因此,您的徽标不是根据标题定位的,而是根据文档窗口定位的。因此,在所有情况下,它将到达距离文档左上角向右 20 像素、向下 20 像素的位置。
尝试在
#header
元素上设置position:relative
。这将使标题保持在其始终出现的位置,并且徽标将使用标题框来查找其左侧和顶部位置,而不是浏览器窗口。I'm going with a wild guess and saying that your logo is inside the header division, but your header is positioned staticly. Therefore, your logo is not being positioned according to the header, but according to the document's window. So it will be going to a position that is 20px right and 20px down from the top left corner of the document in all instances.
Try setting
position: relative
on your#header
element. This will keep the header in the same place as it would always appear, and the logo will use the header box to find it's left and top positions rather than the browser window.