这个问题让我发疯了——我只能找到部分答案,而这些答案在我的整个代码中是行不通的。我正在尝试将混合“点赞按钮”(来自外部网站的 <script>
的片段)和图像(我已将其设置为 <a>
的内联列表居中,其中“点赞按钮”不可用)
要求是: 1. 一切都需要与父 div 的顶部边缘对齐——这就是为什么我使用 ul、li、display:table 和 display:table-cell 来对齐顶部(而不仅仅是内联 block ) 2.所有东西都需要在同一条水平线上,这就是为什么它们需要 float (在没有inline-block的情况下) 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
使子项垂直对齐到 他们的 parent 。 - 设置
flex-wrap
nowrap
(默认值)确保 children 留在一个 行。 - 设置
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 元素需要在 table-cell li 中居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33960420/