How do I set a background-color for the width of text, not the width of the entire element, using CSS?
Put the text in an inline element, such as a <span>
.
<h1><span>The Last Will and Testament of Eric Jones</span></h1>
And then apply the background color on the inline element.
h1 {
text-align: center;
}
h1 span {
background-color: green;
}
An inline element is as big as its contents is, so that should do it for you.
The background color isn't stretching the entire width of screen
Wrapping your container with the section will most likely fix the problem.
Instead of writing this;
<section class="about container py-5 my-5 mx-auto">
try this:
<section class="about py-5 my-5">
<div class="container mx-auto">
You probably have a margin set around your container
Also your about
styles should be set to width: 100%
Apply background color to the width of the text and center it
Try the below code (using flexbox):
.order {
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
}
.order h3 {
font-family: Raleway;
color: white;
background-color: blue;
}
.order h1 {
font-family: Raleway;
background-color: blue;
}
<div class="order">
<h3>Hello world</h3>
<h1>How are you doing</h1>
</div>
how to make background fit content and not full width of container in bootstrap
The default behavior of a <div>
is to be a block-level element, which means that it will always take 100% of the available width unless this behavior is overridden. There are a couple of different approaches you could take to resolve this, either change the nature of your <div>
wrapper, or eliminate that entirely and apply the changes to your anchor tag.
Below is an example of what the former might look like using Bootstrap 4 and as few custom styles as possible.
.myPill a {
background: #d3d3d3;
}
.myPill img {
max-width: 50px;
}
<div class="myPill d-flex-inline text-center">
<a href="#null" class="d-inline-block rounded-pill px-3 py-2">
<img class="img-fluid rounded-circle" src="https://lh3.googleusercontent.com/a-/AOh14Gj07ovBA5rvPW7w90kJll5j3_oqFbc1hpADM4F91w" />
<span class="ml-2">User</span>
</a>
</div>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
How to make background-color only cover the size of element?
Use width: fit-content
and it will have the width of what is inside of the element.
.container {
background-color: yellow;
width: fit-content;
}
<div class="container"> random text </div>
Related Topics
Using "Margin: 0 Auto;" in Internet Explorer 8
Making the Main Scrollbar Always Visible
Is Type="Text/Css" Necessary in a ≪Link≫ Tag
How to Change Font-Family of Drop Down's List Item
Add a Horizontal Scrollbar to an HTML Table
Html5 Localstorage Size Limit for Subdomains
Do You Have to Include <Link Rel="Icon" Href="Favicon.Ico" Type="Image/X-Icon" />
Angularjs: Insert HTML from a String
Iframes and the Safari on the iPad, How Can the User Scroll the Content
How to Make My Flexbox Layout Take 100% Vertical Space
Inline Elements and Line-Height
Difference Between ≪Input Type='Submit' /≫ and ≪Button Type='Submit'≫Text≪/Button≫
Required Attribute Not Work in Safari Browser
How to Go Up a Level in the Src Path of a Url in HTML