To achieve this effect, one common method is to utilize a semi-transparent PNG file. This allows for the desired visual outcome.
If, for any reason, a PNG file cannot be used (such as needing to use a JPG) for the background of the navigation bar, an alternative approach involves creating a pseudo element within the navigation bar.
You can apply both `relative` and `absolute` positioning, as demonstrated below, to position the pseudo element inside and behind the navigation element. By adjusting the styling with `opacity`, you can effectively achieve the transparent look:
body {
background: blue;
}
nav a {
color: red;
background: white;
}
nav {
padding: 1rem;
box-sizing: content;
/* Necessary for proper placement of the pseudo element */
position: relative;
}
/* Define pseudo-element for nav element */
nav::before {
content: "";
display: block;
position: absolute;
top: 0;
left: 0;
/* Stretch to fill nav */
width: 100%;
height: 100%;
/* Position it behind nav content */
z-index: -1;
/* Adjust transparency level */
opacity: 0.5;
/* Specify the background image */
background: url(https://i.imgur.com/BZ6HTf5.jpg);
}
<nav>
<a href="#">Menu item</a>
<a href="#">Menu item</a>
<a href="#">Menu item</a>
</nav>