简体   繁体   English

调整浏览器大小时可以避免浮动元素移动吗?

[英]Can I avoid float elements shifting when I resize my browser?

I am new to the world of coding as well as CSS and have noticed that when I resize my browser, all my float elements move to say the right and the user is forced to scroll horizontally to view say the menu.我是编码世界的新手以及 CSS 并且注意到当我调整浏览器大小时,我所有的浮动元素都会向右移动,并且用户被迫水平滚动以查看菜单。

Firstly is there a need to prevent float elements not changing their position?首先,是否需要防止浮动元素不改变其 position?

Secondly, is there a way I can avoid this if I should be preventing it?其次,如果我应该阻止它,有没有办法可以避免这种情况?

Floats are a pain in the butt.浮动是一个痛苦的屁股。 Primarily because of the one browser that refuses to follow the universal web standards.主要是因为一个浏览器拒绝遵循通用的 web 标准。 Yeah, that one.是的,那个。

There are a million and one tricks to avoid these problems.有一百万零一个技巧可以避免这些问题。 I tell my team to follow these steps:我告诉我的团队遵循以下步骤:

  1. Use absolutely positioned elements sparingly.谨慎使用绝对定位的元素。 If you position based on another element, things can "auto-adjust" but not so much when things are mathematically glued to one spot.如果您 position 基于另一个元素,则事物可以“自动调整”,但当事物在数学上粘在一个位置时就不会那么多了。 They have their place, just not on every element on a page.它们有自己的位置,只是不是在页面上的每个元素上。
  2. Consider a "reset" or grid-based CSS layout.考虑“重置”或基于网格的 CSS 布局。 I personally prefer 960 Grids They take the guesswork out of the various different browsers.我个人更喜欢960 Grids他们消除了各种不同浏览器的猜测。 Will IE push your padding out? IE会推出你的填充吗? Will the IE-specific padding be too much or too little with Firefox? Firefox 的 IE 特定填充是否过多或过少? With a reset and/or a grid, it's not as big a deal.使用重置和/或网格,这没什么大不了的。
  3. When doing percentages (which I personally really like) remember that browser generally don't use math where 1 + 1 = 2. That is to say, if you have a 1000 pixel space and you do two 50% columns side by side, chances are very good one is going to push itself below the other.在计算百分比时(我个人非常喜欢),请记住浏览器通常不会使用 1 + 1 = 2 的数学运算。也就是说,如果您有 1000 像素空间并且并排执行两个 50% 的列,那么很有可能非常好,一个人会把自己推到另一个人之下。 Why?为什么? Because floats, margins, borders, etc. add up, and all differently in different browsers.因为浮动、边距、边框等加起来,并且在不同的浏览器中都不同。 So, think that.9 +.9 = 2 when you design.所以,在你设计的时候认为.9 +.9 = 2。 Does it work all the time?它一直有效吗? Nope.没有。 But it's a start.但这是一个开始。
  4. Test, test, test.测试,测试,测试。 Things like browsershots, Adobe Browser Labs, etc are your friend. browsershots、Adobe Browser Labs 等是您的朋友。 Use them constantly to ensure you're putting out good quality code that looks consistent across all platforms.经常使用它们以确保您提供在所有平台上看起来一致的高质量代码。
  5. Keep it simple.把事情简单化。 You might be working on a project that dictates 100 different style sheets covering every single version of every browser.您可能正在处理一个项目,该项目规定了 100 个不同的样式表,涵盖了每个浏览器的每个版本。 Most of us work on real-world projects with real-world budgets...that means you have to deploy quickly and efficiently.我们中的大多数人都在使用真实世界的预算从事真实世界的项目……这意味着您必须快速有效地进行部署。 Use good quality w3 compliant code and quality CSS and you can completely avoid hacks.使用符合 w3 标准的优质代码和质量 CSS,您可以完全避免黑客攻击。

I'd suggest you figure out if you want to support differing resolutions, depending on your target audience.我建议您确定是否要支持不同的分辨率,具体取决于您的目标受众。 Elements moving around isn't necessarily a bad thing, and planning in advance how you want your site to layout depending on viewport size could be important.元素四处移动不一定是坏事,根据视口大小提前计划您希望站点如何布局可能很重要。

If you can't find a fixed-width, "one layout to rule them all" that works for your needs, take a look at CSS media queries .如果您找不到适合您需要的固定宽度的“一个布局来统治所有这些”,请查看CSS 媒体查询 For a more practical article on them, try A List Apart有关它们的更实用的文章,请尝试A List Apart

The basic idea is that, using separate stylesheets that depend on size, you can basically display a different layout to the user depending on their viewport.基本思想是,使用取决于大小的单独样式表,您基本上可以根据用户的视口向用户显示不同的布局。 Users resizing their browsers will be able to see the new layout dynamically as well.调整浏览器大小的用户也将能够动态地看到新布局。

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

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