简体   繁体   English

如果左侧的另一个div导致它们堆叠,则将div垂直对齐

[英]Vertically align right divs if another div to the left causes them to stack

This page has questions on the left, and radio buttons for ranking/scale answers on the right. 该页面的左侧是问题,右侧是单选按钮,用于排列/缩放答案。 It's designed so that if the question text is too long too fit in the left pane it just keeps going and pushes the radio buttons down a line beneath the question text. 这样做的目的是, 如果问题文本太长而不能容纳在左窗格中,它只会继续前进,并将单选按钮向下推到问题文本下方的一行。 I emphasized if because I want to keep it that way, so it only moves if the text is long, I don't want to set something fixed so that every question does the same thing. 我强调,如果因为我想保持这种方式,那么只有在文本较长的情况下它才会移动,我不想设置固定的内容,以便每个问题都做同样的事情。

The problem is, the N/A radio button doesn't adjust like the other radio buttons. 问题是,“ N / A”单选按钮无法像其他单选按钮一样进行调整。 Is there a way to force it to move down with the others? 有没有办法迫使它与其他人一起向下移动? I've tried surrounding the two sections with a div with vertical align bottom but that didn't work. 我试过用垂直对齐底部的div包围这两个部分,但这没有用。 Maybe I am doing something wrong but whatever I've read with regards to vertically aligning inner divs doesn't seem to accomplish this. 也许我做错了什么,但是我所读到的关于垂直对齐内部div的内容似乎都无法实现。 Is it possible to fix this? 有可能解决这个问题吗?

