What is the best way to create a div with a smaller background color than its content?

Can someone help me with adjusting the size of a gray box in this code so that it is smaller than the text inside it?

.media-body {
  background-color: gray;
}
<div class="media-body">
  <h5>Lagoa Azul</h5>
  <span class="texto-media-body">
    Cras sit amet nibh libero, in gravida nulla. Nulla 
    vel metus scelerisque ante sollicitudin.
   </span>
</div>

Answer №1

When dealing with background color for an element, the entire element gets covered by it. Though there is a property called background-size, it specifically works only for background images.

To tackle this issue, you can utilize a pseudo-element to create a background effect.

.media-body {
  position: relative;           /* Allows child elements to be positioned relative to this container */
  border: 1px solid black;
}

.media-body::before {
  content: '';                 /* Sets empty content                      */
  position: absolute;          /* Positions the pseudo-element absolutely */
  height: 90%;                 /* Determines height and width as required */
  width: 90%;
  top: 5%;                     /* Sets desired vertical offset             */
  left: 5%;
  z-index: -1;                /* Moves element behind other elements       */
  background-color: gray;  
}
<div class="media-body">
  <h5>Lagoa Azul</h5>
  <span class="text-media-body">
    Cras sit amet nibh libero, in gravida nulla. Nulla 
    vel metus scelerisque ante sollicitudin.
   </span>
</div>

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

Steps for aligning an image to the right using Bootstrap Vue

I'm currently learning Bootstrap Vue and I'm trying to align an image on the right using a b-card header. The challenge I'm facing is that when I add the image, it disrupts the perfect size of the template header, causing the image to show a ...

Capturing text content in an HTML attribute - using Selenium

In my research, I found that most questions were about extracting text from <p> ... </p> tags. However, what I am curious about is whether Selenium can be used to extract text from an HTML attribute like this: <li id="bullet" someid="1003"& ...

What steps should I follow to have my edit form component extract values from HTML in Angular?

I created a detailed listing page that includes a picture URL, title, phone number, description, and price. When the user clicks on the Edit button, it redirects them to a page with input forms for each of these fields. The form automatically populates the ...

Utilizing Font Awesome icons within a JSON structure

I'm running into a bit of trouble trying to display font awesome icons. The text is written in json using a rakefile, and I'm attempting to insert a font awesome icon within the text. However, I'm facing difficulties because the text is in j ...

Set iframe or form to be in a fixed position

Having issues with my site where an iframe is contained within a div. When only one character is entered in the telephone box, validation fails and red warning text is displayed. Furthermore, after clicking in the email box and pressing tab twice, the fo ...

Changing React Phone Number input field's default style: A step-by-step guide

Is there a way to modify the appearance of the react-phone-number-input component using this npm package: https://www.npmjs.com/package/react-phone-number-input? I am working with React Hook Form and Tailwind CSS alongside this component. However, I' ...

Applying a filter within an ng-repeat loop to act as a conditional

Is it possible to use the ng-repeat as a conditional in an efficient and optimal way? I am wondering if there is a way for code to be shown only if found selected is true, without having to create a new variable in the back-end. <div ng-repeat="c ...

Conceal or reveal buttons using JavaScript

I am struggling with a function that is supposed to hide certain buttons and create a new button. When I click on the newly created button, it should make the previously hidden buttons visible again. However, the function does not seem to work properly. ...

Tips for customizing the appearance of this div using Material UI's card/box components

Hey there! I am currently using a flexible div to house the h1 and arrow image (you can see it https://i.sstatic.net/OgVBp.png). I was wondering if there is a way to utilize a material UI component to give this "black box" a new look? Here is the jsx code ...

Conceal the browser scrollbars

When I have two DIVs on my webpage that are larger than a browser window, it results in a horizontal scrollbar being displayed. How can I hide this browser scrollbar for the first DIV while still showing it for the second one? For example, if I have two ...

HTML5 for Bulk Video Upload

Is it possible to use the html5 api to record and upload video content directly from the browser? I am facing an issue in a project where the video recordings can be very long or large, causing extended upload times for users. Ideally, I would like the vi ...

Finding a solution to this issue in Angular will consistently result in a false outcome

Can anyone explain why I keep getting the error message "This condition will always return 'false' since the types 'typeof ChartType' and 'ChartType' have no overlap" whenever I try to compare charTypeEnum with ChartType.text? ...

Using JavaScript's DOM manipulation to switch out one HTML tag for another

As a newbie to javascript and DOM, I'm facing an issue with manipulating DOM elements using javascript in the given HTML code. <html> <head> <title>Testing</title> </head> <body> <marquee direction=up heig ...

Exploring the Relationship Between Z-Index and Overflow in a Collapsible Div Featuring Slim Select

Here is my demonstration of embedding a Slim Select dropdown within a collapsible div: CodePen Example <html> <head> <!-- MULTIPLE SELECT DROPDOWNS --> <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ ...

When using the v-for directive to display all elements of an array, only the information of the clicked array should be listed when using the

I am facing an issue with my array of locations. There are 2 divs in play - one containing the list of locations and the other displaying additional info when a location is clicked. The problem arises when I click on a specific location, let's say Sc ...

the dropdown menu toggle is not working as expected

My dropdown icon is not appearing and the menu options are not functioning properly. Despite trying to use a script to display the text when an option is clicked, it doesn't seem to be working. It appears that the toggle functionality is not working ...

The text field is being styled with inline styles automatically

I am trying to set a custom width for a textarea in my form, but the inline styles are automatically overriding my CSS. I have checked my scripts, but I am unsure which one is manipulating the DOM. If you have any insight, please let me know. This is the ...

New button attribute incorporated in AJAX response automatically

data-original-text is automatically added in ajax success. Here is my code before: <button type="submit" disabled class="btn btn-primary btn-lg btn-block loader" id="idBtn">Verify</button> $(document).on("sub ...

What is the best way to display an element once an input field with type "date" is populated with a value

Struggling to create a dynamic form that reveals additional fields when a date picker is used? I've searched for solutions, but everything I find involves complex validations and logic that exceed my needs. I've had some success with revealing e ...

Encountering an unspecified variable in Sass

I am a beginner in the world of sass and I am trying to incorporate a variable from a file named "_designs.scss" into another file called "_component.scss". Despite my efforts, I keep encountering an "Undefined variable: $grey-light-2" error. I have exhaus ...