Div slicing with CSS

Is it possible to slice a div from either side, for example, taking a portion of the left side and making it transparent while keeping everything else in place?

For instance, if there is a div named "box" with CSS styling:

#box {
    width:100px;
    height:100px;
    background:red;
}

I've searched for solutions but only found ways to create transparent border corners. Can a specific area of a div be sliced or made transparent?

Thank you for any assistance.

EDIT

https://i.sstatic.net/4Lg9a.png

This image represents what I'm trying to achieve - making the 50px on the left side of the second div transparent so it appears behind the first div. Using z-index doesn't seem to work for this situation.

Answer №1

One great solution for your current issue could be utilizing the z-index property, which requires a different positioning than static but can effectively adjust the layering order of your elements. If you are experiencing problems such as unclickable items, it's possible that an invisible element is overlapping them - right-click and inspect element to identify if this is the case.

I have included a brief code snippet below to demonstrate how z-index functions. While increasing z-index values consecutively might not be ideal in practice, it serves its purpose for this example.

An essential tip to bear in mind is to opt for positive z-index values whenever feasible. Negative values may cause elements to sink beneath certain layers like the body or pseudo-elements, leading to confusion. Positive numbers generally make more sense to our human brains!

var divs = document.querySelectorAll('div');
var index = 4;

for(var i = 0; i < divs.length; i++) divs[i].addEventListener('click', function(){
  this.style.zIndex = ++index;
});
div {
  width: 200px;
  height: 200px;
  background: grey;
  z-index: -1;
  position: fixed;
  left: 100px;
  top: 100px;
}
div + div {
  z-index: 0;
  left: 120px;
  top: 120px;
  background: red;
}
div + div + div {
  z-index: 1;
  left: 140px;
  top: 140px;
  background: blue;
}
div + div + div + div {
  z-index: 2;
  left: 160px;
  top: 160px;
  background: yellow;
}
<div>-1</div>
<div>0</div>
<div>1</div>
<div>2</div>

For further information, refer to the documentation here: https://developer.mozilla.org/en/docs/Web/CSS/z-index

If you need clarification on any specific issues you're facing, please let me know as I want to ensure I address them accordingly.

Answer №2

To achieve this effect, you can utilize the linear-gradients property along with the after pseudo-element.

I employed the linear-gradient feature to create a div that is partially transparent, and then I utilized an after pseudo-element to conceal half of the transparent area.

body{
    background:url('http://lorempixel.com/500/500');
    background-size:cover;
}
div{
    width:300px;
    height:300px;
    border:2px solid;
    background:linear-gradient(transparent 50%,orange 50%);
    position:relative;
}

div:after{
    position:absolute;
    content:"";
    width:50%;
    height:50%;
    z-index:-1;
    background:orange;
    left:50%;
}
<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

What is the best way to set conditions for document side script?

I'm struggling to disable the horizontal scroll when the viewport width is 480px or less. The script that controls the scroll behavior on my website looks like this: <script> $(function () { $("#wrapper").wrapInner("< ...

How can I add text to an HTML5 SVG similar to using the HTML5 <p> tag?

I am currently working on creating dynamic rectangular boxes and I am facing some difficulties with inserting text into the shapes. The SVG text requires setting x and y coordinates in separate text tags, and doesn't have built-in width and height pro ...

What is the best way to repair a table header?

