如何让此 div 水平居中?



我正在创建一个带有文本编辑器的页面,我可以将除文本编辑器以外的所有内容都居中。

除了div 中带有 id #texteditor 的元素之外的所有元素都居中。这是页面上另一个元素的 css,我成功地将其居中,以及文本编辑器:

header {
background-color: #00B7FF;
color: white;
width: 100%;
height: 150px;
display: inline-block;
text-align: center;
padding: 10px;
}
body {
padding: 50px;
font: 20px "Lucida Grande", Helvetica, Arial, sans-serif;
}
a {
color: #00B7FF;
}
h1 {
text-align: center;
}
p {
display: inline;
}
form {
margin-top: 30px;
}
button {
margin: 0 5px;
display: inline-block;
}
input {
margin: 10px;
}
table {
background-color: #e0e0e0;
width: 800px;
margin: 50px auto;
font-size: 20px;
}
div#texteditor {
width: 900px;
height: 300px;
margin: 0 auto;
}
div#header {
border-bottom: none;
width: 60%;
padding: 10px;
background-color: #e0e0e0;
color: white;
border-radius: 8px 8px 0 0;
}
div#textArea {
border: 2px solid #e0e0e0;
height: 100%;
width: 60%;
}
iframe#wysiwyg {
height: 100%;
width: 60%;
}
.btn {
background-color: DodgerBlue;
/* Blue background */
border: none;
/* Remove borders */
color: white;
/* White text */
padding: 12px 16px;
/* Some padding */
font-size: 16px;
/* Set a font size */
cursor: pointer;
/* Mouse pointer on hover */
}
.btn-primary {
margin: 10px;
display: inline;
}
.btn-white {
background-color: white;
color: #00B7FF;
}
.container {}
.inline {
display: inline;
}
.row {
margin: inherit;
}
.form {
width: 900px;
margin: 0 auto;
}
.form-header {
display: inline-block;
text-align: center;
width: 990px;
margin: 0 auto;
}
.centered-form {
width: 1000px;
margin: 0 auto;
}
.centered-text {
display: inline;
text-align: center;
}
.centered-textfields {
width: 500px;
margin: 0 auto;
text-align: center;
}
.centered-buttons {
width: 208px;
margin: auto;
}
.emptyspace {
margin-top: 50px;
}
[login] {
float: right;
}
<h1>New Study Guide</h1>
<div class="container emptyspace">
<div class="centered-form">
<div class="form-header"><a class="btn btn-primary btn-md inline" href="#" role="button">Upload</a>
<p>or create one from scratch.</p>
</div>
<div class="form emptyspace">
<div id="texteditor">
<div id="header"><button class="btn"><i class="fa fa-bold"></i></button>
<!-- removed for brevity-->
</div>
<div id="textArea"><iframe id="wysiwyg" name="wysiwyg" frameborder="0"></iframe></div>
</div>
</div>
</div>
</div>

提前感谢您的帮助!我也会感谢对我的代码的任何其他批评。

