Narrowing the width of a line-broken <h1> heading

I have encountered a problem that can be best described with two images provided below.

Let's focus on the relevant CSS snippet:

h1 {
    display: inline-block;
    background-color: pink;
}

The first example shows an <h1> tag with a short title that does not wrap. The background color of the tag only extends up to the width of the text it contains. This is the desired appearance for all titles.

In contrast, in the second image, the text has wrapped, causing the tag's width to be 100% of the container instead of just the text width itself. Even though the text almost fills the container, there is still some extra space present.

My main inquiry is understanding the mechanism behind this behavior. Does wrapped text always result in width: 100%;? Are there any workarounds to avoid this limitation?

You can see the same issue demonstrated in this example fiddle.

Your insights are greatly appreciated. Thank you!

Answer №1

You may consider modifying your css as follows:

h1 {
    display: inline;
    background-color: pink;
}

Check out this JSFiddle for reference

After the changes, it will now appear like this:


Note: If you are not a fan of the display:inline approach, an alternative is to enclose your text within a span element and apply the background color to that element instead.

For example:

h1 {
  display: inline-block;
}

span {
  background-color: red;
}
<div class="container">
  <h1><span>Mine eyes have seen the glory of the coming of the lord</span>
  </h1>
  <h1><span>frog</span></h1>
</div>

Here's Your Updated JSFiddle link

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The navigation menu links that are meant to stay fixed at the top of the page are not functioning

On my website, I implemented Foundation's Magellan sticky menu at the bottom. Strangely, when I view the site in Firefox, the menu functions properly. However, when I try to open it in Google Chrome, the links don't work (and the cursor pointer d ...

Using the `ng-repeat` directive as an argument in a Javascript function

I am struggling to pass the ng-repeat element to a JavaScript function, but it's not working as expected. <tr ng-repeat="x in myArry"> <td><input type="text" value="{{x.firstname}}" id="{{x.firstname}}{{$index}}" onblu ...

Spin the text within a div by 90 degrees and align it to the right side

I have been attempting to create an information box that contains a simple info text div. On the right side of this info box, I want to include a text labeled "more info" within a nested div to signify that the info box is interactive. To save space, I wou ...

What steps can I take to ensure my radio button functions according to my specified conditions?

Check out this code: <div className="form-group row mb-3"> <div className="col-sm-2" style={{ fontSize: "1rem" }}> Test:{" "} </div> <div className="col-sm-4"> <div ...

Eclipse Content Assist feature appears to be malfunctioning as it fails to display

Having an issue with Eclipse on Win 10 (64bit) when working with JavaScript projects. I've tested this problem on both Eclipse Photon and version 2018-09 (64 bit versions) and encountered the same problem on both instances: After creating a new JavaS ...

Customizing CSS for Various Browser Sizes

I have a query regarding window-dependent CSS. It seems to be a common issue, but I am facing some difficulties with it on Wordpress. I am currently using the script recommended by Nettuts. My goal is to apply different CSS styles based on the user's ...

Transition the object in smoothly after the slide has changed

How can I make the h4 tags fade in after the divs slide into view, while also adding the class "current" to each visible slide? Check out the example on JSFiddle here. <div class="slider"> <div class="slides"> <div class="slide ...

Is it possible to implement a custom radio tab index without using JavaScript

Is it possible to apply the tabindex attribute on custom radio buttons, hide the actual input element, and use keyboard shortcuts like Tab, Arrow Up, and Arrow Down to change the value? Check out this example on StackBlitz ...

Insert JavaScript into this HTML document

I am looking to integrate the "TimeCircles JavaScript library into my project, but I am facing some challenges in doing it correctly. I plan to include the necessary files at the following location: /js/count-down/timecircles.css and /js/count-down/timecir ...

Transferring Data through the POST Method

Is there a way for me to send just one variable using the post method? I have retrieved the $row[id] from the database and need to include it in the form submission. We know how to send user input using dfs, but what about sending the specific $row[id] v ...

Can you explain the intended function of .reference in the CSS code provided?

Currently delving into some sample HTML from codrops. I came across the following CSS styling. After scouring the web, it seems others have used span.reference, but I can't seem to grasp the purpose of .reference. Any insights would be much appreciat ...

What are some other options to using position: absolute when working within an inline position:relative?

I've been struggling with this problem for some time now and just can't seem to find a solution. The issue involves a series of position: relative spans enclosing text, along with a position: absolute span set to right: 0;. I expected the second ...

How to style font display in PHP Select Menu with two different font sizes

Here is the code for my dropdown menu. I'm trying to figure out how to make the text inside the option value have a smaller font size. I attempted using a span tag to adjust the font size, but it didn't work as expected. <select name="met ...

Overlaying images with non-rectangular shapes

I am struggling to achieve a specific design for my bootstrap card. The card is filled with an image and I have placed text on top of it, which is made visible by using a transparent overlay. I would like the overlay to be slanted, similar to the effect se ...

How do three buttons display identical content?

I have three buttons on my website, each with its own unique content that should display in a modal when clicked. However, I am experiencing an issue where regardless of which button I click, the same content from the last button added is displayed in the ...

Step by step guide on incorporating two background images in a single header

I'm looking to create a header with a background image that appears twice, like this: To achieve the effect, I added opacity of 0.5 to one of the images, but it is affecting everything in my header including text and logo. Is there a way to prevent t ...

Starting a div programmatically and then running into trouble when trying to close it, an error was encountered with an end tag for "div" without a corresponding start tag

I am looking to create the following HTML structure: <div id="my-grid" class="isotope"> <div class="myProject-item">....</div> <div class="myProject-item">....</div> <div class="myProject-item">....</div> ...

Is there a way for me to modify the color of the font?

I have written a code to display a tree structure and I want the text to be in Black color. However, it is appearing in White color, even though I did not mention white anywhere. In the body section, I clearly specified black color but it is still showing ...

Is AJAX malfunctioning while the success function is functioning properly?

the function for success is operational, however the data isn't being saved in the database $(document).ready(function() { $("#ChatText").keyup(function(e){ if(e.keyCode == 13) { var ChatText = $("#ChatText").val(); ...

Angular 2 Form Error: Control Not Found

I'm facing an issue with Angular 2 Forms where adding more than one control seems to be getting ignored. Despite following numerous guides on how to properly implement this, none of the suggested methods seem to work in my case. In my template, I hav ...