{"id":2477,"date":"2017-05-31T11:51:17","date_gmt":"2017-05-31T11:51:17","guid":{"rendered":"http:\/\/cssreset.com\/?p=2477"},"modified":"2017-05-31T11:51:17","modified_gmt":"2017-05-31T11:51:17","slug":"how-to-use-csss-page-break-properties","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/how-to-use-csss-page-break-properties\/","title":{"rendered":"How to Use CSS&#8217;s Page Break Properties"},"content":{"rendered":"<p>As you&#8217;re probably already aware, CSS lets you completely dictate how your HTML elements behave &#8212; how they&#8217;re positioned, what they look like, colors, shapes, etc &#8212; on a webpage. BUT, did you know you can also use CSS to dictate how particular elements are meant to behave when a page or document is printed?\u00a0This sort of CSS sorcery can be done using the page break properties, which is actually a set of three properties (page-break-before, page-break-after, and page-break-inside) that is used to determine where a page break should occur in relation to selected elements.<\/p>\n<p>In this tutorial, we&#8217;re going to go over each of the page-break elements to make sure you understand what their function is and exactly how you might use them in context.<\/p>\n<p>To use any of these properties, you must designate them as paged media, so be sure to use them within the print media queries, like this:<\/p>\n<pre>@media print{\r\n\u00a0 \u00a0 h1{\r\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 page-break-inside: avoid;\r\n\u00a0 \u00a0 }\r\n}<\/pre>\n<h2>Page-Break-Before<\/h2>\n<p>The page-break-before property is used to add a page break before the selected element. To make sure a page break occurs before the element in question, you&#8217;ll need to apply the value &#8220;always&#8221; to the page break property. So, to be sure that the page breaks before a div element with the class of .main, for example, your code would need to look something like this:<\/p>\n<pre>div.main{\r\n\u00a0 \u00a0 page-break-before: always;\r\n}<\/pre>\n<p>In addition to always, other values that this property takes are <strong>auto<\/strong> (this is the default), <strong>avoid<\/strong> (this value would dictate that a page break before the element should be avoided), <strong>left<\/strong> (this forces a page break so that the next page is formatted as a left page), and <strong>right<\/strong> (this forces a page break so that the next page is formatted as a right page). The property also takes the values initial and inherit.<\/p>\n<h2>Page-Break-After<\/h2>\n<p>The page-break-after property is just about the same as the page-break-before property, only this property (as you might be able to guess) makes sure that the page breaks\u00a0<em>after<\/em>\u00a0the selected element, not before. So, for example, to have the page break after the div element with the class of .main, your CSS would need to look like this:<\/p>\n<pre>div.main{\r\n\u00a0 \u00a0 \u00a0page-break-before: always;\r\n}<\/pre>\n<p>The page-break-after property also accepts all of the same values as the page-break-before property, so they really are very similar.<\/p>\n<h2>Page-Break-Inside<\/h2>\n<p>The page-break-inside property refers to the page breaking inside of the element that&#8217;s selected. So if you wanted the page to break right in the middle of a paragraph, you could use this property to do that. Interestingly though, you can also use this property to make sure that a page <em>doesn&#8217;t<\/em> break right in the middle of a paragraph by applying the avoid value to the property, like this:<\/p>\n<pre>p{\r\n\u00a0 \u00a0 \u00a0page-break-inside: avoid;\r\n}<\/pre>\n<p>The code above applies to all paragraph elements on a page, so if you&#8217;re not a fan of page breaks that occur mid-paragraph, this is a good line of code to add to your CSS if your web pages are likely to be printed and you want to make sure that the presentation is the best it can be. The page-break-inside property only takes the values avoid and always (and initial and inherit), and does not take the right or left values like the other two page-break properties do, so be sure to keep that in mind when you&#8217;re coding.<\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>As you&#8217;re probably already aware, CSS lets you completely dictate how your HTML elements behave &#8212; how they&#8217;re positioned, what they look like, colors, shapes, etc &#8212; on a webpage. BUT, did you know you can also use CSS to [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/how-to-use-csss-page-break-properties\/\" title=\"Click to read 'How to Use CSS&#8217;s Page Break Properties'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,36],"tags":[],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/2477"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=2477"}],"version-history":[{"count":2,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/2477\/revisions"}],"predecessor-version":[{"id":2480,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/2477\/revisions\/2480"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=2477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=2477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=2477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}