Make the text part of a div transparent - html

I need to find a way to view the image behind a div through the text in the div:
E.g.
I have the font as a web font. Is this possible without having to do image replacement?

One of the ways to accomplish your specification using only CSS is to overlap the two background images perfectly thereby creating a "transparent" effect that you've described. Here's a fiddle: http://jsfiddle.net/v780v1Ln/.
Note: paddings and such alter the dimensions of the element and affect the coordinates that must be set for background images.
HTML:
<div id = "wrapper">
<h1><span>DRD</span></h1>
</div>
CSS:
* {
padding: 0;
margin: 0;
}
html, body {
height: 100%;
background: #e2e2e2;
}
#wrapper {
position: absolute;
width: 70%;
height: 50%;
top: 25%;
left: 15%;
background: url(http://i58.tinypic.com/2vdieso.jpg)
no-repeat
0 0/500px 362px;
}
#wrapper > h1 {
background-color: #fff;
text-align: center;
position: absolute;
padding: 0 55px 0 25px;
top: 25px;
left: 25px;
}
#wrapper > h1 > span {
font: bold 70px/1 Sans-Serif;
text-transform: uppercase;
background: url(http://i58.tinypic.com/2vdieso.jpg)
no-repeat
-45px -25px/500px 362px;
-webkit-background-clip: text;
background-clip: text;
color: transparent;
}

this worked for me and my project. by stacking the images on top of each other I was essentially able to mirror them even when the position of the box may change
background-image: url("background.jpeg");
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
background-size: cover;
-webkit-text-fill-color: transparent;
-webkit-background-clip: text;
background-clip: text;

Related

Is there a CSS solution for this design?

Here's my issue:
I have a mockup from a design company that wants a text block with a 'broken' square border behind some big text that looks like this (description: there is a small white frame behind large text that is broken up by the text, and then a smaller text link below):
Image of an element on client's website,
In the design, the text is displayed accross the white square frame. The way I have implemented it right now is to make the big text's background color gray. Because the current image's background is gray the desired effect is achieved.
What is needed is to achieve that effect (of breaking the white frame) REGARDLESS of the appearance of the image. Because right now, this happens:
the gray background of the text appears like a box in front of the image -- it ought to be transparent
To further illustrate, if I set the background-color of the big text to transparent, the whole frame is shown (the desired effect is a broken frame):
background: transparent #1
More info if it helps:
The white frame element is just a div with a white border.
I am not sure exactly what to search for in this case, if there is an appropriate CSS solution (preferrable) or if I need to use SVG or maybe a PNG? Thank you for any help.
As #Temani Afif pointed out in the comments, it's not one box, but two separate shapes in CSS.
I made an example to illustrate this using flexbox.
.page {
background-color: black;
width: 400px;
height: 400px;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
.box-top {
width: 100px;
height: 10px;
border-color: white;
border-width: 2px;
border-style: solid;
border-bottom: none;
}
.box-bottom {
width: 100px;
height: 30px;
border-color: white;
border-width: 2px;
border-style: solid;
border-top: none;
}
.separator {
color: white;
width: 100%;
margin: 5px 0;
padding: 0;
font-size: 40px;
text-align: center;
}
<div class="page">
<div class="box-top"></div>
<p class="separator">
Headline
</p>
<div class="box-bottom"></div>
</div>
You can make a square element with a border and use a mask on it:
body {
margin: 0;
min-height: 100vh;
background: black;
box-sizing: border-box;
padding-top: 1px;
}
h2.fancy {
position: relative;
text-align: center;
color: white;
padding-top: 12px;
}
h2.fancy:before {
content: '';
position: absolute;
left: 50%;
top: 0;
transform: translateX(-50%);
width: 100px;
height: 100px;
border: 5px solid white;
clip-path: polygon(0 0, 100% 0, 100% 10px, 0 10px, 0 40px, 100% 40px, 100% 100%, 0 100%);
}
<h2 class=fancy>I'm a fancy title...</h2>
The advantage of this solution is that you can make it scale easily with what might change on various screen sizes. For example, with the title's font-size:
document.querySelector('input.font-size').addEventListener('input', function(e) {
document.querySelector('h2').style.fontSize = e.target.value + 'px';
})
body {
margin: 0;
min-height: 100vh;
background: url(https://picsum.photos/800) center /cover;
box-sizing: border-box;
padding-top: 1px;
}
.overlay {
position: absolute;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,.5);
}
h2.fancy {
z-index: 1;
position: relative;
text-align: center;
color: white;
padding-top: 12px;
}
h2.fancy:before {
content: '';
position: absolute;
left: 50%;
top: 0;
transform: translateX(-50%);
width: 100px;
height: 100px;
display: block;
border: 5px solid white;
clip-path: polygon(0 0, 100% 0, 100% 10px, 0 10px, 0 calc(10px + 1.3em), 100% calc(10px + 1.3em), 100% 100%, 0 100%);
}
input[type=range] {
position: absolute;
bottom: 1rem;
left: 1rem;
z-index: 1;
}
<h2 class=fancy>I'm a fancy title...</h2>
<div class=overlay></div>
<input type=range min=12 max=36 class=font-size>
The disadvantage is that it doesn't work in IE or Edge lower than 18 or in Opera mini. This particular example works in IE 18, though, as it only uses polygon().

Setting background image to div in css leaving an offset in left and right

I have a set of divs in html 5 having class name submenuButton. I want to add a common background image to these divs using css. I tried the following:
.submenuButton{
background-image:url(images/but-up-center.png);
background-position: 5px 0px;
background-repeat:repeat-x;
background-size: contain;
}
This sets the div image leaving 5px from the left and extends to the end of the button. But I want to have this image ended at 5px from the right also.
So, could anybody suggest what can be done in the css so that it leaves 5px space both on the left and the right.
You could use background-clip: content-box; in combination with a 5px padding left and right.
This would give you the following code:
.submenuButton{
padding: 0 5px;
background-image:url(images/but-up-center.png);
background-repeat:repeat-x;
background-size: cover;
background-clip: content-box;
}
More information on background-clip can be found on developer.mozilla.org
Example on jsFiddle
You can do it with a pseudo
.submenuButton {
position: relative;
border: 1px solid red;
height: 100px;
width: 200px;
}
.submenuButton::before {
position: absolute;
content: '';
top: 0;
left: 5px;
right: 5px;
height: 100%;
background:url(http://placehold.it/200/) no-repeat center;
background-size: cover;
}
<div class="submenuButton"></div>

Place div border infront of containing text (with fixed display)

This is what I have:
However, I want the shadow below the text to not appear on top of the border. I can get this effect when my 'position' of the heading is set to anything other than "absolute" or "fixed", but I lose flexiblity in animation... which is what I want to do later.
here is my code:
.feature {
height: 300px;
width: 100%;
background-image: url("http://conceptartworld.com/wp-content/uploads/2013/06/The_Last_of_Us_Concept_Art_Crows_JS-01.jpg");
background-size: cover;
background-repeat: no-repeat;
overflow: hidden;
border-bottom: solid 5px #FFFFFF;
box-shadow: 0 2px 10px #333;
}
.feature h1 {
position: fixed;
font-size: 120px;
vertical-align: text-bottom;
color: #FFFFFF;
margin-left: 50px;
font-family: "oswald", sans-serif;
font-weight: 400;
text-transform: uppercase;
text-shadow: 4px 4px 5px rgba(0,0,0,0.5);
transition: margin 0.5s;
}
#moral {
margin-top: 160px;
}
#studios {
margin-top: 160px;
margin-left: 400px;
}
also... my vertical align text-bottom isn't working...that is why I use the top margin.. can anyone tell me why?
Arman
I think you can't make this effect without making the border line a independent element.
Implement it as an <hr/> and set it z-index greater than the text.

