Vertical alignment in bootstrap's spans

2020-03-09 11:16发布

Is there any work around for vertical alignment of elements or texts in bootstrap spans relative to other spans

For eg. if I have a row and spans like this

<div class="row-fluid">
   <div class="span3" style="background-color:lightblue">Description</div>
   <div class="span9" style="background-color:pink">
   Lorem ipsum Lorem ipsum  Lorem ipsum  Lorem ipsum  Lorem ipsum  Lorem ipsum  Lorem ipsum  ....
   </div>
</div>

It looks like this

enter image description here

So is there a way to show "Description" vertically center of the next span's height?

7条回答
孤傲高冷的网名
2楼-- · 2020-03-09 11:29

In Bootstrap there is no provision for such issue. It is implantation dependent. However you can write a css rule saying if next div exceeds the height of a previous div align the previous div at center height of next div & apply this class to the divs wherever you want this kind of behaviour.

查看更多
smile是对你的礼貌
3楼-- · 2020-03-09 11:38
$('.row').each(function() {
  var rowHeight = $(this).height();
  $(this).find("[class^='span']").each(function() {
    if ( $(this).height() < rowHeight-20) {
      var odstep = (rowHeight - $(this).height()) / 2 ; 
      $(this).css( "padding-top", odstep );
    }
  });
});
查看更多
老娘就宠你
4楼-- · 2020-03-09 11:40

Check out this script-free solution.

<div class="row-fluid">
   <div class="span3" style="background-color:lightblue;margin:0px auto;float:none;">Description</div>
   <div class="span9" style="background-color:pink;clear:both;margin:0px auto;float:none;">
   Lorem ipsum Lorem ipsum  Lorem ipsum  Lorem ipsum  Lorem ipsum  Lorem ipsum  Lorem ipsum  ....
   </div>
</div>

The code in action: jsFiddle

Just remove the inline styling and place it in your css file.

查看更多
Emotional °昔
5楼-- · 2020-03-09 11:43

try display: table; for parent and display: table-cell; for child

JSFiddle

查看更多
叼着烟拽天下
6楼-- · 2020-03-09 11:45

If you are trying to vertically center an element for certain screen sizes you could always use media queries such as:

/*tablet*/
@media (min-width: 768px) and (max-width: 979px) {
  #logo{
    margin-top:70px;
  }
}
/*desktop*/
@media (max-width: 979px) {
  #logo{
    margin-top:20px;
  }
}
/*large desktop*/
@media (min-width: 1200px) {
  #logo{
    margin-top:0;
  }
}
查看更多
ら.Afraid
7楼-- · 2020-03-09 11:50

I worked a lot on this , and a simple solution by using jquery is shown here !

<script>
$(document).ready(function(){
    var relheight=$('.span9').height() / 2;
    $('.span3').css('margin-top', relheight) ;

});
</script>

It works and i tested it also . If you like my answer please vote for me

查看更多
登录 后发表回答