
本文旨在解决在使用 HTML 和 CSS 创建在线调查问卷时,星级评分控件(starQuestion)未能与其他类型的问卷问题(如 opQuestion, shAnQuestion, numQuestion)一样居中显示的问题。通过分析代码结构和 CSS 样式,找出导致该问题的原因,并提供有效的解决方案,确保星级评分控件能够正确居中对齐。
问题分析
根据提供的代码,星级评分控件的 HTML 结构与其它类型的问卷问题存在差异。starQuestion 被包裹在
标签中,而其他问题类型直接使用 标签。这种差异导致了默认的布局行为不同,从而影响了居中显示。 此外,答案中指出星级字符的大小可能过大,导致其总宽度超过了 starQuestion 容器的可用宽度。这也会影响整体的居中效果。
解决方案
要解决星级评分控件未居中显示的问题,可以采取以下步骤:
立即学习“前端免费学习笔记(深入)”;
-
移除不必要的表格标签: 将 starQuestion 的 HTML 结构与其他问题类型保持一致,移除
标签。修改后的 newStarQuestion 函数如下: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(); }
})
}
}登录后复制 -
确保 starQuestion 的 CSS 样式与其他问题类型一致: 检查 .starQuestion 的 CSS 样式是否与其他问题类型(.opQuestion, .shAnQuestion, .numQuestion)的样式一致,特别是 border, border-radius, margin, text-align 属性。
.opQuestion, .shAnQuestion, .numQuestion, .starQuestion {
border: 3px solid black;
border-radius: 25px;
margin: 30px 200px 20px 200px;
text-align: center;
}登录后复制 -
调整 numbers 容器的宽度: 如果星级字符过大导致超出容器宽度,可以适当增加 .numbers 容器的宽度,或者减小星级字符的大小。
.numbers {
margin-bottom: 25px;
/* 增加容器宽度,或者根据需要设置最小宽度 */
min-width: 215px; /* 答案中建议的最小宽度 */
}
/* 或者,减小星级字符的大小 */
label.star:before {
font-size: 16px; /* 根据需要调整大小 */
}登录后复制 -
Flexbox布局: 考虑使用Flexbox布局来控制星级评分的排列和居中。
.numbers {
display: flex;
justify-content: center; /* 水平居中 */
align-items: center; /* 垂直居中(如果需要) */
margin-bottom: 25px;
}
.star {
/* 可选:调整星标之间的间距 */
margin: 0 5px;
}登录后复制 注意事项
- 确保在修改 HTML 和 CSS 后,清除浏览器缓存,以查看最新的效果。
- 根据实际情况调整 CSS 样式,以达到最佳的显示效果。
- 使用开发者工具检查元素的尺寸和布局,有助于快速定位问题。
总结
通过移除不必要的表格标签,确保 CSS 样式一致,并适当调整容器宽度或星级字符大小,可以有效地解决星级评分控件未居中显示的问题。使用 Flexbox 布局可以更灵活地控制星级评分的排列和居中。遵循以上步骤,可以确保星级评分控件在在线调查问卷中正确居中显示,提升用户体验。
|
|
以上就是解决HTML中星级评分控件未居中显示的问题的详细内容,更多请关注php中文网其它相关文章!