Make heading center align

时光怂恿深爱的人放手 提交于 2019-12-23 19:27:37

问题


How to center <h3> in the following code?

h1 {
  color: #666;
  margin: 20px 10px 0px;
  padding: 0px 30px 0px 30px;
  text-align: center;
}

h3 {
  color: #ccc;
  background-color: black;
  margin: 0px;
  padding: 10px 10px 10px 10px;
  text-align: center;
  display: inline-block;
}
<div>
    <h1>title 1</h1>
    <h3>title 3</h3>
</div>

FIDDLE


回答1:


You should add a class to <div> and style <div>.

Currently, you <div> like parent. Set text-align: center.

Example:

h1{
  color: #666;
  margin: 20px 10px 0px;
  padding: 0px 30px 0px 30px;
}

 h3 {
  color: #ccc;
  background-color: black;
  margin: 0px;
  padding: 10px 10px 10px 10px;
  display: inline-block;
}

.center {
  text-align: center;
}
<div class="center">
        <h1>title 1</h1>
        <h3>title 3</h3>
 </div>
 



回答2:


Just apply this css property to the div i.e text-align:center;

like this

<div style="text-align:center;">
    <h1>title 1</h1>
    <h3>title 3</h3>
</div>

Here is the example: https://jsfiddle.net/egv269x0/




回答3:


Apply this css to the h3:

h3 {
  color: #ccc;
  background-color: black;
  margin: 0px;
  padding: 10px 10px 10px 10px;
  //text-align: center;
  display: inline-block;
  margin-left: 50%;
  transform: translate(-50%, 0);
}

The way you tried to do it doesn't work because when using display: inline-block; the element is not full-width anymore and text-align: center; centers the text according to the element's width.




回答4:


You can put it in a div and add style text-align: center. :)




回答5:


If you want to center title 3 : It work without display: inline-block; in css



来源:https://stackoverflow.com/questions/42999528/make-heading-center-align

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!