Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making part of a div transparent

Tags:

html

css

Example Image

I'm trying to do something like this in HTML. It's a header, at the very top of the page. The checkerboard area must be transparent.

I can't think of a good way to do this without using like 15 divs.

like image 332
Tycho Avatar asked Feb 05 '13 21:02

Tycho


People also ask

How do I make part of a div transparent?

Simply just layer the div's and place the images as background images in your CSS. It's clean, neat and very easy to acomplish what you are looking for.

How do I make a section transparent in CSS?

To set the opacity of a background, image, text, or other element, you can use the CSS opacity property. Values for this property range from 0 to 1. If you set the property to 0, the styled element will be completely transparent (ie. invisible).

How do I make a div transparent but not text?

The percentage of opacity is calculated as Opacity% = Opacity * 100 To set the opacity only to the background and not the text inside it. It can be set by using the RGBA color values instead of the opacity property because using the opacity property can make the text inside it fully transparent element.


1 Answers

Future Options

The ideal scenario would be to use a single element with no images.

Masking and/or clipping would be helpful in situations like this, though browser support is limited. It does seem that progress has been made on the spec (below) since I first wrote this answer, so that's encouraging.

  • http://www.w3.org/TR/css-masking/
  • http://www.html5rocks.com/en/tutorials/masking/adobe/

Practical Approach

For now, I would go with an image-based solution. It doesn't need to be complex.

I recommend avoiding raster graphics since high-density displays are becoming more and more common (nearly every tablet/smartphone and 4K PC monitors). To accomplish this, SVG will work in most modern browsers and PNG can be used as a fallback.

Demos

  • Here's a demo using a PNG: http://jsfiddle.net/MxspA/6/.
  • Same demo with IE7 support: http://jsfiddle.net/MxspA/9/ (replaces :before and :after with extra elements).

Source for IE8+ Version

<div id="box">     <div id="knockout"></div> </div>  #box{      position: relative;  }  #knockout {     background-image: url(http://i.stack.imgur.com/AXHM0.png);     width: 105px;     height: 180px;     margin: 0 auto;     }  #knockout:before{     content: " ";     position: absolute;     left: -52px;     width: 50%;     height: 100px;     background-color: #000; }  #knockout:after{     content: " ";     position: absolute;     right: -52px;     width: 50%;     height: 100px;     background-color: #000; } 

Images

Here's a transparent PNG to get you started. Someone with basic Adobe Illustrator skills could recreate this as an SVG image, providing the aforementioned high resolution capabilities. An SVG will work nicely as a background image.

enter image description here

like image 141
14 revs Avatar answered Sep 29 '22 11:09

14 revs