Trying to make a passage look like a book

I’m trying to make a passage look like an open book, I’m trying to get an image of book pages to show as the background, and then have text be placed on the image but I can’t figure it out.

My main question is how to display an image as a background on a single passage and have text wrap with the image.

(Not necessary but I would like a detailed explanation on how the CSS works since I don’t know anything about it)

I’m kinda confused. Are you preparing text of an interactive fiction piece for print publication? Or trying to code a simulation of a print book?

Sorry I should’ve specified, I am trying to code a simulation of a book in my Twine game. I have edited the page to be less confusing

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:

  1. Add the tag “bookimg” to any passages that you want to look like this (this lets us only apply our styles to these pages)
  2. Add the image and two custom blocks to those passages (see “passage template” below)
  3. 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:

  1. 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).
  2. 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.
@@

Thank you so much for taking the extra time to add in all of the descriptions of the CSS it definitely helped a lot. I’m so sorry but I forgot to mention that the passage works like a pop-up.

This is the code that I used for it:

<<link "text">>
 <<run Dialog.create("Book")
     .wikiPassage("PassageName")
       .open()>>
<</link>>

The text and formatting works when it is a passage, but not a pop up… This works beautifully, but I didn’t think there would be in issue so I didn’t think to mention it so this is 100% my fault I am so sorry :sob:.

No worries, I’m busy right now but I’ll see if I can take a look later and see what needs to be done to fix that. My guess is it just means we need an alternative way to refer to the passage instead of using the tag, or something similar.

I don’t know if you’re familiar at all with the browser developer tools (usually right-click and then “Inspect page”), but if you’re interested in learning more, it might be a useful exercise to open that up and take a look at how the actual html looks when the passage stands on its own (see if you can find which elements the css gets applied to), and what looks different when it’s a pop-up (does the element with class “passage” still exist? Does it have the “bookimg” class? etc.)

Okay, pretty simple fix!
In your Dialog.create command, we’re going to add the class “bookimg” to the created dialog like so:

<<run Dialog.create("Book", "bookimg")
     .wikiPassage("Test passage")
       .open()>>

Then, in the stylesheet, change .passage { in the second line to &#ui-dialog-body { since we’re no longer applying the styles to a passage, but a dialogue element.

(The symbols at the beginning of that line are known as “css selectors”. You can look up that phrase to learn more. I like this little game for learning how to use selectors :slight_smile: https://flukeout.github.io/)

Thank you so much!

By the way, I just realized I forgot the closing </html> tag after the image in my passage template. It doesn’t seem to affect anything in my test, but you should probably put it in; it’s possible that leaving it out might prevent sugarcube things from working later in the passages. I edited my post up there so it’s correct now.

Uh, there shouldn’t be <html> tags in passages at all: there’s only supposed to be one of those at the start and end of a whole HTML document. That’s an error and the browser will silently disappear those.

It’s just sugarcube’s notation for starting a verbatim html section: SugarCube v2 Documentation
The html tags don’t end up in the final page.

Oh, wow, TIL. That’s an unfortunate choice of notation. Thanks!

Wait, why are you using them here, though? Is there something in that <img> tag that SugarCube would modify without the verbatim markers?

…And as I was just in the process of typing up: I now am realizing I don’t think the html tags are actually necessary anyway. My past experiments with Twine have all been with Harlowe, so I was just jumping around the sugarcube documentation finding whatever looked like it would do the job for the template passage here, haha. I saw the “verbatim html” section and assumed that meant it wouldn’t accept html tags directly. Which, apparently, is wrong.

(There are probably improvements that could be made to my css as well. I’ve been learning responsive web development over the past couple of weeks, and it’s quite possible I’m in “everything looks like a nail mode” and may have overcomplicated things a bit in some places.)

I’ve been very busy recently so I haven’t been able to test it until now, it works great! Regarding to the html tag I just didn’t use it since all the previous times I’ve used html it has worked fine without an tag. I guess I would have one more question… How would I add an image to “turn” the page and reveal more pages/information?

Is it possible to call dialogs from within dialogs in sugarcube? My first thought would be to add a “next page” button below the book that just links to another dialog formatted in the same way.

Yes, and it will replace the current Dialog with the new Dialog. However you need to at least call .wikiPassage() again manually.

So for example:

<<button "Next Page">>
   <<run Dialog.wikiPassage("page 2")>>
<</button>>

As a peripheral subject, I have always thought Undum had the most beautiful book-like presentation. It doesn’t maintain the physical look past the splash screen and endpaper background, but the parchment feel, the font, the drop-caps, and the text flowing around illustrations feels like an expensive book.

It’s so stupidly simple and it works so well… :sob: Thank you so much for taking the time to go through and figure out Sugarcube to get this.