Creating a Custom Checkbox with HTML and Label

How can I position the label on top of a checkbox?

HTML:

<input type='checkbox' name='thing' value='valuable' id="thing"/>
<label  for="thing">Description</label>

CSS:

input[type=checkbox] {
    display:none;
}
input[type=checkbox] + label {
    background-image:url(uncheck.png);
    height: 20px;
    width: 20px;
    display:block;
    padding: 0 0 0 0px;
}
input[type=checkbox]:checked +label {
    background-image: url(checked.png);
    background-repeat:no-repeat height: 20px;
    width: 20px;
    display:block;
    padding: 0 0 0 0px;
}

Answer №1

The reason for the image appearing behind the element is because a background-image is being used in this case. To resolve this, you can utilize the ::before pseudo-element:

input[type="checkbox"] {
    display:none;
}
input[type="checkbox"] + label {
    height: 20px;
    display:block;
    padding: 0;
    vertical-align: top;
}

input[type="checkbox"] + label::before {
    content: url("uncheck.png");
    width: 20px;
    height: 20px;
    padding-right: 3px;
}
input[type="checkbox"]:checked + label::before {
    content: url("checked.png");
}

This method is the simplest way to incorporate images into elements using CSS. Initially, background-image was utilized before browsers fully supported ::before. However, all browsers that support :checked also support ::before.

Additional modifications made include:

  • Added quotes around strings for the attribute selector and URLs.
  • Changed padding: 0 0 0 0px; to padding: 0;, as both have the same effect.
  • Removed duplicate styles between :checked and default styles. The properties of width, height, and padding-right will apply to :checked + label::before as well as the unchecked version.

Answer №2

By including this code snippet in my CSS file, I was able to resolve the issue.

span {
    display: inline-block;
    margin-left: 20px;
    text-indent: 15px;
}

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

Using jQuery to dynamically insert variables into CSS styles

I am attempting to use jQuery 3 to modify the CSS of a class and change the background color. My first step is converting an rgba color code (properties.color) to a hexadecimal code, which is functioning correctly. Next, I try to change the background co ...

How can you incorporate a horizontal line beneath your text, similar to the one shown in the

https://i.sstatic.net/OzMVr.jpg I have a code snippet that needs a horizontal border similar to the one shown in the image <section class="about"> <div class="container"> <h1 class="text-center"> ...

No matter what I attempt, my presentation refuses to align in the center

My slideshow, which is powered by jQuery/JS and involves absolute positioning for each image, is causing me trouble when trying to horizontally center it on the page. No matter what I do, I can't seem to get it right. The challenge is not only getting ...

The speed of the Ionic app is disappointingly sluggish on devices, causing significant delays

After testing my app in Ionic Lab, it runs smoothly. However, when I create the apk file and install it on my device, the performance is very slow. There are delays in clicking on buttons, pushing pages, opening modals, and closing modals. It seems like t ...

IE11 React application cannot recognize the <main> tag

When using my React application, I encountered the following error message in the dev console while using IE11: Warning: The tag <main> is unrecognized in this browser. If you meant to render a React component, start its name with an uppercase let ...

Can you explain the contrast between "#msg" and "#msg div" when used in a style tag?

<html> <head> <style> #msg { visibility: hidden; position: absolute; left: 0px; top: 0px; width:100%; height:100%; text-align:center; ...

Fullscreen overlay or pop-up display

I have a website with images and iframes displayed within a fancybox. Everything is functioning properly, but now the website's requirement is to have a fullscreen overlay like on the usatoday website. Take a look at for reference. When clicking on ...

Step-by-step guide to implementing a month and year date-picker in Mozilla Firefox

I'm looking to create input fields where users can add the month and year. I tried using the code below, but unfortunately Mozilla Firefox doesn't support it. <input type="month" id="start" name="start"> Does ...

What causes the vertical inconsistency in ul li elements?

Can someone help me troubleshoot this HTML5 snippet? The LI elements are not aligning properly and I can't figure out why Item 1 is lower than Item 2. I need them to line up on the same level. Any suggestions would be greatly appreciated. <!DOCT ...

Guide to inserting a glyphicon within a bootstrap badge

I've been working on a unique way to display hierarchical data using bootstrap badges and AngularJS. My goal is to create a user-friendly experience where people can easily navigate through categories. There are 3 main types of categories: parent: T ...

What is preventing me from clicking on the left side of the link on this CSS flip card while using Chrome browser?

When attempting to click the left side of the link on the back of this flip card in Chrome, it does not respond. However, hovering over the right side allows interaction with the link: /* Vendor prefixed by https://autoprefixer.github.io */ .card { ...

OpenStreetMap is failing to display the full map within the designated div

Here is the code snippet for displaying a map when clicking on a Span text, but not showing it in full: var latitude = document.querySelector('#lati').value; var longitude = document.querySelector('#longi').value; var open_address = doc ...

The bootstrap modal is appearing correctly, but the content within it is not displaying properly

I've been working on a website and added a modal for signup. However, when the user clicks on the signup button, the modal appears but the content inside it is not clickable. Can someone please help me with this issue? Here is the code snippet I am us ...

Transforming a canvas element into an image sans the use of toDataURL or toBlob

Recently, I developed a small tool which involves adding a canvas element to the DOM. Strangely, when trying to use toBlob or toDataURL, I encountered an issue with the canvas being tainted and received the error message (specifically in chromium): Uncaugh ...

Maintain the text layout when copying and pasting from the Angular Application

After noticing that copying and pasting text from an Angular Application to a text editor like Microsoft Word results in losing the original format, I decided to investigate further. An example I used was the angular material website: https://material.ang ...

Challenges with resizing floating divs

As a beginner in Web Development, I am tasked with creating a web portfolio for an assignment. In my design layout, I have a navigation bar in a div floated left, and a content div floated right serving as an about me section containing paragraphs and lis ...

Navigate to the element and retrieve a null offset

Currently, I am attempting to retrieve an element's ID from a select box and then scroll to that specific element using the following code snippet: // move to services $(function () { $("select.jump").change(function() { var selected = $( ...

Setting the height of a div tag in CSS/HTML5 to match the current height of the browser window

Is there a way to make the height of my div tag always equal to the height of the browser's window, so it adjusts automatically when the browser is resized? I have tried using .class { height: 100%; } But this doesn't work. I've learne ...

Ways to make video controls visible following the initial click on the video

I have a collection of videos on my website and I would like them to display with a poster (thumbnail image) initially. The video controls should only appear once the user clicks on the video for the first time. Therefore, when the page is loaded, the cont ...

Swap the text within the curly braces with the div element containing the specified text

I have an input and a textarea. Using Vue, I am currently setting the textarea's text to match what's in the input field. However, now I want to be able to change the color of specific text by typing something like {#123123}text{/#}. At this poin ...