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

eve*_*_mf 5 html css absolute

我有一个包装div和内部我有一个<span>绝对的位置,因为它是Icomoon的一个图标,我需要成为背景的一部分.然后,在同一个级别<span>,另一个div,并在其中我有a <h1>,a <p>,an <input>和a <span>.

我已将<span>绝对定位于包裹(它有相对位置),但是我想把它放在中间,它只需要左:26%,这根本不是50%!因此,当我调整屏幕大小时,问题就变成了它不会停留在包裹的中间.

我在想,为什么会造成这种情况?我刚刚发布了另一篇文章,因为我想在JSFiddle中包含这个问题,但我无法弄清楚如何在JSFiddle中包含字体文件,因此图标不会出现在那里.

有没有人对我能做些什么来解决它有一点想法?

.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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

the*_*ond 21

没有看到一些代码就很难确切地说,但我猜你的问题是你的左边缘是50%,但你想让它的中心坐在它的父母的中心?

在span上尝试这样的东西(除了任何其他样式):

span {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
}
Run Code Online (Sandbox Code Playgroud)

这应该将其自身宽度的一半移动(移动)到它的左侧.

  • 基本上,您可以将各种样式应用于元素,以便对其进行转换.所以在这方面,翻译意味着围绕轴移动它.我们使用的是translateX,它沿着它的X轴移动(在这种情况下向后移动50%).翻译是相对于自身的(而左边:50%将相对于它的父母)所以50%是它自己宽度的一半.您还可以应用平移(-50%, - 50),它将垂直和水平居中(沿X轴和Y轴).值得一看css3,因为它现在非常强大,但请记住浏览器支持(请查看caniuse.com). (2认同)