Flexbox响应式的三列布局

时间:2019-05-28 07:25:47

标签: javascript html css3 bootstrap-4 flexbox

我对三个布局列进行了一次小调查。

在桌面上看起来像这样:

desktop

我的问题是在手机屏幕上。我有一个人在另一个之下,之后又提出了问题。请参见下图。

desktop

我想要实现的是第一个人,然后是问题,第二个人,然后是问题,第三个人,然后是问题,

desktop

有人知道如何解决吗?

这是HTMl:

  <div class="col-md-12 col-xs-12">
<div class="d-table">
        <div class="d-table-row">
            <div class="d-table-cell align-middle"></div>
            <div class="d-table-cell align-middle">
                <div class="titleMiddle">Person 1</div>
            </div>
            <div class="d-table-cell align-middle">
                <div class="titleMiddle">Person 2</div>
            </div>
            <div class="d-table-cell align-middle">
                <div class="titleMiddle">Person 3</div>
            </div>
        </div>
        <div class="d-table-row">
            <div class="d-table-cell align-middle pb-4">
                <div class="question">

                    <div class="smalltitle">
                        <span class="prefiks">1</span>
                        <span>This is a very nice question</span>
                    </div>
                </div>
            </div>
            <div class="d-table-cell align-middle pb-4">
                <div class="horizontal">
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="1">
                        </div>
                        <div class="answerText">1</div>
                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">
                            <input type="radio" name="name" value="2">
                        </div>
                        <div class="answerText">2</div>
                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="3">
                        </div>

                        <div class="answerText">3</div>

                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="4">
                        </div>
                        <div class="answerText">4</div>
                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="5">

                        </div>

                        <div class="answerText">5</div>

                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">
                            <input type="radio" name="name" value="6">
                        </div>
                        <div class="answerText">No answer</div>

                    </div>
                </div>
            </div>
            <div class="d-table-cell align-middle pb-4">
                <div class="horizontal">
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="1">

                        </div>

                        <div class="answerText">1</div>

                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="2">

                        </div>

                        <div class="answerText">2</div>

                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="3">

                        </div>

                        <div class="answerText">3</div>

                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="4">

                        </div>

                        <div class="answerText">4</div>

                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="5">

                        </div>

                        <div class="answerText">5</div>

                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="6">

                        </div>

                        <div class="answerText">No Answer</div>

                    </div>
                </div>

            </div>
            <div class="d-table-cell align-middle pb-4">
                <div class="horizontal">
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="1">

                        </div>

                        <div class="answerText">1</div>

                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="2">

                        </div>

                        <div class="answerText">2</div>

                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="3">

                        </div>

                        <div class="answerText">3</div>

                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="4">

                        </div>

                        <div class="answerText">4</div>

                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="5">

                        </div>

                        <div class="answerText">5</div>

                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="6">

                        </div>

                        <div class="answerText">No answer</div>

                    </div>
                </div>
            </div>
        </div>
    </div>
  </div>

CSS

.d-table {
    display: table!important;
}
.d-table-row {
    display: table-row !important;
}
* {
    -webkit-font-smoothing: antialiased;

}
*, ::after, ::before {
    box-sizing: inherit;
}

.d-table-cell {
    display: table-cell!important;
}

.align-middle {
    vertical-align: middle!important;
}
.align-middle > div {
    margin-right: 50px;
}
.titleMiddle {
    font-weight: 700;
    margin-bottom: 30px;
}
.pb-4, .py-4 {
    padding-bottom: 1.5rem!important;
}

.smalltitle {
  margin-bottom: 20px;
}

.horizontal {
    display: flex !important;
    flex-direction: row !important;
}

.answer {
  margin-left: 15px;
    margin-bottom: 5px;
    padding: 6px;
}

.answerRight {
      display: flex !important;
    flex-direction: row !important;
    align-items: center;
}
input[type='radio'] {
    width: 13px;
    height: 13px;
    background: white;
    border-radius: 50%!important;
    border: 1px solid #343c49;
    -moz-appearance: none;
    -webkit-appearance: none;
}

input[type='radio']:checked {
    background: #06c;
}

.answerText {
    margin-left: 0.375rem;
}

2 个答案:

答案 0 :(得分:1)

您尝试过吗?

