Arranging elements within a card using CSS

I'm currently facing a dilemma regarding the arrangement of elements within the card. My goal is to have the items inside the "top-right-element" class positioned in the top right corner, while the button featuring the pi-chevron-up icon should be placed in the bottom left corner. Below is the provided HTML code:

<div class="customCard" >
      <p-card>
         <ng-template pTemplate="header"gt;
             <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>

Below showcases the corresponding CSS style:

.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); 
}

Initially, all elements are correctly positioned. However, after expanding and then collapsing the text of the card using the button, the entire card disappears. There seems to be an issue with the placement of elements. Any assistance or insights on how to resolve this would be greatly appreciated.

Answer №1

How can an HTML element be positioned anywhere within a container?

The use of the position: absolute rule allows for the precise placement of an element within a container. It is important to note that the parent element must have one of the following rules: position: absolute, position: relative, or position: fixed.

In simple terms, the child element with absolute positioning aligns itself with the nearest outer element that has a position rule of absolute, relative, or fixed.

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>

  // Additional Examples Here

<div>

Experiment with different position rules to see how the child elements behave.

Answer №2

This individual's response mirrors my own thoughts perfectly: Containers. However, I have added my personal touch to it below. I find containers incredibly useful, especially when paired with Bootstrap (do check out their website for a plethora of amazing features and pre-designed icons). Even if the container doesn't contain much, it still aids in maintaining content equilibrium.

.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"/>
         ...
        </svg>
        <!--End snippet from Bootstrap-->
      </h3>
    </div>
   ...
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What causes text inside a fieldset to sometimes appear bigger when viewed on mobile devices?

Have you ever noticed a strange quirk in Chrome that you couldn't quite explain? I recently stumbled upon a peculiar "feature" while using fieldset with responsive design. It seemed that the text within the fieldset appeared disproportionately larger ...

Angular service providing components (TypeScript error)

This is my first time trying to dynamically inject components and so far, I've been successful. However, there's an error in Typescript that's bothering me (I don't like having errors in my code). If you want to check out the app, here ...

Attempting to organize information within a Materialize Card

First and foremost, a huge thank you to everyone who has been so helpful to many people! Secondly, I apologize for posting images in Spanish, as I currently do not have the time to translate them into English. Nevertheless, the text within the photos is ...

Launch a component in a separate browser tab

I decided to open my component "template1" in a new tab, so I set up the path as follows: const routes: Routes = [ {path : 'template1' , component: Template1Component} ]; In the HTML file: <a mat-raised-button color="primary" target="_bl ...

Edit and protect an image added on a website using HTML

I am looking to develop a code snippet that enables users to upload an image, which will then be displayed with cropping and a watermark overlay that can be easily saved by right-clicking. My plan is to use Java/HTML for this online uploading functionali ...

I require help with my digital greeting card

Apologies for any language errors in advance. I'm almost done with my first "online-card" project, but I've hit a frustrating issue. I can't seem to remove the gap between the footer and the tab-content (the Hungarian version is fine). I&apo ...

Updating the src attribute of an image using document.getElementByClassName along with the ngif directive

I am attempting to dynamically modify different icon images on page load using document.getElementsByClassName. The icons are used multiple times and their visibility is controlled by the ngIf directive based on user actions. <img class="viewlist_b ...

Why are the HTML links generated by JS not opening in Chrome?

<a href='http://www.xyz.hu/xyz' alt='Kosár' title='Kosár'>Megtekintés</a> Additionally: - A setInterval function refreshes the sibling's content every second, although it should not affect this specific el ...

width and height specifications for the device

Struggling to determine the device-height after setting predefined device-widths such as: For Extra small devices Phones (<768px) For Small devices Tablets (≥768px) For Medium devices Desktops (≥992px) For Large devices Desktops (≥1200px) I& ...

problems with basic text styling in containers

I've been experimenting with text in boxes and I've noticed that when the screen size changes, extra padding is added to the top of the box even though it's not in the code. Is there a way to prevent this from happening when the screen resiz ...

Ways to get rid of the white horizontal line generated by bootstrap framework?

I'm currently working on a responsive navbar design and I want it to appear transparent over a background image. However, my front-end knowledge is limited as I've only been learning for a week. Can anyone advise me on how to remove the white bar ...

Error message: Angular 12 - Event emitter variable is not defined

I'm currently diving into Angular, specifically working with version 12.0.1 and TypeScript 4.3.4. I'm stumped as to why my event emitter is showing up as undefined. Any suggestions or insights? Here's the error message that keeps popping up ...

Challenges encountered with relative links in the layout of a static website generator resembling padrino, sinatra, or rails, involving HTML, CSS, and haml

I'm currently utilizing a static site generator to create a website that resides on a shared network folder within my workplace. This site serves as a simple tutorial platform for my colleagues; it's not hosted on a server and remains completely ...

Typescript's confidential variables

Currently, I am delving into the world of Angular2 and familiarizing myself with working with classes in javascript for the first time. I'm curious about the significance of using the private parameter in the constructor, as opposed to simply writing ...

An overflow occurs due to the grid column gap

After testing CSS display: grid, I noticed that the grid-column-gap: 10px; property is causing the parent container to break. This makes the green area in my code smaller than the grid area itself. It seems like box-sizing: border-box; doesn't have a ...

Is there a way to process the output of phantom.js in PHP efficiently?

I have successfully retrieved output from a phantom.js request via the command line and it meets my expectations. Now, I am interested in invoking phantom.js from a php script and then analyzing the output for specific content. My phantom.js script appear ...

Out of the blue, the CSS functionality in my React app completely ceased to

I've been developing this website using React and Material UI, and I chose to implement standard CSS for styling. However, after closing my code editor and reopening it, some parts of the CSS do not seem to be loading properly. I'm completely puz ...

What is the process of creating conditional content projection in Angular?

Looking to implement an optional content projection feature in Angular. Here's the desired structure as an example: <app-root> <app-header></app-header> </app-root> By default, a header is displayed here. <app-root&g ...

Troubleshooting property assignment issues within an Angular service

I have created a simple injectable service in TypeScript for Angular 4. This service is designed to fetch a JSON file from the network and store the data as an array in its property called data. Other classes can then access this data using the method getD ...

There are two design issues that need to be addressed: a white background glitch and a layout problem specifically in

I'm facing two issues with my website located at Issue 1 : In Internet Explorer 9, the header displays correctly but the text area is not in the right position. Issue 2: There is a white area around the header that should show the background instead ...