Attempting to position text and an icon using HTML and CSS

My current challenge involves aligning an icon with some text, but I'm facing this issue:

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

The desired alignment is not matching up, and instead it looks like this:

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

.percent {
  float: right;
  display: inline-block;
  position: relative;
}

.card-icon {
  float: left;
}
<div class="col-md-4">
  <div class="card">
    <div class="card-header" id="blue">
      <p class="text-center"> Self Managed <img src="app/assets/images/star.png" alt=""> </p>
    </div>
    <p class="percent"> <img class="card-icon" src="app/assets/images/self.png" alt="Smiley face"> 1% </p>

EDIT ****

After trying the solution provided in the previous answer, the outcome displays as follows: https://i.sstatic.net/YbMyZ.png

Answer №1

.card-icon {float: left;margin-right:10px; }
    <style type="text/css">
     
    </style>
    <div class="col-md-4">
    <div class="card">
    <div class="card-header" id="blue">
    <p class="text-center"> Self Managed <img src="app/assets/images/star.png" alt=""> </p>
    </div>
    <div class = "percent">
    <img class="card-icon" src="app/assets/images/self.png" alt="Smiley face">
    <div class="card-txt"> 1% </div>
    </div>
    </div>
    </div>

Answer №2

Consider utilizing inline-block instead of using floats:

.card-icon {
  display: inline-block;
  vertical-align: middle;
  position: relative;
  z-index: 1;
}

.card-icon:after {
  content: "=";
  position: absolute;
  right: -16px;
  top: 50%;
  transform: translateY(-50%);
}

.percent .card-txt:nth-of-type(1) {
  text-align: center;
}

.card-txt {
  display: inline-block;
  vertical-align: middle;
  text-align: center;
  margin-left: 24px;
}

.block {
  display: block;
}
<div class="col-md-4">
  <div class="card">
    <div class="card-header" id="blue">
      <p class="text-center"> Self Managed <img src="app/assets/images/star.png" alt=""> </p>
    </div>
    <div class="percent">
      <img class="card-icon" src="app/assets/images/self.png" alt="Smiley face">
      <div class="card-txt">
        <span class="block">1%</span>
        <span class="block">service tax + VAT</span>
      </div>
    </div>
  </div>
</div>

Answer №3

There's wisdom in simplicity, and sometimes the best solution is an old one. HTML tables provide a reliable way to control vertical height with broad browser support and backward compatibility. This method also ensures responsiveness, unlike other techniques that may falter when adjusting browser window size.

table {
  margin: 0 auto;
  border: 1px solid blue;
}

table tr td {
  padding: 10px;
  width: 100px;
  text-align: center;
  vertical-align: middle;
  position: relative;
}

table tr .number-cell {
  padding-top:10px;
  width: 80px;  
}

table tr .number-cell .value {
  margin-top: 4px;
    font-size: 42px;
}

table tr .number-cell .sub-heading {
  background: grey
}

table tr .equal-cell {
  width: 0px;
}

table tr td img {
  width: 100%;
}
<div>
  <h1>MY WEBSITE</h1>
  
  <table>
    <tr>
    
      <td class="image-cell">
        <img src="https://www.fillmurray.com/100/100"/>
      </td>
      
      <td class="equal-cell"> 
        <span class="equal">=</span>
      </td>
      
      <td class="number-cell">
        <div class="value">1%</div>
        <div class="sub-heading">service fee</div>
      </td>
      
    </tr>
  </table>
 
</div>

Answer №4

