What is the proper way to correctly position a radio button on a webpage?

http://jsfiddle.net/BD8j2/

I am attempting to position a radio button next to an arrow image, but it seems to be relocating elsewhere inexplicably. My intention is to have the radio button positioned between the arrow and the word "Masculino."

The radio button should not actually submit any data; its purpose is purely aesthetic. However, I would like for the radio button to fill when clicked and then direct the user to another page.

Is it feasible to make the entire box clickable as a link? My idea was to encapsulate the entire div within an <a href>, which I believe could work.

Answer №1

Check out this JSFiddle example: http://jsfiddle.net/BD8j2/8/.

(I have updated the HTML code in this post to resolve issues with invalid markup and cross-browser compatibility, replacing it with a JavaScript solution.)

HTML:

<div class="answers">
    <form>
        <label class="links">
            <input type="radio" name="sex" value="male" />
            <a href="http://w3.org">Male</a>
        </label>
    </form>
</div>
<div class="answers">
    <form>
        <label class="links">
            <input type="radio" name="sex" value="female" />
            <a href="http://w3.org">Female</a>
        </label>
    </form>
</div>

JavaScript:

function init() {
    var link_elems = getElementsByClass('links');
    for(var i = 0; i < link_elems.length; i++) {
        addEvent(link_elems[i], 'click', goToLink);
    }
}
function goToLink(e) {
    if(e.stopPropagation) {
        e.stopPropagation();
    } else if(typeof e.cancelBubble != 'undefined') {
        e.cancelBubble();
    }
    e.currentTarget.children[0].checked = true;
    window.location = e.currentTarget.children[1].href;
}
function addEvent(elem, event, handler) {
    if(elem.addEventListener) {
        elem.addEventListener(event, handler, false);
    } else if(elem.attachEvent) {
        elem.attachEvent('on' + event, handler);
    } else {
        elem['on' + event] = handler;
    }
}
function getElementsByClass(className) {
    if(document.getElementsByClassName) {
        return document.getElementsByClassName(className);
    } else if(document.all) {
        var everything = document.all;
        var all_length = everything.length;
        var matches = [];
        for (var i = 0; i < all_length; i++) {
            if(everything[i].className == className) {
                matches[matches.length] = everything[i];
            }
        }
        return matches;
    }
    return false;
}
addEvent(window, 'load', init);

CSS:

.answers {
    width: 220px;
    height: 50px;
    margin-top: 20px;
    border-top: 1px solid black;
    border-bottom: 1px solid black;
    background-color: #ddd;
    background-image: url('http://fortmovies.com/brazil/arrow.png');
    background-repeat: no-repeat;
    background-position: 0 50%;
}
.answers label {
    float: left;
    margin-left: 75px;
    padding-top: 15px;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 16px;
    font-weight: bold
}
.answers input {
    margin-right: 5px
}

This update simplifies the JavaScript by automating the process of linking names and targets without the need for manual arrays.

Answer №2

Would you like something similar to http://jsfiddle.net/XEk5d/2/? Keep in mind that implementing the functionality to navigate to another page upon clicking would involve using JavaScript.

CSS

#answers {
    width:220px;
    height:50px;
    background-color:#DDDDDD;
    border-top:1px solid black;
    border-bottom:1px solid black;
    margin-top:20px;
}

#arrowcenter {
    width:71px;
    height:31px;
    background-image:url('http://fortmovies.com/brazil/arrow.png');
    background-position:0 50%;
    background-repeat:no-repeat;
    height:100%;
    display:inline-block; 
    vertical-align:middle;
} 

#answerstext {
    background-position:0 50%;
    display:inline-block; 
    vertical-align:middle;
}
form{
    display:inline-block;
}
input{
    vertical-align:middle;
}

HTML

<div id="answers">
    <div id="arrowcenter"></div>
    <form>
        <input type="radio" name="sex" value="male" />
        <div id="answerstext">Masculino</div>
    </form>
</div><!-- end grayAnswer -->?

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 it causing issues having the same version of jQuery included multiple times?

Within my HTML file, referred to as x.html, I've included other HTML files as well. In x.html, I have integrated the jquery library version 1.4.1. It seems that this same version of jquery is also being included from the other HTML files. Even though ...

Need help modifying a UseStatus to target an axios post response efficiently?

Hey there, back again with a head-scratcher I've been dealing with all day. Almost done with a contact form, just stuck on an animation concept that involves three steps: 1. Prompting the user to contact 2. Making the waiting process user-friendly ...

Struggling with aligning a div in the center

