Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make h1 tag center with its actual width

Tags:

html

css

I need to make h1 tag center and also need to make a border around it. To prevent taking up more width I give display: inline-block but the tag does not respond to text-align: center then.

h1 {
  text-align: center;
  border: 1px solid;
  display: inline-block;
}
<div class="container">
  <h1>This is the Title</h1>
</div>
like image 716
ub_303 Avatar asked Dec 24 '22 11:12

ub_303


1 Answers

You can use display: table on heading. This will make heading to have width depending on its content while it will remain a block level element as well. And you can center it using margin: 0 auto property.

.heading {
  border: 1px solid black;
  display: table;
  margin: 0 auto;
  padding: 5px;
}
<h1 class="heading">Heading 1</h1>
like image 160
Mohammad Usman Avatar answered Jan 09 '23 04:01

Mohammad Usman