简体   繁体   English

绝对左侧位置:50%不在中间位置

[英]position absolute left:50% does not position span in the middle

I have a wrap div and inside I have a <span> position absolute to the wrap as it is an icon from Icomoon and I need to be part of the background. 我有一个包装div和里面我有一个绝对的<span>位置,因为它是Icomoon的一个图标,我需要成为背景的一部分。 And then, at the same level that the <span> , another div, and inside this I have a <h1> , a <p> , an <input> and a <span> . 然后,在与<span>相同的级别,另一个div,在其中我有一个<h1> ,一个<p> ,一个<input>和一个<span>

I have positioned the <span> absolute to the wrap (which it has position relative), but although I want to put it in the middle, it just needs left:26%, which is not 50% at all! 我已经将<span>绝对定位到换行(它有相对位置),但是虽然我想把它放在中间,但它只需要左:26%,这根本不是50%! So the problem becomes when I resize the screen, that it doesn't stay in the middle of the wrap. 因此,当我调整屏幕大小时,问题就变成了它不会停留在包裹的中间。

I was wondering, why can it be caused? 我在想,为什么会造成这种情况? I posted another post a while ago because I wanted to include this problem in a JSFiddle but I couldn't work out how to include the fonts files in the JSFiddle so the icons don't appear there. 我刚刚发布了另一篇文章,因为我想在JSFiddle中包含这个问题,但我无法弄清楚如何在JSFiddle中包含字体文件,因此图标不会出现在那里。

Does anyone has a slight idea about what can I do to fix it? 有没有人对我能做些什么来解决它有一点想法?

 .containerOfSites { display: block; height: auto; float: none !important; margin: 0 auto; } .wrapPortalItem { padding: 0; } .insideWrapItem { text-align: center; border: 3px solid black; padding: 15px; position: relative; } .portalItem { background-color: rgba(255, 255, 255, 0.75); padding-top: 3%; padding-bottom: 10%; font-family: 'Open Sans', sans-serif; position: relative; } .portalItem p { margin-bottom: 40%; font-size: 30px; padding: 5px; } .portalItem p>span { font-weight: 900; } .portalItem span.viewMorePs { text-decoration: none; font-size: 18px !important; z-index: 999; } .portalItem h1 { color: #B5D803; font-weight: 900; text-transform: uppercase; text-shadow: 2px 2px 0 #fff; } .insideWrapItem span[class^="iconI-"] { position: absolute; color: white; bottom: 12%; left: 26%; /* <- */ font-size: 14em !important; } 
 <div id="portalPage" class="col-md-24"> <div class="containerOfSites col-md-18 col-sm-24"> <div class="wrapPortalItem col-md-8"> <div class="insideWrapItem"> <span class="iconI-iconDA_automotive img-responsive"></span> <div class="portalItem portA "> <h1>AUTOMOTIVE</h1> <p>web sites<br /> for the<br /> <span> automotive<br /> </span> market</p> <a href="http://motors06.denison-automotive.co.uk/denison/"><span class="viewMorePsGreen">GO</span></a> </div> </div> </div> <div class="wrapPortalItem col-md-8"> <div class="insideWrapItem"> <span class="iconI-iconDA_web"></span> <div class="portalItem"> <h1>DESIGN</h1> <p>web sites<br /> for the small &<br /> large business<br /> for<span> all sectors</span></p> <a href="http://motors06.denison-automotive.co.uk/denison/denison-2/web-sites/"><span class="viewMorePsGreen">GO</span></a> </div> </div> </div> <div class="wrapPortalItem col-md-8"> <div class="insideWrapItem"> <span class="iconI-iconDA_yourbrand"></span> <div class="portalItem"> <h1>BRANDING</h1> <p><span>branding<br /> </span> and<br /> design</p> <a href="http://motors06.denison-automotive.co.uk/denison/denison-2/branding/"><span class="viewMorePsGreen">GO</span></a> </div> </div> </div> <div class="clearfix"></div> </div> </div> 

It's difficult to say exactly without seeing some code, but I'd guess your problem is the the left edge of your is sitting at 50% but you want the center of the to sit in the center of it's parent? 没有看到一些代码就很难确切地说,但我猜你的问题是你的左边缘是50%,但是你想让它的中心位于它的父母的中心?

Try something like this on the span (in addition to any other styles): 在span上尝试这样的东西(除了任何其他样式):

span {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
}

This should move (trasnlate) the half of it's own width to it's left. 这应该将其自身宽度的一半移动(移动)到它的左侧。

To position an absolute element centered, you can use this CSS: 要将绝对元素置于居中位置,您可以使用此CSS:

left:0;
right:0;
margin:0 auto;

like in this example: http://jsfiddle.net/ucjt51Lc/ 就像在这个例子中: http//jsfiddle.net/ucjt51Lc/

left:50% will not work because the element is aligned from the top left corner, not the center. left:50%将无效,因为元素从左上角对齐,而不是从中心对齐。

Please include the font files in jsFiddle. 请在jsFiddle中包含字体文件。 There is a panel on the left side, click on External Resources, paste there the URL of the font and click the plus sign. 左侧有一个面板,单击外部资源,粘贴字体的URL并单击加号。 (I wanted to add this as a comment, but StackOverflow hasn't allowed it, because I have not earned 50 reputation points yet...) (我想把它添加为评论,但是StackOverflow不允许它,因为我还没有获得50点声望点......)

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

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