05-08-2009, 12:45 PM | #1 | ||
zeldinha zippy zeldissima
Posts: 27,827
Karma: 921169
Join Date: Dec 2007
Location: Paris, France
Device: eb1150 & is that a nook in her pocket, or she just happy to see you?
|
epub code snippets (html / css)
after spending several hours yesterday trying to find that drop cap thread i knew i had seen before i thought it might be nice to have ONE thread where we can all post WORKING code snippets that have been tested, for specific effects or workarounds, so that they are easy to find and use by everyone.
please mention briefly the function of the code (i.e. "Drop Caps") and give the (x)html code and the css code, and a link to the original thread if there was a discussion about the problem. to start with, here is the drop cap code from this thread (thanks jellby, peter sorotkin, brewt, llasram, kovid, and everybody else who worked on this tricky problem !!) xhtml : Quote:
Quote:
Last edited by zelda_pinwheel; 05-08-2009 at 01:06 PM. |
||
05-08-2009, 01:01 PM | #2 |
frumious Bandersnatch
Posts: 7,537
Karma: 19000627
Join Date: Jan 2008
Location: Spaniard in Sweden
Device: Cybook Orizon, Kobo Aura
|
Just to comment on the drop cap code:
It may be desirable to add: Code:
p.initial + p { clear: left; } The "white-space: pre;" part of span.first is needed for cases where there is a space between the drop cap and the start of the line, as in "<span class="drop">A</span><span class="first afterA"> day after</span>", since otherwise the space beginning the span.first is dropped (take care with linebreaks inside this span.first). A similar "afterL" class can be needed. If the drop cap is not A or L, use only class="first". When there is some punctuation before the drop cap, this code may be useful: Code:
span.predrop { font-size: 25%; font-weight: normal; vertical-align: top; line-height: 1.9; } Code:
<span class="drop"><span class="predrop">‘</span>I</span><span class="first"> was</span> never so upset in my life!’ |
Advert | |
|
05-08-2009, 01:03 PM | #3 |
zeldinha zippy zeldissima
Posts: 27,827
Karma: 921169
Join Date: Dec 2007
Location: Paris, France
Device: eb1150 & is that a nook in her pocket, or she just happy to see you?
|
thanks for the clarification jellby !
|
05-09-2009, 10:52 AM | #4 |
frumious Bandersnatch
Posts: 7,537
Karma: 19000627
Join Date: Jan 2008
Location: Spaniard in Sweden
Device: Cybook Orizon, Kobo Aura
|
poetry fragments
Novels often have fragments of poetry or songs interspersed in them. As we all know, poetry has some formatting requirements (hard line breaks) and some display customs (centered or indented block).
I use code like the following when I have to format poetry fragments in a text. I use individual <p></p> for each line instead of just <br/> because I want to use the effect of the negative parindent: when a line is too long to fit the screen, it is "soft-broken" and the second portion is indented farther to the right (actually, I'd like it to be left-aligned and preceded by "[", but I don't think that's possible) xhtml: Code:
<div class="poetry"> <p>Sur une gamme chromatique,</p> <p class="indented">Le sein de perles ruisselant,</p> <p>La Vénus de l’Adriatique</p> <p class="indented">Sort de l’eau son corps rose et blanc.</p> <p class="first">Les dômes, sur l’azur des ondes</p> <p class="indented">Suivant la phrase au pur contour,</p> <p>S’enflent comme des gorges rondes</p> <p class="indented">Que soulève un soupir d’amour.</p> <p class="first">L’esquif aborde et me dépose,</p> <p class="indented">Jetant son amarre au pilier,</p> <p>Devant une façade rose,</p> <p class="indented">Sur le marbre d’un escalier.</p> </div> Code:
div.poetry { margin: 1em 0 1em 2em; } div.poetry p { margin: 0; padding-left: 3em; text-indent: -3em; text-align: left; } div.poetry p.indented { margin-left: 2em; } div.poetry p.first { margin-top: 1em; } For pieces with more complex indent pattern or for mainly poetry works I'd probably do something different, like a <div></div> for each stanza (which could include "page-break-inside: avoid" in the css). Last edited by Jellby; 05-09-2009 at 02:22 PM. |
05-10-2009, 06:28 PM | #5 |
zeldinha zippy zeldissima
Posts: 27,827
Karma: 921169
Join Date: Dec 2007
Location: Paris, France
Device: eb1150 & is that a nook in her pocket, or she just happy to see you?
|
wrapped non-rectangular images, yeah baby !!
i've been looking at the adobe sample ebook of Alice in Wonderland (attached). in chapter 6, they've managed to reflow text around a non-rectangular image :
i've been wanting to figure out how to do that for ages and it turns out it's laughably easy. here's the code : Code:
<p><div style="float:left;max-width:170px;width:8.5em;text-align:left; margin:0.25em 0px 0.25em 0.25em"> <img style="display:block;max-width:342px;width:17em;" src="images/alice23a.gif" alt="Alice talks to Cheshire Cat"/> </div> <div style="float:left;max-width:170px;width:8.5em;max-height:220px; height:11em;margin:0.25em 0.25em 0.25em 0px"/> So she set the little creature down, and felt quite relieved to see it trot away quietly into the wood. (...) when she was a little startled by seeing the Cheshire Cat sitting on a bough of a tree a few yards off. </p> div 1 : define the width** as the width of the narrow leg of the image. place the image inside the div, and define the img width** as the actual width of the entire image. div 2 : define the width** as the actual width of image, and define the height as the height of the wide part of the image. this div is an empty placeholder. place text in paragraph following divs. **notice the width is given as max-width in pixels (actual size of the image) and also in ems ; this allows the images to be resized with the text if text size is enlarged or reduced. the 2 divs stack on top of each other and the text wraps around them, first the wide one, then the narrow one. the image overflows its narrow div and is visible beside the text. this will work for left or right side images (just define the float accordingly) with a shape of an upside down L. i imagine it should be possible to make it work for an image with the large bit on the bottom (like a right side up L) but i've not yet tried it out. maybe it would take a third narrow div and add a "clear" to the wide one, to push the wide one down... there might even be other ways to use this idea. Last edited by zelda_pinwheel; 05-10-2009 at 06:31 PM. |
Advert | |
|
05-11-2009, 08:52 PM | #6 |
Boo-Frickety-Hoo-Erizer
Posts: 251
Karma: 686
Join Date: Oct 2007
Device: Kobo Glo HD!
|
My faaaaavorite topic - drop caps in word
Link to directions, etc., at https://www.mobileread.com/forums/showthread.php?t=36889 for how to get to happen out of Microsoft Word.
At least, W2003 on XP. -bjc |
05-11-2009, 08:55 PM | #7 | |
Resident Curmudgeon
Posts: 76,497
Karma: 136564766
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3
|
Quote:
|
|
05-12-2009, 12:37 AM | #8 | |
Boo-Frickety-Hoo-Erizer
Posts: 251
Karma: 686
Join Date: Oct 2007
Device: Kobo Glo HD!
|
Oh, gosh, let's not argue here - the reference is for how to create a DROP CAP for an EPUB using WORD, as opposed to Amaya or Dreamweaver or Notepad. A specialty snippet of a workable css that also happens to work in Word with a coupla caveats.
Quote:
-bjc |
|
05-12-2009, 08:24 AM | #9 |
Addict
Posts: 304
Karma: 2454436
Join Date: Sep 2008
Device: PRS-505, PRS-650, iPad, Samsung Galaxy SII (JB), Google Nexus 7 (2013)
|
This is such a useful thread
Will this be collated for the wiki once everyone has revealed their deepest darkest css secrets? |
05-12-2009, 10:08 AM | #10 | |
zeldinha zippy zeldissima
Posts: 27,827
Karma: 921169
Join Date: Dec 2007
Location: Paris, France
Device: eb1150 & is that a nook in her pocket, or she just happy to see you?
|
Quote:
that is an excellent idea, and we don't have to wait until all is revealed ; we could start it now. would you like to do the honours ? |
|
05-12-2009, 02:39 PM | #11 | |
frumious Bandersnatch
Posts: 7,537
Karma: 19000627
Join Date: Jan 2008
Location: Spaniard in Sweden
Device: Cybook Orizon, Kobo Aura
|
Quote:
Code:
<html> <head> <style> img { opacity: 0.5; /* Just to make the borders visible */ } div.leftpic { float: left; text-align: left; margin: 0.25em; /* Change at will */ } div.leftpic + div.leftpic { margin-left: -0.25em; /* The negative amount in div.leftpic */ } /* The image */ div#cheshire img { max-width: 1400px; /* Real width of the image (optional) */ width: 20em; /* Desired total width */ } /* The narrow part */ div#cheshire { max-width: 645px; /* Width of the narrow part (optional) */ width: 9.2em; /* 645/1400 * 20 = 9.2 */ border: solid red 1px; /* Just to make it visible */ } /* The wide addition */ div#cheshire + div { max-width: 755px; /* 1400-645 = 755 (optional)*/ width: 10.8em; /* 755/1400 * 20 = 10.8 */ max-height: 885px; /* Height of the widhe part (optional) */ height: 12.6em; /* 885/1400 * 20 = 12.6 */ border: solid green 1px; /* Just to make it visible */ } </style> </head> <body> <p> <div class="leftpic" id="cheshire"> <img src="Alice-23.jpg" alt="Alice talks to Cheshire Cat"/> </div> <div class="leftpic"></div> So she set the little creature down, and felt quite relieved to see it trot away quietly into the wood. 'If it had grown up,' she said to herself, 'it would have made a dreadfully ugly child: but it makes rather a handsome pig, I think.' And she began thinking over other children she knew, who might do very well as pigs, and was just saying to herself, 'if one only knew the right way to change them—' when she was a little startled by seeing the Cheshire Cat sitting on a bough of a tree a few yards off. </p> </body> </html> I think the css is self-explanatory, this keeps the settings specific to an image separated from the global ones. The max-widths are actually optional, but the widths (and height) are proportional to them (I guess that makes it impossible to use percent values, as then width and height proportions are not linked). I don't know if it would be possible to do something more self-contained, that allows an easier change of the image size... This is the result: |
|
05-12-2009, 10:48 PM | #12 |
zeldinha zippy zeldissima
Posts: 27,827
Karma: 921169
Join Date: Dec 2007
Location: Paris, France
Device: eb1150 & is that a nook in her pocket, or she just happy to see you?
|
thanks jellby ! i've been playing with it too today, i'm a bit puzzled because although i first used the same code as adobe, it didn't behave the same way (the divs are next to each other, like in your example, but in the original file i believe they are layered on top of each other... but i don't see a z-index or anything anywhere to explain this...).
there are a couple of other things that are puzzling me in that book, for instance they use entity shortcuts like &C; instead of the full code & rdquo ; which i would like to do but i can't seem to get it to work... when i include the shortcuts at the top of my page before the doctype i get an ]> appearing in my page, and the entities never translate... hm, this bit should be in another thread actually... i'll move it tomorrow (too tired now ). |
05-13-2009, 07:38 AM | #13 |
frumious Bandersnatch
Posts: 7,537
Karma: 19000627
Join Date: Jan 2008
Location: Spaniard in Sweden
Device: Cybook Orizon, Kobo Aura
|
Perhaps this css is better:
Code:
div.leftpic { float: left; clear: left; text-align: left; margin: 0 0.5em 0.5em 0; /* 0 m m 0 */ } div.leftpic img { width: 100%; } /* Image size: 1400 x 1998 */ /* Height of the wide part: 885 */ /* Width of the narrow part: 645 */ div#cheshire { width: 20em; /* w */ max-height: 12.6em; /* (885/1400)*w */ } div#cheshire + div.leftpic { width: 9.2em; /* (645/1400)*w */ height: 15.9em; /* ((1998-885)/1400)*w */ margin-top: -0.5em; /* -m */ } |
05-13-2009, 08:10 AM | #14 |
Wizzard
Posts: 1,402
Karma: 2000000
Join Date: Nov 2007
Location: UK
Device: iPad 2, iPhone 6s, Kindle Voyage & Kindle PaperWhite
|
10% centred bar
From netseeker's post here.
CSS Code:
<style type="text/css"> hr.bar10 { width:10%; margin-left:45%; } </style> Code:
<hr class="bar10"/> |
05-13-2009, 08:19 AM | #15 |
Feedbooks.com Co-Founder
Posts: 2,263
Karma: 145123
Join Date: Nov 2006
Location: Paris, France
Device: Sony PRS-t-1/350/300/500/505/600/700, Nexus S, iPad
|
About the text wrapped around an image: I don't think that this is a wise design decision. It'll work fine if you have a large screen, but on a small screen (smartphone) you might end up with something that looks very ugly.
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
direkter Umlaut oder lieber HTML Code in Epub ? | NASCARaddicted | Erste Hilfe | 14 | 06-16-2011 06:54 AM |
Programming language code snippets in ebooks? | Connochaetes | Writers' Corner | 7 | 10-18-2010 03:43 PM |
ebook-convert HTML to EPUB and problem with <pre><code> | mikegr | Calibre | 2 | 03-09-2010 03:27 PM |
css override code for margins? | Amalthia | Calibre | 15 | 08-11-2009 08:20 PM |
Problems generating ePub from HTML/CSS | AlexBell | Calibre | 3 | 07-17-2009 06:10 AM |