CSS -zelfstudie

CSS HOME CSS-introductie CSS-syntaxis CSS-kiezers CSS-handleiding CSS-opmerkingen CSS-kleuren CSS-achtergronden CSS-randen CSS-marges CSS-opvulling CSS Hoogte/Breedte CSS-boxmodel CSS-overzicht CSS-tekst CSS-lettertypen CSS-pictogrammen CSS-links CSS-lijsten CSS-tabellen CSS-weergave CSS Max-breedte CSS-positie CSS Z-index CSS-overloop CSS zweven CSS Inline-blok CSS uitlijnen CSS-combinaties CSS Pseudo-klasse CSS Pseudo-element CSS-dekking CSS-navigatiebalk CSS-dropdownmenu's CSS-afbeeldingengalerij CSS-afbeeldingssprites CSS Attr-kiezers CSS-formulieren CSS-tellers CSS-websitelay-out CSS-eenheden CSS-specificiteit CSS !belangrijk CSS wiskundige functies

CSS Geavanceerd

CSS afgeronde hoeken CSS-randafbeeldingen CSS-achtergronden CSS-kleuren CSS-kleurzoekwoorden CSS-verlopen CSS-schaduwen CSS-teksteffecten CSS-weblettertypen CSS 2D-transformaties CSS 3D-transformaties CSS-overgangen CSS-animaties CSS-tooltips Afbeeldingen in CSS-stijl CSS-beeldreflectie CSS-object-fit CSS-objectpositie CSS-maskering CSS-knoppen CSS-Paginering CSS Meerdere kolommen CSS-gebruikersinterface CSS-variabelen Grootte van CSS-box CSS-mediaquery's CSS MQ-voorbeelden CSS Flexbox

CSS Responsief

RWD Intro RWD-kijkpoort RWD-rasterweergave RWD-mediaquery's RWD-afbeeldingen RWD-video's RWD-frameworks RWD-sjablonen

CSS- raster

Rasterintro Rastercontainer Rasteritem

CSS SASS

SASS-zelfstudie

CSS- voorbeelden

CSS-sjablonen CSS-voorbeelden css-quiz CSS-oefeningen CSS-certificaat

CSS- referenties

CSS-referentie CSS-kiezers CSS-functies CSS Referentie Auditief CSS webveilige lettertypen CSS animeerbaar CSS-eenheden CSS PX-EM-converter CSS-kleuren CSS-kleurwaarden CSS-standaardwaarden Ondersteuning voor CSS-browser

CSS -dekking / transparantie


De opacityeigenschap specificeert de dekking/transparantie van een element.


Transparante afbeelding

De opacityeigenschap kan een waarde aannemen van 0,0 - 1,0. Hoe lager de waarde, hoe transparanter:

Woud

dekking 0.2

Woud

dekking 0,5

Woud

dekking 1
(standaard)

Voorbeeld

img {
  opacity: 0.5;
}

Transparant zweefeffect

De opacityeigenschap wordt vaak samen met de :hover selector gebruikt om de dekking te wijzigen bij mouse-over:

Noorderlicht
Bergen
Italië

Voorbeeld

img {
  opacity: 0.5;
}

img:hover {
  opacity: 1.0;
}

Voorbeeld uitgelegd

Het eerste CSS-blok is vergelijkbaar met de code in voorbeeld 1. Daarnaast hebben we toegevoegd wat er moet gebeuren als een gebruiker over een van de afbeeldingen zweeft. In dit geval willen we dat de afbeelding NIET transparant is wanneer de gebruiker erover zweeft. De CSS hiervoor is opacity:1;.

Wanneer de muisaanwijzer van de afbeelding af beweegt, wordt de afbeelding weer transparant.

Een voorbeeld van een omgekeerd hover-effect:

Noorderlicht
Bergen
Italië

Voorbeeld

img:hover {
  opacity: 0.5;
}


Transparante doos

When using the opacity property to add transparency to the background of an element, all of its child elements inherit the same transparency. This can make the text inside a fully transparent element hard to read:

opacity 1

opacity 0.6

opacity 0.3

opacity 0.1

Example

div {
  opacity: 0.3;
}

Transparency using RGBA

If you do not want to apply opacity to child elements, like in our example above, use RGBA color values. The following example sets the opacity for the background color and not the text:

100% opacity

60% opacity

30% opacity

10% opacity

You learned from our CSS Colors Chapter, that you can use RGB as a color value. In addition to RGB, you can use an RGB color value with an alpha channel (RGBA) - which specifies the opacity for a color.

An RGBA color value is specified with: rgba(red, green, blue, alpha). The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque).

Tip: You will learn more about RGBA Colors in our CSS Colors Chapter.

Example

div {
  background: rgba(76, 175, 80, 0.3) /* Green background with 30% opacity */
}

Text in Transparent Box

This is some text that is placed in the transparent box.

Example

<html>
<head>
<style>
div.background {
  background: url(klematis.jpg) repeat;
  border: 2px solid black;
}

div.transbox {
  margin: 30px;
  background-color: #ffffff;
  border: 1px solid black;
  opacity: 0.6;
}

div.transbox p {
  margin: 5%;
  font-weight: bold;
  color: #000000;
}
</style>
</head>
<body>

<div class="background">
  <div class="transbox">
    <p>This is some text that is placed in the transparent box.</p>
  </div>
</div>

</body>
</html>

Example explained

First, we create a <div> element (class="background") with a background image, and a border.

Then we create another <div> (class="transbox") inside the first <div>.

The <div class="transbox"> have a background color, and a border - the div is transparent.

Inside the transparent <div>, we add some text inside a <p> element.


Test Yourself With Exercises

Exercise:

Use CSS to set the transparency of the image to 50%.

<style>
img {
  : ;
}
</style>

<body>
  <img src="klematis.jpg" width="150" height="113">
</body>