html - 基于媒体查询的中心按钮
问题描述
我正在尝试创建一个按钮,该按钮将在桌面上左对齐,但在移动设备上居中对齐。不知何故,当我使用下面的代码时,我看到的唯一变化是按钮正在调整它的屏幕大小,但它没有居中。我究竟做错了什么?
<!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;
}
}
推荐阅读
- python - Python菜单中的列表索引超出范围
- apache-superset - 有什么方法可以将过滤器传递给 apche 超集中的 url?
- wordpress - 如何从 WordPress 编辑帖子页面的复选框列表部分中排除某些类别或分类?
- python - 如何从 Python SDK 中的 Dataflow 作业连接到 CloudSQL?
- c++ - Cpp/Eigen 将浮点数转换为字节列表
- java - Maven 在 Maven Central 中找不到现有依赖项
- typescript - 如何将 TypeScript 的索引访问类型与可为空的嵌套类型一起使用?
- c# - 来自 html 单选按钮的模型变量 bool
- gruntjs - Grunt:grunt-contrib-sass 输出巨大的 CSS 文件
- maven - maven 究竟如何为相同的执行 ID 创建有效的插件配置?