首页 > 解决方案 > 用 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>

这是有问题的主要图像

标签: javascripthtmlcss

解决方案


有几种方法,但在此示例中,我将“显示:块”应用于图像,以便它可以使用边距属性。

/* 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>

推荐阅读