简体   繁体   English

用css计算内联块元素的空间

[英]calculate space for inline-block elements with css

I'm trying to set up a website header/navigation bar. 我正在尝试设置网站标题/导航栏。 On the left side I have a logo area, on the right side are a few buttons/links, and in the middle, I have a filler empty space so the logo gets pushed to the left, and the links get pushed to the right. 在左侧我有一个徽标区域,右侧是几个按钮/链接,在中间,我有一个填充空的空间,所以徽标被推到左边,链接被推到右边。

Basically I have everything as inline-block elements, inside a parent who has text-align: center . 基本上我将所有内容都作为inline-block元素,在具有text-align: center的父级内部。 This way they are centered on the screen, but still expand to the left and right. 这样,它们以屏幕为中心,但仍然向左和向右扩展。 Also, everything is inside of a div that is 90% of the full width of the screen, so there is some extra space on the sides. 此外,所有内容都在div内部, 90%整个屏幕宽度的90% ,因此侧面有一些额外的空间。

Here's a fiddle to demo what I have 这是演示我所拥有的东西的小提琴

The problem is, when the window is really big, the logo isn't far enough to the left and the links aren't far enough to the right (I want them to basically be right on the far edges of the 90% width centered parent). 问题是,当窗口非常大时,徽标离左边不够远,链接距离不够远(我希望它们基本上位于90%宽度的中心边缘上方父母)。 However, when the window gets small, its perfect (especially before my media query comes into effect). 但是,当窗口变小时,它就完美了(特别是在我的媒体查询生效之前)。

I've used 50% width as the filler space in the middle, but I need a larger space when the window gets bigger (maybe like 80% filler space), is there a way to make the width % for the empty space get bigger as the window resizes with just css (and without like 20 media queries for every 100 pixels size difference)? 我在中间使用了50%的宽度作为填充空间,但是当窗口变大(可能是80%填充空间)时我需要更大的空间,是否有办法使空白空间的width %变得更大因为窗口只用css调整大小(并且每100个像素大小差异没有20个媒体查询)?

If the logo and links had set width properties, I can use calc() but they don't, and I'd like to avoid settings static widths 如果徽标和链接设置了width属性,我可以使用calc()但它们没有,我想避免设置静态宽度

Also, I don't want to use floats!!! 另外,我不想使用花车!

I sense you are looking for display:table / table-cell rather than inline-block 我觉得你正在寻找显示:table / table-cell而不是inline-block

JSfiddle Demo JSfiddle演示

 #bar { font-family: arial; background: #eee; } .space { width: 50%; background: blue; height: 3px; } .text-padding { padding: 10px 0; } .text-align-center { text-align: center; } li { display: inline-block; } .centered { width: 90%; margin: 0 auto; background: tomato; display: table; } .centered div { display: table-cell; } ul { margin: 0; padding: 0; list-style: none; } @media (max-width: 330px) { .logo-text { display: block; } .navigation { display: block; } .li { display: block; } .space { display: none; } } 
 <div id='bar'> <div class='centered text-align-center'> <div class='text-padding logo-text'> Logo </div> <div class='space'></div> <div class='navigation'> <ul> <li class='li text-padding'> one </li> <li class='li text-padding'> two </li> <li class='li text-padding'> three </li> </ul> </div> </div> </div> 

* Edited to provide a solution without floats * *编辑提供没有花车的解决方案*

You could just add another media query to adjust the spacer size when the screen width allows: 当屏幕宽度允许时,您可以添加另一个媒体查询来调整间隔尺寸:

@media (min-width: 800px) {
  .space {
    width: 70%;
  }  
}

https://jsfiddle.net/u93wwLvv/8/ https://jsfiddle.net/u93wwLvv/8/

Add as many media queries as you like. 添加任意数量的媒体查询。

There are several ways to do that. 有几种方法可以做到这一点。 float , display: table / table-cell , and certainly flex . floatdisplay: table / table-cell ,当然还有flex

Flex is new, but is most likely the future, and its current version is a lot more standard than the previous ones. Flex是新的,但很可能是未来,它的当前版本比以前的版本更标准。

Check out the running demo created with the FlexyBoxes tool: 查看使用FlexyBoxes工具创建的运行演示:

Running demo 运行演示

Created with basically only the flex settings, the white-space: nowrap on the <ul> and the flex-container{ display: inline-block; } 基本上只创建了flex设置, <ul>上的white-space: nowrapflex-container{ display: inline-block; } flex-container{ display: inline-block; } in the media query for low resolution. flex-container{ display: inline-block; }在低分辨率媒体查询。

Also check out this potentially interesting answer. 另请参阅这个可能有趣的答案。

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

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