What is the technique for refining the exterior of an element while remaining within its parent container?

I need help with two elements that I have set up like this.

.parent {
  height: 300px;
  width: 300px;
  background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/6/6b/Icecat1-300x300.svg/2048px-Icecat1-300x300.svg.png");
   background-position: center; 
  background-repeat: no-repeat;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  border: 3px pink solid;
}
.children {
  height: 200px;
  width: 200px;
  border: 3px blue solid;
}      
<div class="parent">
  <div class="children"></div>
</div>

Is there a way to apply a filter on the outside of children, like filter-outside: opacity(0.5);, but keep it inside the parent element?

Answer №1

If you're looking to achieve a particular effect with a filter, there's a workaround that can give you similar results. One option is to add a shadow to the parent element using the 'inset' property.
Simply include the line

box-shadow: inset 0 0 0 47px #ffffff8a;
in the style of your .parent class.
Keep in mind that this method is more of a workaround and may not work perfectly if the image within the parent container changes size.

.parent {
  height: 300px;
  width: 300px;
  background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/6/6b/Icecat1-300x300.svg/2048px-Icecat1-300x300.svg.png");
   background-position: center; 
  background-repeat: no-repeat;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  border: 3px pink solid;
  position: relative;
  box-shadow: inset 0 0 0 47px #ffffff8a;
}
.children {
  background-color: transparent;
  height: 200px;
  width: 200px;
  border: 3px blue solid;
}
<div class="parent">
  <div class="children"></div>
</div>

An alternative approach is to apply the shadow directly to the child element and utilize overflow:hidden; on the parent. This ensures that the effect remains consistent even if the image dimensions change.

.parent {
  height: 300px;
  width: 300px;
  background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/6/6b/Icecat1-300x300.svg/2048px-Icecat1-300x300.svg.png");
   background-position: center; 
  background-repeat: no-repeat;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  border: 3px pink solid;
  position: relative;
  overflow: hidden;
}
.children {
  box-shadow: 0 0 0 100000px #ffffff8c;
  background-color: transparent;
  height: 200px;
  width: 200px;
  border: 3px blue solid;
}
<div class="parent">
  <div class="children"></div>
</div>

To achieve the desired effect, it's recommended to use the box-shadow property on the child element as demonstrated in the second snippet. Remember to include overflow:hidden; on the parent element to ensure the shadow respects the boundaries of the container.

Answer №2

div {
background: url("https://upload.wikimedia.org/wikipedia/commons/0/01/Figure_in_Manga_style.png") transparent no-repeat center center / cover;
    width: 100vw;height: 250px;
  display:flex;align-items:center;justify-content:center;
}

span:nth-child(1) {
width:100%;opacity:0.5;height:100%;
position:absolute;top:0;left:0;
background:white;z-index:0;
clip-path: polygon(0% 0%, 0% 100%, 25% 100%, 25% 25%, 75% 25%, 75% 75%, 25% 75%, 25% 100%, 100% 100%, 100% 0%);
}
<div>
  <span></span>
  <span></span>
</div>

Answer №3

If you wish to utilize an unaltered version of the background:

.parent {
  height: 300px;
  width: 300px;
  background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/6/6b/Icecat1-300x300.svg/2048px-Icecat1-300x300.svg.png");
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  border: 3px pink solid;
  position: relative;
}

.children {
  height: 200px;
  width: 200px;
  border: 3px blue solid;
  background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/6/6b/Icecat1-300x300.svg/2048px-Icecat1-300x300.svg.png");
  background-size: 300px 300px;
  background-position: center center;
  z-index: 2;
}

.parent::before {
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  background-color: rgba(255, 255, 255, 0.5);
  display: inline-block;
  z-index: 1;
}
<div class="parent">
  <div class="children"></div>
</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

Apply the transition property to all elements except for the text inside a specific tag

I'm wondering if it's possible to apply a hover transition effect to everything in this tag except the text within it. The desired effect is for the button to fade away while the text remains visible. Below is the code I have written so far: ul ...

What is the best way to display a Bootstrap alert above all other elements on the page?

I need help with adjusting the placement of my bootstrap alert. Currently, when the input box value is not valid and the button is clicked, the alert shows up below the input box. I would like it to appear in the middle of the page, on top of the text box. ...

Manipulating elements with JavaScript to remove them, while ensuring that the empty space is automatically filled

Recently, I decided to enhance my understanding of JavaScript by experimenting with it on various websites. My goal was to use JavaScript to remove the right bar from a webpage and have the remaining body text automatically fill in the space left behind. ...

