作者:董白菜 | 来源:互联网 | 2024-11-11 11:54
在HTML布局中,即使将`top:0%`和`left:0%`设置为元素的定位属性,浏览器中仍然会出现空白填充。这个问题通常与默认的浏览器样式、盒模型或父元素的定位方式有关。为了消除这些空白,可以考虑重置浏览器的默认样式,确保父元素的定位方式正确,并检查是否有其他CSS规则影响了元素的位置。
Alright this is probably a newbie question but it is very much frustrating me. I clearly say in the style tags that the top blue bar needs to be snug against the top and the two side panels need to be snug against the sides.
好吧,这可能是一个新手问题,但令我非常沮丧。我在风格标签中清楚地说,顶部的蓝色条需要紧贴顶部,两个侧面板需要紧贴侧面。
Yet for some reason it has taken the liberty of inserting a blank white space around my html.
然而出于某种原因,它已经冒昧地在我的html周围插入一个空白空格。
Here is the link: http://popularn.com/nate/error.html
这是链接:http://popularn.com/nate/error.html
See that white space on the left and at the top?
看到左边和顶部的空白区域?
Even when I say top:0% and left:0%, it still doesn't work. It's like it's laughing at me and I've had enough. It's like it is starting the document at top:2% and left:2% and there's nothing I can do...
即使我说顶部:0%而左:0%,它仍然无效。这就像它在嘲笑我,我已经受够了。这就像它在顶部开始文件:2%和左:2%,我无能为力......
3 个解决方案
1
Browsers have a set of default styles which are known as 'User-agent styles'. These are a generic set of CSS rules that it applies to elements. You know when you put a H1 in a page, and it appears big, and in bold? These are those styles.
浏览器具有一组默认样式,称为“用户代理样式”。这些是适用于元素的一组通用CSS规则。你知道什么时候你把H1放在一个页面里,它看起来很大,而且是粗体?这些是那些风格。
The base elements in your pages are all styled with these UA rules. Body, HTML, div, etc - they all have a small amount of padding on them, which is where this is coming from.
页面中的基本元素都使用这些UA规则进行样式设置。正文,HTML,div等 - 它们都有少量的填充,这就是它的来源。
Consequently, it's good practice to always use a CSS reset, when you are developing beyond basic styles. There's a couple of good ones I'd recommend. As CSS is hierarchical (hence cascading!) you need to include resets first.
因此,当您开发超出基本样式时,最好始终使用CSS重置。我建议有几个好的。由于CSS是分层的(因此级联!),您需要先包含重置。
Firstly is Eric Meyer's CSS reset. This applies generally to everything, and is invisible for most purposes. You include the file, everything gets reset to base.
首先是Eric Meyer的CSS重置。这通常适用于所有事物,并且对于大多数目的而言是不可见的。您包含该文件,所有内容都将重置为base。
Secondly is Yahoo UI 3 (YUI) reset, which takes a slightly different approach. They let you selectively apply a reset to different areas of a page by including a class. This is useful for some things, but for almost every small/medium sized project I'd recommend Eric's reset linked above - but it's useful for comparison and learning.
其次是Yahoo UI 3(YUI)重置,这需要稍微不同的方法。它们允许您通过包含类来有选择地将重置应用于页面的不同区域。这对某些事情很有用,但对于几乎所有中小型项目,我都建议将Eric重置在上面 - 但它对比较和学习很有用。
Instead of trying to tune out inconsistencies as you go along - using a CSS reset will give you a baseline for all elements which is the same on every browser. Believe me - you want this. When you get further into html, forms for example or fun stuff like that, then this kind of thing is an absolute life saver.
而不是试图在你进行时调整不一致性 - 使用CSS重置将为每个浏览器上的所有元素提供相同的基线。相信我 - 你想要这个。当你进一步进入html,例如表格或类似的有趣的东西,那么这种事情是绝对的生命保护。
Hope that helps!
希望有所帮助!