简体   繁体   English

内联块位置不符合预期(CSS)

[英]Inline-block position not as expected (CSS)

for a bit of fun I decided to make a sliding shelf today. 为了找点乐子,我决定今天做一个滑动架子。 I got it looking and working how I wanted and then decided to put some content in the cards. 我开始寻找并按照自己的意愿进行工作,然后决定在卡中放入一些内容。

I added text to the first card and it moved down almost on to a new line. 我在第一张卡上添加了文字,然后几乎向下移动到了新行。 I can't explain why this happened (though I'm sure there's a simple explanation). 我无法解释为什么会这样(尽管我敢肯定有一个简单的解释)。 Can one of you tell me what I'm missing, please? 可以告诉我我所缺少的吗?

Thank you 🙂 谢谢🙂

 function hideTrigger() { leftTrig.removeAttribute("hidden"); rightTrig.removeAttribute("hidden"); switch (pos) { case 0: leftTrig.setAttribute("hidden", ""); break; case posMax: rightTrig.setAttribute("hidden", "") } } function moveHelper() { boxesCont.style.transform = slideHelper(), hideTrigger(), setTimeout(function() { end = boxCont[posMax].getBoundingClientRect().left <= window.innerWidth ? 1 : 0 }, 300) } function slideHelper() { return "translate(-" + boxSize * pos + "px)" } function moveRight() { pos < posMax && (end ? endHelper() : (pos++, moveHelper())) } function moveLeft() { pos > 0 && (pos--, moveHelper()) } function moveTo(e) { e >= 0 && e <= posMax && (pos = e, moveHelper()) } function endHelper() { pos++; let edgeDif = boxSize - boxMargin - (window.innerWidth - boxCont[posMax].getBoundingClientRect().left); rightTrig.setAttribute("hidden", ""), boxesCont.style.transform = "translate(-" + (boxSize * (pos - 1) + edgeDif) + "px)" } var leftTrig = document.querySelector(".directional.left"); var rightTrig = document.querySelector(".directional.right"); var boxesCont = document.querySelector(".shelf .boxes"); var boxCont = boxesCont.querySelectorAll(".box"); var boxStyle = boxCont[0].currentStyle || window.getComputedStyle(boxCont[0]); var boxMargin = parseFloat(boxStyle.marginLeft); var boxSize = boxCont[0].offsetWidth + 2 * boxMargin; var end = 0; var pos = 0; var posMax = boxCont.length - 1; leftTrig.addEventListener("click", function() { moveLeft() }); rightTrig.addEventListener("click", function() { moveRight() }); moveHelper(); 
 body { margin: 0; font-family: Roboto; text-align: justify; } .shelf { position: relative; overflow-x: hidden; font-size: 0; } .shelf button.directional { position: absolute; transition: opacity 0.3s cubic-bezier(.25, .8, .25, 1); height: 100%; width: 30px; top: 0; opacity: 0; border: 0; border-radius: 0; background: rgba(55, 71, 79, 0.4); color: #F5F5F5; cursor: pointer; z-index: 9999; } .shelf button.directional.left { left: 0; } .shelf button.directional.right { right: 0; } .boxes { white-space: nowrap; transition: transform 0.3s cubic-bezier(.25, .8, .25, 1); } .box { display: inline-block; border-radius: 2px; height: 200px; width: 350px; margin: 0 5px; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24); font-size: 16px; } .box:nth-child(even) { background: #f44336; } .box:nth-child(odd) { background: #2196F3; } .shelf:hover button.directional { opacity: 1; } .shelf:hover button.directional:hover { background: rgba(55, 71, 79, 0.8); } *[hidden] { display: none; } 
 <div class="shelf"> <button class="directional left">&lsaquo;</button> <div class="boxes"> <div class="box">test</div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> <div class="box"></div> </div> <button class="directional right">&rsaquo;</button> </div> 

Link to JSFiddle 链接到JSFiddle

Add vertical-align: top; 添加vertical-align: top; to your .box . 到您的.box

This will get your inline block elements to align themselves vertically across their top points. 这将使您的内联块元素在其最高点垂直对齐。

Problem with inline-block elementsis, most browsers including IE will add 1px around inline and inline-block elements. 内联块元素的问题是,包括IE在内的大多数浏览器都会在内联和内联块元素周围添加1px。

https://davidwalsh.name/remove-whitespace-inline-block https://davidwalsh.name/remove-whitespace-inline-block

https://css-tricks.com/fighting-the-space-between-inline-block-elements/ https://css-tricks.com/fighting-the-space-between-inline-block-elements/

https://matthewlein.com/articles/inline-block-no-space-font/ https://matthewlein.com/articles/inline-block-no-space-font/

https://tylercipriani.com/blog/2012/08/01/display-inline-block-extra-margin/ https://tylercipriani.com/blog/2012/08/01/display-inline-block-extra-margin/

This happens if you stack your inline-block elements. 如果堆叠内联块元素,则会发生这种情况。 Meaning, in order to keep your code more readible if you put line breaks in html before inline-block elements, you will get this gap and it will cause unwanted UI. 意思是,为了使您的代码更易读,如果您在inline-block元素之前在html中放置换行符,则会出现此间隙,这将导致不需要的UI。

So basically you need to set font-size:0 to wrapper element then set your font-size back to normal in inline-block elements if you have to keep them in separate lines like 因此,基本上,您需要将font-size:0设置为wrapper元素,然后将inline-block元素中的font-size设置回正常,如果您必须将它们保持在单独的行中,例如

<li>Some content<li>
<li>Some content<li>
<li>Some content<li>

or you need to merge them in single line 或者您需要将它们合并为一行

<li>Some content<li><li>Some content<li><li>Some content<li>

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

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