Styling with a combined css component

What I’m trying to do:
I’m trying to create a design for an input field with the label on top of the border of the input field, like this:

qUzLB

What I’ve tried and what’s not working:
I thought of using roles, however, this is how I know to do this in normal css:

<style>

    .form-group{

    padding:10px;

    border:2px solid;

    margin:10px;

  }

  .form-group>label{

    position:absolute;

    top:-1px;

    left:20px;

    background-color:white;

  }
 
  .form-group>input{

    border:none;

  }

  </style>

  <form id="first_name">

      <div class="form-group">

           <label>First name</label>

           <input type="text" class="form-control input-lg" />

      </div>

  </form>

First approach

So it needs a parent (?) component - form-group in this situation, and then the position of the input field and label are specified in relation with this form-group. I tried creating 3 Roles: form-input, form-input-label and form-input-field, and specify them in the Assets file as:

.anvil-role-form-input{
  padding:10px;
  border:2px solid;
  margin:10px;
}
.anvil-role-form-input>.anvil-role-form-input-label{
  position:absolute;
  top:-1px;
  left:20px;
  background-color:white;
}

.anvil-role-form-input>.anvil-role-form-input-field{
  border:none;
}

And then I assigned a container with role form-input, a label in it as form-input-label and a text field as form-input-field. It does not work: https://anvil.works/build#clone:XKRYPJBIV5CVPCY5=H6FQR3VNCST5RQ4N3CTTVFFI

Besides this, I cannot find a way to specify a child element working with Roles, is there a way with this approach?

Second approach

I tried to create a custom html component, with the code above. In combination with this answer How to refer to a div in custom html from python and vice-versa - #2 by stucork, I wanted to make create a component that retains the above-mentioned design, bit interactive and changeable. However, the component is no longer interactive when I use it in another form. This is the clone of the app in the answer, showing that its no longer interactive when used in Form1:

https://anvil.works/build#clone:BGZWZMEOPTMPWC4G=YBY7XKBWVHVBL46BQ2VOQYRQ

Is there a solution for either of these approaches? Or is there a third option? Thanks a lot!

I have no CSS skills so my approach would be to do this purely in Python.
You can style the components and hide/show them all programmatically which I think would give you the same outcome.

Can you describe the behaviour you want?

I want to create a design for my input fields where the label of the field lies on the border of the field box, like in the picture on my question. And I want all my input fields to have this design,

Do you think that it’s doable using Python?

How’s this?

.anvil-role-form-input {
  padding: 10px;
  border: 2px solid;
  margin: 10px;
}
.anvil-role-form-input .anvil-role-form-input-label {
  position: absolute;
  top: -20px;
  left: 20px;
  background-color: #d7d7d7;
  z-index: 3;
  font-size: 18px;
  padding: 0;
  margin: 0;
  line-height: 1;
}
.anvil-role-form-input-label .label-text {
  padding: 0;
  margin: 0;
}
.anvil-role-form-input .anvil-role-form-input-field {
  border: none;
}

.anvil-container, .anvil-container div {
  overflow: visible
}

2 Likes

thank you!! this is the result:

https://anvil.works/build#clone:XKRYPJBIV5CVPCY5=H6FQR3VNCST5RQ4N3CTTVFFI

1 Like

I think this would be possible - though maybe not elegant - using an XY panel, a label and a textbox. I don’t think it would beat the slick presentation of @stucork’s solution but I will have a crack…

This is a custom component based on the above - it needs some work to get the inputs to work but it seems very doable in python. Ill finish this up when I have a little more time

3 Likes

thank you for giving it a try! I’m curious to see how you do it