首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >div中div的垂直居中

问div中div的垂直居中
EN

Stack Overflow用户
提问于 2017-08-26 09:17:25
回答 1查看 32关注 0票数 0

我想要完成的是:让div带有“col 1”(投票总数和投票箭头),在父div的中心垂直对齐。

我的html-

代码语言:javascript
复制
  <div class="row container-fluid">
    <div class="col-md-1 centered-div">
      <p id='vote-total-{{question.pk}}'>vote total: {{question.votes}}</p>
    </div>
    <div class="col-md-1 centered-div">
        <p class='question-votes'>
          <a id='question-upvote-{{question.pk}}' href="{% url 'questions:upvote' pk=question.pk %}">
            <span class="glyphicon glyphicon-arrow-up"></span>
          </a>
        </p>
        <p class='question-votes'>
          <a id='question-downvote-{{question.pk}}' href="{% url 'questions:downvote' pk=question.pk %}">
        <span class="glyphicon glyphicon-arrow-down"></span>
          </a>
        </p>
    </div>
    <div class="col-md-4">
      <h2>
        title: {{question.title}}
      </h2>
      <h3>
    details: {{question.details}}
      </h3>
      <p>asked by: <a href="{% url 'accounts:detail' pk=question.user.id %}">{{question.user.username}}</a>,  on: {{question.created_at}}</p>
      <p>
        {% if user.is_authenticated and question.user.username == request.user.username and not hide_delete %}
            <a href="{% url 'questions:delete' pk=question.pk %}" title="delete" class="btn btn-simple">
          <span class="glyphicon glyphicon-remove text-danger"></span>
          <span class="text-danger icon-label">Delete</span>
            </a>
        {% endif %}
      </p>
    </div>
  </div>

我的css -

代码语言:javascript
复制
.container-fluid {
  height: 100%;
  padding-left: 0px;
  padding-right: 0px;
}
.centered-div {
  position: absolute;
  top: 50%;
}

根据我所读到的,我认为在center - div类中,我可以调用top: 50%来根据父div将其垂直居中。有人知道它为什么不工作吗?

EN

回答 1

Stack Overflow用户

发布于 2017-08-26 09:50:21

使用flexbox垂直对齐,这样它就可以在所有屏幕上进行修复

代码语言:javascript
复制
.centered-div {
  position: absolute;
  display:flex;
  justify-content:center;
  align-items:center;
 flex-direction:row;/*change to column if not as in expected direction.*/
 height:80px; /*Set height if not specified earlier*/
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45891078

复制
相关文章

相似问题

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