html - 基于媒体查询的中心按钮

标签 html css mobile

我正在尝试创建一个在桌面上左对齐但在移动设备上居中对齐的按钮。不知何故,当我使用下面的代码时,我看到的唯一变化是按钮正在根据屏幕调整它的大小,但它没有居中。我做错了什么?

<!DOCTYPE html>
<html>
<style>
  body {
  font-family: sans-serif;
}

table {
  height: 100%;
  width: 50%;
  background: red;
}

@media only screen and (max-width: 480px) {
  .centeredButton {
    text-align: center;
  }
}
</style>
  <head>
    <title>Parcel Sandbox</title>
    <meta charset="UTF-8" />
  </head>

  <body>
    <table >
      <tbody>
        <tr>
		<td class="innertd buttonblock centeredButton" bgcolor="#FFA617" style=" padding: 0px; border-radius: 99px; -moz-border-radius: 99px; -webkit-border-radius: 99px; background-color: #FFA617;">
			<a style=" font-size: 14px; font-family: Arial, Helvetica, sans-serif; color: #540021 !important; text-align: center; display: block; text-decoration: none; font-weight: bold; padding: 7px 14px; background-color: #FFA617; border: 10px solid #FFA617; border-radius: 99px; -moz-border-radius: 99px; -webkit-border-radius: 99px;"
			 target="_blank" class="buttonstyles" href=""
			 title="" alias="" conversion="true" data-linkto="https://">Stap
				nu over
			</a>
		</td>
        </tr>
      </tbody>
    </table>

    <script src="src/index.js"></script>
  </body>
</html>

最佳答案

您的表格 CSS 有问题,因为它只有 50% 的宽度,不允许您移动或居中对齐。

将下面的表格 CSS 放在媒体代码中,它会帮助您。

@media only screen and (max-width: 480px) {
   table {
    margin : 0 auto;
   }

  .centeredButton {
    text-align: center;
   }
}

关于html - 基于媒体查询的中心按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57712865/

相关文章:

c# - 如何使用 HtmlAgility Pack 解析 WPF 中 div id 标记之间的值?

css - 行高不适用于我的投资组合网站的移动设备

javascript - 我可以自定义文本区域以允许我添加/删除特定的文本项吗?

javascript - 悬停时的jquery动画边框

html - ionic : multible buttons in header not displaying correct

html - 堆叠行时更改行中列的高度

android - 如何使用电子邮件应用程序从我的应用程序发送电子邮件?

java - setOnClickListener 安卓

javascript - div 中的内容在 safari 中显示在边界外

jquery - 显示加载程序时预加载 div 内容