Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force landscape orientation in CSS

Tags:

css

printing

I do set to landscape the size property of a CSS file to print pages in landscape

@page {
          margin: 0.1cm;
          size: landscape;
          orientation: landscape;
          size: A4;

Some pages are printed into portrait orientation, some in landscape orientation.

How can i detect and force the pages to be printed in landscape using CSS ?

like image 783
Ângelo Rigo Avatar asked Sep 06 '16 19:09

Ângelo Rigo


People also ask

How do you force landscape in CSS?

screen. orientation. lock('landscape'); Will force it to change to and stay in landscape mode.

What is orientation landscape CSS?

portrait. The viewport is in a portrait orientation, i.e., the height is greater than or equal to the width. landscape. The viewport is in a landscape orientation, i.e., the width is greater than the height.

How do I lock screen rotation?

Whether you are holding your device upwards or sideways, swipe down from the top of the screen. Tap the Auto rotate icon to lock your device in your desired position.

How do I change my view from portrait to landscape?

1 Swipe down the screen to access your Quick Settings and tap on Auto Rotate, Portrait or Landscape to change your screen rotation settings. 2 By selecting Auto Rotate,you will easily be able to switch between Portrait and Landscape mode.


2 Answers

The CSS you provided has problems:

  1. The bracket does not close (missing }),
  2. You overwrite size with A4
  3. orientation seems to exist for media queries, but not as an attribute (source). Hence you can check the orientation, but not set it.

I tried

@page {
    size: A4 landscape;
}

in Chrome 60 on Linux and it worked. Seems not to work in Firefox, though.

You could set the width and height of the <body> to whatever you need (only for printing by using the media query) just like paper.css does.

Alternative

You could add

body {
    transform: rotate(-90deg);
}

Seems to be safe to use.

like image 56
Martin Thoma Avatar answered Sep 29 '22 11:09

Martin Thoma


The problem is first you write size: landscape; but than you write size: A4; If not provided domPDF is rendering like default portrait, and it overwrites the landscape. set the second parameter landscape and will work out.

size: A4 landscape; 
like image 24
Vlatko Petrush Avatar answered Sep 29 '22 12:09

Vlatko Petrush