简体   繁体   English

为Chrome中的最后对齐内联块元素添加了额外空间

[英]Extra space added to last justified inline-block element in Chrome

I've come across a slight issue that appears to only occur in Chrome (tested and OK in IE10 and FF 31). 我遇到了一个似乎只出现在Chrome中的小问题(在IE10和FF 31中经过测试和确定)。

In the example provided there is #message and #link , both are set to display: inline-block; 在提供的示例中,有#message#link ,两者都设置为display: inline-block; so that they can be vertically aligned to the middle of each other (the text in #message could vary in length greatly). 这样它们就可以垂直对齐到彼此的中间( #message的文字长度可能会有很大变化)。

text-align: justify; has been set on #container to ensure that #message is aligned to the left and #link to the right. 已设置在#container以确保#message与左边对齐, #link与右边对齐。

The issue is that at certain window sizes a small "space" will appear to the right of #link . 问题是在某些窗口大小的#link右侧会出现一个小的“空格”。

The problem: 问题:

额外填充应用于确定按钮

What it should look like: 应该是什么样的: 没有额外的填充应用于确定按钮

What I am actually trying to achieve: 我实际上想要实现的目标: 将在网站上使用的实际设计

If you view the fiddle and can't see the problem try re-sizing the window. 如果您查看小提琴并且看不到问题,请尝试重新调整窗口大小。

  1. What is causing this issue in Chrome? 导致Chrome出现此问题的原因是什么?
  2. Is there any way to fix this without resorting to using floats (I would like to keep vertical alignment)? 有没有办法解决这个问题而不使用浮动(我想保持垂直对齐)?

JS Fiddle: JS小提琴:

http://jsfiddle.net/vvubdqkk/ http://jsfiddle.net/vvubdqkk/

HTML: HTML:

<div id="container">
    <div id="message">Lorem 1. Ipsum dolor sit amet, consectetur adipiscing elit. Aliquam bibendum gravida tincidunt.</div> 
    <a id="link" href="#">OK</a>
    <div id="info">Lorem 2. Ipsum dolor sit amet, consectetur adipiscing elit. Aliquam bibendum gravida tincidunt.</div>
</div>

CSS: CSS:

#container {
    background-color: red;
    bottom: 0;
    left: 0;
    margin: 10px 5%;
    position: fixed;
    text-align: justify;
    width: 90%;
}
#message {
    color: #FFFFFF;
    display: inline-block;
    max-width: 80%;
    vertical-align: middle;
}
#link {
    background-color: #FFFFFF;
    color: #000000;
    display: inline-block;
    padding: 1em;
}
#info {
    background-color: green;
    color: #FFFFFF;
    display: inline-block;
    margin: 0;
    width: 100%;
}

The overall issue is you are styling #container similar to how you should instead be styling #message. 整体问题是你的样式#container类似于你应该如何设计样式#message。 #Container should simply be an imaginary holder/container of #message, #link, #info. #Container应该只是#message,#link,#info的虚构持有者/容器。

Try getting rid of the #container background color red and instead add it to #message. 尝试删除#container背景颜色红色,而是将其添加到#message。 After doing that you'll encounter a few padding issues with your link (I deleted the padding:1em). 执行此操作后,您的链接会遇到一些填充问题(我删除了填充:1em)。 Next, you can adjust the width % of #message to get the spacing right. 接下来,您可以调整#message的宽度%以获得正确的间距。 You'll notice that I deleted width:90% on your #container. 您会注意到我删除了#container的宽度:90%。

#container {
   bottom: 0;
   left: 0;
   margin: 10px 5%;
   position: fixed;
   text-align: justify;
}
#message {
    background-color: red;
    color: #FFFFFF;
    display: inline-block;
    max-width: 90%;
    vertical-align: middle;
}
#link {
    background-color: #FFFFFF;
    color: #000000;
    display: inline-block;
}
#info {
    background-color: green;
    color: #FFFFFF;
    display: inline-block;
    margin: 0;
    width: 100%;
}