I've been struggling to get my "boxes" div centered on my page so that everything inside that div is centered instead of being pushed to the left. Unfortunately, my CSS vision didn't turn out as expected. If someone could take a look at my jsbi ...

Any ideas on how to create a fading effect for a background image while scrolling using jQuery?

I am looking to create a fading background image that changes as I scroll down my page. I remember seeing something similar with two divs that fade using opacity. It looked like this: <div class="background1"></div> <div class="background2 ...

Targeting multiple browsers in an Angular 11 project with scss: A comprehensive guide

for instance: i'm currently animating the ::placeholder pseudo element when ::focus is active input::placeholder{ color: grey; transition: all 400ms ease; position: absolute; top: 35%; font-size: 15px; } inpu ...

Django authentication ensures secure access for users on

What could be causing the issue with self.request.user.is_authenticated() not functioning correctly in this specific view? class ArticleDetailView(DetailView, CategoryListMixin): model = Article template_name = 'mainapp/article_detail.html&ap ...

What is the method to display HTML code using JavaScript within a span element in a JSP page?

While working on a jsp file, I came across a span tag with the following code: <span id="divBasicSearchResults" style="height:100%;"></span> Below is the Javascript function that generates HTML content for this span: function renderBasicSear ...

Arrange images of different dimensions in a CSS grid layout

I have a large collection of resized images (around 100) that are all either 130 pixels wide or 130 pixels high. I'm looking for a way to style these images so that they appear perfectly centered within a background box that is also 130x130 pixels. T ...

What is the best way to display and conceal an input label when focusing in and out?

This is a snippet of my HTML code: <label for="first_name">First Name</label> <input class="w3-input" type="text" name="first_name" id="first_name"> <label for="last_name">Last Name</label> <input class="w3-input" type="t ...

ReactJS textarea component failing to update after state change

I'm currently working on developing a note-taking and organizing application, but I've encountered a frustrating bug. Here is the code for my component: import React from 'react'; const Note = (props) => { let textarea, noteForm ...

Breaking the page for media printing in Zurb Foundation 5

How can I print specific pages from my website using the Foundation 5 CSS framework? In my CSS, I have included the following code: @media print { hr.page-break{page-break-after:always!important;} } I tried using the .page-break class to insert ...

Please select a text color for displaying tabular data in HTML using XML and XSL as a guide

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> <xsl:template match="/"> <html> <head> <style type="text ...

What causes my variable to show None instead of the current stock price?

I attempted to extract the stock price data from using BeautifulSoup. However, my program is not displaying the stock price and instead showing "None". How can I correctly retrieve the stock price information? My provided code snippet: from bs4 import Bea ...

What is the best way to align isotope and organize its components?

I have searched for solutions to my issue with centering isotope on my project, but none of the existing answers were sufficient. I am facing challenges trying to center isotope without creating extra space between its elements and arranging them in a spec ...

Whenever I create the code using javascript, padding seems to always show up

I have a basic stacked row layout that displays an upper box and lower box without any padding. <span id="route" class="small"> <div class="row" style="border-style:solid;"> <div class="col-md-12 col-12"> <p>UpperBox</p& ...

Tips for customizing Primeng's SplitButton to resemble a Bootstrap button styling

Hey there! I'm working on a project with a Primeng splitbutton and a Bootstrap button. You can check it out in the stackblitz link below: https://stackblitz.com/edit/angular6-primeng-hlxeod?file=src/app/app.component.html I'm having trouble cus ...

Steps for executing ASP code pulled from the database

In my ASP project, I have a single page where clicking on different links retrieves corresponding HTML from the database and loads it. Some links are always present on the page (static HTML), while other divs display dynamic content fetched from the DB. Wi ...

Integrating data binding within a .append function using AngularJS

I followed this code example (http://jsfiddle.net/ftfish/KyEr3/) to develop a form. However, I encountered an issue with an input having ng-model set to "value" and needing to display that value within {{ }} in a TD: angular.element(document.getElementByI ...

Retrieve the text from a string resource in Android while preserving HTML tags

When working on my app, I encountered an issue with displaying a string in a text view while utilizing html markup. Here is how I attempted to retrieve the string: String mystring = getResources().getString(R.string.Terms_And_Conditions); Unfortunately, t ...

Expanding a table cell to cover multiple columns in CSS when neighboring cells are vacant

I am new to CSS and facing an issue with a dynamically generated HTML table. Only one cell contains data, while the adjacent cells remain empty. <tr> <td> Really long data that is forced to wrap around.</td> <td> empty cell</td& ...