<div class="col-md-12 col-xs-12">
    <div class="d-table">
        <div class="d-table-row">
            <div class="d-table-cell align-middle">This is a very nice question</div>
            <div class="d-table-cell align-middle">
                <div class="titleMiddle">Person 1</div>
                    <div class="d-table-cell align-middle pb-4">
                    <div class="horizontal">
                        <div class="answer answerRight">
                            <div class="answerInput">

                            <input type="radio" name="name" value="1">
                        </div>
                        <div class="answerText">1</div>
                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">
                            <input type="radio" name="name" value="2">
                        </div>
                        <div class="answerText">2</div>
                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="3">
                        </div>

                        <div class="answerText">3</div>

                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="4">
                        </div>
                        <div class="answerText">4</div>
                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="5">

                        </div>

                        <div class="answerText">5</div>

                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">
                            <input type="radio" name="name" value="6">
                        </div>
                        <div class="answerText">No answer</div>

                        </div>
                    </div>
                </div>
            </div>
            <div class="d-table-cell align-middle">
                <div class="titleMiddle">Person 2</div>
                                 <div class="d-table-cell align-middle pb-4">
                <div class="horizontal">
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="1">
                        </div>
                        <div class="answerText">1</div>
                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">
                            <input type="radio" name="name" value="2">
                        </div>
                        <div class="answerText">2</div>
                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="3">
                        </div>

                        <div class="answerText">3</div>

                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="4">
                        </div>
                        <div class="answerText">4</div>
                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="5">

                        </div>

                        <div class="answerText">5</div>

                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">
                            <input type="radio" name="name" value="6">
                        </div>
                        <div class="answerText">No answer</div>

                    </div>
                </div>
            </div>
            </div>
            <div class="d-table-cell align-middle">
                <div class="titleMiddle">Person 3</div>
                                <div class="d-table-cell align-middle pb-4">
                <div class="horizontal">
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="1">
                        </div>
                        <div class="answerText">1</div>
                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">
                            <input type="radio" name="name" value="2">
                        </div>
                        <div class="answerText">2</div>
                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="3">
                        </div>

                        <div class="answerText">3</div>

                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="4">
                        </div>
                        <div class="answerText">4</div>
                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="5">

                        </div>

                        <div class="answerText">5</div>

                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">
                            <input type="radio" name="name" value="6">
                        </div>
                        <div class="answerText">No answer</div>

                    </div>
                </div>
            </div>
            </div>
        </div>
    </div>
</div>

或者为每个人使用一个class =“ d-table-row”,其答案如下:

<div class="col-md-12 col-xs-12">
    <div class="d-table">
        <div class="d-table-row">
            <div class="d-table-cell align-middle">This is a very nice question</div>
            <div class="d-table-cell align-middle">
                <div class="titleMiddle">Person 1</div>
                    <div class="d-table-cell align-middle pb-4">
                    <div class="horizontal">
                        <div class="answer answerRight">
                            <div class="answerInput">

                            <input type="radio" name="name" value="1">
                        </div>
                        <div class="answerText">1</div>
                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">
                            <input type="radio" name="name" value="2">
                        </div>
                        <div class="answerText">2</div>
                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="3">
                        </div>

                        <div class="answerText">3</div>

                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="4">
                        </div>
                        <div class="answerText">4</div>
                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">

                            <input type="radio" name="name" value="5">

                        </div>

                        <div class="answerText">5</div>

                    </div>
                    <div class="answer answerRight">
                        <div class="answerInput">
                            <input type="radio" name="name" value="6">
                        </div>
                        <div class="answerText">No answer</div>

                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

我不确定封闭div的数量是否正确,但是此版本会将每个人都放在他们的行中(在台式机和移动设备上)。

答案 1 :(得分:0)

这样在不同的div中创建不同的人

.d-flex{
    display: flex;
}
.row{
    flex-direction: row;
}
.col{
    flex-direction: column;
}
<div class="d-flex row">
    <div class="d-flex col">
        <div>Person 1</div>
        <div class=" d-flex row">
            <span>Q1</span>
            <div>
                <input type="radio" name="q1" value="1" >
                <span >1</span>
            </div>
            <div>
                <input type="radio" name="q2" value="2">
                <span >2</span>
            </div>
            <div>
                <input type="radio" name="q3" value="3">
                <span >3</span>
            </div>
        </div>
    </div>
    <div class="d-flex col">
        <div>Person 1</div>
        <div class=" d-flex row">
            <span>Q1</span>
            <div>
                <input type="radio" name="q1" value="1" >
                <span >1</span>
            </div>
            <div>
                <input type="radio" name="q2" value="2">
                <span >2</span>
            </div>
            <div>
                <input type="radio" name="q3" value="3">
                <span >3</span>
            </div>
        </div>
    </div>
</div>

这可能是结构,并使用媒体查询来提高响应速度。