2

.main-heading {
  display: block;
  font-family: "Josefin-Sans", sans-serif;
  font-size: 70.6px;
  font-weight: bold;
  text-align: center;
  color: #ba9a45;
}

.subheading {
  font-family: "Cardo", sans-serif;
  font-size: 23.5px;
  text-align: center;
  color: #fff;
  padding-top: 30px;
}
<!-- Headings -->

<div>
  <h1 class="main-heading">ATLANTIC</h1>
  <h1 class="main-heading">CASINO</h1>
  <h3 class="subheading">FEED YOUR DESIRES</h3>
</div>

I seem to not be able to make my h1 and h3 background only as wide as the text and not span the whole width of the line.

How can I reduce the background of the h1 and h3 so it only spans to the end of the text?

--

I've managed to resolve my issue by taking into consideration all the advice given by users below.

Have a look at my revised code which works:

<div class="title-headings">
  <h1 class="main-heading">ATLANTIC</h1>
  <h1 class="main-heading">CASINO</h1>
  <h3 class="subheading">FEED YOUR DESIRES</h3>
</div>


.main-heading{
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: max-content;
  font-family: "Josefin-Sans", sans-serif;
  font-size: 70.6px;
  font-weight: bold;
  color: #ba9a45;
}

.subheading{
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: max-content;
  font-family: "Cardo", sans-serif;
  font-size: 23.5px;
  color: #fff;
  padding-top: 30px;
}
derrickogole
  • 131
  • 10

5 Answers5

2

Headings (<h1>, <h2>, ...) are block-level elements (display: block); if you want these to only take up as much space as the text is, you should set these to display: inline.

uncleserb
  • 39
  • 4
2

You can easily do it by:

.element{
    display: inline-block
}

It will now take up only the width it is supposed to take!

Harsh Goel
  • 166
  • 1
  • 7
1

Whilst the comments and other answers are correct in that - display : inline-block is the correct answer if the text was to be on one line, they do not address the fundamental semantic flaw in the code - you should have only one h1 element and for that to be followed by a h2.

It is important to maintain the correct hierarchy or headings in your page structure.

The way that I would do this is to have the h1 to be display: inline-block and within that to have spans for each word - and to have these spanss a display: block so that they are on separate lines.

Note that I added a red border to demonstrate the layout. And also added a text-align: center to the wrapping div to allow al lthe text to be centered.

div {
  text-align: center;
}

.main-heading {
  display: inline-block;
  font-family: "Josefin-Sans", sans-serif;
  font-size: 70.6px;
  font-weight: bold;
  color: #ba9a45;
  border: solid 1px red
}

.main-heading span {
  display: block;
 }

.subheading {
  font-family: "Cardo", sans-serif;
  font-size: 23.5px;
  text-align: center;
}
<!-- Headings -->

<div>
  <h1 class="main-heading">
    <span>ATLANTIC</span>
    <span>CASINO</span>
  </h1>
  <h2 class="subheading">FEED YOUR DESIRES</h2>
</div>
gavgrif
  • 15,194
  • 2
  • 25
  • 27
  • 1
    I have taken on board everything you said just by fiddling around with my code and this is my result. I just want my code to be as clean as possible. .main-heading{ display: block; margin-left: auto; margin-right: auto; width: max-content; font-family: "Josefin-Sans", sans-serif; font-size: 70.6px; font-weight: bold; color: #ba9a45; } .subheading{ display: block; margin-left: auto; margin-right: auto; width: max-content; font-family: "Cardo", sans-serif; font-size: 23.5px; color: #fff; padding-top: 30px; } – derrickogole Aug 28 '20 at 14:50
  • 1
    What I've basically done is use my classes .main-heading and .subheading to make my content have a max-width of content - not needing to target h1 or h3 specifically in my css. And I have also retained the centre alignment of my content by using margin auto. – derrickogole Aug 28 '20 at 14:56
0

To make a block HTML element not span the full width of its container, change its style.display property to inline-block.

You can do it with CSS like this:

display: inline-block

If you wanted to do it in the element itself, the equivalent would be: style="display: inline-block;"

ed2
  • 1,457
  • 1
  • 9
  • 26
0

On .main-heading change display:block; to display:inline-block;

.main-heading {
  display: inline-block;
  font-family: "Josefin-Sans", sans-serif;
  font-size: 70.6px;
  font-weight: bold;
  text-align: center;
  color: #ba9a45;
}

.subheading {
  font-family: "Cardo", sans-serif;
  font-size: 23.5px;
  text-align: center;
  color: #fff;
  padding-top: 30px;
}
<div>
  <h1 class="main-heading">ATLANTIC</h1>
  <h1 class="main-heading">CASINO</h1>
  <h3 class="subheading">FEED YOUR DESIRES</h3>
</div>
Hamza Zaidi
  • 672
  • 5
  • 8