Great question! If all your lines are already wrapped in a class (e.g., <span class="indent">), you can target that class directly in your CSS, and you do not need to add a new class or re-wrap the lines.
Here’s what to do:
1. Use the Existing Class in Your CSS
Suppose your lines look like this:
<span class="indent">This is the first line.</span>
<span class="indent">This is the second line.</span>
<span class="indent">This is the third line.</span>
You can use the :nth-child() selector in CSS to alternate the direction:
.indent:nth-child(odd) {
direction: ltr;
unicode-bidi: embed;
}
.indent:nth-child(even) {
direction: rtl;
unicode-bidi: embed;
}
2. Add This CSS to Your Stylesheet
- Open your EPUB’s CSS file (e.g.,
stylesheet.css).
- Add the above CSS rules at the end of the file.
3. Check the HTML Structure
-
The :nth-child() selector works relative to the parent element.
-
If your <span class="indent"> elements are direct siblings (i.e., one after another inside the same parent, like a <div> or <body>), this will work perfectly.
-
Example:
<body>
<span class="indent">Line 1</span>
<span class="indent">Line 2</span>
<span class="indent">Line 3</span>
...
</body>
-
If your lines are inside different parent elements (e.g., each inside a <p>), you may need to adjust the selector. For example:
<p><span class="indent">Line 1</span></p>
<p><span class="indent">Line 2</span></p>
In this case, you can target the <p> elements:
p:nth-child(odd) .indent { direction: ltr; unicode-bidi: embed; }
p:nth-child(even) .indent { direction: rtl; unicode-bidi: embed; }
4. Repackage and Test
- Save your changes.
- Repackage your EPUB and test in your reader.
Summary Table
| Your HTML Structure | CSS to Use |
|---|
<span class="indent"> siblings | .indent:nth-child(odd) { ... } .indent:nth-child(even) { ... } |
Each line in <p> | p:nth-child(odd) .indent { ... } p:nth-child(even) .indent { ... } |
If you’re unsure, paste a small sample of your HTML and I can give you the exact CSS!