Positioning Bootstrap 4 elements within Angular application views

As an Angular 7 developer, I'm currently working on creating a blog template using Bootstrap 4 within my application. The layout consists of two cards placed in a row - one for displaying the blog posts and the other for showcasing different categories. The category card content is fetched from a separate component and rendered using its selector

<app-blog-category></app-blog-category>
. For a visual reference, please refer to the image linked below. I'm encountering a couple of issues with the view that I need assistance with.

Here are the challenges I'm facing:

  1. The image inside the horizontal card is not occupying the full height and width of the card.
  2. The category card is vertically centered which is not the desired layout.

Below you can find the HTML and CSS code snippets relevant to this layout:

HTML:


<div class="container">
   <div class="row col col-lg-12" style="text-align:center; font-size:22px">All Blogs
     <br><br><br><br>
   </div>
   <div class="row" *ngIf="allBlogs.length>0">
     <div class="col-lg-9 card" *ngFor="let blog of allBlogs">
       <div class="row">
         <div class="col-md-4">
           <a [routerLink]="['/blog', blog.id]">
             <img src="http://localhost:4000/{{blog.imagePath}}" class="card-img-top card-img-size" alt="blog1"></a>
           </div>
           <div class="col-md-8 px-3">
             <div class="card-block px-3">
               <h4 class="card-title">{{blog.title}}</h4>
               <p class="card-text">{{blog.description}}</p>
               <br>
               <a href="#" class="mt-auto btn btn-primary">Read More</a>
             </div>
           </div>
         </div>
       </div>
     </div>
     <div class="col-md-3">
       <div class="container">
         <div class="row">
           <div class="col-md-12 card">
             <article class="filter-group">
                <div class="card-body">
                   <header class="card-header">
                      <h6 class="title">Categories</h6>
                   </header>
                   <ul class="list-menu">
                      <li *ngFor="let category of categories" [routerLink]="['/bycategory/', category.categoryId]">{{category.categoryName}}</li>
                   </ul>
                </div>
             </article>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

CSS:


.card-block {
   font-size: 1em;
   position: relative;
   margin: 0;
   padding: 1em;
   border: none;
   border-top: 1px solid rgba(34, 36, 38, .1);
   box-shadow: none;
}
.card {
   font-size: 1em;
   overflow: hidden;
   padding: 5;
   border: none;
   border-radius: .28571429rem;
   box-shadow: 0 1px 3px 0 #d4d4d5, 0 0 0 1px #d4d4d5;
   margin-top:20px;
} 
.btn {
   margin-top: auto;
}
.filter-group {
   border-bottom: 1px solid #e4e4e4
}
.card {
   position: relative;
   display: -webkit-box;
   display: -ms-flexbox;
   display: flex;
   -webkit-box-orient: vertical;
   -webkit-box-direction: normal;
   -ms-flex-direction: column;
   flex-direction: column;
   min-width: 0;
   word-wrap: break-word;
   background-color: #fff;
   background-clip: border-box;
   border: 1px solid rgba(0, 0, 0, 0.1);
   border-radius: 0.37rem
}
.card-header {
   padding: 0.75rem 1.25rem;
   margin-bottom: 0;
   background-color: #fff;
   border-bottom: 1px solid rgba(0, 0, 0, 0.1)
}
.filter-group .card-header {
   border-bottom: 0
}
.icon-control {
   margin-top: 6px;
   float: right;
   font-size: 80%
}
.list-menu {
   list-style: none;
   margin: 0;
   padding-left: 0
}
.list-menu a {
   color: #343a40
}
a {
   text-decoration: none !important;
   background-color: transparent
}

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

Answer №1

1) The reason for the image appearing like this is because it is set to go full width while maintaining proportional increase. The gap on the left is caused by the left padding in the parent div with the class col-md-4. To address this, one solution is to use a <div> with CSS properties such as background-image: url(), background-size: cover, and background-position: center, instead of an <img> tag. Adding height: 100% can help fit the image vertically. Although the CSS is provided inline, a class can also be used. To remove the padding on the left, the Bootstrap class pl-0 can be added.

<div class="col-md-4 pl-0">
  <a href="#">
    <div class="card-img-top card-img-size" [ngStyle]="{'height': '100%', 'background-image': 'url(' + blog.imagePath + ')', 'background-size': 'cover', 'background-position': 'center'}" alt="blog1"></div>
  </a>
</div>

2) The issue arises when the categories card is added only after the last card in the loop, causing it to be positioned alongside the final card and filling the col-9 with col-3.

To resolve this, all center cards can be wrapped in a column and the .col-9 of cards can be changed to .col-12.

<div class="row" *ngIf="allBlogs.length>0">

  <div class="col">
    <div class="col-md-12 card" *ngFor="let blog of allBlogs">
      <div class="row">
        <div class="col-md-4 pl-0">
          <a href="#">
            <div class="card-img-top card-img-size" [ngStyle]="{'height': '100%', 'background-image': 'url(' + blog.imagePath + ')', 'background-size': 'cover', 'background-position': 'center'}" alt="blog1"></div>
          </a>
        </div>
        <div class="col-md-8 px-3">
          <div class="card-block px-3">
            <h4 class="card-title">{{blog.title}}</h4>
            <p class="card-text">{{blog.description}}</p>
            <br>
            <a href="#" class="mt-auto btn btn-primary">Read More</a>
          </div>
        </div>
      </div>
    </div>
  </div>

  <div class="col-md-3 col-sm-12">
    <app-blog-category></app-blog-category>
  </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

