Align center a label with a checkbox when spanning multiple lines by utilizing the capabilities of Tailwind

I'm facing an issue with aligning a checkbox label that spans multiple lines. How can I center the first line with the checkbox?

<label class="inline-block">
<input class="ml-2 mr-1 mb-1 inline-block" data-id="{{inputDataId}}" type="checkbox" class="{{class}}" {% if required %} required {% endif %}>
<span class="text-sm inline-block">
  {{label}}
</span>
</label>

The current alignment looks like this: https://i.sstatic.net/aylY6.png

Answer №1

Unique Tailwind solution

To achieve the desired result using Tailwing, consider the following code:

<label class="flex">
<input class="ml-2 mr-1 mb-1 mt-1 inline-block" data-id="{{inputDataId}}" type="checkbox" class="{{class}}" {% if required %} required {% endif %}>
<span class="text-sm inline-block">
 sdfsd fsd fsd fsdfds fsdf sdf s fsdfsdf
</span>
</label>

Unique Custom CSS solution:

If you position your label tag with flex positioning, you can easily align the input and text vertically by using "align-items: flex-start;" :

<label class="my-label-class">
<input class="ml-2 mr-1 mb-1 inline-block" data-id="{{inputDataId}}" type="checkbox" class="{{class}}" {% if required %} required {% endif %}>
<span class="text-sm inline-block">
  {{label}}
</span>
</label>

.my-label-class {
  display: flex;
  align-items: flex-start;
}

Unique Bootsrap solution

Alternatively, if you utilize Boostrap classes, you can switch out your Label tag classes with Boostrap ones:

 <label class="d-flex align-items-start">

This will align your input and text to the top.

You have the option to center them using "align-items-center"

Answer №2

To enhance the layout, include flex items-top in the wrapper and eliminate the mb-1 from the input field.

<link rel="stylesheet" href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="304451595c47595e5453434370001e051e02">[email protected]</a>/dist/tailwind.min.css">


<label class="flex items-top">
        <input class="ml-2 mr-1 inline-block" data-id="{{inputDataId}}" type="checkbox" class="{{class}}" {% if required %}
    required {% endif %}>
        <span class="text-sm inline-block">
            Dsfsd fsdfsdfs fsdfsdfsd sdfsdfs sdfsdfsdf sdf sdf dsfsd fsdfsdfs fsdfsdfsd sdfsdfs 
            sdfsdfsdf sdf sdf dsfsd fsdfsdfs
            fsdfsdfsd sdfsdfs sdfsdfsdf sdf sdf dsfsd fsdfsdfs fsdfsdfsd sdfsdfs sdfsdfsdf sdf sdf
       </span>
    </label>

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 the CSS property `backface-visibility` causing compatibility issues across different browsers with animations?

I have created a unique flip animation that displays new numbers resembling an analog clock or calendar with a hinge in the middle. The process is simple: using a div, I have: The bottom half of the first number on one side The top half of the second num ...

How can I locate the exact XPath using an ID and div element?

I am faced with a challenge involving a table of names where each name can be accessed by a specific XPath: //*[@id="right_pane"]/div[1]/table/tbody/tr[INDEX]/td[2], with the index being a number. I am seeking the most efficient way to search for these XPa ...

What could be causing the jQuery news ticker to malfunction on my site?

I recently made some changes to my main page by embedding HTML and adding the following code/script: <ul class="newsticker"> <li>Etiam imperdiet volutpat libero eu tristique.</li> <li>Curabitur porttitor ante eget hendrerit ...

The react-json-schema tutorial is not appearing in the browser

As a complete beginner to web development (html/js), I recently discovered the react-json-schema package and found it works great in the provided sandbox. However, I'm facing an issue with getting the tutorial to work. I followed the tutorial and cre ...

The webpage is jolting every time it's reloaded

I'm facing a frustrating issue on my website. Every time I refresh the page, it jerks and the text seems to compress and then return to normal. Even after removing most of my code, the problem persists and I can't figure out what's causing i ...

