Having difficulty disassociating the radio button from the label

<div class="questions" style="font-size:13pt;margin-left:20px;margin-bottom:10px;">
  <p class="question" id="ques{{i+1}}" style="font-size:17pt;font-weight:normal">{{i+1+". "+a.question}}</p>
  <img *ngIf="isImage[i]" [src]="a.image" class="img-responsive" style="height:30%;width:30%;padding-left:20px;border-radius: 0;">
</div>
<div style="font-weight:normal;font-size:15pt;margin-left:20px">
  <form name="quizform" id="option">
    <label id="label_1_{{i}}"><input id="1_{{i}}" type="radio"  name="q{{i}}" value="{{a.option1}}"> A)  {{a.option1}}</label><br/>
    <!-- [(ngModel)]="ans.answer"   (click)="getAnswer(i,$event.target.value,$event.target.id)"  -->
    <label id="label_2_{{i}}"><input id="2_{{i}}" type="radio"  name="q{{i}}" value="{{a.option2}}"> B)  {{a.option2}}</label><br/>
    <label id="label_3_{{i}}"><input id="3_{{i}}" type="radio"  name="q{{i}}" value="{{a.option3}}"> C)  {{a.option3}}</label><br/>
    <label id="label_4_{{i}}"><input id="4_{{i}}" type="radio"  name="q{{i}}" value="{{a.option4}}"> D)  {{a.option4}}</label><br/><br/>
  </form>
</div>

My goal is to include radio buttons inside the label so that when a user clicks on the label, the radio button is selected. However, I am facing an issue where it overlaps with the label tag. Here is what my CSS file looks like:

#option {
  margin-left: 10px!important;
}

#wrapper {
  width: 100%;
  height: auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

label {
  font-size: 16pt;
  font-weight: normal;
  padding-left: 15px;
  margin-top: -10px
}

#quiz {
  width: 75%;
  height: auto;
  margin-top: 15px;
  text-align: left;
  padding: 0px 50px 0px 50px;
  margin-bottom: 20px;
}

input {
  position: absolute;
  margin-top: 0px;
}

That sums it up.

Answer №1

Working with <laber for=".."> instead of using position: absolute; can make the task easier.

Visit this link for more information.

#option {
  margin-left: 10px!important;
}

#wrapper {
  width: 100%;
  height: auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

label {
  font-size: 16pt;
  font-weight: normal;
  margin-top: -10px
}

#quiz {
  width: 75%;
  height: auto;
  margin-top: 15px;
  text-align: left;
  padding: 0px 50px 0px 50px;
  margin-bottom: 20px;
}

input {
  margin-top: 0px;
}
<div class="questions" style="font-size:13pt;margin-left:20px;margin-bottom:10px;">
    
              <p class="question" id="ques{{i+1}}" style="font-size:17pt;font-weight:normal">{{i+1+".  "+a.question}}</p>
    
              <img *ngIf="isImage[i]" [src]="a.image" class="img-responsive" style="height:30%;width:30%;padding-left:20px;border-radius: 0;">
            </div>
    
    
         <div style="font-weight:normal;font-size:15pt;margin-left:20px">
           <form name="quizform" id="option">
             <input id="1_{{i}}" type="radio"  name="q{{i}}" value="{{a.option1}}"><label for="1_{{i}}" id="label_1_{{i}}"> A)  {{a.option1}}</label><br/>   <!-- [(ngModel)]="ans.answer"   (click)="getAnswer(i,$event.target.value,$event.target.id)"  -->
             <input id="2_{{i}}" type="radio"  name="q{{i}}" value="{{a.option2}}"><label for="2_{{i}}" id="label_2_{{i}}"> B)  {{a.option2}}</label><br/>
             <input id="3_{{i}}" type="radio"  name="q{{i}}" value="{{a.option3}}"><label for="3_{{i}}" id="label_3_{{i}}"> C)  {{a.option3}}</label><br/>
             <input id="4_{{i}}" type="radio"  name="q{{i}}" value="{{a.option4}}"><label for="4_{{i}}" id="label_4_{{i}}"> D)  {{a.option4}}</label><br/><br/>
           </form>
         </div>

Answer №2

Make sure to use position:relative instead of absolute positioning

#option input{
  margin-left: 10px!important;
}

#wrapper {
  width: 100%;
  height: auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

label {
  font-size: 16pt;
  font-weight: normal;
  margin-top: -10px
}

#quiz {
  width: 75%;
  height: auto;
  margin-top: 15px;
  text-align: left;
  padding: 0px 50px 0px 50px;
  margin-bottom: 20px;
}

input {
  margin-top: 0px;
  position: relative;
}
<div class="questions" style="font-size:13pt;margin-left:20px;margin-bottom:10px;">
  <p class="question" id="ques{{i+1}}" style="font-size:17pt;font-weight:normal">{{i+1+". "+a.question}}</p>
  <img *ngIf="isImage[i]" [src]="a.image" class="img-responsive" style="height:30%;width:30%;padding-left:20px;border-radius: 0;">