Utilizing Cookies within an HTML Page

My current code is functioning perfectly, accurately calculating the yearly income based on the input "textmoney." I have a link to a more advanced calculator for a precise prediction. My goal is to find a way for the website to retain the data input from ...

No matter how many PHP syntax combinations I tried, I still can't seem to update

Before jumping to conclusions about the duplicate question, I want to clarify that this is my last attempt. I've tried all possible syntaxes and there seems to be no error, yet I am unable to update the table. I can insert data into the table without ...

Embarking on the journey of creating mobile web applications - where should you begin?

How can I begin learning to create mobile web apps? I have a keen interest in the design aspects, particularly the effects and animations that give them a native appearance. I suspect there are differences in CSS, HTML, and possibly JavaScript compared to ...

Listener for resizing events in jQuery implemented in a separate script file

One issue I am facing is separating my jQuery code from my HTML page, especially when it comes to the resize event. I have an index.html file and a main.js file where I prefer to keep all of my jQuery code. The problem arises when the resize event does no ...

The html select option tag is automatically closed because of the presence of the "/" character within the dynamic value in JSP and JSTL

<select id="ordersSelect" class="drop-down" onchange="somemethod()"> <c:forEach items="${orders}" var="order" varStatus="orderStatus"> <option value="${order.id}"> ${order.publicId} </option> </c:forEach> </select> ...

(Background-sizing: Full) encompassing margins

Can anyone assist me with the background-size: cover; issue? I'm facing a problem where the background image I set in the CSS is covering the padding of my image box instead of staying inside the padding. Is there a way to use background: url() and b ...

What's the best way to display two checkboxes on a single line?

I am working with checkbox filters in WooCommerce and I want to organize them so that two checkboxes appear in one row, followed by the next two in the second row, and so on. The issue can be seen at this URL. I also want this style to apply to "Product Co ...

Using JavaScript to dynamically insert HTML content and create a toggle effect

Within a div, I have a collection of large images that I am attempting to insert into another div using JavaScript toggle functionality. Please test the code snippet provided below. $(".toggleimages").on("click", function(e) { e.preventDefault(); ...

Creating a transparent background in a three.js canvas: a step-by-step guide

I came across a wave particle animation on Codepen (https://codepen.io/kevinsturf/pen/ExLdPZ) that I want to use, but it has a white background. However, when I try to set the body background to red, it doesn't show up once the canvas is rendered. I ...

Vanilla JavaScript error: Unable to access property

I am working on implementing a header with a logo and navigation that includes a menu toggle link for smaller viewports. My goal is to achieve this using Vanilla JS instead of jQuery. However, when I click on the menu toggle link, I encounter the followin ...

The image will come to life with animation as the background position is adjusted using Skrollr

Trying to create a div that switches the background image every X pixels scrolled. Initially experimented with changing the background-image, including using sprites and adjusting background position, but encountered a distracting "flickering" effect. Exa ...

Instruments for crafting an application compatible with a wide range of web browsers

Currently tackling various browser challenges. I find myself wondering whether it's achievable to create an application that is compatible with all browsers, or at least the majority. Should it be a web application? Any recommendations on frameworks a ...

Creating a blank webpage after including a component tag for an Angular form

I encountered an issue while developing an Angular form. It seems that using the app-name-editor tag causes my entire HTML page to go blank, and the form does not display. However, removing the tag restores the webpage's functionality. This leads me t ...

Ensuring the height of the body and content div in Bootstrap 4 combined with Angular set to

Is there a way to center the <div class="card"> in the middle of the page or body? It works perfectly on a desktop browser, but on mobile view, it's not aligning properly. How can I center it using... html, body { height: 100%; background: ...

Font-family declaration in CSS is unresponsive to button element

I've encountered an issue with changing the font of a button using CSS. Despite including the necessary declaration, the font does not seem to update as expected. After conducting some research on this problem: I came across this post, but unfortuna ...

What is the process for automatically activating the Enter key once moving to the next text input field?

Hey everyone, I am looking for a way to automatically trigger the enter button after the user switches to another HTML input type='text'. This is necessary for form validation. Below is the code snippet: <input type="text" class="form-contro ...

Refresh the Content of a Page Using AJAX by Forcing a Full Reload

I have a webpage that automatically updates a section using jQuery AJAX every 10 seconds. Whenever I modify the CSS or JavaScript of that page, I would like to include a script in the content fetched via AJAX to trigger a full page reload. The page is ac ...