Transparent text over a background?

Is it possible to have transparent text on top of a white background with an image behind it, so that the text appears to be the colour of the image behind it.
Here's a JSFiddle of what I started to give you a better understanding of what I mean:
http://jsfiddle.net/g3SfD/
Right now the text is black, but is there a way to make it transparent so that the colour of the text is that of the background image?
HTML:
<div class="image">
<div class="box">
<div class="text">Sample Text</div>
</div>
</div>
CSS:
.text {
color: black;
font-family: Arial;
font-size: 72px;
font-weight: bold;
}
.box {
display: inline-block;
background: white;
padding: 5px 10px;
position: absolute;
top: 50%;
left: 50%;
margin-left: -219px;
margin-top: -47.5px;
}
.image {
background: url('http://www.menucool.com/slider/prod/image-slider-4.jpg') no-repeat;
width: 960px;
height: 420px;
position: relative;
}
Demo
Ok thanks to this example provided by ralph.m, it seems possible using -webkit-background-clip which would only be supported by browsers that can use webkit.
.text {
font-family: Arial;
font-size: 72px;
font-weight: bold;
text-align: center;
background: url('http://www.menucool.com/slider/prod/image-slider-4.jpg') no-repeat;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-size: 200% 200%;
background-position: -200px -50px;
}
Your background size and position may need tweaking to suit.
I guess you're looking for the rgba() function.
It takes 4 parameters, 3 colors channels and one alpha that controls the opacity of the color.

centering a few links inside a container that is 1000% width?

I have a list of nav items that I want to eventually be able to swipe through so I've created an unordered list which initially has a width of 1000% for testing and the links are positioned inline-block. This works well if there are enough links to fill the whole width of the page but if there is only a few links they get stuck to the left of the nav, ideally what I would like to do is have these centered. Ive tried adding text-align: center to the nav but this centers the links in the 1000% wide space and not the 100% page width. Can anyone recommend a solution as I thought this may be achievable with just CSS
JS Fiddle (with just a few links): http://jsfiddle.net/HS4VE/
CSS
nav {
padding: 10px 0;
overflow: hidden;
text-align: center;
border: 1px solid #cccccc;
border-left: 0;
border-right: 0;
}
nav li {
display: inline-block;
vertical-align: middle;
zoom: 1;
margin-right: -4px;
position: relative;
}
nav a {
color: white;
background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #454545), color-stop(100%, #363636));
background-image: -webkit-linear-gradient(#454545, #363636);
background-image: -moz-linear-gradient(#454545, #363636);
background-image: -o-linear-gradient(#454545, #363636);
background-image: linear-gradient(#454545, #363636);
font-size: 14px;
height: 25px;
line-height: 25px;
padding: 0 15px;
width: 186px;
display: block;
border-right: 1px solid white;
text-align: center;
}
nav a.is-active {
background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#fe57ae), to(#fe329b));
background-image: -webkit-linear-gradient(#fe57ae, #fe329b);
background-image: -moz-linear-gradient(#fe57ae, #fe329b);
background-image: -o-linear-gradient(#fe57ae, #fe329b);
background-image: linear-gradient(#fe57ae, #fe329b);
}
.inner {
width: 1000%;
}
Use this:
.inner {
white-space:nowrap;
max-width: 1000%;
}
Demo: http://jsfiddle.net/HS4VE/3/