简体   繁体   English

调整浏览器大小时CSS边框会影响宽度

[英]CSS border effecting width when resizing browser

I made two simple navigation menu using ul and li . 我使用ulli做了两个简单的导航菜单。 One without border and another with border. 一个没有边界,另一个有边界。 The width of the both menu is fixed with 400px. 两个菜单的宽度都固定为400px。

In ther first menu I gave each li a fixed width of 100px and in the second menu according the width calculation I gave fixed width of 98px and 1px border on left and right which in total is 100px for each li . 在第一个菜单中,我给每个li固定宽度100px,在第二个菜单中,根据宽度计算,我给定li固定宽度98px和左右边框1px,每个li总计100px。

Now my question is when I am resizing the the browser resolution using the "Ctrl + -" the first menu stays as it is in any resolution but in the second menu the last element is shifting to the next line under the first element. 现在我的问题是,当我使用“ Ctrl +-”调整浏览器分辨率的大小时,第一个菜单将保持不变,但在第二个菜单中,最后一个元素将移至第一个元素下的下一行。

I want to know why this happens and how to resolve it. 我想知道为什么会这样以及如何解决。 Thanks in advance. 提前致谢。

Here is the JSFiddle Link : http://jsfiddle.net/jhz56/ 这是JSFiddle链接: http : //jsfiddle.net/jhz56/

The border width doesn't scale together with the li width, that's why the menu breaks. 边框宽度不会与li宽度一起缩放,这就是菜单中断的原因。 So one solution can simply be: 因此,一种解决方案可以是:

.nav2 ul li {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

http://jsfiddle.net/jhz56/1/ http://jsfiddle.net/jhz56/1/

This issue is simple. 这个问题很简单。 You are saying you have set the width using px , which will also change once you are changing the screen size, so to minimize this error. 您说的是您使用px设置了宽度,更改屏幕大小后也会更改px ,以最大程度地减少此错误。 I will recommend using % so that in every screen size, it should always have its own width and height 我建议使用%以便在每个屏幕尺寸中,它都应始终具有自己的宽度和高度

This way, it won't come out of its box. 这样,它就不会开箱即用。 I have checked your fiddle, both of the lists were on their place no matter how much zooming. 我检查了你的小提琴,无论放大多少,两个列表都在它们的位置上。

Second thing that you can is to use Media Query to make sure that the with each screen size, the element gets a new style to fir perfectly to the screen size and the needs. 您可以做的第二件事是使用Media Query确保每个屏幕尺寸的元素都具有一种新样式,可以完美地满足屏幕尺寸和需求。

When you press Ctrl with + , the elements get a zoom and are more likely to take more pixels from the browser. 当您使用+按下Ctrl时,元素将缩放,并且更有可能从浏览器中获取更多像素。 Which makes it go a block downwards to fit to the width provided, when you press - the element shrinks and goes near to the left li . 按下时,它会向下移动一个块以适合提供的宽度-元素收缩并靠近左侧li This way, their properties shift. 这样,它们的属性就会发生变化。

Here is a fiddle, http://jsfiddle.net/afzaal_ahmad_zeeshan/7SbLR/ When you try zooming in or out, the element will change its widths and all that other properties. 这是一个小提琴, http://jsfiddle.net/afzaal_ahmad_zeeshan/7SbLR/尝试放大或缩小时,元素将更改其宽度以及所有其他属性。

You can use min-width and max-width instead of width . 您可以使用min-widthmax-width代替width To make sure, it always gets the total width as its min and max. 为了确保这一点,总会得到总宽度的最小值和最大值。

div {
  max-width: 500px;
  min-width: 500px;
  width: 500px; 
}

http://jsfiddle.net/afzaal_ahmad_zeeshan/7SbLR/1/ In this fiddle, slight zooming in and out won't cause an error in the UI, but it doesn't work for alot of zooming (250%+). http://jsfiddle.net/afzaal_ahmad_zeeshan/7SbLR/1/在这个小提琴中,轻微的放大和缩小不会在UI中引起错误,但不适用于大量缩放(超过250%)。 So here media query will be the option. 因此,这里可以选择媒体查询。

This way, even if you zoom in or out, the element will always have a width of 500px . 这样,即使您放大或缩小,元素也将始终具有500px的宽度。 This might be handy for you! 这可能对您来说很方便!

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

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