Html 使用 CSS 将 HTML5 中的按钮居中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19509258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Centering Buttons in HTML5 with CSS
提问by dweeman
I am wondering how I can center a group of social media buttons on my website.
我想知道如何将一组社交媒体按钮集中在我的网站上。
I previously used the <center>
tags, but switched to HTML5, so i have the centering property for my other text and images in the css. However I have links to 4 different social media sites who all use different coding styles to implement the button.
The follow is my code
我以前使用过<center>
标签,但切换到 HTML5,所以我在 css 中为我的其他文本和图像设置了居中属性。但是,我有 4 个不同的社交媒体网站的链接,这些网站都使用不同的编码风格来实现按钮。以下是我的代码
<-- FB -->
<div class="fb-like" data-href="https://www.facebook.com/pages/La-Condesa/242967619184322" data-width="20" data-height="20" data-colorscheme="light" data-layout="standard" data-action="like" data-show-faces="false" data-send="false"></div>
<-- FB-->
<div class="fb-like" data-href="https://www.facebook.com/pages/La-Condesa/242967619184322" data-width="20" data-height="20" data-colorscheme="light" data-layout="standard" data-action="like" data-show-faces="false" data-send="false"></div>
<-- Instagram -->
<style>.ig-b- { display: inline-block; }
.ig-b- img { visibility: hidden; }
.ig-b-:hover { background-position: 0 -60px; }
.ig-b-:active { background-position: 0 -120px; }
.ig-b-32 { width: 32px; height: 32px; background: url(//badges.instagram.com/static/images/ig-badge-sprite-32.png) no-repeat 0 0; }
@media only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min--moz-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2 / 1), only screen and (min-device-pixel-ratio: 2), only screen and (min-resolution: 192dpi), only screen and (min-resolution: 2dppx) {.ig-b-32 { background-image: url(//badges.instagram.com/static/images/[email protected]); background-size: 60px 178px; } }</style><br><br>
<-- Instagram -->
<style>.ig-b- { display: inline-block; }
.ig-b- img { visibility: hidden; }
.ig-b-:hover { background-position: 0 -60px; }
.ig-b-:active { background-position: 0 -120px; }
.ig-b-32 { width: 32px; height: 32px; background: url(//badges.instagram.com/static/images/ig-badge-sprite-32.png) no-repeat 0 0; }
@media only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min--moz-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2 / 1), only screen and (min-device-pixel-ratio: 2), only screen and (min-resolution: 192dpi), only screen and (min-resolution: 2dppx) {.ig-b-32 { background-image: url(//badges.instagram.com/static/images/[email protected]); background-size: 60px 178px; } }</style><br><br>
<a href="http://instagram.com/lacondesafitzroy?ref=badge" class="ig-b- ig-b-32">img src="//badges.instagram.com/static/images/ig-badge-32.png" alt="Instagram" /></a>
<a href="http://instagram.com/lacondesafitzroy?ref=badge" class="ig-b- ig-b-32">img src="//badges.instagram.com/static/images/ig-badge-32.png" alt="Instagram" /></a>
<-- Twitter -->
<a href="https://twitter.com/LaaCondesa" class="twitter-follow-button" data-show-count="false" data-show-screen-name="false">Follow @LaaCondesa</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>
<-- 推特-->
<a href="https://twitter.com/LaaCondesa" class="twitter-follow-button" data-show-count="false" data-show-screen-name="false">Follow @LaaCondesa</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>
<-- Tumblr --><iframe class="btn" frameborder="0" border="0" scrolling="no" allowtransparency="true" height="25" width="114" src="http://platform.tumblr.com/v1/follow_button.html?button_type=2&tumblelog=LaaCondesa&color_scheme=light"></iframe>
<-- 微博--><iframe class="btn" frameborder="0" border="0" scrolling="no" allowtransparency="true" height="25" width="114" src="http://platform.tumblr.com/v1/follow_button.html?button_type=2&tumblelog=LaaCondesa&color_scheme=light"></iframe>
Is there a concise way of centering all of these items in HTML5? Or will I have to do it seperately for each? If that's the case, how can I center these items?
是否有一种简洁的方法可以在 HTML5 中将所有这些项目居中?还是我必须为每个人单独做?如果是这种情况,我如何将这些项目居中?
Thanks
谢谢
EDIT: Sorry I left out, they are all contained within a container with the following properties
.container {
position: static;
height: 700px;
width: 780px;
top: 50%;
left: 50%;
margin: 10px;
margin-left: auto;
margin-right: auto;
}
编辑:对不起,我遗漏了,它们都包含在具有以下属性的容器中
.container {
position: static;
height: 700px;
width: 780px;
top: 50%;
left: 50%;
margin: 10px;
margin-left: auto;
margin-right: auto;
}
Check out www.lacondesa.com.au/test.html to see
查看 www.lacondesa.com.au/test.html 查看
回答by Dan Goodspeed
Try putting the buttons inside a div with the style
尝试将按钮放在带有样式的 div 中
text-align:center;
margin:auto;
回答by Sasidharan
Give specific width inside a div and then put margin:auto
.
在 div 内指定特定宽度,然后将margin:auto
.
div
{
width://your width;
margin:auto;
}
回答by varun
this may work. Try,
这可能有效。尝试,
margin : 0px auto;