Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

HTML button with multiple colors

I’m trying to create a button that looks like this using html. I want the button to look like one is on top of the other, not just two rectangles next to each other (so the rounded edges/border-radius is important).

If it helps, I’m looping over a list of documents and displaying them as buttons in this format. It needs to be able to adjust its’ width for different document names/extensions. I don’t want the .pdf/.docx to be covered if the document name is too long.

button design

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I started by creating two buttons and having each on a different z-index, but I’m wondering if there’s a better way to do this.

Is there a way to dynamically size two buttons so that they appear overlapping like this, but are able to adjust their location/width so their text is still visible?

I’m sure this is a simple fix – but I’m stuck

<button style="position: absolute; padding: 10px 20px; background-color: darkgray; border-radius: 15px; border: none; z-index: 1;">Document</button>
<button style="position: absolute; padding: 10px 50px; background-color: gray; border-radius: 15px; border: none; z-index: 0;">.pdf</button>

>Solution :

Use a single button but wrap the document name in a span, then you can apply different styling to the document name and the button itself.

body {
  margin: 20px;
}

.buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}

button {
  border: 0;
  background: #555;
  color: white;
  font-family: sans-serif;
  font-size: 25px;
  border-radius: 15px;
  padding: 0 20px 0 0;
  white-space: nowrap;
}

button>span {
  background: #888;
  border-radius: 15px;
  display: inline-block;
  padding: 10px 20px;
  margin-right: 5px;
}
<div class="buttons">

  <button>
    <span>DocumentName</span> .pdf
  </button>

  <button>
    <span>a-very-long-document-name</span> .docx
  </button>

</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading