![]() |
#1 |
Evangelist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 474
Karma: 41524
Join Date: Sep 2011
Device: Kobo Libra 2 & Clara BW
|
Padding v margin?
As I understand it, the basic difference between padding and margin is that margin is adding space around an element and padding is adding space within an element.
I don't understand how the difference works for things such as p and h1 with pure text (no borders, decoration, etc.). When I compare, for example, "margin: 1em" to "padding: 1em" in a p or h1 definition, the padding takes up a bit more space. Why is this? I've looked at a few tutorials, which usually have some variation of what I wrote in the first paragraph and apply padding v margin to things which have borders, in which case the around v. within difference makes more sense. Why the difference I'm seeing for normal text? As is often the case, I feel I'm missing something simple. |
![]() |
![]() |
![]() |
#2 |
A Hairy Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 3,291
Karma: 20171067
Join Date: Dec 2012
Location: Charleston, SC today
Device: iPhone 15/11/X/6/iPad 1,2,Air & Air Pro/Surface Pro/Kindle PW & Fire
|
margin tends to give space around the container, padding gives space within the container. Mostly noticeable when you are dealing with borders for example. However sometimes margin can be eaten by the renderer.
The box model diagram is the best thing to study for understanding...but it seems you got that. Application is just experience. |
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Resident Curmudgeon
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 78,914
Karma: 143098300
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3
|
In some cases, when you use a page break command to split a page, using a margin to have some top stop on the split may not work. Padding will work.
|
![]() |
![]() |
![]() |
#4 | |
Evangelist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 474
Karma: 41524
Join Date: Sep 2011
Device: Kobo Libra 2 & Clara BW
|
Quote:
What's the container for a normal text paragraph? If I'm reading your entire post correctly, including "margin can be eaten by the renderer" the major difference is that the renderer can shrink margins (but either not padding or they don't shrink padding as much) and that's why I'm seeing, for example, a 1em padding as taking more space than a 1em margin, at least in the Calibre editor (which is where I'm looking). |
|
![]() |
![]() |
![]() |
#5 |
A Hairy Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 3,291
Karma: 20171067
Join Date: Dec 2012
Location: Charleston, SC today
Device: iPhone 15/11/X/6/iPad 1,2,Air & Air Pro/Surface Pro/Kindle PW & Fire
|
That is correct. The box model works for any element in HTML, including the <p>...you may see some used more effectively than the <p>, but it is there...
Here is a tutorial page for anyone else who is interested, but it seems you got the hang of it. |
![]() |
![]() |
Advert | |
|
![]() |
#6 |
Evangelist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 474
Karma: 41524
Join Date: Sep 2011
Device: Kobo Libra 2 & Clara BW
|
Just to make sure I understand, margin and padding should work the same around normal text (p, h1, etc.), but some ereaders might treat them differently.
For example, in a pure text document if I want a heading which starts a page to be separated a bit from the top of the page and the paragraph which follows it, I'd normally use: Code:
h1 { font-size: 1.5em; margin: 0.5em 0; line-height: 1.2em; /* reduce space between lines if text wraps */ } I'd guess conversion to kepub and how the reader renders that might also cause differences. Last edited by foosion; 09-20-2024 at 02:31 PM. |
![]() |
![]() |
![]() |
#7 |
frumious Bandersnatch
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 7,542
Karma: 19001583
Join Date: Jan 2008
Location: Spaniard in Sweden
Device: Cybook Orizon, Kobo Aura
|
Margins are combined/collapsed, padding is not. A div with bottom-margin:2em followed by a div with top-margin:1em will have 2em spacing between their contents (the largest margin survives). The same with padding will result in 3em spacing (they're just added). If there are borders, margin goes outside, padding goes inside.
|
![]() |
![]() |
![]() |
#8 | |
Evangelist
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 474
Karma: 41524
Join Date: Sep 2011
Device: Kobo Libra 2 & Clara BW
|
Quote:
In my test case, there's no explicit div. If what I read is correct, the default margin, padding and border for div is 0. I tried surrounding a chapter with <div> ... </div> (just inside body) and defined div as margin; 0. It didn't make any difference (as it shouldn't, based on the default). |
|
![]() |
![]() |
![]() |
#9 |
frumious Bandersnatch
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 7,542
Karma: 19001583
Join Date: Jan 2008
Location: Spaniard in Sweden
Device: Cybook Orizon, Kobo Aura
|
body, html and @page could all have margins (default or defined) that might be collapsed with other element margins. Depending on the reading application, you can set custom page margins that could be internally implemented as one of those, or something else. For debugging purposes, you could add some thin borders to better see your boxes.
|
![]() |
![]() |
![]() |
#10 |
Well trained by Cats
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 30,876
Karma: 59840450
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
|
Many devices control Margins (ignore publishers style version)
I use Padding to sneak past that roadblock |
![]() |
![]() |
![]() |
#11 | |
Fanatic
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 517
Karma: 8500000
Join Date: Aug 2013
Location: Hamden, CT
Device: Kindle Paperwhite (11th gen), Scribe, Kindle 4 Touch
|
Quote:
Other than the inside/outside part, the biggest difference between margins and padding is that margins collapse and padding does not. An example: Code:
.p1 { margin-top: 1em; margin-bottom: 1em; } .p2 { padding-top: 1em; padding-bottom: 1em; } |
|
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
top margin padding in Apple Books | kaiaua | ePub | 21 | 05-24-2020 03:20 PM |
CSS: Margin vs. Padding | ButWhy | Workshop | 6 | 07-31-2015 12:55 PM |
Top-margin (bottom-margin?) for Kobo? | Notjohn | Sigil | 14 | 02-28-2015 06:43 AM |
CSS: margin-top and margin | Leonatus | ePub | 16 | 06-16-2014 04:29 AM |
calibre ignore margin-top and margin-bottom | bender | Calibre | 2 | 12-11-2009 06:58 AM |