Having trouble applying CSS styling to the div element

I'm new to CSS and struggling with aligning content within a div. Specifically, I can't get the actual price (striked off) to display next to the discounted price in the card - it keeps appearing underneath. Any ideas on how I can improve my CSS to fix this? Thank you.

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

Snippet of my code:

#products{
  padding-top:3%;
  display:flex;
  justify-content:center;
}
#product-content {
  margin-bottom:5%;
  padding-bottom:2%;
}
.mat-card-title {
  cursor:pointer;
  white-space:pre-line;
}
#tag {
  float:left;
  margin-right:1rem;
}
#category {
  margin-left:25%;
  position:relative;
}
#wishlist {
  margin-left:auto;
  margin-right:auto;
}
#discounted-price {
  float:right;
}
#actual-price {
  text-decoration:line-through;
}
#add-to-cart {
  float:right;
}
<div class="container" id="products">
  <mat-card class="product-content" *ngFor="let product of products" id="product-content">
    <mat-card-header>
      <mat-card-title (click)="navigate()">{{ product.name }}</mat-card-title>
    </mat-card-header>
    <mat-card-content>
      <div>
        <div id="tag">{{ product.tags }}</div>
        <div id="category">{{ product.category }}</div>
        <div id="wishlist" (click)="addToWishList(product)">
          <i [ngStyle]="{ color: product.isInWishList ? 'yellow' : '' }" class="fa fa-star-o fa-2x" aria-hidden="true"></i>
        </div>
        <div id="discounted-price">Rs. {{ getDiscountedPrice(product) }}/-</div>
        <div id="actual-price" *ngIf="product.discountedPerc === 0; else displayOriginalPrice">-</div>
        <ng-template id="displayOriginalPrice" #displayOriginalPrice>{{ product.actualPrice }}</ng-template>
        <div id="add-to-cart">ADD TO CART</div>
      </div>
    </mat-card-content>
  </mat-card>
</div>

Answer №1

Utilizing CSS FLEX can create a visually appealing layout where items are aligned in one row effortlessly.

#products {
  padding-top: 3%;
  display: fixed;
  position: inherit;
  margin: 0 auto 0 auto;
}

#product-content {
  margin-bottom: 5%;
  padding-bottom: 2%;
}

.mat-card-title {
  cursor: pointer;
  white-space: pre-line;
}

#tag {
  position: relative;
  width: 6rem;
  text-align: center;
  border-radius: 0.5rem;
  background-color: darkorange;
  font-family: Verdana, Geneva, Tahoma, sans-serif;
}

#category {
  position: relative;
  font-family: Verdana, Geneva, Tahoma, sans-serif;
  text-align: center;
}
... (remaining code unchanged)
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet" />
<div class="container" id="products">
  <mat-card class="product-content" *ngFor="let product of products" id="product-content">
    ... (remaining HTML code unchanged)
        </div>
      </div>
    </mat-card-content>
  </mat-card>
</div>

The addition of CSS with line-through ensures that prices are struck-off for a more visually appealing display.

It's important to note the removal of float and margin properties for proper functioning when using FLEX.

To learn more about FLEX, check out this resource here!

Answer №2

<div class="container" id="products">
      <mat-card
        class="product-content"
        *ngFor="let product of products"
        id="product-content">
        <mat-card-header>
          <mat-card-title (click)="navigate()">{{ product.name }}</mat-card-title>
        </mat-card-header>
        <mat-card-content>
          <div>
            <div id="tag">{{ product.tags }}</div>
            <div id="category">{{ product.category }}</div>
            <div id="wishlist" (click)="addToWishList(product)">
              <i
                [ngStyle]="{ color: product.isInWishList ? 'yellow' : '' }"
                class="fa fa-star-o fa-2x"
                aria-hidden="true"
              ></i>
            </div>
            <div id="discounted-price">Rs. {{ getDiscountedPrice(product) }}/-</div>
            <div
              id="actual-price"
              *ngIf="product.discountedPerc === 0; else displayOriginalPrice">-</div>
            <ng-template  #displayOriginalPrice id="displayOriginalPrice"><div> {{
              product.actualPrice
            }}</div></ng-template>
            <div id="add-to-cart">ADD TO CART</div>
          </div>
          <div id="new-div-in-ng-template">New div added in ng template<</div>
        </mat-card-content>
      </mat-card>
    </div>

Add the new div inside the ng template 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

Outputting HTML from a function

