What is the best way to position multiple elements within mat-card-content using Angular, CSS, and HTML

I have been attempting to align the button vertically within the mat-card-content, but without success. The title is currently left-aligned and centered vertically as desired. When I make changes to my HTML or add more divs, the alignment gets worse.

https://i.sstatic.net/uwvwr.png

To better understand the layout, I used contrasting background colors whenever adjustments were made.

<div>
  <div *ngFor ="let recipe of cookbook">
    <mat-card class="recipe">
      <mat-card-content class="title"> {{recipe.title}}
        <button mat-icon-button [matMenuTriggerFor]="menu" class="more">
          <mat-icon>more_vert</mat-icon>
        </button>
        <mat-menu #menu="matMenu">
          <button mat-menu-item>
            <span>Edit</span>
          </button>
          <button mat-menu-item>
            <span>Delete</span>
          </button>
        </mat-menu>
        </mat-card-content>
    </mat-card>
  </div>
</div>

.recipe {
width : 400px;
height: fit-content;
background-color: #d4d4d4;
padding: 15px;
margin: 15px;
border-radius:4px;
}

.more {
right:-20%;
background-color: red;
}

.title{
  background-color: green;
  text-align: left;
}





The only somewhat successful attempt at alignment was using "right:" in my CSS for the .more button, but it varies depending on the length of the title and sometimes exceeds the mat-card.

Any suggestions on how I can improve this alignment?

Answer №1

If you want to achieve this layout, consider using flexbox

Here's a sample code snippet for reference:

You can also check out the Stackblitz example I created https://stackblitz.com/edit/angular-ivy-mvecpg

To learn more about custom theming in Angular Material, take a look at this guide https://material.angular.io/guide/theming-your-components

.recipe {
  display: flex;
  flex-direction: column;
  width: 400px;
}

.recipe__card {
  background-color: #d4d4d4;
  border-radius: 4px;
  box-shadow: 0 4px 2px -2px #ccc;
  padding: 8px;
  margin-bottom: 12px;
}
  
.recipe__card-content {
  padding-bottom: 0 !important;
  padding-top: 0 !important;

  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: green;
  padding: 4px 8px;
}

.recipe__title {
  font-weight: bold;
}

.recipe__more-btn {
  background: red;
  border: none;
  margin: 8px;
}
<section class="recipe" *ngFor="let recipe of cookbook">
  <mat-card class="recipe__card">
    <mat-card-content class="recipe__card-content">
      <span class="recipe__title">{{recipe.title}}</span>
      <button mat-icon-button [matMenuTriggerFor]="menu" class="recipe__more-btn">
          <mat-icon>more_vert</mat-icon>
        </button>
      <mat-menu #menu="matMenu">
        <button mat-menu-item>
            <span>Edit</span>
          </button>
        <button mat-menu-item>
            <span>Delete</span>
          </button>
      </mat-menu>
    </mat-card-content>
  </mat-card>
</section>

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

Using Angular DataTables to make an AJAX call from browser's local storage

