javascript - 用 css 居中 html 内容
问题描述
我正在为自己构建一个 WIP 占位符页面,并且我坚持使用 CSS 将媒体内容居中。我设法让它在移动设备上工作,但内容在桌面上稍微偏右。
更新:我已经添加了整个代码以及我选择支持它的 CSS。如您所知,CSS 的 .display 部分是我遇到最多麻烦的地方(假设我已经解决了这个问题)。根据我在这里被告知并在其他地方阅读的内容,我最初在 HTML 中尝试的标签不适用于 HTML5,所以我希望让 CSS 来完成它。就像我之前提到的,在移动设备上预览时,它工作正常,页面底部的链接很好地堆叠,但在完整的桌面上它们都崩溃了。
下面是代码:
@charset "utf-8";
/* CSS Document */
.content {
max-width: 500px;
margin: auto;
}
.display{
position:relative;
display: block;
padding: 0;
margin:0 auto;
width: auto;
text-align:center;
display:flex;
justify-content:center;
}
.button {
background-color: #FFFFFF;
border: #000000;
border-bottom-width: 2px;
text-decoration-color: #FFFFFF;
padding: 15px 32px;
text-align: center;
display: inline-block;
font-size: 16px;
}
.help-block.with-errors {
color: #ff0000;
margin-top: 5px;
}
.overlay {
position:fixed;
top:0;
left:0;
right:0;
bottom:0;
background-color:rgba(0, 0, 0, 0.8);
z-index:100;
color:white;
display: none;
font-family: 'source_sans_proregular';
line-height: 25px;
-webkit-text-size-adjust:none;
text-size-adjust:none;
}
.container {
width:100%;
height:100%;
margin:0;
padding:0;
border:0;
}
.container td {
vertical-align: middle;
text-align: center;
}
.centerAlign {
text-align: center;
}
.margin2x {
margin-bottom: 24px;
}
.margin1x {
margin-bottom: 12px;
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>mAdjetey is getting an upgrade</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="index_files/mine.css" rel="stylesheet" type="text/css">
<link rel="shortcut icon" href="index_files/favicon.png">
</head>
<a name="tepin"></a>
<div class="display" align="center">
<img src="index_files/uc_main.png" style="max-width:100%" height="auto" alt="Website under redesign">
</div>
<div class="display">
<div class="w3-topbar">
<a href="email.html"><img src="index_files/icon_email.png" alt="Email"></a>
</div>
</div>
<div class="display" align="center">
<a href="#tepin" class="button">Back to top of page</a>
</div>
</html>
解决方案
有几种方法,但在此示例中,我将“显示:块”应用于图像,以便它可以使用边距属性。
/* CSS */
.display img {
display: block;
padding: 0;
/* maybe some top margin too? margin: 30% auto 0 */
margin: 0 auto;
width: auto;
}
<!-- HTML -->
<div class="display" align="center">
<img src="index_files/uc_main.png" style="max-width:100%" height="auto" alt="Website under redesign">
</div>
推荐阅读
- c# - 在点击索引 ActionResult 之前在 HomeController() 中加载数据库数据时的 MVC 进度条 C#
- python - 如何在一张图中绘制多个折线图(叠加/分组)
- mysql - 如何组合两个不同日期的表并加入另一个表?
- c# - 如何从 PowerBi 数据集中获取数据库数据?
- java - 在 Android Java 中未分配值
- python - 如何为我的脚本创建一个带有 python“日志”模块的日志包?
- python - ImportError:添加到 INSTALLED_APPS 后没有名为“first_app”的模块
- charts - 谷歌图表动画不适用于堆叠图表
- ruby - 如何在 Ruby 中循环的每次迭代中读取数字?
- php - 使用 Docker Compose 远程解释器进行调试:数据库容器未知