margin: 0 auto;添加到标题和文本区域,因为它们是分开的,或者将它们包装成一个div并对其执行此操作。

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.form-header {
display: inline-block;
text-align: center;
width: 990px;
margin: 0 auto;
}
div#texteditor {
width: 900px;
height: 300px;
margin: 0 auto;
}
header {
background-color: #00B7FF;
color: white;
width: 100%;
height: 150px;
display: inline-block;
text-align: center;
padding: 10px;
}
body {
padding: 50px;
font: 20px "Lucida Grande", Helvetica, Arial, sans-serif;
}
a {
color: #00B7FF;
}
h1 {
text-align: center;
}
p {
display: inline;
}
form {
margin-top: 30px;
}
button {
margin: 0 5px;
display: inline-block;
}
input {
margin: 10px;
}
table {
background-color: #e0e0e0;
width: 800px;
margin: 50px auto;
font-size: 20px;
}
div#texteditor {
width: 900px;
height: 300px;
margin: 0 auto;
}
div#header {
border-bottom: none;
width: 60%;
margin: 0 auto;
padding: 10px;
background-color: #e0e0e0;
color: white;
border-radius: 8px 8px 0 0;
}
div#textArea {
border: 2px solid #e0e0e0;
height: 100%;
width: 60%;
margin: 0 auto;
}
iframe#wysiwyg {
height: 100%;
width: 60%;
}
.btn {
background-color: DodgerBlue;
/* Blue background */
border: none;
/* Remove borders */
color: white;
/* White text */
padding: 12px 16px;
/* Some padding */
font-size: 16px;
/* Set a font size */
cursor: pointer;
/* Mouse pointer on hover */
}
.btn-primary {
margin: 10px;
display: inline;
}
.btn-white {
background-color: white;
color: #00B7FF;
}
.container {}
.inline {
display: inline;
}
.row {
margin: inherit;
}
.form {
width: 900px;
margin: 0 auto;
}
.form-header {
display: inline-block;
text-align: center;
width: 990px;
margin: 0 auto;
}
.centered-form {
width: 1000px;
margin: 0 auto;
}
.centered-text {
display: inline;
text-align: center;
}
.centered-textfields {
width: 500px;
margin: 0 auto;
text-align: center;
}
.centered-buttons {
width: 208px;
margin: auto;
}
.emptyspace {
margin-top: 50px;
}
[login] {
float: right;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css">
<h1>New Study Guide</h1>
<div class="container emptyspace">
<div class="centered-form">
<div class="form-header"><a class="btn btn-primary btn-md inline" href="#" role="button">Upload</a>
<p>or create one from scratch.</p>
</div>
<div class="form emptyspace">
<div id="texteditor">
<div id="header"><button class="btn"><i class="fa fa-bold"></i></button>
<!-- removed for brevity-->
</div>
<div id="textArea"><iframe id="wysiwyg" name="wysiwyg" frameborder="0"></iframe></div>
</div>
</div>
</div>
</div>

我已经从 #header,#textarea 中删除了 60% 的宽度并应用于 #texteditor。

header {
background-color: #00B7FF;
color: white;
width: 100%;
height: 150px;
display: inline-block;
text-align: center;
padding: 10px;
}
body {
padding: 50px;
font: 20px "Lucida Grande", Helvetica, Arial, sans-serif;
}
a {
color: #00B7FF;
}
h1 {
text-align: center;
}
p {
display: inline;
}
form {
margin-top: 30px;
}
button {
margin: 0 5px;
display: inline-block;
}
input {
margin: 10px;
}
table {
background-color: #e0e0e0;
width: 800px;
margin: 50px auto;
font-size: 20px;
}
div#texteditor {
width: 60%;
height: 300px;
margin: 0 auto;
}
div#header {
border-bottom: none;
padding: 10px;
background-color: #e0e0e0;
color: white;
border-radius: 8px 8px 0 0;
}
div#textArea {
border: 2px solid #e0e0e0;
height: 100%;
}
iframe#wysiwyg {
height: 100%;
width: 60%;
}
.btn {
background-color: DodgerBlue;
/* Blue background */
border: none;
/* Remove borders */
color: white;
/* White text */
padding: 12px 16px;
/* Some padding */
font-size: 16px;
/* Set a font size */
cursor: pointer;
/* Mouse pointer on hover */
}
.btn-primary {
margin: 10px;
display: inline;
}
.btn-white {
background-color: white;
color: #00B7FF;
}
.container {}
.inline {
display: inline;
}
.row {
margin: inherit;
}
.form {
width: 900px;
margin: 0 auto;
}
.form-header {
display: inline-block;
text-align: center;
width: 990px;
margin: 0 auto;
}
.centered-form {
width: 1000px;
margin: 0 auto;
}
.centered-text {
display: inline;
text-align: center;
}
.centered-textfields {
width: 500px;
margin: 0 auto;
text-align: center;
}
.centered-buttons {
width: 208px;
margin: auto;
}
.emptyspace {
margin-top: 50px;
}
[login] {
float: right;
}
<h1>New Study Guide</h1>
<div class="container emptyspace">
<div class="centered-form">
<div class="form-header"><a class="btn btn-primary btn-md inline" href="#" role="button">Upload</a>
<p>or create one from scratch.</p>
</div>
<div class="form emptyspace">
<div id="texteditor">
<div id="header"><button class="btn"><i class="fa fa-bold"></i></button>
<!-- removed for brevity-->
</div>
<div id="textArea"><iframe id="wysiwyg" name="wysiwyg" frameborder="0"></iframe></div>
</div>
</div>
</div>
</div>

margin-left:200px 适用于代码。它被放置在中间。

div#texteditor {

宽度:900px;

高度:300像素;

左边距:200px;

}

最新更新