Search code examples
htmlcsspaddingmargin

CSS margin and padding


I tried to remove padding and margin in my CSS code but it isn't working. Idk what to do because I started CSS programming 3 days ago, also i've already watched course for CSS.

I tried use * tag to accept margin and padding to all code but it didn't work.

* {
  margin: -0px;
  padding: 0px;
}

body {
  height: 2000px;
}

#side aside a {
  position: absolute;
  text-decoration: none;
  color: white;
  font-size: 16px;
  padding: 20px;
  background: grey;
  width: 10%;
  height: 3%;
}

#fir {
  top: 30px;
}

#sec {
  top: 90px;
}

#thi {
  top: 150px;
}

#for {
  top: 210px;
}
<div id="side">
  <aside>
    <a href="" id="fir">Главная</a>
    <a href="" id="sec">О нас</a>
    <a href="mailto:decsam@mail.com" id="thi">Наша почта</a>
    <a href="" id="for">Контакты</a>
  </aside>
</div>


Solution

  • The problem is that you're using: position:absolute; and padding. You should remove the padding and use display:flex instead. When display is set to flex, it creates a flex container, allowing your contents to be displayed next to/above each other.

    Replace your css with this:

    * {
        margin: 0px;
    }
    
    body {
        height: 2000px;
    }
    
    #side aside a {
        display: flex;
        text-decoration: none;
        color: white;
        font-size: 16px;
        padding: 20px;
        background: grey;
        width: 10%;
        height: 3%;
    }
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>TestServer</title>
        <link rel="stylesheet" href="main.css">
    </head>
    <body>
        <div id="side">
            <aside>
                <a href="" id="fir">Главная</a>
                <a href="" id="sec">О нас</a>
                <a href="mailto:decsam@mail.com" id="thi">Наша почта</a>
                <a href="" id="for">Контакты</a>
            </aside>
        </div>
    
    </body>
    </html>

    If you want to remove all padding whatsoever, including inside your containers, then replace your css with this:

    * {
        margin: 0px;
    }
    
    body {
        height: 2000px;
    }
    
    #side aside a {
        display: flex;
        text-decoration: none;
        color: white;
        font-size: 16px;
        background: grey;
        width: fit-content;
    }
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>TestServer</title>
        <link rel="stylesheet" href="main.css">
    </head>
    <body>
        <div id="side">
            <aside>
                <a href="" id="fir">Главная</a>
                <a href="" id="sec">О нас</a>
                <a href="mailto:decsam@mail.com" id="thi">Наша почта</a>
                <a href="" id="for">Контакты</a>
            </aside>
        </div>
    
    </body>
    </html>