How do I make the table header fixed so only the table body scrolls? * { padding: 0px; margin: 0px; } body { padding: 0px; margin: 0px; } .responsive { max-width: 100%; height: auto; } /* Other CSS code here */ /* Include necessary exter ...

I attempted to utilize the <map> tag in creating a circular image that functions as a hyperlink, but unfortunately, it is not functioning correctly

I've been trying to create a round button using the code below, but something seems to be wrong with it. The issue I'm facing is that the area around the image is also clickable, even though I have used border-radius. Can someone please take a lo ...

Connect a responsive div to a main div

I'm relatively new to the world of Javascript, CSS, and HTML. Currently, I'm attempting to anchor a div to the center and bottom of its parent div. The parent div contains a responsive background image and my fa fa-arrow icon is correctly positi ...

Arrange various numbers in a single row to be in line with

I'm utilizing bootstrap in an attempt to design the layout depicted below: https://i.sstatic.net/0w06U.png Here is a simplified version of my code: .curr { font-size: 12px; font-weight: 700; letter-spacing: .5px; } .price { -webkit-tap-h ...

Switching a conditional className to a styled component

Currently, I am in the process of converting my project from plain CSS to styled components using styled-components. So far, I have successfully converted all of my components except for one. I have looked at various examples on Stack Overflow but none of ...

What is the best way to contain my content within a bordered box?

As a beginner in HTML and CSS, I have successfully created a basic website with a simple menu at the top that includes links to different pages like "Home," "About," and "Contact." The website will mainly feature information and images, but I believe it wo ...

Issue: Stylesheet not being loaded on Index.html through Gulp

Issue The .css file is not being loaded by the index.html despite setting up a gulp.js file. It seems like the folder directory might be causing the problem. However, the .scss files in the /src/scss folder are compiling correctly into CSS within the /bui ...

In which location are HTML files stored within WordPress plugins?

Can someone help me locate the HTML files within WordPress plugins? I'm looking to make edits to the HTML and CSS files of a plugin, but I can't seem to find the specific HTML file. Any guidance on where these files are stored would be greatly ap ...

What steps can be taken to resolve the issue of "Uncaught TypeError: undefined is not a function"?

Check out the JSfiddle link for reference: http://jsfiddle.net/mostthingsweb/cRayJ/1/ Including my code below along with an explanation of what's going wrong: This is a snippet from the HTML header, showing all the necessary links: <head> ...

The dimensions of the image are not conforming to the div's size

I am experiencing an issue with a div that contains an image. The image is not respecting the size of the div and is overlapping it. .bs-col-md-6 { box-sizing: border-box; -ms-flex-positive: 0; flex-grow: 0; 0; */ -ms-flex-negativ ...

Is there a way to organize an array of elements into three columns using PHP?

My array of menu items is being displayed on the screen in 3 columns, but I want them to appear differently without altering the HTML structure. Currently, they are listed like this: 1 2 3 4 5 6 7 8 9 I would like them to display as follows: 1 4 7 ...

Dropdown with multiple selections organized in a hierarchical structure

I am in need of the following : Implement a drop-down menu that reflects hierarchical parent-child relationships. Include a checkbox for each node to allow for selection. If all child nodes are selected, their parent node should be automatically ch ...

Alignment of content layout across two wrapper elements

My goal is to achieve a specific layout where each pair of cards in a two-column layout is aligned based on the card with the largest content. Both cards share the same content layout and CSS. If you have any ideas or implementations that could help me ac ...

Creating an interactive draggable box with jQuery-UI is as easy as pie

Is it possible to make the innerDropzone div draggable in the following code snippet? div.example { width: 560px; height: 750px; display: block; padding: 10px 20px; color: black; background: rgba(255, 255, 255, 0.4); -webkit-border ...

The appearance of my website appears differently depending on the resolution

Just recently, I began exploring the world of HTML/CSS/JS and created a handy tool for my personal use. However, I encountered an issue when viewing it on different resolutions which caused some elements to look distorted. The tool I made allows me to inp ...

Develop a JSON structure that represents an array of dynamically generated fields in PHP

Need help converting data from a PHP form into a specific JSON format. Struggling to create the right array that can be easily converted into JSON format. Here is a snippet of the array structure: Array ( [user_id] => [title2] = ...

Elements appearing distorted in Firefox and Internet Explorer

I'm completely new to the world of web development and I am attempting to construct a website for my company all by myself. However, I am encountering an issue with a "red info box" on one of my pages. While it displays perfectly on Chrome, it appear ...

Formatting text and images in CSS

Looking to align images at the bottom of each column in a div with 3 columns, but struggling due to varying text lengths. Any tips on how to achieve this? https://i.sstatic.net/fuPgl.png Thank you, Carolin #content_row{ margin-top:150px; marg ...