css如何集中这个内容

时间:2014-04-27 12:01:10

标签: html css

我为我的问题做了一个小提琴。

就是这样 http://jsfiddle.net/8Kx8c/

这是我的代码的结果: enter image description here

我需要做两件事:

首先,使用边框将div内的所有内容居中。我的意思是这个div <div id="agentsLegend">

其次,将文本居中放在圆圈的中心。文本存在于此代码中:

<span class="oneLegend">
    <div class="breakLegend oneAgentLegent"></div>
    Break
</span>

我试过了:

text-align:center;
vertical-align:middle;

但内容不集中。

6 个答案:

答案 0 :(得分:2)

您还需要将vertical-align设置为.oneAgentLegent

最终将行高设置为#agentsLegend

<强> DEMO

#agentsLegend {
    border: 1px solid black;
    border-radius:10px;
    display:inline-block;
    margin-top:5px;
    height:38px;
    line-height:30px;/* tune baseline height here */
    padding-left:5px;
    padding-right:5px;
}
.oneLegend {
    display:inline-block;
    text-align:center;
    vertical-align:middle;
}
.oneAgentLegent {
    border-radius:50%;
    width:20px;
    height: 20px; 
    display:inline-block;
        vertical-align:middle;/* make this to vertical-align:middle to text aside */
}

使您的内联框居中:在父级上使用text-align:center

<强> DEMO 2

body {
    text-align:center;/* parent of your box in fiddle demo */
}

编辑,如果你想使用display:table,请将display:table-cell添加到span,这样它就可以轻松地垂直居中:http://jsfiddle.net/8Kx8c/9/

答案 1 :(得分:2)

我对CSS进行了一些更改。这些可以帮助您解决问题。

  1. 降低高度并在#agentsLegend元素中添加填充顶部。

      

    高度:28px;填充顶:9px;

  2. 在.oneLegend元素中将vertical-align:middle;更改为vertical-align:top;

  3. line-height: 20px;添加到.oneLeged元素。
  4. FIDDLE

答案 2 :(得分:1)

http://jsfiddle.net/DUgKb/19/

你不能有一个包裹div的跨度。由于跨度不是块元素,因此无法正常工作。

  1. 添加包装div以使所有内容居中。

     .center {
       text-align: center;
      }
    
  2. 垂直对齐图标

    #agentsLegend {
        display: inline-block;
        padding: 5px;
    }
    
    .oneAgentLegent, .oneLegend {
       display: inline-block;
       vertical-align: middle;
    }
    

答案 3 :(得分:1)