I'm curious about the method used to create this body fadeIn effect

Recently, I stumbled upon the website "". I must say, it is truly remarkable. I can't help but wonder how the content on this page is cleverly faded in and slides up. This specific page caught my attention: ...

Create an endless loop of two distinct animations with animate.css

Lately, I've encountered an issue with animate.css while working on my latest project. What I'm aiming to achieve is to update a paragraph of text on my website every five to ten seconds using jQuery. However, I don't want the text to simpl ...

Running a Python script through a Javascript event

I'm looking to enhance my webpage by implementing a feature where users can generate a personalized QR code with the click of a button. My current setup involves a Python script that creates the QR code image and saves it as a .png file. I want to int ...

How to access the api variable in JavaScript

When attempting to retrieve variables from an API object, I encountered the obstacle of them being nested inside another object named "0" in this particular case. Here is a screenshot from the console: enter image description here Below is the JavaScrip ...

Enable hyperlink to open in a new browser tab after user clicks on button embedded inside an iFrame

<head> </head> <div> <iframe src="https://....."></iframe> </div> https://i.sstatic.net/dk21i.png I'm looking for a way to open the link in a new tab when clicking on this button. Currently, the page loads wi ...

Separating text for tooltips from the element itself

I'm looking to enhance my website by adding tooltip descriptions to elements based on their CSS classes. While tooltips are usually added using the element's title attribute, I have numerous elements with the same lengthy description and want to ...

The hamburger menu fails to close when clicked on in Bootstrap

I seem to be having an issue with the hamburger menu on my website. When I click on it, the menu opens as expected. However, when I click on it again, the menu doesn't close properly. It appears to flash or maybe it is closing and reopening quickly up ...

Is it ideal to have a single view for the entire Django website?

Imagine wanting to create a website using only one HTML template. One idea is to have a single view that handles all possible inputs and outcomes, such as when a new object is created by clicking a button and a form appears while the rest of the page remai ...

The iframe remains unresponsive and fails to resize as needed

I am facing an issue while trying to embed an iframe into one of my websites. It seems that the responsiveness is lost and it does not scale properly on mobile devices. You can see the issue here. Interestingly, when I place the same iframe on a plain HTM ...

Is there a way to not limit the height of parent elements when using the Bootstrap 4.6 grid system or Flexbox

I'm struggling to make Bootstrap/Flexbox work smoothly within a height restriction. In my design, I have a <main> element with a set height. In the application itself, this height is calculated so that the footer aligns perfectly at the bottom ...

It's impossible for me to align two blocks at the same level

Check out the following code snippet. I am trying to group mid_left and mid_right within a single mid div tag, but struggling with positioning mid_right correctly - either outside of the mid tag or not at the same level as mid_left. I have experimented wit ...

How can the object currently being dragged be chosen using JQueryUI Draggable?

Working with JQueryUI draggable, I am interested in applying styling to the draggable element while it is being dragged. Various attempts have been made using code like this: $(".ui-widget-content").draggable({ drag: function(event, ui) { $(this).cs ...

Choose dropdown menus that are pre-filled by JSON may require a manual refresh to show the content

Occasionally, I encounter an issue with a JSON file used to populate select boxes, where the items in the drop down fail to display until the page is refreshed. Despite no errors in the console or log, the loading is seamless on most occasions, leaving me ...

Tips for ensuring long text wraps to the next line when it exceeds the width of the browser window

I'm struggling with CSS styles and properties. I want the text to wrap to the next line instead of overflowing off the browser screen. I've illustrated what's currently happening versus what I actually need. https://i.stack.imgur.com/mPGt8 ...

Obtaining an HTML horizontal scroll bar

When using bootstrap and the row class, I am encountering a horizontal scroll bar. <div class="row" style="margin-top: 10px; margin-right: 10px;"> <div class="card col-md-3" style="max-height:fit-content; ba ...