Help with positioning images

Hi all,

I’m writing my first twine game and need some help with positioning images. I’m trying to position multiple images side by side that also fades out when hovered. I’ve successfully done this using HTML, but when the same code is placed is Twine the images stack on top of each other instead.

Here’s what I’m trying to accomplish.

Here’s what happens when code is placed in Twine.

Here the code for reference.

<!DOCTYPE html>
<html>
<head>
<style>

.row {
  display: flex;
}
.container {
  position: relative;
  width: 40%;
  padding: 5px;

}

.image {
  opacity: 1;
  display: block;
  width: 100%;
  height: auto;
  transition: .5s ease;
  backface-visibility: hidden;
}


.middle {
  transition: .5s ease;
  opacity: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  text-align: center;
}

.container:hover .image {
  opacity: 0.3;
}

.container:hover .middle {
  opacity: 1;
}

.text {
  background-color: #4CAF50;
  color: white;
  font-size: 16px;
  padding: 16px 32px;
}
</style>
</head>
<body>
<div class="row">

<div class="container">
  <img src="img/apple.jpg" alt="Avatar" class="image" style="width:100%">
  <div class="middle">
    <div class="text">Apple</div>
  </div>
</div>

<div class="container">
  <img src="img/apple.jpg" alt="Avatar" class="image" style="width:100%">
  <div class="middle">
    <div class="text">Apple</div>
  </div>
</div>
</div>

  
</body>
</html>

Any help is much appreciated.

We would need to see the code in your Twine passage in order to tell exactly what’s going on.

I suspect the problem is that you have the HTML in the passage laid out the same as you do in the HTML you included, which means that you have a bunch of linebreaks between things in Twine. SugarCube normally turns linebreaks in the code into <br> elements, so you’d have to disable that default behavior.

Try adding a “nobr” tag to the passage to see if that fixes things. (See the “nobr” passage tag, <<nobr>> macro, Config.passages.nobr setting, and Line Continuation markup documentation for details.)

Hope that helps! :slight_smile:

1 Like

I agree with HiEv about using the <> macro to remove the
elements that will automatically be added to the generated HTML, however I added your CSS to the Story Stylesheet area of a new project and your HTML structure to that project’s main (Start) passage like so.
(note: example is using TWEE Notation)

:: Story Stylesheet [stylesheet]
.row {
  display: flex;
}
.container {
  position: relative;
  width: 40%;
  padding: 5px;
}
.image {
  opacity: 1;
  display: block;
  width: 100%;
  height: auto;
  transition: .5s ease;
  backface-visibility: hidden;
}
.middle {
  transition: .5s ease;
  opacity: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  text-align: center;
}
.container:hover .image {
  opacity: 0.3;
}
.container:hover .middle {
  opacity: 1;
}
.text {
  background-color: #4CAF50;
  color: white;
  font-size: 16px;
  padding: 16px 32px;
}

:: Start
<div class="row">
<div class="container">
  <img src="img/apple.jpg" alt="Avatar" class="image" style="width:100%">
  <div class="middle">
    <div class="text">Apple</div>
  </div>
</div>
<div class="container">
  <img src="img/apple.jpg" alt="Avatar" class="image" style="width:100%">
  <div class="middle">
    <div class="text">Apple</div>
  </div>
</div>
</div>

… then viewed the resulting story HTML file within Chrome and saw the following.
apple-test-small

As stated by HiEv, we would need to see the code within your Twine project to be able to debug why you are seeing the result you are.

note: If you are new to using CSS flex then I suggest reading this css-tricks article, as it may help with you using features like justify-content, flex-grow and flex-wrap.

Thanks for the replies. It appears that the code in twine wasn’t written properly. I copied the HTML on to a new passage and it worked! :smiley:

I have another question - how do I go about setting the images as links? I tried adding the HTML attribute

data-passage="ImagePassageName"

to the image element but that doesn’t seem to work, even on static images.

I also tried adding the div element inside the

<a data-passage="PassageName"></a> 

element, but here’s what happens instead.

Here’s the code in twine

<div class="row">

<a data-passage="PassageName">
<div class="container">
  <img src="img/apple.jpg" alt="Avatar" class="image" style="width:100%">
  <div class="middle">
    <div class="text">Apple</div>
  </div>
</div>
</a>

<a data-passage="PassageName">
<div class="container">
  <img src="img/apple.jpg" alt="Avatar" class="image" style="width:100%">
  <div class="middle">
    <div class="text">Apple</div>
  </div>
</div>
</a>

</div>

I should add that I’m using Sugarcube 2.28.2.

It needs class="link-internal" to be recognized as a SugarCube link. Also, you should put tabindex="0" there as well, so that the page can be navigated using the keyboard, in order to help those with disabilities use the page. (Similarly, the “alt” attribute for the image should describe the image for visually impaired people.) So if you want to send people to a passage named “PassageName” then the anchor line should be:

<a data-passage="PassageName" class="link-internal" tabindex="0">

You can see how this is coded yourself by putting a standard passage link into a passage, playing the story, and then right-clicking on the link and inspecting that element. That will show you the HTML for that element and the CSS which is applied to it in the inspector windows. I’ve found using that technique to be quite helpful for a lot of things like this.

Hope that helps! :slight_smile:

Thanks for for the reply. The images are clickable and functions as intended, but shrinks automatically when I wrap the anchor around it, regardless if
class="link-internal" is added or not.

My guess is that the anchor is an inline element and it’s not supposed to wrap around block elements in the first place? Is there another way to do this?

My bad. I misread the problem in your post. I thought the links weren’t working.

Do you have a link to the apple image so I could try it out myself and see what the issue is?

Here’s the image I’m using.

apple

Ah, the problem was the “width: 40%” in the “container” class. Remove that and it looks fine.

I just used the trick where I right-clicked on an apple image, inspected the element, clicked on different lines of HTML until I saw which one was causing the gap between the images. Once I found the problem element (the “container” <div>), then I looked at its CSS to see what was causing that. Disabling “width: 40%” made the image look fine, so I knew that was the source of the problem.

Try that trick yourself before removing that line from your CSS, that way you can see how to find and fix things like that yourself in the future.

Hope that helps! :slight_smile:

Problem solved - removing “width: 40%” did the trick. And thanks for the helpful tip!

If you use the data-passage content attribute on the anchor element, then SugarCube does all that for you—i.e., [[foo|bar]] and <a data-passage="bar">foo</a> generate completely equivalent anchor elements.