https://jsfiddle.net/x5y49d0n/ https://jsfiddle.net/x5y49d0n/

  .qg { width: 700px; /* this is only to demonstrate a smaller screen */ padding-top: 10px; margin-top: 0; } .qg-row { margin-left: 10px; margin-bottom: 10px; overflow: auto; } .qg-head { padding-top: 5px; padding-bottom: 5px; background-color: #fcfcfc; } [class*="qg-guide"] { font-size: 11px; min-height: 10px; text-align: center; float: left; } [class*="qg-right-section"] { float: right; width: 500px; } .qg-right-section-1 { width: 60px; } .qg-right-section-5 { width: 300px; } [class*="qg-guide-"] { word-wrap: break-word; } .qg-guide-1 { empty-cells: hide; width: 100%; min-width: 100%; } .qg-guide-5 { /* replicate in mobile */ empty-cells: show; width: 20%; min-width: 20%; } .qg-guide-7 { empty-cells: show; width: 14.28%; min-width: 14.28%; } .qg-label { font-size: 13px; float: left; margin-bottom: 10px; } [class*="qg-input"] { font-size: 11px; width: 60px; text-align: center; float: left; } 
 <div class="qg"> <div class="qg-table"> <div class="qg-row qg-head"> <div class="qg-guide-label"></div> <div class="qg-guide qg-right-section-1"> <div class="qg-guide-1">&nbsp;</div> </div> <div class="qg-guide qg-right-section-5"> <div class="qg-guide-7">High7</div> <div class="qg-guide-7"></div> <div class="qg-guide-7"></div> <div class="qg-guide-7">Neutral4</div> <div class="qg-guide-7"></div> <div class="qg-guide-7"></div> <div class="qg-guide-7">Low1</div> </div> </div> <div class="qg-row"> <div class="qg-label">Far far away, behind the word mountains, far from the countries Vokalia and Consonantia.</div> <div class="qg-guide qg-right-section-1"> <div class="qg-input-1"> <input type="radio" name="0LoMVa" value=""><br> N/A<br> </div> </div> <div class="qg-right-section-5"> <div class="qg-input-5"><input type="radio" name="0LoMVa" value="5"><br>5<br></div> <div class="qg-input-5"><input type="radio" name="0LoMVa" value="4"><br>4<br></div> <div class="qg-input-5"><input type="radio" name="0LoMVa" value="3"><br>3<br></div> <div class="qg-input-5"><input type="radio" name="0LoMVa" value="2"><br>2<br></div> <div class="qg-input-5"><input type="radio" name="0LoMVa" value="1"><br>1<br></div> </div> </div> </div> </div> <div class="qg"> <div class="qg-table"> <div class="qg-row qg-head"> <div class="qg-guide-label"></div> <div class="qg-guide qg-right-section-1"> <div class="qg-guide-1">&nbsp;</div> </div> <div class="qg-guide qg-right-section-5"> <div class="qg-guide-5 qg-guide-item-0">High0</div> <div class="qg-guide-5 qg-guide-item-1"></div> <div class="qg-guide-5 qg-guide-item-2">Neutral2</div> <div class="qg-guide-5 qg-guide-item-3"></div> <div class="qg-guide-5 qg-guide-item-4">Low4</div> </div> </div> <div class="qg-row"> <div class="qg-label">Far far away, behind the word mountains.</div> <div class="qg-guide qg-right-section-1"> <div class="qg-input-1"> <input type="radio" name="W1MkXk" value=""><br> N/A<br> </div> </div> <div class="qg-right-section-5"> <div class="qg-input-5"><input type="radio" name="W1MkXk" value="5"><br>5<br></div> <div class="qg-input-5"><input type="radio" name="W1MkXk" value="4"><br>4<br></div> <div class="qg-input-5"><input type="radio" name="W1MkXk" value="3"><br>3<br></div> <div class="qg-input-5"><input type="radio" name="W1MkXk" value="2"><br>2<br></div> <div class="qg-input-5"><input type="radio" name="W1MkXk" value="1"><br>1<br></div> </div> </div> </div> </div> 

在此处输入图片说明

This is what I want it to do: 这就是我想要的:

在此处输入图片说明

Also, these questions are created programmatically using a template, so the divs used have to be the same, IOW I can't add a div (with an additional class) to the 1st question that fixes the issue without the 2nd question having the same div. 另外,这些问题是通过使用模板以编程方式创建的,因此所用的div必须相同,但我不能在没有第二个问题相同的情况下向解决该问题的第一个问题添加div(带有附加类) div。 This is the critical requirement which is making it difficult for me to find a solution. 这是至关重要的要求,这使我很难找到解决方案。 Can this be solved? 这可以解决吗?

You can achieve the effect you want by adding a clear-fix after the .qg-label class. 您可以通过在.qg-label类之后添加一个明确的修补程序来达到所需的效果。 I did this in the fiddle by manually inserting an empty <div> with the style clear:both; 我通过手动插入样式为clear:both;的空<div>来完成此操作clear:both; . You could instead use the clear-fix class of your favorite library or create you own such as: 您可以改用您喜欢的库的clear-fix类,也可以创建自己的库,例如:

(This is a classic clearfix example from this answer ) (这是此答案中的经典clearfix示例)

.clearfix:after {
   content: " "; /* Older browser do not support empty content */
   visibility: hidden;
   display: block;
   height: 0;
   clear: both;
}

Another solution is to include all radio buttons in the same parent element, give it a fixed width, and add float:right; 另一种解决方案是将所有单选按钮都包含在同一父元素中,为其设置固定宽度,然后添加float:right; to it and the radio element children. 和广播元素的孩子们。 This should be a simple fix. 这应该是一个简单的修复。 I showed this in comparison to your original fiddle here - fiddle. 与您原来的小提琴- 小提琴相比,我展示了这一点

Is it possible to change DOM structure?? 是否可以更改DOM结构? If yes then just try pushing 如果是,那就尝试

<div class="qg-guide qg-right-section-1"></div> inside <div class="qg-guide qg-right-section-1"></div>

<div class="qg-guide qg-right-section-5">

code

        <div class="qg-row qg-head">
            <div class="qg-guide-label"></div>
            <div class="qg-guide qg-right-section-5">
                <div class="qg-guide-5 qg-guide-item-0">High0</div>
                <div class="qg-guide-5 qg-guide-item-1"></div>
                <div class="qg-guide-5 qg-guide-item-2">Neutral2</div>
                <div class="qg-guide-5 qg-guide-item-3"></div>
                <div class="qg-guide-5 qg-guide-item-4">Low4</div>

                <div class="qg-guide qg-right-section-1">
                  <div class="qg-guide-1">&nbsp;</div>
                </div>
            </div>
        </div>

and change css 并更改CSS

.qg-right-section-5 {
    width: 360px;
}
.qg-right-section-1 {
 width: 60px;
 float: left;
}

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

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