如何使用百分比在文本和横向上制作DIV的文本中心?

时间:2017-05-31 05:45:08

标签: html css html5 css3 responsive-design

我可以在中心创建文本,但使用px值。当我尝试使用百分比时,它无法正常工作。我试过 display:table;并显示:table-cell 以及 display:flex; 辩解内容:中心; //水平居中 对齐项:中心; ,但它不起作用。

我的父母div有高度:7.7%。

注意:我已更新完整的标题代码

以下是JSFIDDLE链接:

https://jsfiddle.net/Anthony_Chien/f7n77068/#&togetherjs=gR8VEaLgqR

HTML:

    <div class="vav-main">
    <nav class="navbar navbar-default navheader">
        <div class="navbar-header navlogo">
            <span><a href ="#">DS</a></span>
        </div>
        <div class="navheadersec2">
            <div class="navheadermargin">
                <div class="btn-group">
                    <button class="btn btn-default btn-md dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        <span class="headerspanbox"> <div class="headerdivbox"></div> </span>Default <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu">
                        <li><a href="#">Action</a></li>
                        <li><a href="#">Another action</a></li>
                        <li><a href="#">Something else here</a></li>
                    </ul>
                </div>

                <!--<div class="header-icons">
                    <span class="icons-group"><img src="assets/images/icons/undo.png" /></span>
                    <span class="icons-label">Undo</span>
                </div>-->

                <span class="btn-group icons-grp"><img src="assets/images/icons/undo.png" /><br>Undo</span>
                <span class="btn-group icons-grp"><img src="assets/images/icons/redo.png" /><br>Redo</span>
                <span class="btn-group icons-grp"><img src="assets/images/icons/zoom-in.png" /><br></span>
                <span class="btn-group icons-grp"><img src="assets/images/icons/zoom-out.png" /><br></span>
                <span class="btn-group icons-grp"><img src="assets/images/icons/group.png" /><br>Group</span>
                <span class="btn-group icons-grp"><img src="assets/images/icons/pattern.png" /><br>Pattern</span>
                <span class="btn-group icons-grp"><img src="assets/images/icons/lock.png" /><br>Lock</span>
                <span class="btn-group icons-grp"><img src="assets/images/icons/select.png" /><br>Select</span>
                <span class="btn-group icons-grp"><img src="assets/images/icons/resize.png" /><br>Scale</span>
                <span class="btn-group icons-grp"><img src="assets/images/icons/rotate.png" /><br>Rotate</span>
                <span class="btn-group icons-grp"><img src="assets/images/icons/rotate.png" /><br>Forward</span>
                <span class="btn-group icons-grp"><img src="assets/images/icons/backward.png" /><br>Backward</span>
                <span class="btn-group icons-grp"><img src="assets/images/icons/panel.png" /><br>Panel</span>
            </div>
        </div>
        <div class="navheadersec3">
            <div class="navheadermargin">
                <div class="headersec3para">
                    <p class="headerpara">Code</p>
                    <p class="headerpara">Some text</p>
                </div>
                <p class="header-price">2,550</p>
                <span class="btn-group icons-grp info-icon"><img src="assets/images/icons/info.png" /></span>
                <p class="header-price add-to-cart">ADD</p>
            </div>
        </div>
    </nav>
</div>

CSS:

    .vav-main{
      width: 100%;
      height: 100%;
    }


    .navheader{
      width: 100%;
      height: 7.7%;
      background-color: #ffffff;
      border: solid 1px #b8b8b8;
     }

    .navlogo {
      width: 5%;
      height: 100%;
      background-color: #ffffff;
      position:relative; /* Add this property */
      transform:translate(-50% -50%); /* Add this property */
      top:50%; /* Add this property */
      left:50%; /* Add this property */
     }

    .navlogo span {
      font-family: SourceSansPro;
      font-size: 24px;
      font-weight: bold;
     }

    .navheadersec2 {
      width: 64.76%;
      display: inline-block;
      border-right: solid 1px #b8b8b8;
      height: 100%;
    }

    .navheadersec3{
      width: 30.214%;
      display: inline-block;
      margin-left: -4px;
    }

    .navheadermargin .headersec3para{
      width: 30.53%;
      height: 100%;
      float: left;
      margin-left: 6.10%;
    }

    .headersec3para .headerpara:first-child{
      font-size: 12px;
      margin: 0px !IMPORTANT;
      font-weight: bold;
    }

    .headersec3para .headerpara{
      font-size: 10px;
      margin: 0px !IMPORTANT;
    }

    .header-price{
      margin: 0px;
      height: 100%;
      width: 16.03%;
      float: left;
      text-align: center;
      line-height: 34px;
      font-weight: bold;
      font-size: 18px;
      font-family: SourceSansPro;
    }

    .info-icon{
      margin: auto;
      text-align: center;
      line-height: 34px;
    }

    .info-icon > img{
      width: 20px;
      height: 20px;
    }

    .add-to-cart{
      width: 30.534%;
      font-size: 15px;
      color: #009cff;
    }

    .navheadermargin{
      margin-top: 10px;
      height: 34px;
      margin-bottom: 10px;
    }

    .navheadermargin .btn-group{
      margin-left: 1.3%;
      height: 100%;
      width: 13.510%;
      float: left;
    }

    .navheadermargin .header-icons{
      float: left;
      width: 24px;
      height: 100%;
    }

    .header-icons .icons-group{
      height: 21px;
    }

    .header-icons .icons-group img{
      height: 20px;
      width: 20px;
    }

    .header-icons .icons-label{
      height: 15px;
      font-family: SourceSansPro;
      font-size: 10px;
    }

    .navheadermargin .btn-group button{
      height: 100%;
      width: 100%;
    }

    .headerspanbox{
      display: inline-block;
    }

    .headerspanbox .headerdivbox{
      background-color: #1fb6ff;
      margin-right: 10px;
      height: 14px;
      width: 18px;
    }

