在HTML中对齐div标签?

时间:2018-02-07 21:41:33

标签: html css

我有以下HTML代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8"> 
        <title>Homepage</title>
    <link rel="stylesheet" href="main.css" type="text/css">
    </head>
    <body>
    <div class="topbar">
        <div class="title">Welcome</div>
        <div class="control">
            <span style="padding-right: 0.5em;"><a href="index.html">Bla</a></span>
            <span style="padding-right: 0.5em;">Blue</span>
            <span style="padding-right: 0.5em;"><a href="contact.html">Blub</a></span>
        </div>
    </div>
    <div class="main">
        <div class="text">
            Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam 
        </div>
    </div>
</body>

以下CSS文件:

.body { 
    overflow: hidden; 
    color: black; 
}
.html { 
    overflow: hidden; 
    margin: 0;
}
.title {
    color: black;
    font-family: 'Arial';
    font-weight: bold;
    font-size: 20pt;
    padding: 0 0 0 0;
    margin-left: 20%;
    margin-top: 15px;
    margin-bottom: 15px;
    text-align: left; 
    float: left;
}
.control {
    color: black;
    font-family: 'Arial';
    font-size: 15pt;
    padding: 0 0 0 0;
    margin-left: 0;
    margin-right: 20%;
    margin-top: 21px;
    text-align: right;
}
.text { 
    margin-left: 20%;
    margin-right: 20%;
    font-family: 'Arial';
    font-size: 12pt;
    text-align: justify; 
}
.topbar {
    padding: 0 0 0 0;
    overflow: hidden;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    width: 100%;
    background-color: rgba(241, 241, 241, 0.9);
    border-bottom: solid 1px black;
}
.main {
    padding: 0 0 0 0;
    overflow: hidden;
    top: 0;
    left: 0;
    right: 0;
    width: 100%;
    padding-top: 75px;
}

结果是: Result

如何对齐两个文本?

1 个答案:

答案 0 :(得分:1)

margin:0正文并更正拼写错误:它不是.body而只是body(与html相同),因为它们不是类,而是标记。

有关说明:您的顶栏已修复并设置为left:0,因此与其他内容不同,默认的正文边距不会应用于此。这就是为什么你有这个小错过的原因(8px)。

body {
  overflow: hidden;
  color: black;
  margin:0;
}

html {
  overflow: hidden;
  margin: 0;
}

.title {
  color: black;
  font-family: 'Arial';
  font-weight: bold;
  font-size: 20pt;
  padding: 0 0 0 0;
  margin-left: 20%;
  margin-top: 15px;
  margin-bottom: 15px;
  text-align: left;
  float: left;
}

.control {
  color: black;
  font-family: 'Arial';
  font-size: 15pt;
  padding: 0 0 0 0;
  margin-left: 0;
  margin-right: 20%;
  margin-top: 21px;
  text-align: right;
}

.text {
  margin-left: 20%;
  margin-right: 20%;
  font-family: 'Arial';
  font-size: 12pt;
  text-align: justify;
}

.topbar {
  padding: 0 0 0 0;
  overflow: hidden;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  width: 100%;
  background-color: rgba(241, 241, 241, 0.9);
  border-bottom: solid 1px black;
}

.main {
  padding: 0 0 0 0;
  overflow: hidden;
  /* not needed since the element is static
  top: 0;
  left: 0;
  right: 0;
  */
  width: 100%;
  padding-top: 75px;
}
<div class="topbar">
  <div class="title">Welcome</div>
  <div class="control">
    <span style="padding-right: 0.5em;"><a href="index.html">Bla</a></span>
    <span style="padding-right: 0.5em;">Blue</span>
    <span style="padding-right: 0.5em;"><a href="contact.html">Blub</a></span>
  </div>
</div>
<div class="main">
  <div class="text">
    Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam
  </div>
</div>