简体   繁体   English

无法将单选按钮与文本对齐

[英]Unable to align the radio buttons with text

I intend to align the radio buttons with the text side by side. 我打算将radio按钮与文本并排对齐。 I've managed to align the radio buttons vertically in a straight line, but I'm now unable to keep the radio buttons and text side by side. 我设法使radio按钮垂直垂直对齐,但是现在无法使radio按钮和文本并排放置。

Code

 var allQuestions = [{ question: "Who is the Prime Minister of the United Kingdom?", choices: ["David Cameron", "Gordon Brown", "Winston Churchill"], correctAnswer: 0 }, { question: "Who is the Prime Minister of India?", choices: ["Rahul Gandhi", "Arvind Kejrival", "Narendra Damodardas Modi"], correctAnswer: 2 }, { question: "Who is the Prime Minister of America?", choices: ["Donald Trump", "Barack Obama", "Hilary Clinton"], correctAnswer: 1 }, { question: " Who averaged one patent for every three weeks of his life?", choices: ["Thomas Edison", "Tesla", "Einstein"], correctAnswer: 0 }, { question: "What continent is cut into two fairly equal halves by the Tropic of Capricorn?", choices: ["Africa", "South America", "Australia"], correctAnswer: 2 }, { question: "What explorer introduced pigs to North America?", choices: ["Christopher Columbus", "Galileo", "Mussorie"], correctAnswer: 0 }, { question: "What F-word is defined in physics as a “nuclear reaction in which nuclei combine to form more massive nuclei”? ", choices: ["Furnace", "Fusion", "Fission"], correctAnswer: 1 }, { question: "What was the first planet to be discovered using the telescope, in 1781?", choices: ["Uranus", "Jupiter", "Mars"], correctAnswer: 0 }, { question: "Who is the chief minister of West Bengal, India?", choices: ["Buddhadev Bhattacharya", "Jyoti Basu", "Mamta Banerjee"], correctAnswer: 2 }, { question: "Which is the fastest growing religion in the world?", choices: ["Islam", "Christianity", "Hinduism"], correctAnswer: 0 } /* { question: "Who is the Prime Minister of America?", choices: ["Donald Trump","Barack Obama","Hilary Clinton"], correctAnswer:1 }, { question: "Who is the Prime Minister of America?", choices: ["Donald Trump","Barack Obama","Hilary Clinton"], correctAnswer:1 }, { question: "Who is the Prime Minister of America?", choices: ["Donald Trump","Barack Obama","Hilary Clinton"], correctAnswer:1 }, { question: "Who is the Prime Minister of America?", choices: ["Donald Trump","Barack Obama","Hilary Clinton"], correctAnswer:1 }, { question: "Who is the Prime Minister of America?", choices: ["Donald Trump","Barack Obama","Hilary Clinton"], correctAnswer:1 } */ ]; $(document).ready(function() { var currentquestion = 0; var correctAnswers = 0; $(function() { $("#progressbar").progressbar({ value: 0 }); }); $('#question').html(allQuestions[currentquestion].question); $('label[for=option0]').html(allQuestions[currentquestion].choices[0] + "<br/>"); $('label[for=option1]').html(allQuestions[currentquestion].choices[1] + "<br/>"); $('label[for=option2]').html(allQuestions[currentquestion].choices[2] + "<br/>"); $("#next").click(function() { if ($("input[name=option]:checked").val() == allQuestions[currentquestion].correctAnswer) { correctAnswers++; }; currentquestion++; $(function() { $("#progressbar").progressbar({ value: currentquestion * (allQuestions.length), }); }); if (currentquestion < allQuestions.length) { $('#question').html(allQuestions[currentquestion].question); $('label[for=option0]').html(allQuestions[currentquestion].choices[0] + "<br/>"); $('label[for=option1]').html(allQuestions[currentquestion].choices[1] + "<br/>"); $('label[for=option2]').html(allQuestions[currentquestion].choices[2] + "<br/>"); if (currentquestion == allQuestions.length - 1) { $('#next').html("Submit"); //$('#next').off("click"); $('#next').click(function() { // if($("input[name=option]:checked").val()==allQuestions[currentquestion].correctAnswer){ // correctAnswers++; // }; // alert(correctAnswers); alert("Your Score is " + correctAnswers + " out of " + currentquestion + " and your percentage is " + (correctAnswers * 100 / (currentquestion)) + "%"); }); } }; }); }); 
 .ui-widget-header { background-image: none !important; background-color: #FF7860 !important; //Any colour can go here } label { display: block; text-align: center; } /*input[name="option"] { float:left; }*/ #progressbar { margin: auto; margin-top: 20px; float: none; width: 50%; /*height: 100%;*/ } #container { text-align: center; } span { margin: 5em; padding: 3em; } 
 <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"> <h3 id="question"></h3> <form id="form"> <input type="radio" name="option" value="0" id="option0" checked> <label for='option0'> <br/> </label> <input type="radio" name="option" value="1" id="option1"> <label for='option1'> <br/> </label> <input type="radio" name="option" value="2" id="option2"> <label for='option2'> <br/> </label> </form> <br/> <a href="#" id="next" class="button">Next</a> <br/> <div id="progressbar"></div> </div> 

change your display:block in your label to inline-block and remove the <br /> outside the <label> in HTML, wrap label/input with a div 更改您的display:block label中的行inline-block并删除HTML中<label>之外的<br /> ,用div包装label/input

 var allQuestions = [{ question: "Who is the Prime Minister of the United Kingdom?", choices: ["David Cameron", "Gordon Brown", "Winston Churchill"], correctAnswer: 0 }, { question: "Who is the Prime Minister of India?", choices: ["Rahul Gandhi", "Arvind Kejrival", "Narendra Damodardas Modi"], correctAnswer: 2 }, { question: "Who is the Prime Minister of America?", choices: ["Donald Trump", "Barack Obama", "Hilary Clinton"], correctAnswer: 1 }, { question: " Who averaged one patent for every three weeks of his life?", choices: ["Thomas Edison", "Tesla", "Einstein"], correctAnswer: 0 }, { question: "What continent is cut into two fairly equal halves by the Tropic of Capricorn?", choices: ["Africa", "South America", "Australia"], correctAnswer: 2 }, { question: "What explorer introduced pigs to North America?", choices: ["Christopher Columbus", "Galileo", "Mussorie"], correctAnswer: 0 }, { question: "What F-word is defined in physics as a “nuclear reaction in which nuclei combine to form more massive nuclei”? ", choices: ["Furnace", "Fusion", "Fission"], correctAnswer: 1 }, { question: "What was the first planet to be discovered using the telescope, in 1781?", choices: ["Uranus", "Jupiter", "Mars"], correctAnswer: 0 }, { question: "Who is the chief minister of West Bengal, India?", choices: ["Buddhadev Bhattacharya", "Jyoti Basu", "Mamta Banerjee"], correctAnswer: 2 }, { question: "Which is the fastest growing religion in the world?", choices: ["Islam", "Christianity", "Hinduism"], correctAnswer: 0 } /* { question: "Who is the Prime Minister of America?", choices: ["Donald Trump","Barack Obama","Hilary Clinton"], correctAnswer:1 }, { question: "Who is the Prime Minister of America?", choices: ["Donald Trump","Barack Obama","Hilary Clinton"], correctAnswer:1 }, { question: "Who is the Prime Minister of America?", choices: ["Donald Trump","Barack Obama","Hilary Clinton"], correctAnswer:1 }, { question: "Who is the Prime Minister of America?", choices: ["Donald Trump","Barack Obama","Hilary Clinton"], correctAnswer:1 }, { question: "Who is the Prime Minister of America?", choices: ["Donald Trump","Barack Obama","Hilary Clinton"], correctAnswer:1 } */ ]; $(document).ready(function() { var currentquestion = 0; var correctAnswers = 0; $(function() { $("#progressbar").progressbar({ value: 0 }); }); $('#question').html(allQuestions[currentquestion].question); $('label[for=option0]').html(allQuestions[currentquestion].choices[0] + "<br/>"); $('label[for=option1]').html(allQuestions[currentquestion].choices[1] + "<br/>"); $('label[for=option2]').html(allQuestions[currentquestion].choices[2] + "<br/>"); $("#next").click(function() { if ($("input[name=option]:checked").val() == allQuestions[currentquestion].correctAnswer) { correctAnswers++; }; currentquestion++; $(function() { $("#progressbar").progressbar({ value: currentquestion * (allQuestions.length), }); }); if (currentquestion < allQuestions.length) { $('#question').html(allQuestions[currentquestion].question); $('label[for=option0]').html(allQuestions[currentquestion].choices[0] + "<br/>"); $('label[for=option1]').html(allQuestions[currentquestion].choices[1] + "<br/>"); $('label[for=option2]').html(allQuestions[currentquestion].choices[2] + "<br/>"); if (currentquestion == allQuestions.length - 1) { $('#next').html("Submit"); //$('#next').off("click"); $('#next').click(function() { // if($("input[name=option]:checked").val()==allQuestions[currentquestion].correctAnswer){ // correctAnswers++; // }; // alert(correctAnswers); alert("Your Score is " + correctAnswers + " out of " + currentquestion + " and your percentage is " + (correctAnswers * 100 / (currentquestion)) + "%"); }); } }; }); }); 
 .ui-widget-header { background-image: none !important; background-color: #FF7860 !important; } h3, #next { text-align: center; display:block } #form div { margin: auto; max-width: 205px } label { display: inline-block; } input[name^="option"] { float: left; } #progressbar { margin: auto; margin-top: 20px; float: none; width: 50%; /*height: 100%;*/ } 
 <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"> <h3 id="question"></h3> <form id="form"> <div> <input type="radio" name="option" value="0" id="option0" checked> <label for='option0'></label> </div> <div> <input type="radio" name="option" value="1" id="option1"> <label for='option1'></label> </div> <div> <input type="radio" name="option" value="2" id="option2"> <label for='option2'></label> </div> </form> <br/> <a href="#" id="next" class="button">Next</a> <br/> <div id="progressbar"></div> </div> 

Just set your CSS for label to display "inline-block": 只需将CSS的标签设置为显示“行内阻止”即可:

CSS 的CSS

    label{
      display: inline-block;
      text-align: center;
    }

HTML 的HTML

    <form id="form">
          <label for='option0'></label><input type="radio" name="option" value="0" id="option0" checked><br />
          <label for='option1'></label><input type="radio" name="option" value="1" id="option1"><br />
          <label for='option2'></label><input type="radio" name="option" value="2" id="option2"><br />          

EDIT 编辑

So, both answers don't solve the problem... 因此,两个答案都不能解决问题...

What you have to do is that : 您要做的是:

1) Set you CSS for label : 1)为您的标签设置CSS:

label{   display: inline-block; }

2) Create a new class for radio buttons : 2)为单选按钮创建一个新类:

 .rad_in {
   position: absolute;
   left : 25%;
 }

3) And then your html goes like this (with the radio button class) : 3)然后您的html如下所示(带有单选按钮类):

  <form id="form"> <input class="rad_in" type="radio" name="option" value="0" id="option0" checked><label for='option0'></label><br /> <input class="rad_in" type="radio" name="option" value="1" id="option1"><label for='option1'></label><br /> <input class="rad_in" type="radio" name="option" value="2" id="option2"><label for='option2'></label><br /> </form> 

Here is the fiddle : https://jsfiddle.net/8fv3cnc6/ 这是小提琴: https : //jsfiddle.net/8fv3cnc6/

See my edited answer and the fiddle : 看到我编辑的答案和小提琴:

.rad_in {
  position: absolute;
  left : 25%;
}

https://jsfiddle.net/8fv3cnc6/ https://jsfiddle.net/8fv3cnc6/

PS: Sorry, i had to post another answer since I can't comment... PS:对不起,由于我无法发表评论,我不得不发表另一个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM