Aligning Cards with HTML and CSS

Is there a way to position the image on the right side of the card without using margin-left? I want to avoid issues where the image drops to the next line if the title is too long.

<div class="block_container1">
  <div class="tit" *ngFor="let c of cdata">{{c.name}}</div>
  <div style="text-align: right;"><img src="assets/imgs/32.png"></div>
</div>

.block_container1 > div {
    display: inline-block;
    vertical-align: middle;
}

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

Answer №1

To align the <img> element within a <div> element to the right, you can use the CSS property float: right;. Remember to clear the float property for the next <div> element. To ensure proper alignment, consider using the line-height property for your <div> elements.

Here is an example snippet of CSS code:

.block_container1>div {
  display: inline-block;
  vertical-align: middle;
}

.tit {
  line-height: 38px
}

.myImg {
  float: right;
  line-height: 38px
}
<div class="block_container1">
  <div class="tit" *ngFor="let c of cdata">hello</div>
  <div class="myImg"><img src="https://www.w3schools.com/tags/smiley.gif"></div>
</div>

Answer №2

To incorporate the image as a background in your block_container1 class, adjust the background-position to "top right," and ensure it does not repeat:

.example { 
  min-height:500px;
  background-image:url('https://cdn3.iconfinder.com/data/icons/fabric-features-clothes-materials-glyph/64/41_design-sample-color-palette-128.png'); 
  background-repeat:no-repeat;
  background-position:top right;
  margin:40px;
  }
<div class="example">

  <h2>Example</h2>
  
  This is an example.
  
</div>

To create padding around the image, assign a margin to .block_container1. For more information on background positions, visit this link.

Answer №3

Consider using the flexbox method:

.container_box {
  display: flex;
  justify-content: space-around;
  align-items: center;
 }
<div class="container_box1">
  <div  class="title" *ngFor="let item of items">{{item.name}}</div>
  <div  style="text-align: right;"><img  src="assets/imgs/32.png"></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

Is there a way to showcase the content of a text file in a sequential manner using Javascript/AJAX?

I am attempting to retrieve text data from a server file and exhibit it within a specific division on my website. Check out the AJAX/Javascript code I have been working with: <body onload="loadXMLDoc()"> <script> function loadX ...

Ensure the active pill remains visible upon refreshing the page with Bootstrap 4

Currently, I am in the process of building a website with Bootstrap 4, and I have chosen to use pills for my navigation bar buttons. The issue that I am facing is that whenever I refresh the page while on a different tab, it always redirects me back to the ...

One div is experiencing trouble with the `align-items: center` property, while the other divs are functioning

Presenting my React component: <div className='sidebar'> <div className="sidebarItem"> <span className="sidebarTitle">About Me</span> <img src="https://wallpapera ...

Ensure JSF encodes special characters in Unicode

Can JSF be forced to escape special characters using the unicode method &#228; instead of the named entity approach &auml;? Issue: Some older mobile devices (such as a HTC Desire with Android 2.3.4) fail to display a page containing named entities ...

Having Trouble with jQuery toggleClass

I'm currently working on a project where I have implemented a button that displays a menu when clicked. I am attempting to change the color of the button when it is active, however, the toggleClass function is not behaving as expected. Below is the co ...

Using multiple html files with ui-router's ui-view feature

Is it possible to create a complex UI using multiple HTML files with ui-view in AngularJS? I am trying to achieve a layout similar to this: I attempted to implement something on Plunker, but it seems like I'm struggling to grasp the underlying concep ...

Divide a JavaScript string within a pair of HTML elements

I am currently working with a JavaScript string that contains HTML code to be integrated into my app. However, I am facing an issue where the string includes unnecessary tags in the head section, which end up displaying as text at the top of the content. H ...

Stop the color buffer from being automatically cleared in WebGL

Removing gl.clearColor(c[0],c[1],c[2],1.0); gl.clear(gl.COLOR_BUFFER_BIT ); does not stop the screen from getting cleared at the start of the next draw cycle. Is there a method to avoid this situation? I am aiming for an overpaint effect. ...

What could be causing my div elements to not appear on the page?

Hey there, this is my debut post here so bear with me on the formatting. I'm currently working on a project and encountering some hurdles with my divs. Even though I've left space for them, I just can't seem to get my divs (or cards) to disp ...

What is the best way to restrict users from inputting anything other than alphabetic characters in an HTML text input field

I'm trying to create a text input field that will only accept one letter from A to Z (not case sensitive). I've managed to limit the input to one character using the maxlength attribute, but users can still enter numbers or special characters. &l ...

Error: The selector "html" is not considered pure as it does not contain any local class or id. Pure selectors must include at least one local class or id

When working on my Next.js project, I included the following code in _app.js: import '../src/styles/style.module.scss'; import '../src/styles/main.module.scss'; This is the content of main.module.scss: // ./src/styles/main.module.scss ...

I'm looking for assistance on how to set the minimum height using jQuery. Can anyone provide

My attempt to use the minHeight property in one of my divs is not successful, and I am unsure why... <div id="countries"> <div class="fixed"> <div class="country" style="marging-left:0px;"></div> <div class="country"&g ...

What steps should I take to insert a divider in a dropdown menu?

I am attempting to enhance my dropdown menu by incorporating a separator using the following code: <li class='divider'></li> Below is my HTML code with the separator included: <ul class="dropdown-menu dropdown-menu-right" id="ul ...

Sliders are malfunctioning in the mobile view

I am facing an issue with WordPress sliders. They display properly on desktop mode but not in mobile mode. I want the full slider to show up on mobile view as well. For more details, please refer to the images and URL below: https://i.sstatic.net/yR5N5. ...

How can I use Moment.js to show a custom message based on the current day of the week?

Currently, I am utilizing Moment.js to dynamically showcase various messages based on the day of the week. For instance, if it's: If it's Monday, I want it to display "It's Monday!" If it's Tuesday, I'd like it to show "Happy Tu ...

What is the method for creating bootstrap columns within a single row that scroll horizontally instead of wrapping to the

I am looking to create rows with columns that total more than 12, arranged in a way that allows the columns to scroll horizontally. Here is an example of what I have attempted: <div class="container-fluid"> <div class="row" ...

divs with sticky sidebars are a common design choice

I have a sidebar for each div and I want to make all the sidebars fixed when scrolling down. You can find the code snippet here: var tmpWindow = $(window), sidebar = $('.sidebar'), sidebarHeight = sidebar.height(), offsetBottom = $( ...

To style a text box next to text within a paragraph in CSS, simply create an empty box

I have some CSS classes defined as follows: .p_box { position: relative; } .p_box span { background-color: white; padding-right: 10px; } .p_box:after { content:""; position: absolute; bottom: 0; left: 0; right: 0; top: ...

How can I make CSS text-overflow ellipsis trigger a pop-up to display lengthy text when clicked?

I'm encountering an issue with a lengthy sentence in a table cell which I've truncated using text-overflow to display ellipsis. Is there a way to enable users to click on the dots (...) and view the full text in a popup window? table { wi ...

React Native View will automatically resize to accommodate its content when it is centered

I am facing an issue with a React Native View. I want the View to extend up to a certain width but not exceed it. Inside this View, there is a Text element that should fill out the full width of its parent. The problem arises when I try to center align the ...