Are there any other CSS properties that can achieve the same functionality as "contain: content;"?

Searching for an alternative to the contain: content; CSS property that is compatible with older browsers. Is there a solution out there? Perhaps a CSS hack or workaround?

Answer №1

Depending on the context in which you are using contain: content, there may be instances where you can replace it with:

overflow: hidden

This change should work fine because contain: content essentially is a shorthand for contain: layout paint.

In terms of visual outcomes, it is contain: paint that closely resembles the effect achieved by overflow: hidden.

Example in Action:

.outer {
  position: relative;
  float: left;
  margin-right: 12px;
  width: 100px;
  height: 100px;
  background-color: rgb(255, 0, 0);
}

.outer p {
  position: absolute;
  top: 0;
  right: 0;
  margin: 3px;
  padding: 0;
  color: rgb(255, 255, 255);
  font-size: 24px;
  font-weight: bold;
}

.outer-3 {
  contain: content;
}

.outer-4 {
  overflow: hidden;
}

.inner {
  width: 50px;
  height: 50px;
  background-color: rgb(255, 255, 0);
}

.inner-2,
.inner-3,
.inner-4 {
  margin-top: 75px;
}
<div class="outer outer-1">
<p>1</p>
<div class="inner outer-1"></div>
</div>

<div class="outer outer-2">
<p>2</p>
<div class="inner inner-2"></div>
</div>

<div class="outer outer-3">
<p>3</p>
<div class="inner inner-3"></div>
</div>

<div class="outer outer-4">
<p>4</p>
<div class="inner inner-4"></div>
</div>

In the scenario provided above:

  1. Outer does not have specific rules applied and Inner has no margin-top
  2. Outer has no special rules while Inner has a margin-top set to 75px
  3. Outer applies contain: content and Inner features a margin-top of 75px
  4. Outer uses overflow: hidden and Inner includes a margin-top of 75px

For Further Information:

Answer №2

Another option is to incorporate data attributes within your HTML and style them using CSS.

Take a look at this solution: How to Select Elements by Data Attribute in CSS

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

Is there a way for the React select component to adjust its width automatically based on the label size?

After defining a React select component, it looks like this: <FormControl> <InputLabel id="demo-simple-select-label">Age</InputLabel> <Select labelId="demo-simple-select-label" id=&quo ...

The animation in my SVG comes to a halt at a certain point

I am having an issue with my SVG element (shown below) where it stops animating after a certain period of time. I want the animation to continue indefinitely like in this jsfiddle. .path { stroke-dasharray: 20; animation: dash 10s linear; } @ ...

The grid feature in the Bones theme is malfunctioning and causing issues

I am currently using the Bones theme from Themble, which has a grid system in its design. I attempted to apply this grid system to the header of my website in order to transform menu items into icons on mobile devices and display the full menu on desktop s ...

Listen up, Javascript keyboard input recorder

I am aiming for the search bar to pop up when you type. The code below is functioning, but I am facing an issue where the search bar pops up even when typing in a different input field, such as the comment input. Is it feasible for the search bar not to ...

When the mouse hovers over DIV2, the background image of DIV1 will be replaced

I have a full-screen background div with the ID #background-change. Within that full-screen background, I have 3 Divs named .fullscreen-column-1, .fullscreen-column-2, etc. My goal is to change the background image of #background-change when the mouseove ...

Adding padding to the top causes the scrollbar to hide at the bottom in Firefox

My HTML page includes the following configured DIV: <div id="contentDiv" style="padding-top: 20px; padding-left: 20px; overflow: auto; height: 100%;" > The content within this DIV is large enough to require a vertical scrollbar. When viewed in IE, ...

Tips for eliminating the gap between Bootstrap 4 columns

Is there a way to eliminate the spacing between Bootstrap columns? I have three columns set up using Bootstrap but no matter what I do, I can't seem to get rid of the space between them. <!doctype html> <html lang="en> <head> ...

HTML/CSS code for a header with elements centered across the full width of the screen

Having an outdated WordPress theme from 2010, I wanted to give it a modern touch by adding a sticky header. I found a plugin called myStickymenu that seemed to work well. However, my current theme's header has a fixed width of 960px and I desired to m ...

When using IndexedDB with a Javascript To-Do list, the dates for all items appear to be the same after adding a new item to the list

I developed a To-Do List using Javascript, HTML, and IndexedDB to store the items in the database so that they won't be deleted when the browser is refreshed. I also want to include the date for each item, however, whenever I add an item, the date end ...

Creating a Date Computation Tool that Adds Additional Days to a Given Date

I have a challenge where Date 0 represents the start date, Date 1 is the result date after adding a certain number of days (NDays). I am looking for help in JavaScript to calculate Date0 + NDays = Date1. Please assist me as my coding knowledge is quite l ...

Efficiently handling jumbled strings in Python: A guide to parsing

Looking to pull out ID numbers and their pass/fail status from an XML file, the data I have is messy and I'm new to Python. Specifically, I have a list of strings with the info and need to extract the codes and status. Currently, I'm struggling w ...

Adaptable triple-column design

After completing the layout of my website, everything seems to be functioning well. However, I would like to ensure that I haven't overcomplicated it or used poor techniques. The structure involves a main DIV with a nested table DIV, inside of which ...

HTML structure consisting of a 3 by 3 grid

Creating a 3x3 grid with cells that have varying heights depending on content is proving to be challenging. Except for the cells marked with red arrows, the rest of the grid has fixed height in pixels. I attempted to build a 3x3 grid using divs to experi ...

Is it possible to activate the :active pseudoclass by pressing the 'enter' key on the keyboard using solely CSS?

The CSS: a:focus { opacity: .75 } a:active { transform: translateY(4px) } The purpose: When keyboard users tab to the link, the :focus style serves as a visual indicator Upon pressing the enter key to activate the link, the :active style provides ...

Tips for expanding the boundary of a Font Awesome icon

The image is in a h1 editing margin of h1 only edits margin-left increases the margin on the left of the image. trying to increase margin of i element does nothing either on i element does nothing either. <h1 class="display-4"><i class="fas fa- ...

Implementing Node.js with browser cache and handling 304 responses

I am currently in the process of creating a single page application with a standard HTML layout as shown below: <html> <head> <title>...</title> <link rel="stylesheet" media="all" type="text/css" href="css/main.css"> ...

Issues with click events in the navigation menu

How can I make my menu close when clicking on other parts of my website, instead of opening? I know that I should use a click event for this, but when I implemented a click event, my menu encountered 2 unwanted problems: 1- Whenever I clicked on a menu i ...

The footer moves upwards when a file is downloaded

I am facing an issue with my website footer and its CSS styling. #footer { position: absolute; bottom: 0; width: 100%; height:8rem; } The main goal is to keep the footer at the bottom of the page, regardless of the amount of content on th ...

Activating Button within Featherlight Modal

This is a follow up question from Featherlight hide div on Close After successfully resolving the issue with opening the lightbox, I have encountered another problem. I have added a submit button that should trigger an on('submit', function) whe ...

Using Paper.js to access and manipulate document.body.style.opacity within a paperscript

My website is essentially one large canvas image. Currently, the body fades in when loaded using this code: <body onLoad="document.body.style.opacity='1'"> However, I want to initiate this fade within my paperscript because sometimes the ...