Removing space at the top left and right of div

后端 未结 9 1842
迷失自我
迷失自我 2020-12-01 13:02

I am starting to work with css and have basic issue.

I have a div element:

相关标签:
9条回答
  • 2020-12-01 13:08

    You need to reset both the default padding and margin attributes in your stylesheet:

    html, body {
        margin: 0;
        padding: 0;
    }
    

    As @Jason McCreary mentions, you should also look into using a reset stylesheet. The one he links to, Eric Meyer's CSS reset, is a great place to start.

    It also looks like you're missing a semi-colon in your css, it should look as follows:

    .top
    {
        background-color:#3B5998;
        margin-left:0px;
        margin-top:0px;
    }
    
    0 讨论(0)
  • 2020-12-01 13:08

    There's padding on the <body> of the page. You can fix this like so:

    body
    {
        padding: 0;
    }
    
    0 讨论(0)
  • 2020-12-01 13:08

    If you need some padding inside the div, you should choose padding:

    padding:top right bottom left;
    

    example:

    padding:5px; /* 5px padding at all sides)*/
    padding:5px 3px; /* top & bottom 5px padding but right left 3px padding) */
    padding:5px 3px 4px; /* top 5px, bottom 4px padding but left right 3px) */
    padding:1px 2px 3px 4px; /* top 1px, right 2px bottom 3px & left 4px) */
    

    Similarly to control the space outside the div, you can use margin.

    Margin will use exact same formula.

    0 讨论(0)
  • 2020-12-01 13:16

    After long time I found the correct solution for me:

    html, body {
      margin: 0;
      padding: 0;
      overflow-x: hidden;
    }
    

    We need define the overflow in the horizontal.

    0 讨论(0)
  • 2020-12-01 13:18

    If other answers don't work, try position:absolute; top: 0px; left: 0px; or display:flex.

    0 讨论(0)
  • 2020-12-01 13:18

    I used this and it worked for me:

    body {
        background-position: 50% 50%;
        margin:0px;
        padding:0px !important;
        height:100%;
    }
    
    0 讨论(0)
提交回复
热议问题