这使我发疯-我只能找到部分答案,这些答案在我的全部代码中都行不通。我正在尝试将混合的“喜欢的按钮”(来自外部站点的<script>片段)和图像(我设置为<a>的“喜欢的按钮”不可用)的内联列表居中放置

要求是:
1.一切都需要与父div的顶部边缘对齐-这就是为什么我使用ul,li,display:table和display:table-cell使其与顶部对齐的原因(而不仅仅是内联块)
2.一切都必须在同一水平线上,这就是为什么它们需要浮动(在没有内联块的情况下)的原因
3.一切都需要居中(这是我无法实现的!)

这是我第一次使用该论坛,而且我并不是真正的网页设计师,因此请保持冷静!我附上了我认为相关且有用的代码。



#contact {
  position: relative;
  top: 1em;
  margin-left: 0;
  width: 100%;
  display: inline-block;
}
#contactinfo {
  margin-right: 0;
  color: #00CCFF;
  text-decoration: none;
  text-align: center;
  width: 100%;
  padding-bottom: 1em;
}
#share {
  position: relative;
  text-align: center;
  min-height: 1em;
  display: inline;
  vertical-align: top;
}
ul.button {
  display: table;
  min-height: 2em;
  list-style: none;
  list-style-position: inside;
  padding-left: 0.5em;
  margin-left: 0em;
  text-align: center;
  float: right;
}
ul.button li {
  display: table-row;
  vertical-align: top;
  text-align: center;
  height: 2em;
  padding: 0.5em;
}
img.sharebutton {
  max-width: 50px;
  max-height: 35px;
  padding-bottom: 1em;
}

<div id="contact">
  <div id="contactinfo">
    <h2>
email <a href="mailto:info@lysamorrison.com"> info@lysamorrison.com</a><br>
call 0796 99 777 68
</h2>
  </div>
  <div id="share">
    <div class="sharebuttons">
      <ul class="button">
        <li>
          <a href="http://www.eventbrite.com/o/lma-training-amp-consultancy-8057832740" target="_blank">
            <img src="eventbrite.png" class="sharebutton" />
          </a>
        </li>
      </ul>
    </div>
    <div class="sharebuttons">
      <ul class="button">
        <li>
          <a href="http://www.meetup.com/LMA-Personal-Professional-Development-Whitley-Bay-Meetup/#upcoming" target="_blank">
            <img src="meetup.png" class="sharebutton" />
          </a>
        </li>
      </ul>
    </div>
    <div class="sharebuttons">
      <ul class="button">
        <li>
          <script src="//platform.linkedin.com/in.js" type="text/javascript">
            lang: en_US
          </script>
          <script type="IN/FollowCompany" data-id="1586406"></script>
        </li>
      </ul>
    </div>
    <div class="sharebuttons">
      <ul class="button">
        <li><a href="https://twitter.com/LMAtrainconsult" class="twitter-follow-button" data-show-count="false" data-show-screen-name="false">Follow @LMAtrainconsult</a>
          <script>
            ! function(d, s, id) {
              var js, fjs = d.getElementsByTagName(s)[0],
                p = /^http:/.test(d.location) ? 'http' : 'https';
              if (!d.getElementById(id)) {
                js = d.createElement(s);
                js.id = id;
                js.src = p + '://platform.twitter.com/widgets.js';
                fjs.parentNode.insertBefore(js, fjs);
              }
            }(document, 'script', 'twitter-wjs');
          </script>
        </li>
      </ul>
    </div>
    <div class="sharebuttons">
      <ul class="button">
        <li>
          <div class="fb-follow" data-href="https://www.facebook.com/lmatrainingandconsultancy" data-height="35" data-layout="button" data-show-faces="true"></div>
        </li>
      </ul>
    </div>
  </div>
  <!--End of share div-->
</div>

最佳答案

Flexbox是这里的解决方案;使用3个特定的属性来满足您的需求。


设置
align-items
flex-start使子项垂直对齐到
他们的父母。
设置
flex-wrap
设置为nowrap(默认值)可确保孩子留在一个
线。
设置
justify-content
center将孩子在父母中水平居中


这是一个简化的示例:



*{box-sizing:border-box;margin:0;padding:0;vertical-align:middle;}
ul{
    background:black;
    align-items:flex-start;
    display:flex;
    flex-flow:row nowrap;
    justify-content:center;
    list-style:none;
    padding:2px;
}
li{
    margin:2px;
}

<ul>
    <li><a href="#"><img height="80" src="http://lorempixel.com/100/80/abstract/1/" width="100"></a></li>
    <li><a href="#"><img height="50" src="http://lorempixel.com/75/50/abstract/2/" width="75"></a></li>
    <li><a href="#"><img height="150" src="http://lorempixel.com/75/50/abstract/3/" width="200"></a></li>
</ul>

关于css - float 元素需要在表格单元格li中居中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33960420/

10-16 14:29