Remove line breaks between images when compiling

I would step back a bit and take a look at why the images come out on their own lines in the first place, and see if that can be solved at the source. There is nothing wrong with having more than one graphic on a line, fundamentally. HTML treats graphics much like the editor in Scrivener does—they kind of act like “letters” in how the flow around text and wrap to the width of the display.

But most images in ebooks don’t work like that, and as a result there is one line of CSS in our provided default that forces a one-image-per-line output. Thus what we are observing here is not how eBooks work by default, this is something Scrivener is doing, and if that is the case you can simply switch off that behaviour!

Let’s use the sample project provided by JoRo to start from:

  1. Firstly I drag each image out of the table, into the Binder, so that I can use them elsewhere.

  2. In the file called “2”, I add:

    <$img:1> <$img:2> <$img:3>
    

    (Feel free to drag in the images themselves if that is what you prefer, it’s all the same to Scrivener.)

  3. Run a test compile with the provided settings. Drill down into the “Scrivener Test/source/Scrivener Test/OPS” subfolder and locate the HTML file that has the images (Quick Look is handy here—but in this example the answer is obvious as there are no section breaks, meaning only one body.xhtml file to concern ourselves with).

  4. In your browser, with this file loaded, right-click on one of the images in the vertical column of them, and “Inspect” it (the command may be different depending on the browser). Open the CSS or “Styles” sidebar if necessary.

  5. All right, now you can see exactly which forces are influencing the image you right-clicked on. The suspicious line is here:

    display: block;
    

    A “block” element in HTML always falls on its own line, kind of a like a paragraph would. Ordinarily images act like letters, as I described earlier, but with this one simple CSS command we’ve told all images in the book to stop acting like letters, and start acting like paragraphs.

    Most browser have a checkmark beside each CSS line. Try disabling this line and examine the result. That’s also a very useful capability if you don’t know what you are looking for, as the results update in real time within the browser.

  6. We’ve now identified the culprit, here’s how to fix it:

    1. Edit your compile format, and click on the CSS pane.

    2. We can’t modify the CSS itself, because that is on the “Default Stylesheet” side of things. We can only edit the “Custom Stylesheet”. However given how CSS works this is no problem. We can override earlier declarations in the CSS file by adding the following line in the “Custom Stylesheet” column:

      img { display: inline; }
      

      “Inline” is the default behaviour for images, so we’re mainly just telling the eBook reader to ignore the earlier declaration to convert how images display, and go back to how they work by default in HTML.

You’ll want to proof other images in your project and make sure they are all working as intended. Normal figures on their own lines should be fine, including those with captions. It would mainly be a matter of any custom image uses in contexts where the image may not have been added to its own line, and you’ve been relying on this block behaviour the whole time to “correct” that problem. An image placeholder in the title Suffix field for instance, might technically be on the same line as the chapter heading, but display on its own line centred below it thanks to how the CSS works.

Now myself, I would take all of this a little further. I would put a paragraph style on the line with images, maybe call it “Image Row”. Then instead of changing how all images work, I could be much more precise about it, as Scrivener will put an HTML container around the images, named after the style:

<p class="image-row"><img src="..."/> <img src="..."/> <img src="..."/></p>

With something like that we can be much more surgical with CSS, treating images inside of “image-row” containers differently from images not inside those containers:

.image-row img { display: inline; }

So that’s something to consider if you have figures elsewhere that depend on block display (and you can’t fix them individually). It’s also something to consider if you want to do a little more with this idea. For example:

/* Supporting CSS for the "Image Row" style. */
.image-row img {
	display: inline;
	padding: 0em 0.5em 0em 0.5em;
}
.image-row {
	text-align: center;
	margin-left: auto;
	margin-right: auto;
	white-space: nowrap;
}

Because we have a container around the images, we can do stuff to all of the images collectively as well, with the CSS that targets the .image-row class by itself. With that we make sure the images are centre-aligned on the screen, and that if the screen is too narrow, they won’t wrap to the next line. As for the images themselves, we also add a little padding between them so they look nicer.

I’ve attached a copy of the sample project with these revisions made to it, in case any instructions above are unclear.

image_row.zip (923 KB)