It seems like the issue is caused by rounding off errors. 似乎问题是由四舍五入的错误引起的。 The page seems to work ie there is no red gap on certain screen widths. 页面似乎工作,即某些屏幕宽度没有红色间隙。 You can test by re-sizing the window one pixel at a time. 您可以通过一次重新调整窗口大小来测试。 The appearance of red gap seems to be a function of container's width. 红色间隙的出现似乎是容器宽度的函数。

Here is my workaround: 这是我的解决方法:

jsFiddle Demo jsFiddle演示

It uses an extra div plus two vertical align techniques: 它使用额外的div加上两个垂直对齐技术:

  • The message is vertically aligned using mixed line-heights technique 使用混合线高技术垂直对齐消息
  • The button is vertically aligned using absolute positioning 按钮使用绝对定位垂直对齐

The CSS (revised 8/26/2014): CSS(2014年8月26日修订):

#container {
    color: #FFFFFF;
    background-color: #FF0000;
    position: fixed;
    left: 5%;
    right: 5%;
    bottom: 10px;
}
#tempwrap {
    line-height: 3; /* sets the _outer_ line height of #message as well as height of #link */
    position: relative; /* for positioning #link */
}
#message {
    background: rgba(255, 255, 255, .5);
    line-height: normal; /* the _inner_ line height */
    max-width: 80%; /* room for #link */
    display: inline-block;
    vertical-align: middle;
}
#link {
    color: #000000;
    background-color: #FFFFFF;
    padding-left: 1em;
    padding-right: 1em;
    position: absolute;
    right: 0;
    top: 50%; /* top aligns with middle of parent */
    margin-top: -1.5em; /* the height is 3em so push 3/2em upwards */
}
#info {
    background-color: #008000;
}

MY CODEPEN 我的CODEPEN

For my test, i have using : 对于我的测试,我使用:

  • Position absolute to fix the vertical align of #link and i fix his size 定位绝对以修复#link的垂直对齐,我修复了他的大小
  • I modified the html struture 我修改了html struture
  • I modified width to compare with your picture. 我修改了宽度以与你的图片进行比较。

HTML HTML

<div id="container">
  <div id="inner_top">
      <div id="message">Lorem 1. Ipsum dolor sit amet, consectetur adipiscing elit. Aliquam bibendum gravida tincidunt. Lorem 1. Aliquam bibendum gravida.nnnnn<br/><p style="color:yellow;text-align:right;margin:0">Read more</p></div> 
      <div id="link"><a  href="#">OK</a></div>
  </div>
  <div id="info">
        Lorem 2. Ipsum dolor sit amet, consectetur adipiscinngg elit. Aliquam bnbibendum       gravidda tinciduntt.
  </div>
</div>

CSS CSS

#container {
    background-color: rgba(0, 0, 0, 0.8);
    bottom: 0;
    left: 0;    
    position: fixed;
    text-align: justify;
    width: 100%;
}
#message {
    color: #FFFFFF;
    position:relative;
    max-width: 80%;
    vertical-align: middle;
    padding-top:1em;
    padding-bottom:1em;

}
#link {
    position:absolute;top:50%;right:0;
    background-color: #FFFFFF;
    color: #000000;
    margin-top:-25px;
    width:50px;
    height:50px;
}
#link a{
  color: #000000;
  position:relative;
  height:15px;
  display:block;
  padding-top:15px;
  text-align:center
}

#inner_top{position:relative;width: 90%; margin: 10px 5%;}

#info {

    color: #FFFFFF;   
    margin: 0;
    width: 90%;
    height:200px;
    margin: 10px 5%;
    overflow-y:auto;

}

SIMPLE & BEST HACK - TRY DEMO 简单而最好的黑客 - 尝试演示

Note: margin = -0.5px and transform = 0.99px doesn't apply any extra margin or width to your div#container and that also doesn't force the a#link to move or push the Next Pixel. 注意: margin = -0.5pxtransform = 0.99px不会对div#container应用任何额外的边距或宽度,也不会强制transform = 0.99px a#link移动或推送Next Pixel。

