skip to Main Content

i have a problem with the positioning of elements in the card. I would like that the elements inside the class "top-right-element" are placed in the top right corner while the button with the pi-chevron-up is placed in the bottom left corner. This is the html code.

<div class="customCard" >
      <p-card>
         <ng-template pTemplate="header">
             <p class="m-0">
                {{item.id}}
             </p>
             <div class="top-right-element">
                 <table>
                     <td>
                         <div class="progressBar-toolbar" [style]="{'margin-right': '0.25em', 'margin-left': 'auto'}">
                             <p-progressBar [value]="progressValue" [showValue]="true" class="customProgress" ></p-progressBar>
                         </div>
                     </td>
                     <td *ngIf = "warnings > 0">
                         <i class="pi pi-exclamation-circle alert" [style]="{'margin-right': '1em'}"></i>
                     </td>
                 </table>
                </div>
        </ng-template>
        {{expanded ? item.description : item.description.slice(0,10)}}
        <ng-template pTemplate="footer">
                <div class="input-container">
                    <p-button icon="pi pi-chevron-down" *ngIf="!expanded"  class="bottom-right-button" (click)="toggleExpanded()" [style]="{'box-shadow':'none'}" ></p-button>
                    <p-button icon="pi pi-chevron-up" *ngIf="expanded"  class="bottom-right-button" (click)="toggleExpanded()" [style]="{'box-shadow':'none'}"></p-button>
                </div>
        </ng-template>
    </p-card>    
</div>

This is the css:

.bottom-right-button {
    position: absolute;
    bottom: 0;
    right: 0;
    margin-bottom: 10px; /* Optional: Adjust as needed */
    margin-right: 10px; /* Optional: Adjust as needed */
}

.top-right-element {
    position: absolute;
    top: 0;
    right: 0;
    margin-top: 10px; /* Optional: Adjust as needed */
    margin-right: 20px; /* Optional: Adjust as needed */
}

.input-container {
    position: relative;
    //margin-left: auto;
}

.customCard {
    margin-left: 0.25rem;
    margin-right: 0.25rem;
    background-color: #ffffff; 
    color: #696969; 
    padding: 10px 20px; 
    border: 1px solid transparent;
    border-radius: 4px; 
    box-shadow: 2px 3px 4px rgba(0, 0, 0, 0.2); 
}

The elements are positioned correctly at the beginning, but when I click the button to expand the text of the card and then close it, the entire card disappears. There are issues with the positioning of the elements. Can anyone help me?

2

Answers


  1. How to place an HTML element anywhere in a container?

    position: absolute rule is used to place an element specifically in a container. But there is a prerequisite that the parent element should have one of the rules, position: absolute, position: relative, or position: fixed.

    In other words, the absolute child element positions itself to the least outer element with absolute, relative, or fixed position rule.

    Example

    <div style="width:200px; height:200px; border:3px solid orange; position: relative">
      <strong>Outer Container</strong>
      <div> Line 2 </div>
      <div> Line 3 </div>
      <div style="border:3px solid red; height:100px; position:relative; ">
        <strong> Inner Container</strong>
        <div style="position:absolute; bottom:0px; left:0px;">
          Inner Bottom Left
        </div>
      </div>
    
    
      <div style="position:absolute; right:0px; top:0px"> Top Right</div>
      <div style="position:absolute; bottom:0px; right:0px"> Bottom Right</div>
    <div>

    Change the position rules and observe the child’s behaviors.

    Login or Signup to reply.
  2. The person’s answer here is exactly what I came to answer with : Containers so I just added my own spin on it below.
    I like to use containers with Bootstrap (check out the website it has a lot of great features and pre-built icons, etc.). Even if the container is empty, it helps to evenly balance content.

    .row{
      margin: 5px;
    }
    
    .col1{
      border: solid green;
      padding: 2px;
      margin: 2px;
    }
    
    .col2{
      border: solid blue;
      padding: 2px;
      margin: 2px;
    }
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
    
    <div class="container">
      <div class="row">
        <div class="col1">
          <h3>Box 
            <!--Snippet from Bootstrap-->
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-1-square" viewBox="0 0 16 16">
              <path d="M9.283 4.002V12H7.971V5.338h-.065L6.072 6.656V5.385l1.899-1.383z"/>
              <path d="M0 2a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2zm15 0a1 1 0 0 0-1-1H2a1 1 0 0 0-1 1v12a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1z"/>
            </svg>
            <!--End snippet from Bootstrap-->
          </h3>
        </div>
        <div class="col2">
          <h3>Box 
            <!--Snippet from Bootstrap-->
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-2-square" viewBox="0 0 16 16">
      
              <path d="M6.646 6.24v.07H5.375v-.064c0-1.213.879-2.402 2.637-2.402 1.582 0 2.613.949 2.613 2.215 0 1.002-.6 1.667-1.287 2.43l-.096.107-1.974 2.22v.077h3.498V12H5.422v-.832l2.97-3.293c.434-.475.903-1.008.903-1.705 0-.744-.557-1.236-1.313-1.236-.843 0-1.336.615-1.336 1.306"/>
      
                <path d="M0 2a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2zm15 0a1 1 0 0 0-1-1H2a1 1 0 0 0-1 1v12a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1z"/>
           </svg>
            <!--End snippet from Bootstrap-->
          </h3>
        </div>
      </div>
      <div class="row">
        <div class="col2">
          <h3>Box 
            <!--Snippet from Bootstrap-->
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-3-square" viewBox="0 0 16 16">
              <path d="M7.918 8.414h-.879V7.342h.838c.78 0 1.348-.522 1.342-1.237 0-.709-.563-1.195-1.348-1.195-.79 0-1.312.498-1.348 1.055H5.275c.036-1.137.95-2.115 2.625-2.121 1.594-.012 2.608.885 2.637 2.062.023 1.137-.885 1.776-1.482 1.875v.07c.703.07 1.71.64 1.734 1.917.024 1.459-1.277 2.396-2.93 2.396-1.705 0-2.707-.967-2.754-2.144H6.33c.059.597.68 1.06 1.541 1.066.973.006 1.6-.563 1.588-1.354-.006-.779-.621-1.318-1.541-1.318"/>
              <path d="M0 2a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2zm15 0a1 1 0 0 0-1-1H2a1 1 0 0 0-1 1v12a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1z"/>
            </svg>
            <!--End snippet from Bootstrap-->
          </h3>
        </div>
        <div class="col1">
          <h3>Box 
            <!--Snippet from Bootstrap-->
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-4-square" viewBox="0 0 16 16">
              <path d="M7.519 5.057q.33-.527.657-1.055h1.933v5.332h1.008v1.107H10.11V12H8.85v-1.559H4.978V9.322c.77-1.427 1.656-2.847 2.542-4.265ZM6.225 9.281v.053H8.85V5.063h-.065c-.867 1.33-1.787 2.806-2.56 4.218"/>
              <path d="M0 2a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2zm15 0a1 1 0 0 0-1-1H2a1 1 0 0 0-1 1v12a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1z"/>
            </svg>
            <!--End snippet from Bootstrap-->
          </h3>
        </div>
      </div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search