I’m not 100% sure this is the best way to do it, but this was my attempt, trying to get something that scales decently with device size: https://imgur.com/a/6mACixW
My explanation of how it all works is going to be a little long, sorry, but it boils down to needing to do three things:
- Add the tag “bookimg” to any passages that you want to look like this (this lets us only apply our styles to these pages)
- Add the image and two custom blocks to those passages (see “passage template” below)
- Add some special styles and adjust the values to match whatever image and text you use (see “styles” below)
Passage template
<html><img id="book-image" src="LINK TO THE IMAGE OF A BOOK YOU WANT" alt=""></img></html>
@@#page-1.pages;
TEXT OF FIRST PAGE OF BOOK
@@
@@#page-2.pages;
TEXT OF SECOND PAGE OF BOOK
@@
What this does is define three “objects” in the passage:
- An html image (using verbatim html to prevent whatever sugarcube tried to do with it when I used sugarcube’s image tag. I’m not that familiar with sugarcube).
- Two sugarcube custom elements, which are basically just boxes labeled “page 1” and “page 2”, holding the content for page 1 and page 2 of the book respectively
The order is important. If you define the image after, it’ll show up on top of the text instead of underneath.
Styles
.bookimg{
.passage {
color: black;
width:100%;
display:grid;
grid-template-columns:17% 1fr 1fr 17%;
}
#book-image{
grid-column-start: 1;
grid-column-end: -1;
grid-row-start: 1;
width: 100%;
}
.pages {
grid-row-start:1;
padding:10px;
margin-top:20%;
font-size:1cqw;
}
#page-1{
grid-column-start:2;
}
#page-2{
grid-row-start:1;
grid-column-start:3;
}
}
The image I used in my example was https://img.magnific.com/free-photo/old-book-open-dark-wood-background_1373-581.jpg (free-to-use image I found online). If you use a different image, you’ll need to adjust the styles a bit. I’ll go through the whole thing and note which styles can be adjusted to fit different images.
First of all, the .bookimg{ } around the whole thing means we’re only going to apply all the styles inside of those curly braces on any passage with the “bookimg” tag.
Next up, .passage is the box on the page that contains your text, but not the navigation.
color:black sets the font color to black, so you can see it on the white pages.
width:100% makes it as big as the width of that section of the page (instead of as big as the background image naturally wants to be, which might be much smaller or much bigger).
display:grid and grid-template-columns are the important thing here. We define one column on either side that’s 17% of the total width, to account for the book’s margins and the extra space at the side of the image, and then two columns in the middle that are each “1 fraction” of the remaining space, in other words they each get half of the space in between. If you use a different image, you’re going to want to adjust the 17% columns to match however much space is actually to either side of your image. (Make sure to use percentages so that they scale with the window size!)
#book-image applies styles to the book image (which we gave that id in the passage text). We say that it starts in the first column (1), and ends in the last column (-1) (meaning it covers all the columns), is on the first (only) row, and gets as wide as it can.
.pages applies styles to both the pages at once.
- We say the pages also start in the first grid row, so that it puts them on top of the image instead of trying to push them down to a second row.
padding:10px adds a little space between the text and the edges of the pages so the text doesn’t meet at the center of the book.
margin:20% pushes the text down to the right place in the image. You will need to adjust this if you use a different image. (Remember to keep it a percentage again).
font-size:1cqw says that the text is 1 “container query width”, aka the font size scales with the width of the passage. This means that our text gets smaller when the image gets smaller, and bigger when the image gets bigger, so it should always fit in the image. You will need to adjust this until your text fits in the pages. (Note that the font size can use decimals: 1.2cqw will be quite a big bigger than 1cqw.)
Finally, we say that the first page starts in the second column (the first “1 fraction” column after the 17% margin we defined) and the second page starts in the third column.
Again, sorry for the length of the explanation and let me know if you have any more questions…
This is the full passage I used to test, including the random text:
<html><img id="book-image" src="https://img.magnific.com/free-photo/old-book-open-dark-wood-background_1373-581.jpg" alt=""></img></html>
@@#page-1.pages;
Here is some test representing a book. Etiam diam metus, tempus ut leo at, ullamcorper sodales ligula. Maecenas ac tincidunt arcu, id semper augue. Fusce quis lectus tincidunt, scelerisque eros a, tempor augue. Donec vel porttitor nulla, at lobortis nisi. Donec velit metus, convallis sit amet nunc vitae, gravida fermentum nisl.
Mauris sit amet augue ipsum. Mauris consequat, purus non pellentesque auctor, felis tellus fermentum lorem, vitae volutpat sem sem et massa.
@@
@@#page-2.pages;
Pellentesque elit tortor, laoreet non turpis non, efficitur pulvinar mi. Etiam ultricies lectus nec tincidunt condimentum. Donec eget blandit ipsum. Praesent et neque pretium, egestas dolor eu, aliquet lacus. Etiam a ligula sodales, lobortis odio sit amet, sollicitudin enim. In porta gravida purus, sit amet varius mi consectetur eu. Nullam mi purus, rutrum quis molestie et, molestie at lacus. Maecenas non libero a lacus egestas pulvinar. Praesent luctus fringilla neque, et efficitur lectus finibus sit amet.
@@