
本文旨在解决在使用 HTML 和 CSS 创建在线调查问卷时,星级评分控件(starQuestion)未能与其他类型的问卷问题(如 opQuestion, shAnQuestion, numQuestion)一样居中显示的问题。通过分析代码结构和 CSS 样式,找出导致该问题的原因,并提供有效的解决方案,确保星级评分控件能够正确居中对齐。
根据提供的代码,星级评分控件的 HTML 结构与其它类型的问卷问题存在差异。starQuestion 被包裹在 <table><tr><td> 标签中,而其他问题类型直接使用 <div> 标签。这种差异导致了默认的布局行为不同,从而影响了居中显示。
此外,答案中指出星级字符的大小可能过大,导致其总宽度超过了 starQuestion 容器的可用宽度。这也会影响整体的居中效果。
要解决星级评分控件未居中显示的问题,可以采取以下步骤:
立即学习“前端免费学习笔记(深入)”;
function newStarQuestion() {
questionnaire.insertAdjacentHTML('beforeend',
`<div class="starQuestion">
<div class="questionName" contenteditable="true">Your Question</div>
<button class="remove-qu" type="button">remove Question</button>
<div class="numbers">
<input class="star star-5" id="star-5" type="radio" name="star" disabled/>
<label class="star star-5" for="star-5"> </label>
<input class="star star-4" id="star-4" type="radio" name="star" disabled/>
<label class="star star-4" for="star-4"> </label>
<input class="star star-3" id="star-3" type="radio" name="star" disabled/>
<label class="star star-3" for="star-3"> </label>
<input class="star star-2" id="star-2" type="radio" name="star" disabled/>
<label class="star star-2" for="star-2"> </label>
<input class="star star-1" id="star-1" type="radio" name="star" disabled/>
<label class="star star-1" for="star-1"> </label>
</div>
</div>`);
let removeQuestions = document.querySelectorAll('.remove-qu');
if (removeQuestions) {
removeQuestions.forEach((item) => {
item.onclick = function () { this.parentNode.remove(); }
})
}
}.opQuestion, .shAnQuestion, .numQuestion, .starQuestion {
border: 3px solid black;
border-radius: 25px;
margin: 30px 200px 20px 200px;
text-align: center;
}.numbers {
margin-bottom: 25px;
/* 增加容器宽度,或者根据需要设置最小宽度 */
min-width: 215px; /* 答案中建议的最小宽度 */
}
/* 或者,减小星级字符的大小 */
label.star:before {
font-size: 16px; /* 根据需要调整大小 */
}.numbers {
display: flex;
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中(如果需要) */
margin-bottom: 25px;
}
.star {
/* 可选:调整星标之间的间距 */
margin: 0 5px;
}通过移除不必要的表格标签,确保 CSS 样式一致,并适当调整容器宽度或星级字符大小,可以有效地解决星级评分控件未居中显示的问题。使用 Flexbox 布局可以更灵活地控制星级评分的排列和居中。遵循以上步骤,可以确保星级评分控件在在线调查问卷中正确居中显示,提升用户体验。
以上就是解决HTML中星级评分控件未居中显示的问题的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号