首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CSS/HTML如何为移动视图对齐这些元素?

CSS/HTML如何为移动视图对齐这些元素?
EN

Stack Overflow用户
提问于 2016-12-23 22:21:49
回答 2查看 1.3K关注 0票数 0

我试图使上面的元素在它们的div中均匀水平地对齐。在上面,我试图使‘高’和‘低’对齐,这样他们就不会对进度条和垂直内联。这些复选框都很好,但我在日期中使用了“float:right”--这是不太理想的,因为它稍微向上撞了一下。

我的html

代码语言:javascript
复制
<div class="show-for-small-only">
            <div class="parents linkable">
            <%= check_box_tag "homework[id][]", homework.id, checked = false, class: "narrowtr" %>
            <%= link_to homeworks_engagement_view_item_path(:id => homework.id) do %>
                    <div class="progress-meter-interest-mobile horizTranslate linkable" data-bar-length="<%= homework.average_interest * 100 / 5 %>"><div class="text-center"><%= homework.average_interest %></div></div>
                    <font size="1" class="linkable mobile-center"><%= homework.significance.sig_option %></font>&emsp;&ensp;
                    <font size="1" class="linkable mobile-right"><strong><%= formatted_date(homework.due)%></strong></font>
                </div>
                <% end %>
                <% end %>

我的CSS:

代码语言:javascript
复制
.parents {
   border-bottom: 20px;
   background-color: white;
   top: 0px;
   margin:0px; 
   height: 60px; 
   padding: 15px 2px 2px 3px;    
   border-width: 2px;
   border-bottom-width:1px;
   border-bottom-color:Grey;
   border-bottom-style: solid;
   width: 100%;
}

.linkable{
color: black;
}

酒吧司:

代码语言:javascript
复制
.progress-meter-interest-mobile{
  background-color: #FFD733;
  height: 20px;
  display: inline-block;
}

对齐日期与浮动:右..。

代码语言:javascript
复制
.mobile-right{
  float:right;
  margin-right: 2px;
}



.mobile-center{
   display: inline-block;
   text-align: center;
   margin-right: 6px;
   background-color: #EEE;
 }

.horizTranslate只是条形图的css动画。

这是我真正坚持的两个中间元素--酒吧和“高”和“低”。我已经尝试过显示:内联和显示:内联块和玩不同的div,但似乎无法得到它。

有什么想法吗?感谢你的指导。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-12-24 09:45:14

由于您寻找像布局这样的表,所以请使用内联块并给它们一个宽度(以及一个小的标记更改,我在其中交换了progress/text标记)。

注意,不推荐使用font元素,因此我将其替换为span

代码语言:javascript
复制
.parents {
  margin-bottom: 10px;
  height: 40px;
  padding: 15px 2px 2px 3px;
  border-bottom: 1px solid Grey;
  width: 240px;
}
.linkable {
  color: black;
}
.progress-meter-interest-mobile {
  background-color: #FFD733;
  height: 20px;
  text-align: center;
}
.parents.linkable > * {
  display: inline-block;
  vertical-align: middle;
}
.progress-meter-interest-mobile.low {
  width: 30px;
}
.progress-meter-interest-mobile.high {
  width: 60px;
}
.parents.linkable > :nth-child(2) {
  width: calc(100% - 130px);
}
.parents.linkable > :nth-child(3) {
  width: 30px;
  font-size: 11px;
}
.parents.linkable > :nth-child(4) {
  width: 60px;
  text-align: right;
  font-weight: bold;
  font-size: 11px;
}
代码语言:javascript
复制
<div class="show-for-small-only">
  <div class="parents linkable">
    <input type="checkbox">
    <div>
      <div class="progress-meter-interest-mobile low">2</div>
    </div>
    <span class="linkable">Low</span>
    <span class="linkable">12/12/2016</span>
  </div>
  <div class="parents linkable">
    <input type="checkbox">
    <div>
      <div class="progress-meter-interest-mobile high">4</div>
    </div>
    <span class="linkable">High</span>
    <span class="linkable">12/12/2016</span>
  </div>
</div>

当然,您可以像建议的那样使用flexbox,尽管您仍然需要交换标记,以使“仪表”正常工作,并给每个元素一个宽度,使它们垂直对齐。

代码语言:javascript
复制
.parents {
  margin-bottom: 10px;
  height: 40px;
  padding: 15px 2px 2px 3px;
  border-bottom: 1px solid Grey;
  width: 240px;
}
.linkable {
  color: black;
}
.progress-meter-interest-mobile {
  background-color: #FFD733;
  height: 20px;
  text-align: center;
}
.parents.linkable {
  display: flex;
  align-items: center;
}
.progress-meter-interest-mobile.low {
  width: 30px;
}
.progress-meter-interest-mobile.high {
  width: 60px;
}
.parents.linkable > :nth-child(2) {
  width: calc(100% - 130px);
}
.parents.linkable > :nth-child(3) {
  width: 30px;
  font-size: 11px;
}
.parents.linkable > :nth-child(4) {
  width: 60px;
  text-align: right;
  font-weight: bold;
  font-size: 11px;
}
代码语言:javascript
复制
<div class="show-for-small-only">
  <div class="parents linkable">
    <input type="checkbox">
    <div>
      <div class="progress-meter-interest-mobile low">2</div>
    </div>
    <span class="linkable">Low</span>
    <span class="linkable">12/12/2016</span>
  </div>
  <div class="parents linkable">
    <input type="checkbox">
    <div>
      <div class="progress-meter-interest-mobile high">4</div>
    </div>
    <span class="linkable">High</span>
    <span class="linkable">12/12/2016</span>
  </div>
</div>

票数 1
EN

Stack Overflow用户

发布于 2016-12-23 23:08:14

您应该检查。它解决了我的问题好几次。Flexbox医生 --在您的情况下,以下内容应该会有所帮助

代码语言:javascript
复制
.container {
  align-items:stretch;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41308442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档