What are some effective ways to propose horizontal lines as useful breaks for web page layout?

I'm currently working on enhancing the print layout of my web pages using CSS. One issue I've encountered is figuring out how to properly handle page breaks. I have managed to avoid breaks in the middle of images and text blocks that should stay together, which is great. However, there's a specific scenario where a horizontal rule separates a new section with just its title (and possibly a brief description), leaving the rest of the section for the next page. This results in a confusing format as ideally, I would prefer the entire section to move to the next page if the title is too close to the bottom of the current one.

The HTML structure is quite simple, consisting of a horizontal rule, header, description paragraph, followed by the content of the section. I attempted to group the header and description within a division to prevent any page breaks inside or after it like this:

<a id="section_1"></a>
<div class="no-page-break">
    <h2>Section 1</h2>
    <p>Section 1 Description</p>
</div>
<div>
    <!-- all the content for this section -->
</div>
<hr />
<a id="section_2"></a>
<div class="no-page-break">
    <h2>Section 2</h2>
    <p>Section 2 Description</p>
</div>
<div>
    <!-- all the content for this section -->
</div>
<!-- and so on -->
.no-page-break { page-break-after: avoid; page-break-inside: avoid }

My expectation was to make each block unbreakable and shift them entirely to the next page, but unfortunately, it didn't work as intended. It's possible that I've been pondering over this problem for too long. Does anyone have suggestions on how to ensure that each section of my page stays intact without breaking up in such a manner?

Note: I don't want to enforce a break before every section, as that might lead to wasted space on some pages.

Bonus: If you know how to hide the horizontal rule if it gets split due to a page break, that would be a nice touch. But it's not a major concern at the moment.

Answer №1

To achieve the desired effect, one potential method is to reposition the horizontal rule to the end of the preceding section and just before where you intend to insert the page break. Subsequently, encase all content following this point within a designated node and assign it a "no-page-break" attribute. Printing dynamics involve numerous variables that may not be accurately interpreted by browsers; thus, strategic grouping of elements can greatly aid in resolving such issues.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I recently installed Bootstrap 5 and jQuery 3.6 on my website, but I'm encountering an issue where the dropdown button is not functioning properly when I try to embed them. Could there be

I have this code: I recently downloaded Bootstrap 5 and JQuery 3.6, but I am facing an issue where the dropdown button is not working when I embed them in my project. Can you take a look at my code to see if anything is missing? <head> <ti ...

Identify and correct improper or invalid HTML closing tags in Visual Studio Code

As part of my transition to Visual Studio Code from Adobe Brackets, I am looking to replicate the feature that highlights incorrect close markup in my code. This feature is particularly helpful in identifying duplicated </div> tags like shown in the ...

Position a box to the right of a Bootstrap 3 fluid container

I've been grappling with Bootstrap and just launched a web app using Bootstrap 3. I'm trying to position a box on the right side of the screen, but despite several hours of researching and testing, I can't seem to find the solution... This i ...

Automatically remove the entered data

Let's say I have a text input field with the initial value of "insert text here" coded like this: <input type="text" value="insert text here" /> What I want is for the text inside the input field to disappear automatically when the user clicks ...

How can the object currently being dragged be chosen using JQueryUI Draggable?

Working with JQueryUI draggable, I am interested in applying styling to the draggable element while it is being dragged. Various attempts have been made using code like this: $(".ui-widget-content").draggable({ drag: function(event, ui) { $(this).cs ...

Is there a way to include a text file in HTML for referencing or posting on my website?

Managing a website where my colleagues frequently post updates can be time-consuming as I am the only one knowledgeable in html and css. I find myself constantly formatting their posts and creating new pages for them. I am exploring ways to simplify this ...

Issues arise when setting a delay for CSS transitions to occur due to the

I'm trying to create a scroll bar that only appears when hovered over, similar to how it works on Facebook where the scroll bar shows up slowly instead of all at once. I've tried using CSS transition delay, but it doesn't seem to be working ...

Position the image on top of the details

I have an image with a spinner rotating around it through animation, accompanied by two boxes containing information as depicted in the uploaded image below. However, I would like to position the image above the boxes rather than beside them. .about-img ...

vertical alignment, almost there

Is there a way to align a block of text so that the top of the first line is positioned 50% down from the top of the td element? Imagine this scenario, where the top of the first row is exactly at the 50% mark. -------------- | | | ...

Learn how to implement the JQuery ReplaceWith function with the resources provided by materials.io icon

I would like the favorite_border icon to switch to the favorite icon when clicked. As we are using material.io and both icons have the class material-icons, I am unsure about how to implement this using jQuery. What steps should I take to achieve this? (w ...

My website is currently being hosted on Github, but I am experiencing issues with the CSS. I keep getting an error message saying "Failed to load resource: the server responded

view image here I recently uploaded my website on Github and am facing an issue with the CSS file not working properly. Can someone please help me troubleshoot this problem? website link Github repository I have reviewed the links connecting the HTML t ...

Choose only the initial element, regardless of its category

Is there a way to apply a specific style exclusively to the very first element within a container, only if it is an h1, h2, or h3? However, if there are multiple headings of different levels on the same page, only the initial one should receive the designa ...

html table displaying incorrect data while using dynamic data in vue 3

example status 1 Hello there! I'm trying to create a table (check out example status 1 for guidance). Here's the code snippet I am using: <table> <tr> <th>Product</th> <th>Price</th> <th>Av ...

Calculating the sum of all elements in an array

Hi, I am currently attempting to retrieve the total value from an array (such as Arr[1] + Arr[2], and so forth). Unfortunately, I am struggling to find a solution. Below is my existing function: this.hourlyTotals = function() { var custsperhour = Math ...

Android tablet (Nexus) optimized for full-height website viewing

I have been attempting to retrieve the Nexus tablet's height by using $(window).height();, but I am encountering difficulties because it seems to include the height of the URL bar in the result, which disappears when the user scrolls. My webpage is d ...

Designing versatile buttons with HTML

When accessing the website on a phone, I have trouble seeing and tapping on these two buttons because they are too far apart. I would like to change it so that once a file is selected, the 'choose file' button will be replaced by the upload butto ...

Is the image clickable to slide back and forth?

How can I achieve a sliding effect when clicking on the bottom-coupon class? Currently, I am only able to implement show and hide functionalities without any sliding effects. The picture slowly hiding from right to left, with the coupon-layer sliding out f ...

After moving to a new server, the media queries seem to be unresponsive

Currently, I am in the process of working on a website stored in a Github repository that I have also published live on another server for testing purposes involving APIs and databases. However, after transferring an exact duplicate of the Github files to ...

The height of the TinyMCE editor refuses to adjust

Hello, I'm new to utilizing Tinymce within an AngularJS environment. Specifically, I am working with the Tinymce library version 4.3.13. I have constructed some HTML and angularJS code to implement the Tinymce editor, but I've encountered a probl ...

Fuzzy backdrop with razor-sharp edges

In the process of designing a website, I am in need of a header with a blurred background that retains sharp edges. To achieve this effect, I implemented the use of ::before in my CSS code. Current Outcome: (For full clarity, it may be necessary to view ...