dennis dennis - 1 month ago 9
CSS Question

white space div when i add text in the middle

im getting a white space when im putting text into the div. How to remove that ? i would like to ask you aswell how to make the text "welkom op dennis website" automatic center in the middle of the div.

here you can see the code :



.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>




Answer

The space between your navigation and blue text field is from collapsing margins. You'll need to remove the margins created by your <p> element in .top, more on Collapsing Margins.

If you need the text vertically centered as well, you can use relative positioning and translate.

Other Notes

  • <text> is not a valid HTML element, use <p>, <span>, <div>, <a> etc. instead. I switched it to an <a> in my answer.
  • I see that you're using percentage heights. Those can be tricky. In order for percentage heights to work a height has to be set on the parent element. If that parent element's height is a percentage, then it's parent needs a height set. So on and so forth all the way to the root element <html> if percentages are used. In my answer I switch the heights to px values.
  • A number of block level elements (<div>, <nav>) had width: 100%; applied to them, I removed them as they're not needed. A block level element will always take up 100% width of it's containing element by default.
  • To vertically center your navigation items I set the line-height of the <a> elements equal to the height of the <nav> element.
  • I removed your .container element as it wasn't doing anything useful. You might need it later (likely in a different location) if you decide to add media queries and limit it's width for various viewport sizes.

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>