</div>
<div style="font-weight:normal;font-size:15pt;margin-left:20px">
  <form name="quizform" id="option">
    <label id="label_1_{{i}}">
    <input id="1_{{i}}" type="radio"  name="q{{i}}" value="{{a.option1}}"> A)  {{a.option1}}</label><br/>
    <!-- [(ngModel)]="ans.answer"   (click)="getAnswer(i,$event.target.value,$event.target.id)"  -->
    <label id="label_2_{{i}}"><input id="2_{{i}}" type="radio"  name="q{{i}}" value="{{a.option2}}"> B)  {{a.option2}}</label><br/>
    <label id="label_3_{{i}}"><input id="3_{{i}}" type="radio"  name="q{{i}}" value="{{a.option3}}"> C)  {{a.option3}}</label><br/>
    <label id="label_4_{{i}}"><input id="4_{{i}}" type="radio"  name="q{{i}}" value="{{a.option4}}"> D)  {{a.option4}}</label><br/><br/>
  </form>
</div>

Answer №3

It is not mandatory to assign position:absolute to the input field. Simply eliminate "absolute" and remove padding-left from the label if you prefer no padding on the left side of the label.

#option{
margin-left:10px!important;}

#wrapper {
width: 100%;
height: auto;
display: flex;
align-items: center;
justify-content: center;} 

label{
font-size:16pt;
font-weight:normal;
}

#quiz{
width:75%;
height:auto;
margin-top:15px;
text-align:left;
padding:0px 50px 0px 50px;
margin-bottom:20px;}
<div class="questions" style="font-size:13pt;margin-left:20px;margin-bottom:10px;">

     <p class="question" id="ques{{i+1}}" style="font-size:17pt;font-weight:normal">{{i+1+".  "+a.question}}</p>

     <img *ngIf="isImage[i]" [src]="a.image" class="img-responsive" style="height:30%;width:30%;padding-left:20px;border-radius: 0;">
   </div>


<div style="font-weight:normal;font-size:15pt;margin-left:20px">
  <form name="quizform" id="option">
    <label id="label_1_{{i}}"><input id="1_{{i}}" type="radio"  name="q{{i}}" value="{{a.option1}}"> A)  {{a.option1}}</label><br/> 
    <label id="label_2_{{i}}"><input id="2_{{i}}" type="radio"  name="q{{i}}" value="{{a.option2}}"> B)  {{a.option2}}</label><br/>
    <label id="label_3_{{i}}"><input id="3_{{i}}" type="radio"  name="q{{i}}" value="{{a.option3}}"> C)  {{a.option3}}</label><br/>
    <label id="label_4_{{i}}"><input id="4_{{i}}" type="radio"  name="q{{i}}" value="{{a.option4}}"> D)  {{a.option4}}</label><br/><br/>
  </form>


</div>

Answer №4

Many times, when working with CSS, there is a tendency to overcomplicate things. In such cases, it's recommended to remove the position:absolute from the input.

Furthermore, styling becomes more challenging when inline styles are mixed with CSS. To address this issue, I have simply transferred the styles to the CSS.

Lastly, instead of using <br> to create block displays, it's preferable to achieve the same effect using CSS.

#option {
  margin-left: 10px!important;
}

#wrapper {
  width: 100%;
  height: auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

label {
  font-size: 16pt;
  font-weight: normal;
  padding-left: 15px;
  display:block;
}

#quiz {
  width: 75%;
  height: auto;
  margin-top: 15px;
  text-align: left;
  padding: 0px 50px 0px 50px;
  margin-bottom: 20px;
}

input {
  margin-top: 0px;
}
.questions{font-size:13pt;margin-left:20px;margin-bottom:10px;}
.question {font-size:17pt;font-weight:normal;}
.img-responsive{height:30%;width:30%;padding-left:20px;border-radius: 0;}
.form-container{font-weight:normal;font-size:15pt;margin-left:20px;}
<div class="questions" >
  <p class="question" id="ques{{i+1}}">{{i+1+". "+a.question}}</p>
  <img *ngIf="isImage[i]" [src]="a.image" class="img-responsive" >
</div>
<div class="form-container">
  <form name="quizform" id="option">
    <label id="label_1_{{i}}"><input id="1_{{i}}" type="radio"  name="q{{i}}" value="{{a.option1}}"> A)  {{a.option1}}</label>
    <label id="label_2_{{i}}"><input id="2_{{i}}" type="radio"  name="q{{i}}" value="{{a.option2}}"> B)  {{a.option2}}</label>
    <label id="label_3_{{i}}"><input id="3_{{i}}" type="radio"  name="q{{i}}" value="{{a.option3}}"> C)  {{a.option3}}</label>
    <label id="label_4_{{i}}"><input id="4_{{i}}" type="radio"  name="q{{i}}" value="{{a.option4}}"> D)  {{a.option4}}</label>
  </form>
</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

Make sure the static variable is set up prior to injecting the provider

