LoginSignup
4
4

More than 5 years have passed since last update.

Chrome で body のマージンがゼロで可視な要素がすべて fixed だと印刷できない

Posted at

できません。

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style>
        body {
            margin: 0;
        }
        .a {
            position: fixed;
            left: 20px;
        }
        .b {
            position: fixed;
            left: 40px;
        }
    </style>
</head>
<body>
    <div class="a">A</div>
    <div class="b">B</div>
</body>
</html>

どうやら body の高さ(あるいは html の高さ?)がゼロになるからのようです。

適当な縦のマージンを指定したり、

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style>
        body {
            margin: 1px 0;
        }
        .a {
            position: fixed;
            left: 20px;
        }
        .b {
            position: fixed;
            left: 40px;
        }
    </style>
</head>
<body>
    <div class="a">A</div>
    <div class="b">B</div>
</body>
</html>

もしくは、なにかしら body の高さを伸ばす要素があれば印刷できます。

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style>
        body {
            margin: 0;
        }
        .a {
            position: fixed;
            left: 20px;
        }
        .b {
            position: fixed;
            left: 40px;
        }
        .hidden {
            visibility: hidden;
            height: 1px;
        }
    </style>
</head>
<body>
    <div class="a">A</div>
    <div class="b">B</div>
    <div class="hidden"></div>
</body>
</html>
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4