.card-icon {float: left;position:relative;z-index:1; }
.card-icon:after {content:"=";position:relative; left:25px;}
.percent .card-txt:nth-of-type(1) {text-align:center;position:relative; left:-55px;}
.card-txt {text-align:center; width:400px;background:#ddd;}
  <div class="col-md-4">
      <div class="card">
          <div class="card-header" id="blue">
              <p class="text-center"> Self Managed <img src="app/assets/images/star.png" alt=""> </p>
          </div>
          <div class = "percent">
              <img class="card-icon" src="app/assets/images/self.png" alt="Smiley face">
              <div class="card-txt"> 1% </div>
              <div class="card-txt"> service tax + VAT </div>
          </div>
      </div>
  </div>    

Answer №5

To have more control, you can utilize flex styling. Here's an example:

.card {
  position: relative;
  display: flex;
  flex-direction: column;
  
  margin: 0;
  padding: 0;
  
  width: 500px;
}
.card-header {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  
  padding: 10px;
  
  background-color: blue;
  color: white;
  font-size: 24px;
}
.card-content {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  
  background-color: gray;
}
.card-header > img {
  position: absolute;
  right: 0;
  width: 50px;
  height: 50px;
}
<div class="card">
   <div class="card-header">
     <span>Self Managed</span>
     <img src="app/assets/images/star.png" alt="">
  </div>
  <div class="card-content">
    <img class="card-icon" src="app/assets/images/self.png" alt="Smiley face">
    <span>1%</span>
  </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

The page scroll disappears once the jQuery dialog is closed

Here is the code I use before closing the dialog... $('body').css('overflow','hidden'); $('.ui-widget-overlay').css('width','100%'); $('#content').removeClass('lightbox_bg' ...

Establish a minimum width requirement for a Bootstrap4 webpage and ensure that scrollbars are visible

As I work on designing a website, I am facing an issue with responsiveness that I am willing to accept for now. I am looking to establish a minimum width for the overall page to be around 1000px, with a horizontal scrollbar appearing if the screen width is ...

The content displayed on body.innerHTML does not match the information found in the page source code

Why is the page source newer than the document.body.innerHTML, and how does this happen? While watching a video on YouTube and inspecting the page source in Chrome's console, I noticed that YouTube assigns a unique signature to all videos. Here are t ...

Unable to assign unique identifiers to elements within a user interface framework

I am having difficulty assigning an id to components. Scenario 1: - Trying to assign an id to an HTML component. <h1 id="demo-h1">Demo Heading</h1> Assigning id to HTML component Scenario 2: - Attempting to assign an id to a componen ...

Certain HTML elements on the webpage are not functioning properly when attempting to incorporate a div tag

One of the challenges I'm currently facing is that the links for HOME and ABOUT ME in the header section are not accessible when the 'data' div is added. Strangely, the DOWNLOAD and CONTACT ME links still work fine. This issue seems to be oc ...

Ways to incorporate a dynamic value into a chart created with chart.js?

I want to create a doughnut chart representing the number of individuals who have tested positive for Coronavirus and the number of deaths related to it. How can I transfer the same data from the top container into the chart? The confirmedCases and deaths ...

What could be causing this issue? Do you think I'm headed in the right direction?

Can someone assist me with troubleshooting my "getTutorNames" method? I've encountered an error message in my program that seems to be related to a variable being defined twice. The full problem description can be found in the provided link. I have be ...

Saving the content of a div as a PDF

There are several aspects to consider when it comes to this question, but I'm having trouble finding the information I need. Hopefully someone can provide an answer. Essentially, what I want to achieve is: Imagine a div that is 400 X 400 px in size. ...

How to Implement Screenreader and Ontab Functionality for Hover Visibility Accessibility

Hello there! I am excited to share that I am currently in the process of creating my very first React app. The purpose of this project is to develop a writer's application that can be easily accessible to all users. As of now, I have successfully bui ...

Overlapping input labels in Angular MaterializeCSS

I am currently facing an issue while attempting to use Angular MaterializeCSS () for designing a form. The problem I'm encountering is that my labels and inputs are overlapping, and despite following the documentation instructions by wrapping my form ...

What is the best way to achieve this state using jQuery?

I am looking to create a jQuery script that performs the following action: If an element with the class 'slide1', 'slide2' or 'slide3' also has the class "on", then locate an element with the class 'slide-1', ' ...

What is the best way to extract the contents of a textarea and save them to a separate JavaScript file

Is there a way to extract the content of a textarea element to a separate JavaScript file without directly including the JavaScript file in the HTML code? I am unable to embed the JavaScript file into the HTML file due to compatibility issues with MathStep ...

"Exploring the Functionality of Dropdown Menus in ASP.NET Core 1

Looking for a unique way to create a dropdown menu in ASP.Net Core 1.0? I've scoured the internet but haven't found a solution specifically tailored to this new platform. Can anyone provide guidance on how to build a large dropdown menu in Core 1 ...

Experiencing an overflow of redirects in PHP contact form

I am encountering an issue on my website where there are too many redirects occurring when attempting to submit a form that is supposed to be sent via email. The connection between the form and email seems to be correct. Form Code: <section class=" ...

Stealthy Google Recaptcha integrated with a dynamic AJAX form

My website includes an ajax form: <form id="my_form"> <input type="text" id="field1" /> <input type="submit" value="submit" /> </form> I also have this JavaScript code: document.getElementById("my_form").onsubmit = fu ...

The content box is not aligning properly with the content inside, causing it to vanish instead of overflowing with

There seems to be an issue with the content boxes on one of my pages. They all use the same CSS, but for some reason, there is a scroll bar in one of the content boxes and the footer doesn't align properly. The problem arises when I add overflow:auto ...

Issues with the alignment of columns using Bootstrap framework

I've created a design in Photoshop using the Bootstrap 12-column guide, but when I try to implement it using Bootstrap, the results are completely off. https://i.stack.imgur.com/vQwOt.png Here's my HTML code: <!DOCTYPE html> <html lan ...

Vertical alignment using Bootstrap buttons for pull-up and pull-down functionality

My design includes a row of Bootstrap buttons displayed horizontally. These buttons come in two sizes - medium and small, with the requirement that two small buttons should occupy the same vertical space as one medium button when stacked vertically. To il ...

Add a border-bottom with varying widths to the div or span element

My class called underlined has some styling applied: <style> .underlined { border-bottom: 1px dotted blue; } </style> When I compare a span and a div with this class, I noticed that the gap between the text and the border is different: < ...

The MVC5 model is unable to retrieve the text value entered in the textbox

I'm having trouble understanding what I'm doing wrong. Controller: public class PictureController : Controller { // GET: Picture public ActionResult Index() { return View(); } public ActionResult AddGroup() { ...