Position the Crossmark to align with text using CSS styling

How can I properly align a crossmark and a checkmark with the text behind them? The checkmark looks aligned well, but the crossmark appears to be slightly off to the top-right. I'm hesitant to adjust margins and paddings as it may cause issues when the screen size changes. Is there a more reliable solution to fix this alignment problem?

.list {
    font-family: sans-serif;
    margin: 1rem 0 1rem !important;
}

.checkmark {
    transform: rotate(45deg);
    height: 24px;
    width: 12px;
    border-bottom: 7px solid lightgreen;
    border-right: 7px solid lightgreen;
}

.crossmark {
    height: 24px;
    width: 12px;
    position:relative;
}

.crossmark::after{
     position: absolute;
     content: '';
     width: 24px;
     height: 0px;
     border: solid orange;
     border-width: 0 0px 7px 0;
     transform: rotate(45deg);
     top:0;
 }

.crossmark::before{
     position: absolute;
     content: '';
     width: 24px;
     height: 0px;
     border: solid orange;
     border-width: 0 0px 7px 0;
     transform: rotate(-45deg);
     top:0;
 }

.highlighted-err {
    background-color: red;
    color: white;
    border-radius: 0.5rem;
    padding: 0.5rem;
}

.highlighted-success {
    background-color: darkgreen;
    color: white;
    border-radius: 0.5rem;
    padding: 0.5rem;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3a5855554e494e485b4a7a0f140a10c">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

<div class="row list" id="type-msg">

</div><div class="row list highlighted-success" id="size-msg">
    <div class="col-md-1">
        <div class="checkmark"></div>
    </div><div class="col-md-11">
        <div>Image files must be 600 x 450 pixels or larger.</div>
    </div>

</div><div class="row list highlighted-err" id="aspect-msg">
    <div class="col-md-1">
        <div class="crossmark"></div>
    </div><div class="col-md-11">
        <div>Image files must have a width to height ratio of 3:4.</div>
    </div>
    
</div>

Answer №1

Ensure to add a crossmark both before and after

   top:0;

Substitute the 2 with

   top:12px;

When rotating 45deg and -45deg, position the element in the center (crossmark is 24px, so center is 12px), and then rotate. The transform origin should be set to rotate the center of the element.

Answer №2

After modifying your absolute positioning by changing top: 0; to top: 50%; and including translate(0, -50%) in each transform, the before and after elements are now centered within the parent element.

These adjustments will maintain the central alignment even if you alter the fixed widths and heights.

.list {
    font-family: sans-serif;
    margin: 1rem 0 1rem !important;
}

.checkmark {
    transform: rotate(45deg);
    height: 24px;
    width: 12px;
    border-bottom: 7px solid lightgreen;
    border-right: 7px solid lightgreen;
}

.crossmark {
    height: 24px;
    width: 12px;
    position:relative;
}

.crossmark::after{
     position: absolute;
     content: '';
     width: 24px;
     height: 0px;
     border: solid orange;
     border-width: 0 0px 7px 0;
     transform: translate(0, -50%) rotate(45deg);
     top: 50%;
 }

.crossmark::before{
     position: absolute;
     content: '';
     width: 24px;
     height: 0px;
     border: solid orange;
     border-width: 0 0px 7px 0;
     transform: translate(0, -50%) rotate(-45deg);
     top: 50%;
 }

.highlighted-err {
    background-color: red;
    color: white;
    border-radius: 0.5rem;
    padding: 0.5rem;
}

.highlighted-success {
    background-color: darkgreen;
    color: white;
    border-radius: 0.5rem;
    padding: 0.5rem;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3a5855554e494e485b4a7a0f140a1408">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

<div class="row list" id="type-msg">

</div><div class="row list highlighted-success" id="size-msg">
    <div class="col-md-1">
        <div class="checkmark"></div>
    </div><div class="col-md-11">
        <div>Image files must be 600 x 450 pixels or larger.</div>
    </div>

</div><div class="row list highlighted-err" id="aspect-msg">
    <div class="col-md-1">
        <div class="crossmark"></div>
    </div><div class="col-md-11">
        <div>Image files must have a width to height ratio of 3:4.</div>
    </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

"Adjusting the Height of an MUI Autocomplete Input Field: A Simple Guide

I've been attempting to shrink the size of the Input field in MUI autocomplete but haven't had any luck. Here's the code I tried: styling: autocompleteStyle: { height: '2.3rem', color: `${theme.palette.grayDark1} !important ...

HTML5 input placeholder adapts its size and position dynamically as data is being

During my interaction with the input fields on my bank's website, I noticed that the placeholder text undergoes a unique transformation. It shrinks in size and moves to the upper-left corner of the input field while I am entering information. Unlike t ...

Styling the background of a container in CSS using Bootstrap

Currently, I am utilizing bootstrap and trying to find an elegant solution for adding a background to my container. Specifically, I am working with the bootstrap class container, as opposed to container-fluid. The official Bootstrap documentation advises ...

Experiencing difficulties positioning svg text path in the center using CSS on desktop devices

I've looked into other SVG text questions, but I'm struggling to understand my mistake. Currently, I'm working on a live site at and implementing the following code: <svg class="svg-width desktop" style="margin:auto"> <path ...

I am looking to build an array that can store five numbers inputted by the user. Following that, I want to utilize a for loop to display the contents of the array. How can I accomplish this task?

Looking for help to store 5 unknown numbers in an array and then display them after the user has entered all 5 numbers. Can anyone assist me in creating an array of size 5 and using a for loop to show the numbers? Here is the code I currently have: ...

[Vue alert]: Issue in created function: "TypeError: Unable to assign value to undefined property"

I'm currently in the process of developing a VueJS application where I have implemented a child component called Child.vue that receives data from its parent. Child.vue export default{ props:['info'], data: function(){ ...

Tips for sending parameters to a web service URL through a Phonegap HTML page

In my Phone Gap application, I have an HTML page where I need to pass the values like name, contact, etc. to a Webservice URL. However, I am unsure of where to write the code and how to call the webservice URL. Here is a snippet of my HTML code: <div ...

Guide on showcasing active users currently online within the system utilizing CodeIgniter

I'm currently working with CodeIgniter and trying to display live online users using this framework. I attempted some code, but I'm feeling a bit lost now. I found a helpful video that I learned from: Alternatively, I followed these steps as sh ...

Tips for choosing an option from a react dropdown using Cypress

Exploring Cypress: I am currently working with the following code snippet. Although it seems to be functioning, I have a feeling that there might be a more efficient way to achieve the desired result. There are 25 similar dropdowns like this one on the pag ...

ASP.NET MVC does not automatically refresh when changes are made to the CSS file

Currently, I am diving into the world of Bootstrap 4 within an ASP.NET MVC project, exclusively making changes to the index.cshtml file in JetBrains Rider. However, I've encountered a strange issue that has me stumped. Each day, when I begin writing ...

Configuring Dialog Placement to the Top in Material-UI

I'm in the process of creating a popup dialog, but I'm encountering an issue where the dialog pops up in the center of the page. How can I position it at the very top of the page when the popup button is clicked? Below is the code snippet: < ...

Utilizing jQuery UI to dynamically alter CSS classes

I'm currently working on a project and could use some assistance. I have divs that are droppable using jQuery UI. While everything is functioning properly and I can drag and drop, I am looking to modify the opacity of the div I am dragging by changing ...

In Firefox 50.0, the button's resizing feature causes the parent div container outline to adjust size

I am currently working on developing a responsive design with the feature of having a button that appears to "float" under a div element. My desired outcome is achieved in Opera and Chrome: https://i.sstatic.net/Vm55V.png In Firefox, the result is not as ...

How to use jQuery to maintain an image at the top of a div

Within my HTML, there is a primary root div element containing an image. <div id="root"> <img id="msg"/> </div> Using jQuery, I am able to prepend multiple div elements inside the main root div. However, the problem arises when these ...

Include a new class in the classList of an element at a specific index

Is it possible to add a class to a specific position in order to maintain a certain order, especially when it goes against the logic that targets the class based on its position? link.closest('item').classList.add('c-class') // Contrad ...

Finding image input loading

When working with the following code: <input type="image" ... onLoad="this.style.opacity = 1" /> Everything seemed to be functioning well in IE, but encountered an issue in Chrome where the onLoad event failed to trigger upon image load. It's ...

Triggering a targeted action upon the user's click on any HTML element

Currently, I am in the process of developing navigation buttons that trigger a dropdown sub-menu when clicked by utilizing checkbox inputs. Upon clicking the label, the input is checked, causing the menu to drop down, and when the label is clicked again, t ...

"Graphs not Displaying Properly in ChartJs

Seeking assistance with rendering a chart inside a bootstrap popover. Despite various debugging attempts, the chart refuses to render. Any help or insight would be greatly appreciated. Below is my HTML code: <div id="popover-content" style=&qu ...

Want to know how to get images to show up when sharing a link on Twitter?

For illustration purposes, I am using a CNN article page: The HTML head section of the webpage contains the following tag: <meta name="twitter:image" content="http://i2.cdn.turner.com/money/dam/assets/150114084151-inside-tracker-120x90.png"> Howe ...

Multiple callbacks triggered by jQuery CSS transitions

I am experiencing an issue with my menu animation. I am curious as to why the slideDown transition is occurring twice. You can view the JSFiddle here. <ul class=top-menu"> <li>Item 1</li> <li>Item 2</li> <ul cl ...