Enhancing the appearance of standard HTML checkboxes

This HTML snippet displays a pair of checkboxes positioned side by side

<div id="mr_mrs">
    <ul class="mr_mrs form-no-clear">
        <li id="mr" class="popular-category">
            <label for="Mr" id="mr">Mr</label>
            <span><input name="Mr" id="Mr" type="checkbox" /></span>
            <div class="clearboth"></div>
            </li>
        <li id="mrs" class="popular-category">
            <label for="Mrs" id="mrs">Mrs</label>
                <span><input name="Mrs" id="Mrs" type="checkbox" /></span>
            <div class="clearboth"></div>
        </li>
    </ul>
</div>

I am looking to personalize the checkboxes to have a square shape and add some color when they are selected. Please refer to the image linked below for reference:

Is there a way to achieve this customization?https://i.sstatic.net/ZvN6I.png

Answer №1

To place the two controls side by side, you can set their display property to inline-block: display: inline-block. (By default, a div element has a block display.)

In order to allow the user to click on the label text to check the box, there are a couple of options available. Both options are demonstrated in the example provided below.

  1. <label> surrounds both the text and the checkbox.
  2. Alternatively, you can use
    <label for="myId">some text</label><input id="myId">
    to link the label to the input field.*

*Please, note that when using the for/id method, it should be label-for and input-id. In the provided example, the same id was assigned to both elements, but ids should be unique on a page.

.checkbox {
  margin: 4px;
  display: inline-block;
}
<div class="checkbox">
  <label>Box 1<input type="checkbox"></label>
</div>
<div class="checkbox">
  <label for="myId">Box 2</label>
  <input id="myId" type="checkbox">
</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

How to Implement a Dropdown Menu in HTML and CSS for Navigation Bars

Currently, I am in the process of creating a website at www.bossfakeid.co.uk. I am looking to add a dropdown menu to the "Products" section of the navbar. I found a dropdown menu solution that I would like to implement from this link: http://codepen.io/wil ...

When a website is deployed to an application, the process includes MVC bundling and managing relative CSS image paths

When trying to convert relative image paths to absolute paths, there are numerous queries on stackoverflow addressing this issue. For example, take a look at this thread: MVC4 StyleBundle not resolving images This question recommends using new CssRewrite ...

What is the best way to showcase a JSON object in an attractive format on a webpage?

Similar Question: JavaScript data formatting/pretty printer var theobject_string = '{...}'; // I have a JSON object as a string. Is there a way to present this string in a visually appealing manner on an HTML webpage? I would like the ...

a container holding two floating divs

I have been attempting to create two divs positioned side by side, with one containing content and the other acting as a sidebar. Both of these divs are contained within another larger container div. Despite my efforts, I have not been successful in achiev ...

Eliminate the .html extension from the URL and initiate a redirect

I need assistance removing the .html extension from URLs that are displayed in the address bar. To achieve this, I used the following code: RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.html -f This solution ...

Displaying a variety of images in an HTML document can be achieved without the need to manually write out a separate tag

As a newcomer to HTML, I find myself faced with a challenge. I have a folder containing 100 images, all sharing a common prefix in their names but differing in the second part. For instance, some examples include: "content_diffusion_delay_AthfT@jurEYTgf" a ...

What is the process for generating an Electronic Program Guide for television?

Welcome to the forum! I'm a front-end developer at a company for 6 months now, currently working on a TV app. It's my first experience in this field and I'm facing some challenges, particularly with creating an epg for the app. Unfortunately ...

Ensuring that images are the perfect size for their parent container

Exploring the functionalities of angular bootstrap carousel, I am eager to showcase various images and enable clicking on them. The challenge I'm facing is that my images are not uniform in size and don't fit perfectly within my bootstrap column ...

Transform black and white image to vibrant colors and add various hover effects

There are three images displayed on this webpage: This website is built on WordPress and utilizes the WP Bakery plugin for designing layouts. The images here are set to change from color to grayscale, and back to color upon mouseover. The following CSS c ...

What could be the reason that the div is not being centered in the middle of the body?

<style> .maincont { width: 8em; height: 8em; background: purple; } body { background: limegreen; display: flex; flex-direction: column; place-content: center; place-items: center; } </style> <body> ...

Displaying pictures under specific conditions

I've recently completed the coding and CSS for my website after working on it for quite some time. Now, I want to enhance it by incorporating new features. The website is fully operational and generates revenue primarily through Google AdSense. I am i ...

Interacting with a span button within the parent element using its specific id with Selenium and Java

Can I click on a span button nested within a div element and use the input's id to ensure test stability? HTML: <div class="idit-go"> <input id="IDITForm@checkRuleVO" class="GoLong align-left idit-go-text" type="text" value="" title="Valida ...

Utilize information stored in a database to automatically navigate to a different webpage

I am currently working with PHP, HTML, and a mySQL database. Here are my current project requirements: Retreive specific data from the database and output it. Compare this data with predetermined values. If the data falls outside of the specified range, ...

The div remains unchanged when the button is clicked

My webpage is filled with several large div elements. There's a button on the page that, when clicked, should switch to the next div. Despite my efforts, the code I've written doesn't seem to be working as expected. :( This is the HTML st ...

Add some text onto the Box Component in Material UI

I am looking to replicate the hover functionality (UI) seen in this example: Desired UI Source: Since adjusting background image styles can be complex, I have opted to use the Box Component from Material UI. Within the Box Component, I have implemented th ...

As a useful tool in XML processing, xsl:value-of function effectively filters out any

When creating XML that contains HTML tags, the challenge arises when trying to convert all the XML data to HTML using XSLT. The issue lies in handling HTML tags included within the XML. Specifically, I need to retrieve the content within the summary tag wi ...

Triggering unexpected syntax error upon clicking the dropdown menu in the navigation bar

I encountered an error when attempting to click the dropdown list on my website. The error message reads as follows: Uncaught Syntax error, unrecognized expression: #. This error only seems to occur when using Chrome or Edge browsers. Currently, I am usi ...

Styling CSS variables uniquely

I have limited knowledge of HTML and CSS, so I am unsure how to search for a similar post on StackOverflow. My apologies if this is a duplicate question. I am looking to achieve the following: margin-horizontal { margin-left: value; margin-right: va ...

Add extra space to the dimensions of the div by incorporating padding

Showing my issue with an accompanying image: I am looking for a solution to include padding in the width: 50%; calculation. Currently, the first div's width is actually 50% + 2em because the padding was not factored in initially. Is there a way to i ...

How to create a vertically scrollable div within another div by utilizing scrollTop in jQuery

I need assistance with scrolling the #container div inside the .bloc_1_medias div. The height of #container is greater than that of .bloc_1_medias. My goal is to implement a function where clicking on the "next" and "previous" text will scroll the #contai ...