【问题标题】:white space div when i add text in the middle当我在中间添加文本时的空白 div
【发布时间】:2017-02-24 09:35:40
【问题描述】:

当我将文本放入 div 时,我得到一个空白。如何删除它?我也想问一下如何让“welkom op dennis website”这个文本自动居中在div的中间。

这里你可以看到代码:

.container {
  max-width: 100%;
  max-width: 100%;
  margin: 0;
  padding: 0;
  display: inline-block;
}
html,
body {
  margin: 0px;
  padding: 0px;
}
.nav {
  height: 5%;
  width: 100%;
  background-color: white;
}
.top {
  height: 40%;
  width: 100%;
  background-color: #1E90FF;
}
.nav {
  background-color: #444;
}
.nav a {
  display: inline-block;
  background-color: #444;
  font-family: Arial;
  padding: 10px 20px;
  text-decoration: none;
  color: white;
  float: right;
}
.nav a:hover {
  background-color: #1E90FF;
}
.logo {
  color: white;
  display: inline-block;
  padding: 10px 20px;
  font-family: Arial;
  text-decoration: none;
}
p.center {
  padding: 150px 550px;
  color: white;
  font-family: Arial;
  font-size: 25px;
   {}
<header>
  <title>Dennis Zwart Home Pagina</title>
  <link href="css/MyStyle.css" rel="stylesheet" style="css" />
</header>

<body>
  <div class="container">

    <div class="nav">
      <text class="logo">Dennis Zwart</text>
      <a href="#">Contact</a>
      <a href="#">Games</a>
      <a href="#">Foto's</a>
      <a href="#">Hobby's</a>
      <a href="#">Home</a>
    </div>
    <div class="top">
      <p class="center">Welkom op de website van Dennis Zwart</p>
    </div>
  </div>

</body>

【问题讨论】:

  • 文字不是已经居中了吗?
  • 是的,但我正在寻找一种更简单的方法^^
  • 我必须为学校制作自己的网站,我更好地学习它
  • 你也需要垂直居中的文本吗?

标签: html css


【解决方案1】:

这是因为p 元素具有自然边距(由浏览器定义)。删除它:

p {
  margin-top: 0;
}

然后删除p 水平填充并将文本居中

text-align: center;

为了去除屏幕右侧的空白区域。

p {
  margin-top: 0;
  text-align: center;
}

.container {
  max-width: 100%;
  max-width: 100%;
  margin: 0;
  padding: 0;
  display: inline-block;
}
html,
body {
  margin: 0px;
  padding: 0px;
}
.nav {
  height: 5%;
  width: 100%;
  background-color: white;
}
.top {
  height: 40%;
  width: 100%;
  background-color: #1E90FF;
}
.nav {
  background-color: #444;
}
.nav a {
  display: inline-block;
  background-color: #444;
  font-family: Arial;
  padding: 10px 20px;
  text-decoration: none;
  color: white;
  float: right;
}
.nav a:hover {
  background-color: #1E90FF;
}
.logo {
  color: white;
  display: inline-block;
  padding: 10px 20px;
  font-family: Arial;
  text-decoration: none;
}
p.center {
  padding: 150px 0px;
  color: white;
  font-family: Arial;
  font-size: 25px;
}
<header>
  <title>Dennis Zwart Home Pagina</title>
  <link href="css/MyStyle.css" rel="stylesheet" style="css" />
</header>

<body>
  <div class="container">

    <div class="nav">
      <text class="logo">Dennis Zwart</text>
      <a href="#">Contact</a>
      <a href="#">Games</a>
      <a href="#">Foto's</a>
      <a href="#">Hobby's</a>
      <a href="#">Home</a>
    </div>
    <div class="top">
      <p class="center">Welkom op de website van Dennis Zwart</p>
    </div>
  </div>

</body>

【讨论】:

  • 感谢它的工作:D,如何修复网站左侧的白色区域?
  • @dennis 不要以padding 居中您的文本,我会编辑我的答案以修复白色区域。阅读它...
  • 页面变小了很多^^
  • 感谢您的关注。另一个人用解释解决了所有问题^^
【解决方案2】:

导航和蓝色文本字段之间的空间来自折叠边距。您需要删除由&lt;p&gt; 元素在.topmore on Collapsing Margins 中创建的边距。

如果你也需要文字垂直居中,你可以使用相对定位和翻译。

其他说明

  • &lt;text&gt; 不是有效的 HTML 元素,请改用 &lt;p&gt;&lt;span&gt;&lt;div&gt;&lt;a&gt; 等。我在回答中将其切换为&lt;a&gt;
  • 我发现您使用的是百分比高度。这些可能很棘手。为了使百分比高度起作用,必须在父元素上设置高度。如果该父元素的高度是一个百分比,那么它的父元素需要一个高度集。如果使用百分比,以此类推一直到根元素&lt;html&gt;。在我的回答中,我将高度切换为 px 值。
  • 许多块级元素(&lt;div&gt;&lt;nav&gt;)应用了width: 100%;,我删除了它们,因为它们不需要。默认情况下,块级元素将始终占据其包含元素的 100% 宽度。
  • 为了使导航项垂直居中,我将&lt;a&gt; 元素中的line-height 设置为等于&lt;nav&gt; 元素的高度。
  • 我删除了您的.container 元素,因为它没有做任何有用的事情。如果您决定添加媒体查询并针对各种视口大小限制其宽度,您可能稍后需要它(可能在不同的位置)。

html,
body {
  margin: 0px;
  padding: 0px;
}
.nav {
  height: 45px;
  background-color: white;
}
.top {
  height: 300px;
  background-color: #1E90FF;
}
.nav {
  background-color: #444;
}
.nav .logo {
  float: left;
}
.nav a {
  display: inline-block;
  background-color: #444;
  font-family: Arial;
  padding: 0 20px;
  text-decoration: none;
  line-height: 45px;
  color: white;
  float: right;
}
.nav a:hover {
  background-color: #1E90FF;
}
p.center {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
  margin: 0;
  color: white;
  font-family: Arial;
  font-size: 25px;
  text-align: center;
}
<header>
  <title>Dennis Zwart Home Pagina</title>
  <link href="css/MyStyle.css" rel="stylesheet" style="css" />
</header>

<body>
    <div class="nav">
      <a class="logo" href="#">Dennis Zwart</a>
      <a href="#">Contact</a>
      <a href="#">Games</a>
      <a href="#">Foto's</a>
      <a href="#">Hobby's</a>
      <a href="#">Home</a>
    </div>
    <div class="top">
      <p class="center">Welkom op de website van Dennis Zwart</p>
    </div>
</body>

【讨论】:

  • 感谢您的关注。我会把你的笔记抄下来,以便稍后阅读。
猜你喜欢
  • 1970-01-01
  • 2016-01-13
  • 2023-03-05
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
  • 2013-11-22
  • 2022-01-17
  • 2018-06-22
相关资源
最近更新 更多