As a beginner, I have a question about best practices. Is it more effective to return HTML from a function like the following: function displayHtml($userName) { $htmlMsg = " <html> <head> <title>Displaying HTML</title&g ...

Is there a way to remove a specific item (identified by its ID) from an array with just a

As a newcomer to Angular 8, I am seeking assistance with deleting an item from an array using (click)="deleteEmployee(el.id)". I attempted to use splice but encountered an error. Below is the code in Component.ts: employee: Employe; id: number; _e ...

Change the color of this element and input field background

Having trouble with setting the background color of an input field to red in this code: $(document).ready(function(){ $(".abc").focus(function(){ $(this).attr('background', 'red'); $("label").text('Insert tex ...

Implementing Node.js microservices with AWS Cognito leveraging Amplify's best practices

I am currently working on developing a Node.js API that is broken down into several small APIs (microservices) communicating with each other through requests and responses. Additionally, I am utilizing Angular for the frontend. My next step is to enhance ...

The alignment of elements in the div seems to be slightly skewed

Currently, I am in the process of creating a website with the temporary name yeet.io. I am facing an issue where I am attempting to center both an input and h1 element inside a div vertically and horizontally, but they keep appearing misaligned for some r ...

Place the p elements outside of the divs and align them accordingly

I have a div containing multiple p elements and I need to align buttons next to certain p elements. I want the buttons to be displayed only for specific p elements, which will be controlled using JavaScript. Normally, I would put each p element in a separa ...

Pictures on aspx websites utilizing a Master Page

On my master page, I have two images - a logo at the top and a Facebook link at the bottom. There is also an admin folder containing .aspx pages specifically for admin logins. For all non-admin pages, the images are accessed using the src of image/logo.jp ...

Placing one element beside another element

I am facing an issue with my layout where I have a list taking up 100% of the height and next to it, I am attempting to place an image box. Here is the fiddle link for reference: https://jsfiddle.net/wrdvkgyj/ Even after setting my image container to inl ...

Alerting JavaScript with element Selector doesn't function at full capacity

I am currently implementing Notify JS from the following source : Below is the HTML code I am using: <div> <p><span class="elem-demo">aaaa</span></p> <script> $(".elem-demo").notify( "Hello Box" ...

The popup input fields of ckeditor encounter issues when combined with a bootstrap 5 modal (ckeditor version 4)

Encountered an error while using ckeditor in a bootstrap 5 modal. It seems to be a common issue with various solutions available for different bootstrap versions, but I'm struggling to find one for bootstrap 5. Could someone please take a look? Here ...

What are the best scenarios for creating a constructor in Angular 2 using Typescript?

Check out these sample constructors I found in the Angular 2 documentation: export class AppComponent implements OnInit { title = 'Tour of heroes'; heroes: Hero[]; selectedHero: Hero; constructor(private heroService: HeroService ...

Creating word spacing in CSS paired with a navigation menu using Dreamweaver

As a beginner in Dreamweaver, CSS, and HTML, I may have made some mistakes or overlooked simple solutions. I am currently struggling with how to separate words in my navigation menu, as they always appear centered. Despite trying adjustments like "word-spa ...

Fixing the "Cannot find name" error by targeting ES6 in the tsconfig.json file

I recently started learning AngularJS through a tutorial. The code repository for the tutorial can be accessed at this link. However, upon running npm start using the exact code provided in the tutorial, I encountered the following error: Various TS2304 e ...

Text directly in the middle

Explanation In an attempt to replicate the "middle centered text" feature, such as "Responsive Front-end Development," on this website, I am currently exploring various methods. As a newcomer to web development, I have searched for templates to provide me ...

Unable to Anticipate User Input in Angular Using Scanner or Keyboard

Currently grappling with Angular 6 and facing an issue. I have a text box and a submit button, with a requirement for functionality to allow input through either manual keyboard typing or a Barcode scanner. The desired behavior is for the submit button to ...

Tips for expanding text within a grid

While working on an auto-fill grid that moves elements to the next line when the width reaches 800px, I encountered an issue with my text inside the grid not stretching. https://i.sstatic.net/OeJ3I.png Below is my CSS code: ul { display: grid; ...

Ensure that the link's color remains constant and remove any styling of text-decoration

Attempting to create a customized header. My goal is to change the color and remove text decoration in the navbar. Below is my code snippet: ReactJS: import React from 'react'; import './Header.css'; function Header() { return ( ...

Tips for integrating a Typeform widget into an Angular 4 application

The marketing department is requesting to integrate Typeform into a static page quickly and easily. I am using Angular 4, and the integration will be dynamic from the CMS. This means we need to support various types of widgets, such as the one with the fol ...

Show the child div using JavaScript when hovering over it

I am currently working with the following HTML structure: <div id="wrapper"> <div onmouseover="displayDiv()"> <div id="thisIsTheDivToDisplay"></div </div> <div onmouseover="displayDiv()"> <div id="thisIsT ...

Ways to make a component gradually appear and disappear in an Angular application

I have developed a task management application using Angular and I wanted to implement a fading effect for each task when it is created and before it is deleted. Despite successfully applying the fade in effect at the todo-item component level, I encounter ...