您可以通过对父级vertical-align设置的line-height组合使用display:table;来进行垂直对齐,并设置margin:0 auto#agentsLegend { border: 1px solid black; border-radius:10px; display:inline-block; margin-top:5px; line-height:38px;/* <-- vertical alignment */ padding-left:5px; padding-right:5px; display:table; /* <-- horizontal alignment */ margin:0 auto; /* <-- horizontal alignment */ } .oneLegend { display:inline-block; text-align:center; vertical-align:middle;/* <-- vertical alignment */ } .oneAgentLegent { border-radius:50%; width:20px; height: 20px; vertical-align:-10%;/* <-- vertical alignment */ display:inline-block; } .agent-table-wrap td.breakAgentClass, .breakLegend { background: #6b4e30; /* Old browsers */ /* IE9 SVG, needs conditional override of 'filter' to 'none' */ background: url(data:image/svg+xml; base64, PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iIzZiNGUzMCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiM5OTg5NzIiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+); background: -moz-linear-gradient(top, #6b4e30 0%, #998972 100%); /* FF3.6+ */ background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #6b4e30), color-stop(100%, #998972)); /* Chrome,Safari4+ */ background: -webkit-linear-gradient(top, #6b4e30 0%, #998972 100%); /* Chrome10+,Safari5.1+ */ background: -o-linear-gradient(top, #6b4e30 0%, #998972 100%); /* Opera 11.10+ */ background: -ms-linear-gradient(top, #6b4e30 0%, #998972 100%); /* IE10+ */ background: linear-gradient(to bottom, #6b4e30 0%, #998972 100%); /* W3C */ filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#6b4e30', endColorstr='#998972', GradientType=0); /* IE6-8 */ color: #fff; } .agent-table-wrap td.idelAgentClass, .idelLegent { background: #fcda19; /* Old browsers */ /* IE9 SVG, needs conditional override of 'filter' to 'none' */ background: url(data:image/svg+xml; base64, PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2ZjZGExOSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiNmZmZmZmYiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+); background: -moz-linear-gradient(top, #fcda19 0%, #ffffff 100%); /* FF3.6+ */ background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #fcda19), color-stop(100%, #ffffff)); /* Chrome,Safari4+ */ background: -webkit-linear-gradient(top, #fcda19 0%, #ffffff 100%); /* Chrome10+,Safari5.1+ */ background: -o-linear-gradient(top, #fcda19 0%, #ffffff 100%); /* Opera 11.10+ */ background: -ms-linear-gradient(top, #fcda19 0%, #ffffff 100%); /* IE10+ */ background: linear-gradient(to bottom, #fcda19 0%, #ffffff 100%); /* W3C */ filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#fcda19', endColorstr='#ffffff', GradientType=0); /* IE6-8 */ color: #333; } .agent-table-wrap td.pauseAgentClass, .pauseLegend { background: #1283b7; /* Old browsers */ /* IE9 SVG, needs conditional override of 'filter' to 'none' */ background: url(data:image/svg+xml; base64, PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iIzEyODNiNyIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiNiYWQxZTIiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+); background: -moz-linear-gradient(top, #1283b7 0%, #bad1e2 100%); /* FF3.6+ */ background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #1283b7), color-stop(100%, #bad1e2)); /* Chrome,Safari4+ */ background: -webkit-linear-gradient(top, #1283b7 0%, #bad1e2 100%); /* Chrome10+,Safari5.1+ */ background: -o-linear-gradient(top, #1283b7 0%, #bad1e2 100%); /* Opera 11.10+ */ background: -ms-linear-gradient(top, #1283b7 0%, #bad1e2 100%); /* IE10+ */ background: linear-gradient(to bottom, #1283b7 0%, #bad1e2 100%); /* W3C */ filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#1283b7', endColorstr='#bad1e2', GradientType=0); /* IE6-8 */ color: #fff; } .agent-table-wrap td.talkingAgentClass, .talkingLegend { background: #4ea51c; /* Old browsers */ /* IE9 SVG, needs conditional override of 'filter' to 'none' */ background: url(data:image/svg+xml; base64, PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iIzRlYTUxYyIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiNhYWU1NzkiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+); background: -moz-linear-gradient(top, #4ea51c 0%, #aae579 100%); /* FF3.6+ */ background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #4ea51c), color-stop(100%, #aae579)); /* Chrome,Safari4+ */ background: -webkit-linear-gradient(top, #4ea51c 0%, #aae579 100%); /* Chrome10+,Safari5.1+ */ background: -o-linear-gradient(top, #4ea51c 0%, #aae579 100%); /* Opera 11.10+ */ background: -ms-linear-gradient(top, #4ea51c 0%, #aae579 100%); /* IE10+ */ background: linear-gradient(to bottom, #4ea51c 0%, #aae579 100%); /* W3C */ filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#4ea51c', endColorstr='#aae579', GradientType=0); /* IE6-8 */ color: #fff; } .agent-table-wrap td.voicemailAgentClass, .voiceMailLegend { background: #8a38aa; /* Old browsers */ /* IE9 SVG, needs conditional override of 'filter' to 'none' */ background: url(data:image/svg+xml; base64, PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSI0JSIgc3RvcC1jb2xvcj0iIzhhMzhhYSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiNkYjk1ZjQiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+); background: -moz-linear-gradient(top, #8a38aa 4%, #db95f4 100%); /* FF3.6+ */ background: -webkit-gradient(linear, left top, left bottom, color-stop(4%, #8a38aa), color-stop(100%, #db95f4)); /* Chrome,Safari4+ */ background: -webkit-linear-gradient(top, #8a38aa 4%, #db95f4 100%); /* Chrome10+,Safari5.1+ */ background: -o-linear-gradient(top, #8a38aa 4%, #db95f4 100%); /* Opera 11.10+ */ background: -ms-linear-gradient(top, #8a38aa 4%, #db95f4 100%); /* IE10+ */ background: linear-gradient(to bottom, #8a38aa 4%, #db95f4 100%); /* W3C */ filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#8a38aa', endColorstr='#db95f4', GradientType=0); /* IE6-8 */ color: #fff; } 。父母水平居中。

Demo Fiddle

CSS

{{1}}

答案 4 :(得分:1)

您可以更改以下属性:

#agentsLegend {
  display: table;   
}
.oneLegend {
  display:table-cell;
}

http://jsfiddle.net/8Kx8c/1

答案 5 :(得分:1)

您需要在line-height:38px;上添加#agentsLegend,在vertical-align:middle;上添加oneLegend

#agentsLegend {
    border: 1px solid black;
    border-radius:10px;
    display:inline-block;
    margin-top:5px;
    height:38px;
    line-height:38px; /*Addded line*/
    padding-left:5px;
    padding-right:5px;
}
.oneLegend {
    display:inline-block;
    text-align:center;
    vertical-align:middle; /*Addded line*/
}

以下是Fiddle链接。