Twine Version: 2.3.14 Story Format: Sugarcube 2.34.1
Hello there, I had a quick question! I’ve developed ways of styling both the choice container and the individual choices in my game. However, I’m finding that having to enclose each choice in a div container is slowing me down (entirely self-imposed, I know). Is there a way to use widgets to create HTML shortcuts so that I don’t have to write each div container out each time?
For reference, this is what a typical set of choices looks like in my code:
Which door do you take?
<<nobr>>
<div class="choices">
<<link '<div class="choice-item">"The red, angry door."</div>' 'reddoor'>><<fair_plus "$courage" +10>><</link>>
<<link '<div class="choice-item">"The yellow, happy door."</div>' 'yellowdoor'>><<fair_plus "$compassionate" +10>><</link>>
<<link '<div class="choice-item">"The green, sleepy door."</div>' 'greendoor'>><<fair_plus "$calm" +10>><</link>>
</div>
<</nobr>>
I did see this post while I was searching for similar questions, but I’m not sure how to apply that specific example to my code. Any help in this area would be much, much appreciated! Thank you!
Assuming that you’re referring to the choice-item wrappers, you shouldn’t have to use them now as you can simply piggyback off the choices wrapper to style the links contained within.
For example, in your CSS:
/* Change the `.choice-item` selector to `.choices a`. */
.choices a {
/* Former `.choice-item` style properties go here. */
}
You target a elements (normal HTML links) within the choices wrapper because that’s what the <<link>> macro generates.
That should allow you to write your example as:
Which door do you take?
<<nobr>>
<div class="choices">
<<link '"The red, angry door."' 'reddoor'>><<fair_plus "$courage" +10>><</link>>
<<link '"The yellow, happy door."' 'yellowdoor'>><<fair_plus "$compassionate" +10>><</link>>
<<link '"The green, sleepy door."' 'greendoor'>><<fair_plus "$calm" +10>><</link>>
</div>
<</nobr>>
NOTE: There are other ways to accomplish your goal, but the above is probably the simplest.
It does not. Aside from the class choice-item being on a different element, which may be fine regardless, the <<fair_plus>> macro/widget will execute as soon as the link is rendered rather than when it’s activated as with the <<link>> version.
Your intention appears to be show a list of styled links, with each link being on its own line.
HTML includes two elements <ul> (unordered list) and <ol> (ordered list) that are specifically designed to do that.
If you change your code to the following…
<ul class="choices">
<li><<link 'The red, angry door.' 'reddoor'>><<fair_plus "$courage" +10>><</link>></li>
<li><<link 'The yellow, happy door.' 'yellowdoor'>><<fair_plus "$compassionate" +10>><</link>></li>
<li><<link 'The green, sleepy door.' 'greendoor'>><<fair_plus "$calm" +10>><</link>></li>
</ul>
… then each link will appear on its own line, and you wont need the <<nobr>> macro as SugarCube knows to automatically suppress line-breaks within the structure of such lists.
You didn’t include an example of the CSS being applied to either the .choices class or the .choice-item class so I can’t include those settings in the following, but to remove the default dot and left padding from the <ul> element you would need to add the following to the .choices class…
Ah, thank you so much for this, it’s definitely much more helpful to style links within the .choices class, and will definitely simplify writing normal choices greatly! The only reason why I have a separate “choice-item” wrapper is to differentiate standard choices from other choices that might appear in the container, for example romantic choices (“flirt-item”), informational choices (“ask-item”), lying choices ("lie-item), or etc. Here’s an example of what I mean:
Which is why I wasn’t initially piggybacking off of the .choices container alone. But since the vast majority of choices are “standard,” stying the links within .choices will still definitely streamline things!
Ah, I see what you mean by using unordered lists so I don’t have to bother with <>! Given that I technically have different prefixes/styling for different types of choices/links, I think this will only apply to the “standard” ones, but still very helpful to keep in mind!
Well, if you go with a list element, then you still wouldn’t need the wrappers. Just apply a class to each list item that’s different from a normal choice.
If you mean like with your original <div.choices> wrapper, then not with the base <<link>> macro, since there’s no easy way to add classes to its element. You’d either need to use you original <<link>> text wrapper or just wrap the <<link>> itself.
Oh. If you’re not against add-ons there is an add-on macro by Akjosch named <<ilink>> that does allow you to add an ID and/or classes directly to the macro’s element. It’s part of their enhancedmacros.js gist.
After copy-pasting the code into your JavaScript section, the usage would be like:
Oh wow, that’s all perfect, thank you so much! That makes sense why it would take extra work to wrap/add classes to the <> macro. I’ll play around with things and see what ends up feeling best; thanks for taking the time to help!
Now I have another issue that’s stumping me! Using the suggested configuration here:
…and here:
I’m noticing that my choices are sliiiightly positioned different from before, and trying to figure out which property is controlling that placement is driving me crazy. Here’s what I mean:
This is me switching between my original code (seen in the first post) and the new “unordered list” code. It’s pretty subtle, but using list items seems to shift everything very slightly up and to the right, even though both examples use the same .choices wrapper.
<<nobr>>
<div class="choices">
<<link '<div class="choice-item">"Does he not deserve mercy?"</div>' 'prologue1.0.1'>><<setcompassionate +5>><</link>>
<<link '<div class="choice-item">"Does he not deserve justice?"</div>' 'prologue1.0.2'>><<setloyal -5>><</link>>
<<link '<div class="choice-item">"Does he not deserve the slowest death imaginable?"</div>' 'prologue1.0.3'>><<setcompassionate -5>><</link>>
</div>
<</nobr>>
(Note how the border of the “choices” box is aligned with the main text.)
New code
And the new code:
<ul class="choices">
<li><<link '"Does he not deserve mercy?"' 'prologue1.0.1'>><<setcompassionate +5>><</link>></li>
<li><<link '"Does he not deserve justice?"' 'prologue1.0.2'>><<setloyal -5>><</link>></li>
<li><<link '"Does he not deserve the slowest death imaginable?"' 'prologue1.0.3'>><<setcompassionate -5>><</link>></li>
</ul>
(Notice how the border of the choice box is not exactly aligned with the main body text, and there’s a larger gap between it and the bottom of the screen.)
To my eye, all of the styling involved here is exactly the same, so I can’t figure out exactly what element is causing the shifting of the text placement, except that it’s just the hidden nature of using list items? Am I missing something more obvious?
note: adding html to the start of the your html .choices CSS select serves no real purpose, so you can either change that selector back to the original .choices suggested by both TME and me; or change it to ul.choices if you want to make it specific to the <ul> element.
Generally you can use your web-browser’s Web Developer Tools to answer most CSS related questions…
If you inspect the elements that make up the “choices” structure, and select the parent <ul> element, you will see:
a coloured visual representation of any margin and/or padding being applied to that element.
a list of all the CSS rules that are being applied to that element.
And you will notice that:
both margins & padding is being applied.
some of it is due to your own CSS. (the .choices selector rule)
some of it is due to SugarCube’s default CSS. (the .passage ol, .passage ul selector rule)
some of it is due to the web-browser’s default CSS. (the ul rule)
Thank you, you were right that it was the default ul rule that was the culprit! My Inspector tool has been acting funky, but it turned out to be a recent update to the Mac OS–your comment caused me to reinstall my browser and that cleared the issue as well. Thank you so much for the help!