Pass an image into an input field with the attribute type="filename" using JavaScript directly

My goal is to automate the process of uploading images by passing files directly to an input type="filename" control element using JavaScript. This way, I can avoid manually clicking [browse] and searching for a file in the BROWSE FOR FILES dialog. The re ...

Switching Font Family Option for Selection Mat in Angular Material

I'm currently working on changing the font of the options inside mat-select ("In what city were you born", etc). After some experimentation, I found that by setting ViewEncapsulation to none (allowing styles from other files to bleed in), I am able t ...

Looking to position the Secondary Navigation Bar on squarespace at the bottom of the page, distinct from the primary Navigation Bar

When SALES PAGE becomes the secondary navigation option Instructions for positioning style to Bottom Center I am attempting to place it at the bottom of the navigation bar. Can you provide me with the necessary code or styles in squarespace? When I choose ...

Store information during each iteration

Below is a snippet of my JavaScript code: for (var i in data){ var trans_no = data[i]; var transno = trans_no.transno; var transdate = trans_no.transdate; var dropno = trans_no.drop; var cusname ...

Issue encountered while attempting to establish a connection between Angular App and asp.net core signalr hub

I have developed an ASP.NET Core 3.1 application that utilizes Microsoft.AspNetCore.SignalR 1.1.0 and Microsoft.Azure.SignalR libraries. In the Startup file, the SignalR setup is as follows: services.AddSignalR().AddAzureSignalR("Endpoint=https://xx ...

Understanding the Union Type in Typescript and Its Application in Angular Development

I came across this piece of code: interface Course { code: string; name: string; user: number | { id: number; name: string; }; } This indicates that a course object can contain either the user object or the user key. When fetching the cour ...

Preventing access to websites through a web application using JavaScript

I am in the process of creating a web application that enables users to create a list of websites they wish to block, preventing access from their browsers. My goal is to block websites on all browsers, but I have narrowed my focus to Chrome for now. I ha ...

Changing the hidden input value to the data-id of a selected <a> element

I've set up a form with a dropdown menu and subdropdowns, generated using a foreach loop through a @Model. When I click on one of the options, I want the value of my hidden input field to match the data-id of the clicked item. This is what I have in ...

Container contents are spilling out of control after adjusting container height to auto

I'm facing an issue on my webpage where the text overflows the container div, even after setting the height to auto. Here's how the page is structured: <html> <head> <body> <div id="wrapper"> <div id="inner_wrapp ...

Hide the selection box when hovering over the Div

Looking for a quick solution. I want the option drop down to close when another div element is hovered over. First, open the drop down and hover over the red element on the right side. When hovering over the red element, I would like the drop down to clos ...

Why do my paths encounter issues when I alter the manner in which I deliver my index.html file?

I recently made a decision to change the method of serving my index.html file from app.use('/', express.static(__dirname + '/..')); to app.get('/', function(req, res) { res.sendFile(path.join(__dirname + '/../index.htm ...

The DOM seems to be producing NaN when receiving user input

Currently, I am in the process of developing a depreciation calculator that utilizes user input fields to generate an alert with the resulting value. When I set the variables to predefined test values without incorporating user input, the calculator funct ...

Is there a way to make the product list view in Magento much more compact and smaller in size?

My issue is pretty straightforward. I find Magento's product display in list view to be too large for my liking. The images are bigger than necessary and each product takes up too much space. I want to resize the listing so that each product only occu ...

How can I ensure that buttons in a row utilize the full 100% of available space?

I have a row of buttons in my HTML code like this: HTML : <div class="buttons"> <div><input type="button"/></div> <div><input type="button"/></div> <div><input type="button"/></div& ...

Vertical text frozen at the top of a table

My goal is to design a table with frozen threads and vertically oriented labels in the header. I have made an attempt at implementing this below, but as a newcomer to CSS, I am facing several challenges. One issue I've encountered with my solution ...

When utilizing the Angular library in an Ionic project, make sure to call the inject() function within an injection context

I have been working on a project that involves an angular workspace containing an angular web app and an angular ionic app. In an effort to reuse my existing authentication service, which utilizes AngularFireauth and AngularFirestore, I extracted the servi ...

What is the best way to create text boxes that can expand and collapse within a UIWebView?

I am looking to present text in a UIWebView with expandable or collapsible boxes that contain titles. The user should be able to tap the bar of a collapsed box to expand it. Is there a simple solution available that can be easily integrated into a local ...

Easy Registration Page using HTML, CSS, and JavaScript

In the process of creating a basic login form using HTML and CSS, I'm incorporating Javascript to handle empty field validations. To view my current progress, you can find my code on jsfiddle My goal is to implement validation for empty text fields ...

Is it feasible to utilize VAST for delivering HLS streams? Can m3u8 files be incorporated into VAST tags for delivery?

We are integrating a video player that supports VAST pre-roll, mid-roll, and post-roll video ads. I'm wondering if it's feasible to include m3u8 files in VAST tags? I've reviewed the vast specification and examples, but haven't come ac ...

Having trouble pushing to an array in Angular typescript? It seems to be returning as undefined

As a newcomer to Angular with a basic understanding of JavaScript, I am attempting to create a program that can solve Sudoku puzzles. In a 9x9 grid, there are a total of 81 points or squares. To efficiently check for violations of Sudoku rules - no repeati ...