-1

Just like the title, I want a box with size 320px*40px, with 4 smaller boxes, size 80px*40px, and with no margin or padding. I used * { margin: 0; padding: 0; }, but the margin is still there.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    
    .box {
      height: 40px;
      width: 320px;
      border-top: 3px solid #ff8500;
      border-bottom: 1px solid #edeef0;
    }
    
    .box .minibox {
      padding: 0;
      margin: 0;
      text-decoration: none;
      display: inline-block;
      font-size: 12px;
      color: #4C4C4C;
      width: 80px;
      height: 40px;
      line-height: 40px;
      text-align: center;
    }
    
    .box .minibox:hover {
      background-color: #edeef0;
      color: #ff8400;
    }
  </style>
</head>

<body>
  <div class="box">
    <a href="#" class="minibox">guideline</a>
    <a href="#" class="minibox">guideline</a>
    <a href="#" class="minibox">guideline</a>
    <a href="#" class="minibox">guideline</a>
  </div>
</body>

</html>

I tried to add margin: 0; padding: 0; in the selector of the tag, but nothing happened.

Christian
  • 4,902
  • 4
  • 24
  • 42
bene
  • 1
  • 1
  • 2
    That has to do with the `line-height`, `height`, and `width`. There is 0 margin for hyperlinks now. – m4n0 Nov 13 '22 at 03:36
  • i checked again, and i think there is no problem with the "line-height","height" and "width". Thank you for your comment, i'll check again. – bene Nov 13 '22 at 03:46

3 Answers3

-1

There is no margin or padding.

enter image description here

As you can see in the screenshot above, using dev tools on a .minibox class, the only thing affecting the look of the box is the width, height and also line-height. If you delete those properties then the box will be as wide and as high as the content inside of it.

  • Thank you for your answer. This is what confusing me. If the ".box"'s size is 320*40, the ".minibox"'s size is 80*40, then 4 ".minibox"s can be in a line. But they're not. Maybe I am now too confusing to make it clear. I'll try again. Thanks a lot! – bene Nov 13 '22 at 04:13
-1

The gap that I wanted to remove is the enter key I tapped before. The only thing I need to do is to put 4 hyperlinks in a line in my code.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    
    .box {
      height: 40px;
      border-top: 3px solid #ff8500;
      border-bottom: 1px solid #edeef0;
    }
    
    .box .minibox {
      padding: 0;
      margin: 0;
      text-decoration: none;
      display: inline-block;
      font-size: 12px;
      color: #4C4C4C;
      width: 80px;
      line-height: 40px;
      text-align: center;
    }
    
    .box .minibox:hover {
      background-color: #edeef0;
      color: #ff8400;
    }
  </style>
</head>

<body>
  <div class="box">
    <a href="#" class="minibox">guideline</a><a href="#" class="minibox">guideline</a><a href="#" class="minibox">guideline</a><a href="#" class="minibox">guideline</a>
  </div>
</body>

</html>
4b0
  • 21,981
  • 30
  • 95
  • 142
bene
  • 1
  • 1
-1

It is because of how inline-block elements behave when there is a new-line between them. The new-line is treated as a white-space and that is why you are seeing this gap between a-tags.

Just remove the new-line between the a-tags and there will be no space between them:

* {
  margin: 0;
  padding: 0;
}

.box {
  height: 40px;
  width: 320px;
  border-top: 3px solid #ff8500;
  border-bottom: 1px solid #edeef0;
}

.box .minibox {
  padding: 0;
  margin: 0;
  text-decoration: none;
  display: inline-block;
  font-size: 12px;
  color: #4C4C4C;
  width: 80px;
  height: 40px;
  line-height: 40px;
  text-align: center;
}

.box .minibox:hover {
  background-color: #edeef0;
  color: #ff8400;
}
<!--
  Notice the last > is moved to next line which
  doesn't render a new-line character between a-tags
-->
<div class="box">
  <a href="#" class="minibox">guideline</a
  ><a href="#" class="minibox">guideline</a
  ><a href="#" class="minibox">guideline</a
  ><a href="#" class="minibox">guideline</a>
</div>

<!--
  Or put all a-tags on same line
-->
<div class="box">
  <a href="#" class="minibox">guideline</a><a href="#" class="minibox">guideline</a><a href="#" class="minibox">guideline</a><a href="#" class="minibox">guideline</a>
</div>

<!-- It is the same with letters with new-line between -->
<div>
  hello
h
e
l
l
o
</div>
Bqardi
  • 682
  • 3
  • 10