danfickle / danfickle/openhtmltopdf

Top and Bottom Margin collapse is not as per standards

Open
#147 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2.2k
Forks
423
PR merge metrics
No merged PRs in 30d

Description

Hi @danfickle ,

As per [https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model/Mastering_margin_collapsing](url) :

> If there is no border, padding, inline part, block formatting context created, or clearance to separate the margin-top of a block from the margin-top of its first child block, the collapsed margin ends up outside the parent.

In the below scenario, if I provide margin-top to a child block, the margin is not getting applied to the child block. I specifically had to provide border/ padding on the parent block to get the margin.

Here I have provided 0.75in margin on the child block and there is no border/padding or similar property is provided on the parent/ child to offset the margin on the child and apply it. Below is the output that I get, whereas the expectation is to get 0.75in of margin from the top. As per CSS Box Model specifications, the child margin should go beyond your parent block and get applied.

![capture](https://user-images.githubusercontent.com/6688232/32648741-412fe3de-c61d-11e7-88e4-2d40db515b6c.PNG)

If I open the same html in a browser, it provides 0.75in of margin from the top. Here goes a snapshot of it :

![capture1](https://user-images.githubusercontent.com/6688232/32648918-026ca334-c61e-11e7-9e40-49f8d510b000.PNG)

I analysed the BlockBox class and I feel the collapseTopMargin should take the child blocks parent top margin from its entire DOM tree up till the root. Then we should collapse the top margin of the child block only if the parents margin top exceeds that of the childs. However, this is just a naive approach. There are other subtleties that should be factored in for margin collapsing.

```


@page
{
size:8.5in 11in;
-fs-page-orientation: portrait;
margin-left: 10px;
margin-right:15px;
margin-top:10px;
margin-bottom: 0;
}





Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.