Tested: Chrome 36 and Safari 5.1.7 测试: Chrome 36和Safari 5.1.7

#link {
    -webkit-margin-start: -0.5px;
    -webkit-margin-end: -0.5px;
    -webkit-transform: translate(0.99px, 0px);
}

/* With Your CSS */
...................

This case is a perfect candidate for using the flexbox layout. 这种情况是使用flexbox布局的完美候选者。 You can keep your existing code as is but add the following lines. 您可以保留现有代码,但添加以下行。 This will keep your original code as a fallback for browsers which don't support flexbox. 这将使您的原始代码保留为不支持flexbox的浏览器的后备。 Since Chrome does support the current flexbox syntax all the way back to version 21 , this should safely eliminate your problem. 由于Chrome确实支持当前的flexbox语法,直到版本21 ,这应该可以安全地消除您的问题。

Codepen Demo Codepen演示

Modified CSS 修改CSS

#container {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
}

#message {
  flex: 1;
}

You'll need to vendor prefix the code for comprehensive browser support, but since you're only worried about the bug in Chrome this is not strictly necessary (unprefixed support goes back to version 29, although version 27 still holds .54% of global market share, so you might throw -webkit- in to be on the safe side). 您需要为代码提供前缀,以获得全面的浏览器支持,但由于您只担心Chrome中的错误,因此这并不是绝对必要的(未加固定的支持可以追溯到版本29,尽管版本27仍然占据全球.54%市场份额,所以你可能会把-webkit-放在安全的一边)。

Since flexbox can be a little confusing to use at first, if you haven't used it before there is a good overview with examples at CSS-Tricks. 因为一开始使用flexbox会有点混乱,如果你没有使用过它,那么在CSS-Tricks的例子中有一个很好的概述。 I don't have enough reputation points to post more than two links but just Google "css tricks flexbox". 我没有足够的声望点来发布超过两个链接,但只有谷歌“css技巧flexbox”。

Hope this helps. 希望这可以帮助。

You can try the these tricks which may help you. 您可以尝试这些可能对您有帮助的技巧

Using float : right to your #link also do the trick. 使用float : right#link也可以做到。

It's a bit of a hack but adding margin-right: -1px; 这有点像黑客但增加了保证金权利:-1px; seems to fix the issue for me: 似乎为我解决了这个问题:

#link {
    background-color: #FFFFFF;
    color: #000000;
    display: inline-block;
    padding: 1em;
    margin-right: -1px;
}

Problem is this will push the box right by 1 pixel in all other browsers. 问题是这会在所有其他浏览器中将框右移1个像素。

Edit: Setting overflow:hidden in the container div may resolve this, though. 编辑:设置溢出:隐藏在容器div中可能会解决此问题。

I think that I just might have the solution to your problem. 我想我可能已经解决了你的问题。 I've been fiddling around with display: inline-block quite alot myself and now and then encountered some margin issues in both the webkit browsers, in my case, mostly Safari though. 我一直在摆弄显示:inline-block很多我自己现在然后在webkit浏览器中遇到一些边缘问题,在我的情况下,主要是Safari。 However, what usually does the trick for me is simply to set the font-size of the parent div to 0, and then reset the font size of the child divs to their respective original font sizes, in pixels mind you. 但是,对我来说通常的诀窍就是将父div的字体大小设置为0,然后将子div的字体大小重置为各自的原始字体大小(以像素为单位)。

#container {
...
font-size: 0;
}

#message, #link, #info {
font-size: 16px;
}

Modified JS Fiddle: 修改JS小提琴:

http://jsfiddle.net/vvubdqkk/9/ http://jsfiddle.net/vvubdqkk/9/

giving margin-right to -2px will work check fiddle , I have tested by resizing as well. 给予-2px的保证金权利将起作用检查小提琴 ,我已通过调整大小进行测试。

CSS CSS

#link {
background-color: #FFFFFF;
color: #000000;
display: inline-block;
padding: 1em;
margin-right:-2px;
}

Sometime we need to do weird things in order to make things work 有时我们需要做一些奇怪的事情,以使事情有效

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

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