I am exploring a method to retrieve data from local storage via an ajax call and load it into a table as a JSON object. fetchDataForTable() { this.jsonService.retrieveJson().subscribe(response => { this.dataToDisplay = response.data; }); t ...

Utilizing PHP to Filter SQL Queries Based on HTML Select Boxes

I'm currently working on a project to create a platform where individuals can purchase and sell new or used cars. One of the key features I'd like to implement is a simple price filter using PHP, SQL statements, and HTML select boxes for drop-dow ...

With Firefox, when submitting a form only the final repeated region value is $_POSTed and not all of them

There seems to be an issue with the code below when running it in Firefox. The problem occurs when passing the URL variable UserName. In Firefox, only the last record is being used from the record-set, regardless of which button is clicked. This page utili ...

Steps for incorporating scrollIntoView in a horizontal testimonial table

I am sorry if the title has caused any confusion, but it is exactly as it states. I want to create a testimonial card using a table as the base. I have almost completed it, but I am struggling with the final touches. For instance, I want to implement a fun ...

Retrieve data from an API and store it in a JSON array

My website is having an issue where it's fetching data from an API and displaying it in a table, but all the cells are returning undefined values. The data in the API is structured as an array with multiple datasets inside curly braces that I am unsu ...

Emphasizing Text Within Div Element

Imagine having a div element structured as such: <div id="test" class="sourcecode"> "Some String" </div> Can CSS and JavaScript be utilized to highlight a specific portion of that string based on a search query? For instance, if the search q ...

revealing the hidden message on the back of the card

I have been working on creating flipping cards for my website using CSS, but I am facing an issue. Even after styling my cards as per the provided CSS code, I am unable to make them flip when I hover my mouse over them. .row { padding-top: 25px; } .c ...

Ways to retrieve a specific item from a constantly changing knockout observableArray without employing a foreach loop

Why can I only access one property ('member_A') of an Element in an observableArray using an HTML <input>? I am attempting to add a new object of type abc() to the observableArray "list_of_abc" when the button "ADD To List of abc" is click ...

Designing a slider to display a collection of images

I am currently working on a slider project using HTML, javascript, and CSS. I'm facing an issue where only the first two images (li) are being displayed and it's not transitioning to the other (li) elements. Below is the HTML code snippet: <se ...

PHP does not provide any error when an update on a database row fails

When I try to update values retrieved from the database into a form on submission, the process fails without any error message. Below is the PHP code snippet: <?php session_start(); $username=$_SESSION['uname']; $cn=mysqli_connect("loc ...

Repetition of Angular 4 component in routing for a specific path

Encountering an unusual issue where a specific route causes the component to be duplicated in the DOM when directly launched, but functions correctly when navigated from the main page. The problematic route is contact, utilizing the ContactComponent. You ...

What steps can I take to resend a fetch request in React.js if I receive a 408 or 503 error?

There are times when I am fetching data from a server and the request takes too long, resulting in the server responding with a 408 status code. This is how my code currently looks: export class ResponseError extends Error { public response: Response; ...

Concealing a hyperlink within a DIV by removing or hiding it

I am looking to temporarily hide or remove a specific link (team.aspx) within a drop-down menu, with the intention of adding it back in later. Here is my current code: <div id="nav_menu"> <ul id="nav"> <li class="current"><a href= ...

Steps to ensure checkboxes are enabled for selection only when another box has been checked

Consider having 3 checkboxes: 1 'parent' checkbox and 2 'child' checkboxes. If the parent checkbox is selected, you should be able to select either or both of the child checkboxes. If the parent checkbox is deselected, neither of the ...

What is the best way to update the type = date values with my "SearchDate" parameter in a Python template?

I have the variable search_date in my view and I need to display this data in a template. How can I achieve this? In the object, the date is set as "2020-09-01". I tried to write the following code but it did not work as expected: {% extends 'bas ...

Using preventDefault in the compositionend event does not make any difference

var inputNode = document.getElementById('view_1'); inputNode.addEventListener('compositionend', function(e) { console.log(e.cancelable); // true e.preventDefault(); }); <div id="view_1" class="view" contenteditable="true> &l ...

Achieve the effect of background images fading in once the page loads, then disappearing when an accordion tab is

Currently on a project, I have implemented two background images that cross-fade on this website: www.alexarodulfo.com. I am interested in exploring the following possibilities: 1) Achieving a more subtle fade-in effect for the images, perhaps allowing th ...

Steps to trigger a Bootstrap modal when the user clicks anywhere on the webpage

I need assistance with setting up a Bootstrap dialogue modal to open at the clicked position on a mousedown event when a user interacts with the page. Despite my attempts, the dialogue modal is not opening where it should be. Here's what I've tri ...

Is there a way to use JavaScript to import several custom fonts at once?

Is there a way to import a long list of custom fonts as icons using Javascript? I have been obtaining the font list from an API, but importing them one by one with pure CSS using @font-face is time-consuming and difficult to maintain. @font-face { fon ...

Automatically refresh a table within a webpage with updated database information without the need to manually refresh the

I'm a beginner in PHP and AJAX. The issue I am currently encountering is displaying a table on a JSP page and wanting to auto-update the table without refreshing the page every 10 seconds. I retrieve values from a database using a PHP page Below is t ...