截图图片:

enter image description here

5 个答案:

答案 0 :(得分:3)

.vav-main{
  width: 100%;
  height: 100%;
 }


.navheader{
  width: 100%;
  height: 7.7%;
  background-color: #ffffff;
  border: solid 1px #b8b8b8;
 }

.navlogo {
  width: 5%;
  height: 100%;
  background-color: #ffffff;
  position:relative; /* Add this property */
  transform:translate(-50% -50%); /* Add this property */
  top:50%; /* Add this property */
  left:50%; /* Add this property */
 }

.navlogo span {
  font-family: SourceSansPro;
  font-size: 24px;
  font-weight: bold;
 }
  
  .vav-main> .navbar {
   margin-bottom: 0px;
   min-height:initial;
 }
<div class="vav-main">
  <nav class="navbar navbar-default navheader">
    <div class="navbar-header navlogo">
        <span><a href ="#">DS....</a></span>
    </div>
 </nav>
</div>

您可以使用flex来实现相同目标。

更新下面的小提琴

<强> working fiddle

.vav-main{
  width: 100%;
  height: 100%;
 }


.navheader{
  width: 100%;
  height: 7.7%;
  background-color: #ffffff;
  border: solid 1px #b8b8b8;
 }

.navlogo {
  height: 100%;
  width:100%;
  background-color: #ffffff;
  display:flex;
  justify-content:center;
  align-items:center;
  flex-flow:column nowrap;
 }

.navlogo span {
  font-family: SourceSansPro;
  font-size: 24px;
  font-weight: bold;
 }
 
 .navlogo span a {
   align-self:center;
 }
  .vav-main> .navbar {
   margin-bottom: 0px;
   min-height:initial;
 }
<div class="vav-main">
  <nav class="navbar navbar-default navheader">
    <div class="navbar-header navlogo">
        <span><a href ="#">DS....</a></span>
    </div>
 </nav>
</div>

答案 1 :(得分:2)

您可以在容器上使用display: table;,在子项上使用display: table-cell;,使您的导航元素垂直和水平居中。然后,Ypu可以根据您需要的布局设置容器和元素width

可能的例子:

&#13;
&#13;
.vav-main{
  position: relative;
  width: 100%;
  height: 100%;
 }


.navbar{
  display: table;
  width: 100%;
  background-color: #ffffff;
  border: solid 1px #b8b8b8;
 }

.navlogo {
  height: 100;
  background-color: #ffffff;
  width: 5%;
 }

.navlogo span {
  font-family: SourceSansPro;
  font-size: 24px;
  font-weight: bold;
 }
 
 .navbar-header {
  display: table-cell;
  padding: 3.5%;
  vertical-align: middle;
  text-align: center;
 }
&#13;
<div class="vav-main">
  <nav class="navbar navbar-default">
    <div class="navbar-header navlogo">
      <span><a href ="#">DS</a></span>
    </div>
    <div class="navbar-header">
       ........
    </div>
 </nav>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

.navheader{
  width: 100%;
  height: 100px;
  background-color: #ffffff;
  border: solid 1px #b8b8b8;
 }

.navlogo {
  width: 5%;
  height: 100%;
  background-color: #ffffff;
}
.navlogo span {
  font-family: SourceSansPro;
  font-size: 24px;
  font-weight: bold;
  display:table;height:100%;width:100px;
 }
 .navlogo span a{
	 display:table-cell;vertical-align:middle;text-align:center;margin:0;
 }
<div class="vav-main">
  <nav class="navbar navbar-default navheader">
    <div class="navbar-header navlogo">
        <span><a href ="#">DS</a></span>
    </div>
 </nav>
</div>

答案 3 :(得分:1)

您可能正在使用bootstrap进行此操作,如果有,那么您可以像下面一样尝试,删除span tag,因为a和span标记都是inline elements,所以你可以使用一个如果需要的话。

&#13;
&#13;
.nav-main {
  width: 100%;
  height: 100%;
}

.nav-main > .navheader {
  width: 100%;
  height: 7.7%;
  background-color: #ffffff;
}

.navlogo {
  width: 100%;
  height: 100%;
  background-color: #ffffff;
  text-align: center;
}

.navlogo > a {
  font-family: SourceSansPro;
  font-size: 24px;
  font-weight: bold;
  display: inline-block;
  margin-top: 8px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="nav-main">
  <nav class="navbar navbar-default navheader">
    <div class="navbar-header navlogo">
      <a href="#">DS</a>
    </div>
    <div>
    </div>
  </nav>
</div>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

我不修改你的代码,我只是写一些代码块。希望这对你有所帮助。如您所见,石灰块内的文本始终按水平线对齐中间,垂直对齐中间。

$('#header .text').css('left', 'calc(50% - '+$('#header .text').css('width')+' / 2)');
$('#header .text').css('top', 'calc(50% - '+$('#header .text').css('height')+' / 2)');
#header{
  width:100%;
  height:200px;
  border:1px blue solid;
}
#box{
  width:30%;
  height:80%;
  background-color:lime;
  text-align:center;
  position:relative;
}
#box .text{
  border:1px red solid;
  position:absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id='header'>
<div id='box'>
  <div class='text'>abchghjghj</div>
</div>
</div>