In our Angular6 application, we utilize a globalcontextServiceFactory to initialize the application before rendering views. This process involves subscribing to get configuration from a back-end endpoint and then using forkJoin to retrieve environment app ...

A New Approach to Initializing Web Pages in ASP.NET (with a Surprising Twist)

Well, I've encountered quite the puzzling issue with a project I'm working on (it could even make it to the Daily WTF), and I'm hoping for some help in finding a solution. (Apologies in advance for the lengthy explanation...) About a month ...

Expand the <div> to match the complete height of its containing <div> element

There are multiple div blocks within a parent block: one for the menu on the left, and one for the text on the right. How can you make the right inner block stretch to the full height of the parent block like the left one? The parent block has a minimum he ...

Encountering an Uncaught Error: MyModule type lacks the 'ɵmod' property

I am currently working on developing a custom module to store all my UI components. It is essential that this module is compatible with Angular 10 and above. Here is the package.json file for my library: { "name": "myLibModule", &qu ...

Displaying a Bootstrap loading spinner on the right side of a list item

I want to position a Bootstrap loading spinner on the right side of a ui li, with the text "Keywords" on the left side of the ui li. My current HTML code is as follows: <ul class="list-group" id="my-list"> <li class="l ...

Deploying Angular Micro Front Ends with Module Federation can lead to CORS errors

I am facing an issue with CORS error when trying to access the remoteEntry.js file from my two micro front end containers in a Docker environment. Everything works fine in local development, but running it in Docker causes this problem. Any advice on how ...

Can CSS be used to horizontally align individual characters by adjusting letter-spacing?

When I apply the CSS rule letter-spacing: 16px;, I observe that the characters are aligned to the left within their designated space, causing the cursor to jump towards the right as if an additional space was added after each character. I am wondering if ...

Position the Facebook Like Button in alignment with the counts box

Is it possible to align the Facebook likes count box at the bottom of my Like Button on my website? I'm looking for help with this task. Here is a visual representation of how I want the fb button to be positioned or styled: ...

Efficiently handling multiple form submissions using a single jQuery Ajax request

I'm working on a page that has 3-4 forms within divs, and I want to submit them all with just one script. Additionally, I want to refresh the content of the specific div where the form is located. However, I'm unsure of how to specify the current ...

"Error: The specified object does not have the capability to support the property or method 'includes'." -[object Error]

Recently, I developed a method that utilizes both indexOf() and includes(). However, I encountered an error message stating "Object doesn't support property or method 'includes'". I have attempted to run the method on both Internet Explorer ...

The function req.send('null') does not exist in the Node.js MySQL library

I am struggling with inserting a form into a MySql database. The values are stored in the database from the form, but the table displayed on the page does not refresh. I can only see the entries when I restart the server and revisit the page. Furthermore, ...

Verify Departure on External Links within Wordpress

Let me set the stage for you: We're dealing with a bank website here. So, whenever a user wants to click on an external link (could be to a Facebook page or a partner website), we need to have a notification box pop up saying "You are about to lea ...

How to align Font Awesome icons inside `<span>` vertically with each other

Trying to vertically align multiple font awesome icons: ############################# icon icon icon icon icon icon ############################# <div class="announcement-card-body modal-body card border-0"> <span class="info fa fa-info- ...

Modifying the attributes/properties within a class of a bootstrap button

Here is the code that I have: <b-button id="search-button" size="md" class="mt-1 w-100" type="submit" @click="someEvent()" >Example </b-button If we imagine calling someEvent() and wanting to modi ...

The status of the xmlhttp object remains unchanged

I was attempting to create an application showcasing the use of AJAX. Being new to AJAX, I couldn't pinpoint the error in my code. The XMLHttpRequest object is being created, but nothing else seems to be working. The ready state doesn't change to ...

Issues with script execution on Ajax tab

I'm currently using JQuery UI tabs to load content through Ajax. I have a situation where two tabs are supposed to load HTML content and execute a script to hide or show certain elements in the loaded content. However, I encountered an issue where the ...

Searching with Mat-autocomplete across multiple fields simultaneously

I am struggling with a mat-autocomplete that contains 5000 objects, each consisting of a first name and a last name. My goal is to search for both the first name and last name simultaneously regardless of the input order. Currently, I can only search one ...

Deactivate the button if the mat-radio element is not selected

Here is my setup with a mat-radio-group and a button: <form action=""> <mat-radio-group aria-label="Select an option"> <mat-radio-button value="1">Option 1</mat-radio-button> <mat-radio-b ...

Encountering an error in Angular Material Table: Unable to define property 'filterPredicate' for undefined object

I am facing an issue with my Angular Material Table that has client-side filtering. The problem arose when I switched to retrieving data from a service instead of using hardcoded data. Now, I am receiving an error message saying: "Cannot set property &apos ...

JavaScript function for automatic scrolling to the bottom of the page is not functioning as expected

I'm working on incorporating a terminal/console feature into my website. I came across the JavaScript functions for scrolling down a page, namely window.scrollTo(0,document.body.scrollHeight); and window.scrollTo(0,document.querySelector(".fakeSc ...