# Buttons

### My least favourite way to center a div

## The Challenge

here is the code for *rainbow mode*

~~~css

.rainbow {

  background: linear-gradient(in hsl longer hue 90deg, red 0 100%);

  background-size: 200% auto;

  background-clip: text;

  animation: rainbow 10s linear infinite;

  color: transparent;

}

~~~





the important line here is the `background-clip: text` what we do is draw the entire screen as a rainbow and then clip everything but the text.



Then we make the text transparent and all we are left with is the rainbow effect behind it. This however limits everything to being exclusively text





## The UI



My first attempt to make buttons was just to use ascii boxes to draw around them which works but people expect that buttons change when hovered over and so it was confusing when they stayed the same.



The first thing I tried was changing which boxes I used (there are a plethora), but at the small scale it was hard to notice the difference, that's when I decided to have them change size as well. 

To do this I intentionally left a bunch of space around the default button which would be filled out by the full button.



~~~typescript

  const button = () => {

    const buttonRows = [

//two rows here are nothing but space

      `   ${gap}   `,

      ` ╭─${line}─╮ `,

      ` │ ${text} │ `,

      ` ╰─${line}─╯ `,

      `   ${gap}   `,

//aditionally there is one space at the beginning and end of each string

    ];

    return buttonRows.join("\n");

  };



  const hoverButton = () => {

    const buttonRows = [

      `╔──${line}──╗`,

      `│  ${gap}  │`,

      `│  ${text}  │`,

      `│  ${gap}  │`,

      `╚──${line}──╝`,

    ];

    return buttonRows.join("\n");

  };



~~~



then it was as simple as hooking them up to the state and it works



Ideally they would work just via css but for now it's in JS. You have to pick your battles.

### Changing buttons



Go ahead and press these buttons for a bit and see how they change text when you do

[Dark Mode](https://lucas-leprince.com/bts/button?mode=dark)

[Light Mode](https://lucas-leprince.com/bts/button?mode=light)

[Rainbow Mode](https://lucas-leprince.com/bts/button?mode=rainbow)

[Terminal Mode](https://lucas-leprince.com/bts/button?mode=terminal)

[JSON](https://lucas-leprince.com/bts/button.json)

[JWT](https://lucas-leprince.com/api/bts/button/token)

[Database](https://lucas-leprince.com/database.md)

the problem I was facing was that as the label  were changing the width of the buttons.
I'm sure you're clever and realized that and are currently yelling at the screen saying just have a set width which I did do but that ended up making the buttons padded weirdly

[Dark Mode](https://lucas-leprince.com/bts/button?mode=dark)

[Light Mode](https://lucas-leprince.com/bts/button?mode=light)

[Rainbow Mode](https://lucas-leprince.com/bts/button?mode=rainbow)

[Terminal Mode](https://lucas-leprince.com/bts/button?mode=terminal)

[JSON](https://lucas-leprince.com/bts/button.json)

[JWT](https://lucas-leprince.com/api/bts/button/token)

[Database](https://lucas-leprince.com/database.md)

After all that I arrived at this final iteration

[Dark Mode](https://lucas-leprince.com/bts/button?mode=dark)

[Light Mode](https://lucas-leprince.com/bts/button?mode=light)

[Rainbow Mode](https://lucas-leprince.com/bts/button?mode=rainbow)

[Terminal Mode](https://lucas-leprince.com/bts/button?mode=terminal)

[JSON](https://lucas-leprince.com/bts/button.json)

[JWT](https://lucas-leprince.com/api/bts/button/token)

[Database](https://lucas-leprince.com/database.md)

### the horrors

Now is time to show you the code that I think is very gross but is (to my understanding) the best solution here

~~~html

 <div

   style={{

     position: "relative",

     left: "50%",

     top: "50%",

     width: "0",

     height: "0",

     display: "flex",

     justifyContent: "center",

     alignItems: "center",

   }}

 >

   <pre

     style={{

       position: "relative",

       right: "50%",

       bottom: "50%",

       width: "fit-content",

     }}

   >

     {text}

   </pre>

 </div>

 {hover ? <pre>{hoverButton()}</pre> : <pre>{button()}</pre>}

~~~

This code will forever haunt my nightmares and I will do my best to explain why.

boxDrawing

Unfortunately that's where I was stuck for the longest time

#### Other things I tried

* `position:absolute` - the problem is that this breaks the `background-clip:text` of *rainbow mode* which is the whole reason we are in this mess in the first place

* 'display:flex' - this would solve the problem except that once again either the button or the text have to be position absolute

Ultimately the conclusion I came to is that we need to use `position:relative` 

and the only real way to have  `position:relative` not cause weird spacing issues is by  having it take up no space at all

### Redemption

A week after I threw in the towel I discovered [this site](https://www.joshwcomeau.com/css/center-a-div/#centering-a-stack-of-elements-8) talking about how to center a div. At the time I was showing someone the depths of CSS but then I saw something

but then I saw that you can actually stack elements into the same grid box and realized this was the nice clean solution I was looking for

~~~html

<div

 style={{

    display: "grid",

    placeContent: "center",

    placeItems: "center",

  }}

>

  <div

    style={{

      gridRow: "1",

      gridColumn: "1",

    }}

  >

    <pre>{text}</pre>

  </div>

  {hover ? (

    <pre

      style={{

        gridRow: "1",

        gridColumn: "1",

      }}

    >

      {hoverButton()}

    </pre>

  ) : (

    <pre

      style={{

        gridRow: "1",

        gridColumn: "1",

      }}

    >

      {button()}

    </pre>

</div>

~~~

and while yes it is certainly more code than before it is more accurate to what I want which is 'make one box and shove everything into it'

I think that's a lesson I keep learning, that if you have to wrestle the CSS too much it is likely that taking a more declarative approach will pay off in the end

[Return Home](https://lucas-leprince.com/index.md)

[Behind the scenes](https://lucas-leprince.com/bts.md)

[Dark Mode](https://lucas-leprince.com/bts/button?mode=dark)

[Light Mode](https://lucas-leprince.com/bts/button?mode=light)

[Rainbow Mode](https://lucas-leprince.com/bts/button?mode=rainbow)

[Terminal Mode](https://lucas-leprince.com/bts/button?mode=terminal)

[JSON](https://lucas-leprince.com/bts/button.json)

[JWT](https://lucas-leprince.com/api/bts/button/token)

[Database](https://lucas-leprince.com/database.md)

[get Progress](https://lucas-leprince.com/progress.md)

#### Social Media 

[GitHub](https://github.com/LePetitPrince-4)

[GitLab](https://gitlab.com/LePetitPrince4)

[LinkedIn](https://www.linkedin.com/in/lucas-leprince/)

[PGP key](https://lucas-leprince.com/pgp-key)

[RSS Feed](https://lucas-leprince.com/feed)

[Sitemap](https://lucas-leprince.com/sitemap.xml)

[permalink](https://lucas-leprince.com/permalink/v2/buttons/md)