0

What I am trying to do is, creating a layout like Magzine. I use CSS Grid in my HTML to create a layout, anyhow layout looks good when i render it or open it in a browser, but when i hit Ctrl + P to take a print out the Header and Footer are getting distorted, what i want is like they will appear in the middle with some padding on both Header and Footer

enter image description here

As you can see the footer going into left and the body text and header are getting collapsed with each other. Same is happening with footer.

enter image description here

* {
    box-sizing: border-box;
    -moz-box-sizing: border-box;
}

body {
    width: 21cm;
    min-height: 29.7cm;
    padding: 1cm;
    margin: 1cm auto;
    border-radius: 5px;
    background: white;
    font-size: 14px;
    font-family: 'Droid Serif', serif;
}

header,
footer {
    text-align: center;
    padding: 10px;
}
h1{
    margin: 0 auto;
}

.container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(12, 1fr));
    grid-gap: 1em;
    grid-auto-rows: minmax(auto-fill, auto);
    grid-template-areas: "h h h h h h h h h h h h" "m m m c c c c c c c c c" "l l l l l l r r r r r r";
}

.container>div {
    column-count: 2;
}

p::first-letter {
    font-style: italic;
    font-weight: bold;
    font-size: 40px;
}

h2 {
    text-align: center;
    font-style: italic;
    font-weight: bold;
    column-span: all;
}

.container>.item_LayoutOne_100Percent {
    grid-area: h;
}

.container>.item_LayoutTwo_50Percent_layoutOne {
    grid-area: l;
}

.container>.item_LayoutTwo_50Percent_layoutTwo {
    grid-area: r;
}

.container>.item_LayoutThree_75Percent {
    grid-column: c;
}

.container>.item_LayoutFour_25Percent {
    grid-column: m;
}
h1{
    margin-left: auto;
    margin-right: auto;
    text-align: center;
}
@page {
    size: A4;
    margin: 11mm 17mm 17mm 17mm;
}

@media print {
    footer {
        position: fixed;
        bottom: 0;
        margin:-10px;
    }
    header {
        position: fixed;
        top: 0;
    }
    p {
        page-break-inside: avoid;
    }

    html,
    body {
        width: 210mm;
        height: 297mm;
    }
}
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Magzine Design</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- <script src="main.js"></script>  -->
    
</head>

<body>
    <header>
        <h1>Some Content Name</h1>
    </header>
    <div class="container">
        <div class="content-block item_LayoutOne_100Percent">
            <h2>Demo Heading for 100% Layout</h2>
            It was this, as much as anything, that gave people courage, and I suppose the new arrivals from Woking also helped
                to restore confidence. At any rate, as the dusk came on a slow, intermittent movement upon the sand pits
        </div>
        <div class="content-block item_LayoutTwo_50Percent_layoutOne">
            <h2>Demo Heading for 50% Layout</h2>
            it was this, as much as anything, that gave people courage, and I suppose the new arrivals from Woking also helped
                to restore confidence. At any rate, as the dusk came on a slow, intermittent movement upon the sand pits
        </div>
        <div class="content-block item_LayoutTwo_50Percent_layoutTwo">
            <h2>Demo Heading for 50% Layout</h2>
                it was this, as much as anything, that gave people courage, and I suppose the new arrivals from Woking also helped to restore
                confidence. At any rate, as the dusk came on a slow, intermittent movement upon the sand pits began
        </div>
        <div class="content-block item_LayoutFour_25Percent">
            <h2>Demo Heading for 25% Layout</h2>
                it was this, as much as anything, that gave people courage, and I suppose the new arrivals from Woking also helped to restore
                confidence. At any rate, as the dusk came on a slow, intermittent movement upon the sand pits began
        </div>
        <div class="content-block item_LayoutThree_75Percent">
            <h2>Demo Heading for 75% Layout</h2>
                it was this, as much as anything, that gave people courage, and I suppose the new arrivals from Woking also helped to restore
                confidence. At any rate, as the dusk came on a slow, intermittent movement upon the sand pits began
        </div>

    </div>
    <footer>
        Some Content Name
    </footer>
</body>

</html>

This is my snippet, please help me if I am doing wrong somewhere, I have tried many methods on @print like providing padding or margin to the header and footer, but nothing worked.

DevProf
  • 564
  • 7
  • 19
  • 2
    Please only provide minimal code required to represent your issue. We don't need walls of boiler plate text to have to scroll through to review your html structure. See [mcve] – charlietfl Aug 03 '18 at 16:51
  • 1
    Your question title and question text seem to be asking different questions. The title is about numbering footers; the text seems to be about distorted/collapsed content. What's your actual question? Please keep the guidelines from [*How do I ask a good question?*](/help/how-to-ask) in mind, such as keeping your question specific, targeted, and concise. – T.J. Crowder Aug 03 '18 at 16:52
  • Sorry for the `Title and Question text`, i have edited my question. – DevProf Aug 03 '18 at 17:03
  • 1
    It still does seem to ask two questions. I also don't think that the code in your question produces the problem you describe. It does look fine to me. – Sumurai8 Aug 03 '18 at 20:15

1 Answers1

0

Here is my updated solution in @media print {}

footer {
    position: fixed;
    bottom: 0;
    left: 0; 
    right: 0;
    margin:-10px;
    margin-left: auto;
    margin-right: auto;
}
header {
    position: fixed;
    top: 0;
    left: 0; 
    right: 0;
    margin: auto;
}

I've used this code several times in my projects, which is inspired by this SO answer. Specifying both left and right as 0 allows a margin of auto to center the elements within the <body>.

dandeto
  • 567
  • 4
  • 12
  • Thank you for your valuable reply but if i remove position fixed from both `header` and `footer`, then it will not appear on every page when i used to print the pages. – DevProf Aug 04 '18 at 05:49
  • Thank you very much! I tested my updated code to make sure it works with multiple pages, and it does. The header and footer are centered at the bottom and top on each page. – dandeto Aug 05 '18 at 05:57