How do I remove the top margin in a web page?

前端 未结 17 975
眼角桃花
眼角桃花 2020-11-29 02:26

I have had this problem with every web page I have created. There is always a top margin above the \'main container\' div I use to place my content in the center of the pag

相关标签:
17条回答
  • 2020-11-29 03:18

    I have been having a similar issue. I wanted a percentage height and top-margin for my container div, but the body would take on the margin of the container div. I think I figured out a solution.

    Here is my original (problem) code:

    html {
        height:100%;
    }
    
    body {
        height:100%;                    
        margin-top:0%;
        padding:0%; 
    }
    
    #pageContainer {
        position:relative;
        width:96%;                  /* 100% - (margin * 2) */
        height:96%;                 /* 100% - (margin * 2) */           
        margin:2% auto 0% auto;
        padding:0%;
    }
    

    My solution was to set the height of the body the same as the height of the container.
    html { height:100%; }

    body {
        height:96%;     /* 100% * (pageContainer*2) */
        margin-top:0%;
        padding:0%; 
    }
    
    #pageContainer {
        position:relative;
        width:96%;                  /* 100% - (margin * 2) */
        height:96%;                 /* 100% - (margin * 2) */           
        margin:2% auto 0% auto;
        padding:0%;
    }
    

    I haven't tested it in every browser, but this seems to work.

    0 讨论(0)
  • 2020-11-29 03:23

    Is your first element h1 or similar? That element's margin-top could be causing what seems like a margin on body.

    0 讨论(0)
  • 2020-11-29 03:23

    A lot of elements in CSS have default padding and margins set on them. So, when you start building a new site, it's always good to have a reset.css file ready. Add this to make to rub the default values, so you have more control over your web page.

    /* CSS reset */
    body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,blockquote,th,td { 
        margin:0;
        padding:0;
    }
    html,body {
        margin:0;
        padding:0;
    }
    
    
    /* Extra options you might want to consider*/
    
    table {
        border-collapse:collapse;
        border-spacing:0;
    }
    fieldset,img { 
        border:0;
    }
    input{
        border:1px solid #b0b0b0;
        padding:3px 5px 4px;
        color:#979797;
        width:190px;
    }
    address,caption,cite,code,dfn,th,var {
        font-style:normal;
        font-weight:normal;
    }
    ol,ul {
        list-style:none;
    }
    caption,th {
        text-align:left;
    }
    h1,h2,h3,h4,h5,h6 {
        font-size:100%;
        font-weight:normal;
    }
    q:before,q:after {
        content:'';
    }
    abbr,acronym { 
        border:0;
    }
    

    I hope this helps all fellow developers this is something that bugged for me a while when I was learning.

    0 讨论(0)
  • 2020-11-29 03:23

    I had this exact same problem. For me, this was the only thing that worked:

    div.mainContainer { padding-top: 1px; }
    

    It actually works with any number that's not zero. I really have no idea why this took care of it. I'm not that knowledgeable about CSS and HTML and it seems counterintuitive. Setting the body, html, h1 margins and paddings to 0 had no effect for me. I also had an h1 at the top of my page

    0 讨论(0)
  • 2020-11-29 03:24

    Try margin -10px:

    body { margin-top:-10px; }
    
    0 讨论(0)
  • 2020-11-29 03:24

    The same issue has been driving me crazy for several hours. What I found and you may want to check is html encoding. In my html file I declared utf-8 encoding and used Notepad++ to enter html code in utf-8 format. What I DID forget of was UTF-8 BOM and No BOM difference. It seems when utf-8 declared html file is written as UTF-8 BOM there is some invisible extra character at the begining of the file. The character (non-printable I guess) effects in one row of "text" at the top of the rendered page. And you cannot see the difference in source-view of the browser (Chrome in my case). Both nice and spoiled files seem identical to the very character, still one of them renders nicely and the other one shows this freaking upper margin.

    To wrap it up the solution is to convert the file to UTF-8 NO BOM and one can do it in i.e. Notepad++.

    0 讨论(0)
提交回复
热议问题