Scaling & Placement of Buttons

Twine Version: 2.3.2
Story Format: Sugarcube 2.28.2

I am currently designing a UI for use in a bit of an adventure game. It is mostly dependent on the <> function. However, the problem I face is that the buttons in this case must maintain a specific shape, namely square. But for this to work for screens other than my own, they must also be able to scale to different sizes. I have found no way of accomplishing this without also deforming the button itself so that it is no longer square.

.inventory button {
 	background-image: url(XXXXX);
  	max-height: 100%;
  	max-width: 100%;
  	height: 125px;
  	width: 125px;
  	margin: 5%;
  	font-family: Impact;
  	font-size: 110%

This is the code I am currently working with.

I do also have a secondary problem of the button placement. In this instance, I need to place it in the lower-left corner of a UI bar I have created. The only way I have found to do this is with the “margin” property. Unfortunately, “margin” does not appear to scale to the available space when it comes to vertical size, so if the button would land outside the available space, it just adds a scrollbar, rather than adapting the margin-space.

Unfortunately, you need to put code like that inside of a “preformatted text” marker for it to be displayed properly in this forum (select the text and click the </> button).

Anyways, for buttons that will always be square, first put this in your Stylesheet section:

.square button {
	margin: 5%;
	font-family: impact;
	font-size: 110%
.square button:after, .square button:before {
	content: "";
	display: block;
	padding-bottom: calc(50% - 11px);

The “- 11px” is due to the height of the text being 22 pixels. You’ll have to adjust that if you use a different text height.

Then the buttons in your passage should look something like this:

<span class="square"><<button "This is a test">>
	(Code goes here.)

and now they should appear square.

Enjoy! :grinning:

This unfortunately did not solve the problem. I was already able to make the buttons square. The issue I was having was that if I also wanted the buttons to scale according to the available screen-size, they would deform (becoming rectangular, etc.) as the aspect-ratio was not necessarily the same.

With my limited knowledge of Sugarcube, it would seem that the best solution would be to somehow lock the width of the button to the height of the button, so that the two always remain the same. Then have the height scale off of the available space. However, I have no idea how i’d do that, as it’d require one of the variables to be dependent on percentage, whilst the other would be dependent on px.

I’m having troubles understanding exactly what it is you need without being able to see the UI bar that you’re talking about, since I don’t know what it looks like or how it scales.

Could you make a simple example project showing what it looks like?

I have attached an example of my current working code. In the example provided, please ignore the text of the passage as it is not the issue at hand in this instance.

But alongside the project I can also illustrate the problem with screenshots. This first one shows what the sidebar in question looks like with an aspect ratio identical to my own.

Which is all fine and good.
But this next one shows what it looks like with an aspect ratio that has a much smaller width, as compared to my own. Or if the game is in a window that is then resized to be narrower. The buttons thus “phase” outside of the bounds of the sidebar, putting down a scroller instead of shrinking the buttons to an appropriate size.

In theory, this could be solved by making the sidebar a static size, instead of a percentage-size of the screen. However, this still runs into the issue of making the actual text window absolutely tiny on small screens. Which isn’t exactly ideal.

Button (132.7 KB)

Hopefully this will do what you’re looking for.

In the JavaScript section you’d put:

/* Create the Left UI Bar. */
var leftUiBar = $('<div id="left-ui-bar"></div>').insertAfter("#ui-bar");
var leftBody = leftUiBar.append('<div id="left-ui-bar-body"></div>');

/* Automatically show the contents of the StoryLeftSidebar passage in the right-ui-bar-body element. */
postrender["Display Left Sidebar Contents"] = function (content, taskName) {
	setPageElement('left-ui-bar-body', 'StoryLeftSidebar');

And in your “StoryLeftSidebar” passage you’d add a “nobr” tag and then put:

<div class="buttonbox">
	<span class="inventory uibtn">
		<<button "inv">>
	<span class="spells uibtn">
		<<button "spells">>
	<span class="save uibtn">
		<<button "save">>
	<span class="perks uibtn">
		<<button "perks">>

And finally, the Stylesheet section gets this:

/* Button Elements */
button {
	cursor: pointer;
	color: 	white;
	height: 50px;
	width: 100px;
  	background: gray;
	background-size: 100% 100%;
	border: 1px solid darkgray;
	line-height: normal;
	transition-duration: 200ms;
	user-select: none;
	border-width: 2px;
	border-radius: 8px;
	border-color: darkgray;
button:hover {
	background: lightgray;
	background-size: 100% 100%;
	border-color: white;
	border-width: 2px;
	border-radius: 3px;

.buttonbox {
	width: 95%;
	margin: auto;
.uibtn {
	display: inline-block;
	width: 45%;
	min-width: min-content;
.uibtn button {
	position: relative;
	left: -3px;
	width: 92%;
	min-width: min-content;
	height: auto;
	margin: 5%;
	font-family: Impact;
	font-size: 110%
.uibtn button:after, .uibtn button:before {
	content: "";
	display: block;
	padding-bottom: calc(50% - 11px);

/* Styling and Colours of the Left UI Bar. */
/* Layout and Positioning of the Left UI Bar. */
#left-ui-bar {
	position: fixed;
	top: 0;
	right: 80%;
	width: 20%;
	height: 100%;
	margin: 0;
	padding: 0;
	background-color: #222;
  	border-right: 1px solid #444;
	text-align: center;
	z-index: 50;
#left-ui-bar-tray {
	position: absolute;
	top: .2%;
	left: 0;
	right: 0;
#left-ui-bar-body {
	height: 90%;
	height: calc(100% - 2.5em);
	margin: 2.5% 0;
	padding: 0 1.5%;
	line-height: 1.5;
	overflow: hidden auto;

That should keep the buttons square, the content within the buttons, keep the buttons within the UI bar, and scale them appropriately.

Hopefully it should just be minor tweaks after that.

Enhoy! :grinning: