Guide to wrapping a container around an inline element using inline pseudoelements, all without relying on absolute positioning

I need a way to group my inline elements together in a container so that I can control their placement and add a background color.

My issue is with two inline block elements, where the inner one has two pseudo elements that are also inline. When I try to wrap these elements in another inline or inline-block element, the background color only applies to the non-pseudo element's text width, ignoring the :before and :after containers.

https://i.sstatic.net/GOhbk.png

Is there a solution to this problem without resorting to absolute positioning? My goal is to have the background color encompass all child (pseudo) elements.

span {
background: green ;
}
.wrap {
  white-space:nowrap;
  display: inline-block;
}
.content:after,.content:before {
  content: " ";
  display: inline-block;
  width: 50%;
  height: 1em;
  padding:0px;
  margin:0px;
  background: red;
}
.content {
   display:inline-block;   
}
<span>
  <div class="wrap">
     <div class="content">
        foo bar baz
      </div>
  </div>
</span>
<BR/>
<span>
  <div class="wrap">
      <div class="content">
        foo bar baz foo bar 
      </div>
  </div>
</span>
<BR/>
<span>  
  <div class="wrap">
      <div class="content">
        foo bar bazfoo bar baz foo bar baz
      </div>
  </div>
</span>

Answer №1

If you're looking to make the red area equal to the green one, CSS grid can help achieve this using the fr unit as shown below:

.content:after,
.content:before {
  content: " ";
  height: 1em;
  background: red;
}

.content {
  display: inline-grid;
  white-space: nowrap;
  grid-template-columns: 1fr 2fr 1fr;
  align-items: center;
  background: green;
  margin:5px;
}
<div class="content">
  foo bar baz
</div>
<br>

<div class="content">
  foo bar baz foo bar
</div>
<br>
<div class="content">
  foo bar bazfoo bar baz foo bar baz
</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

Choosing HTML elements within nested DIV tags

In the process of creating a Hangman-style game using javascript/jQuery, I encountered an issue with selecting HTML content from virtual keyboard keys : <div class="square keyboard"> <div class="content"> <div class="table"> ...

Leverage CSS to design numerous elements that span the full width of their parent container

Is there a way to create a CSS horizontal menu bar where the <a> elements collectively expand to fill the parent's width? Here is an example of the HTML structure: <div id="parent"> <a href="/">Home</a> <a href="/le ...

HTML and CSS - Aligning text and managing scrollbar visibility

I am currently diving into the world of coding and embarking on the journey of crafting a website for my father. This project not only helps me gain valuable experience but also builds up my portfolio. Utilizing unsemantic and normalize frameworks, I enco ...

Tips for triggering two hover effects simultaneously

I have a collection of images, all of which are intended to display the same hover effect. To achieve this, I implemented two layers: one for the image itself and one for the hover effect. View the code demo here: http://jsfiddle.net/jmXdh/13/ Upon hoveri ...

The fixed positioned div with jQuery disappears when scrolling in Firefox but functions properly in Chrome, IE, and Safari

Click here to see a div located at the bottom of the right sidebar that is supposed to behave as follows: As you scroll down the page, the div should change its class and become fixed at the top of the screen until you reach the bottom of the parent el ...

React: When attempting to import '../node_modules/bootstrap/dist/css/bootstrap.min.css' in the code, an error occurred

I am utilizing the reactstrap library and have managed to install both Bootstrap and Reactstrap successfully. However, I am facing an issue where when I include import '../node_modules/bootstrap/dist/css/bootstrap.min.css';, React does not compi ...

What are the best methods for creating a responsive div container?

Hello, I am having trouble with the responsiveness of my div. I want these two images to stack on top of each other when the window is resized. I have attached a screenshot to demonstrate exactly what I am trying to achieve. I can't seem to figure out ...

Navigating through the options list and returning to the top for a fresh start: a simple guide

I'm looking for a way to make a list of 5 elements automatically scroll through each one every 3 seconds. Once it reaches the last element, I want it to loop back and start again from the first element. Here is what I've tried so far. var heigh ...

Continuously animate a series of CSS properties

Here's the code snippet I'm working with: @keyframes ball1 { 0% { transform: translateX(0px); opacity: 0%; } 50% { opacity: 100%; } 100% { transform: translateX(120px); opacity: 0%; } } @keyframes ball2 { 0 ...

How to precisely position a glyphicon in the center of a navbar

I am struggling to center my glyphicon (with its accompanying text) in the middle of the navbar, ensuring it remains centered and responsive even when the window is resized. I want to prevent it from scrunching together when the window is narrow and avoid ...

What is the best technique for vertically aligning text within two divs placed side by side?

Thank you for taking the time to read this. I have a piece of code similar to the one below. While using line-height works when there is only one line of text, it becomes problematic when the comments section contains multiple lines. I am wondering what w ...

Combining SCSS Code

I have been working in UI development for quite some time, but I haven't had the opportunity to work with Sass. However, for my upcoming project, I will need to use Sass. While I am new to Sass, after reading through the guide, I feel confident that I ...

What is the best way to customize the color and width of div elements on a webpage?

I am trying to customize the colors and widths of multiple div elements using arrays and a for loop in my HTML code. However, I am having trouble figuring out how to properly implement this. Specifically, I want 5 divs on the page with different background ...

Next JS Event Listener Failing to Detect Scroll Events

Currently, I am attempting to change the state and display a shadow in the navigation bar when the user scrolls, but for some reason it is not detecting the event. I am working with nextJS 13 and tailwind css. const [shadow, setShadow] = useState(false) ...

Utilize the v-for second argument in VueJS 2 to showcase the index and index+1

For a VueJS 2 project, I am tasked with enhancing the display of the first and second elements in an array by making them stand out from the rest. To achieve this, I am utilizing the v-for syntax to iterate over a child component within a parent component. ...

What is the best way to center a div within another div without using the margin tag?

I am struggling to find a way to center a container (div) on my webpage. I've tried various methods like using margin:auto, margin:0 auto;, margin-left:auto;, and margin-right:auto;, but none of them seem to work. Additionally, I'm unsure about ...

The alignment of my card icons changes depending on the size of the paragraph

Here are my cards I am attempting to give them a relative position and the parent div an absolute position, but it is not working as expected. Despite several attempts, I still cannot get the desired result of positioning the cards correctly within the p ...

Styling and Script Files on a JQuery Mobile Website

Is it necessary to include CSS and JS files in every HTML page for a mobile site developed with JQueryMobile? <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" /> <script src="http://code.jquery.com/jqu ...

Reorganize content from one div to another when the screen size changes using Bootstrap

Creating a login screen with 2 columns has been my recent project. For large screens: The left column contains the login controls while the right column displays an image. On small screens: The top column shows the image, and the bottom column houses the ...

Display new information within a div element seamlessly without refreshing the page

Just a heads-up, I'm new to HTML development... Currently working on a website using Bootstrap. There are some buttons on the left side shown in the screenshot, and my goal is to update the content on the right without having to reload the entire pag ...