我建立了一个“类似于Buzzfeed的”测验应用程序,每个问题有四个答案选择。在移动设备上,我对布局(一个2x2的盒子)感到非常满意。
但是,在台式机上,答案只是在屏幕上水平显示为4个选项。
主要问题是我正在使用Angular生成所有问题和答案选择,这使样式有点棘手。
关于如何“优雅地”编码html / css的任何想法,以便它始终将答案选项显示为2x2框?我曾尝试使用媒体查询,但感觉就像是在大伤口上缠上创可贴。
下面的当前代码和图像:
HTML:
<div class="jumbotron text-center" ng-hide="quizComplete">
<h3>{{ questions[number].ask }}</h3>
<div class="row">
<div ng-repeat="answer in questions[number].answers" class="choice">
<div ng-click="recordChoice(answer.points)">
<span class="centerer"></span>
<span class="centered">{{ answer.choice }}</span>
</div>
</div>
</div>
</div>
CSS:
.choice {
position: relative;
display: inline-block;
width: 128px;
height: 128px;
margin: 8px;
background: rgba(183,222,237,1);
background: -moz-linear-gradient(45deg, rgba(183,222,237,1) 0%, rgba(33,180,226,1) 49%, rgba(113,206,239,1) 92%, rgba(183,222,237,1) 100%);
background: -webkit-gradient(left bottom, right top, color-stop(0%, rgba(183,222,237,1)), color-stop(49%, rgba(33,180,226,1)), color-stop(92%, rgba(113,206,239,1)), color-stop(100%, rgba(183,222,237,1)));
background: -webkit-linear-gradient(45deg, rgba(183,222,237,1) 0%, rgba(33,180,226,1) 49%, rgba(113,206,239,1) 92%, rgba(183,222,237,1) 100%);
background: -o-linear-gradient(45deg, rgba(183,222,237,1) 0%, rgba(33,180,226,1) 49%, rgba(113,206,239,1) 92%, rgba(183,222,237,1) 100%);
background: -ms-linear-gradient(45deg, rgba(183,222,237,1) 0%, rgba(33,180,226,1) 49%, rgba(113,206,239,1) 92%, rgba(183,222,237,1) 100%);
background: linear-gradient(45deg, rgba(183,222,237,1) 0%, rgba(33,180,226,1) 49%, rgba(113,206,239,1) 92%, rgba(183,222,237,1) 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#b7deed', endColorstr='#b7deed', GradientType=1 );
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
cursor: pointer;
cursor: hand;
}
.choice div {
position: absolute;
background: transparent;
width: 100%;
height: 100%;
padding: 5px;
top: 0;
left: 0;
text-decoration: none; /* No underlines on the link */
z-index: 10; /* Places the link above everything else in the div */
}
.centerer {
display: inline-block;
height: 100%;
vertical-align: middle;
}
.centered {
display: inline-block;
vertical-align: middle;
color: white;
}
移动视图(首选):
桌面视图(希望看起来像移动视图):
最佳答案
使用标记的最简单解决方案是设置row
的宽度。您的选择为128像素,总边距为16像素,因此将row
的宽度设置为该宽度的两倍,并可能要多一些,以允许在inline(-block)
元素之间添加大约4像素的空白。
.row
{
width:300px;
}
Demo
我不会说这是最优雅的解决方案,只是最简单的解决方案,无需重构代码。您也可以尝试使用
float:left
而不是inline block
和.choice:nth-child(odd){clear:both}
来使其他所有元素都从新行开始。Demo