I have a page with a full-screen background that has some blocks with a black background on it. I would like to achieve that the text in those black blocks looks like it's cut out from the black box so you can see the background image.
Below is a simplified version of the HTML and CSS set-up:
.background {
background-image: url(https://picsum.photos/200/300);
height: 100vh;
width: 100vw;
}
.blackbox {
background-color: black;
padding: 30px;
display: inline-block;
margin-top: 100px;
margin-left: 400px;
}
.text {
font-size: 40px;
font-weight: bold;
font-family: 'Helvetica', 'Arial', sans-serif;
background-clip: text;
-webkit-background-clip: text;
color: transparent;
background-image: url(https://picsum.photos/200/300);
}
<div class="background">
<div class="blackbox">
<div class="text">TEXT</div>
</div>
</div>
If I move the background image to the child text element, the clipping works but it doesn't match with the background-image on the parent element. I thought this question had my answer but the child element doesn't have a background.
This should be responsive. mix-blend-mode is what you're looking for:
Source (https://www.w3schools.com/howto/howto_css_cutout_text.asp)
.background {
background-image: url(https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/800px-Image_created_with_a_mobile_phone.png);
background-size: cover;
position: relative;
height: 200px;
}
.text {
background-color: black;
color: white;
font-size: 5vw;
font-weight: bold;
margin: 0 auto;
padding: 0 15px 0 15px;
text-align: center;
position: absolute;
height: 200px;
line-height: 200px;
mix-blend-mode: multiply; /* This makes the cutout text possible */
}
<div class="background">
<div class="text">TEXT</div>
</div>
Related
I am working out a page that will have a quick-paced shuffle of images on one side, and would like to put bold and visually-attractive text that will lay over a corner of it as a mask and invert the colours of whatever is behind it. Are there any ways to style the text to do this in css?
I haven't been able to find any common solutions or workarounds for what I'm looking to achieve, only how to invert a selected colour.
This should work. mix-blend-mode: difference; makes the trick. I attached a working snippet.
.container {
position: relative;
height: 300px;
width: 400px;
display: flex;
}
.color-1 {
width: 33%;
background-color: red;
}
.color-2 {
width: 33%;
background-color: violet;
}
.image {
width: 33%;
background-image: url(https://picsum.photos/200/300);
}
p {
margin: 0;
width: 100%;
position: absolute;
font-family: 'Helvetica', sans-serif;
font-weight: 600;
text-align: center;
font-size: 50px;
top: 100px;
left: 0;
right: 0;
color: white;
mix-blend-mode: difference;
}
<div class="container">
<div class="color-1"></div>
<div class="color-2"></div>
<div class="image"></div>
<p>Inverting Text</p>
</div>
use a background color for the text
add a shadow below the text to enhance the contrast https://www.w3schools.com/cssref/tryit.php?filename=trycss3_text-shadow
h1 {
text-shadow: 1px 1px red;
}
Find a color that works well with all the images
Use mix-blend-mode: difference; https://css-tricks.com/methods-contrasting-text-backgrounds/
<!DOCTYPE html>
<html>
<head>
<style>
* { margin: 0; padding: 0 }
header {
overflow: hidden;
height: 100vh;
background: url(https://cdn.britannica.com/70/191970-050-1EC34EBE/Color-wheel-light-color-spectrum.jpg) 50%/ cover
}
h2 {
color: white;
mix-blend-mode: difference;
font: 900 35vmin/35vh cookie, cursive;
text-align: center
}
</style>
</head>
<body>
<header>
<h2 contentEditable role='textbox' aria-multiline='true'>And stay alive...</h2>
</header>
</body>
</html>
Need help setting the header size so that accommodate the image and text.
I've tried changing the background size using the "background-size" property but it doesn't change.
header {
background-color: red;
background-size: 200px 100px;
}
header h1 {
font-size: 50px;
text-align: center;
text-decoration-line: underline overline;
text-decoration-style: double;
position: relative;
margin-top: -100px;
margin-bottom: 100px;
}
header .pic {
border: 5px #000 outset;
border-radius: 50px;
}
<header>
<img class="pic" src="" alt="" width="200px">
<h1>Placeholder Text</h1>
</header>
The header background appears until the middle of the text and image when I want it to be on the bottom of the text and image (whichever is lower).
try this :
header {
background: url("img/index.jpg") ;
background-color: red;
background-size: 200px 100px;
}
header h1 {
font-size: 50px;
text-align: center;
text-decoration-line: underline overline;
text-decoration-style: double;
position: relative;
margin-top: -100px;
margin-bottom: 100px;
}
header .pic {
border: 5px #000000 outset;
border-radius: 50px;
}
<header>
<h1>Placeholder Text</h1>
</header>
When you want a background image in a container you do something like this:
header {
background-image: url('https://picsum.photos/800/400');
background-size: cover;
background-repeat: no-repeat;
height:100vh;
width: 100vw;
}
h1 {
color: black;
font-size: 50px;
text-align: center;
}
Instead of using a normal image tag, you declare you background image in the CSS.
See a working example here: https://codepen.io/Angel-SG/pen/dwOEvy
I think you might be looking for the display: flex
Try this
/* Reset
*******************/
* { margin: 0; padding: 0; box-sizing: border-box; }
img { max-width: 100%; }
/* Styles
*******************/
header { background-color: red; display: flex; align-items: center; justify-content: space-between; padding: 1rem; }
header .pic { width: 200px; }
header h1 {
font-size: 50px;
text-align: center;
text-decoration-line: underline overline;
text-decoration-style: double;
position: relative;
}
<header>
<img class="pic" src="https://sg.fiverrcdn.com/photos/105746482/original/3e2a4ad867ed23117cfda223391c35ab42bb99fc.png">
<h1>Placeholder Text</h1>
</header>
I have code that display and contain 2 lines, and act like a Link.
I try to move the Second line to the nearest point at the top of the First line- but without success.
I try via margin-bottom:20px and margin-bottom:-20px ,but all it does, it covers the image with blue and not move close the Second line to the top first line.
How do i move it up?
Here's the HTML code:
<div class="elements-promo">
<div class="elements-promo__copy-wrapper">
<div class="elements-promo__content">
<a class="fill" href="example.com/">
<h2 class="elements-heading ">Top Ten Working Tools</h2>
<div class="elements-heading2">Best tools ever!</div>
</a>
</div>
</div>
</div>
Here's the CSS code:
.elements-heading2 {
color: #fff;
font-family: arial;
font-size: 20px;
}
.fill
{
display: block;
height: 90%;
width: 100%;
text-decoration: none;
}
h2
{
font-family: arial;
}
.elements-promo {
background-color: #f93d66;
background: linear-gradient(145deg, #4772d9, #5be0e0);
border-radius: 4px;
height: 62px;
}
.elements-promo__copy-wrapper {
-ms-flex: 0 0 75%;
flex: 0 0 75%;
margin: 6px 0px;
background-position: 100% left top;
background-repeat: no-repeat;
max-height:60px;
}
#media (min-width:160px) {
.elements-promo__copy-wrapper {
background-image: url("//cdn.pbrd.co/images/HghWJ2p.png")
}
}
.elements-promo .elements-heading {
color: #fff;
padding-top:5px;
}
.elements-promo__content {
padding-right:15px;
text-align:right;
}
Here's a Live one: JSfiddle
This will work for you.
.elements-heading2 {
color: #fff;
font-family: arial;
font-size: 20px;
position: relative;
top: -19px;
}
I am trying vertical align an image in a div. I have a div which displays a coloured background and I need to place other objects inside that div, but centred vertically.
I have craeted a sjfiddle to try and explain.
JSFiddle
First of all you should get rid of the margin-top: -60px in .room-name . Then there are two texts, not just one. Take a look at the settings below, I think that might be what you want (?) The essential part for the centering is the relative position, to: 50% and transform: translatey(-50%), but also the background position for the background image.
#wrapper-new {
width: 100%;
}
#record-section {
width: 100%;
float: left;
height: 100%;
border-bottom-width: 1px;
border-bottom-style: solid;
border-bottom-color: #333;
height: 80px;
}
#room-section {
width: 20%;
background-color: #E9E9E9;
display: inline-block;
float: left;
height: 80px;
}
.direction-image {
display: inline-block;
background-repeat: no-repeat;
background-image: url(https://s2.postimg.org/te7o9w9ix/orbitor_small_17.png);
background-position: 0 center;
position: relative;
top: 50%;
transform: translateY(-50%);
}
.room-name {
font-family: Tahoma, Geneva, sans-serif;
font-size: 30px;
font-weight: bold;
color: #006;
display: inline-block;
margin-left: 100px;
width: 200px;
float: left;
margin-right: 30px;
}
.floor-name {
font-family: Tahoma, Geneva, sans-serif;
font-size: 26px;
font-weight: bold;
color: #666;
}
<div id="wrapper-new">
<div id="record-section">
<div id="room-section">
<div class="direction-image">
<div class="room-name">Box
<div class="floor-name">Ground</div>
</div>
</div>
</div>
</div>
</div>
To vertically center children you just need to add display: flex and align-items: center for element immediate parent and all its children will be centered vertically.
Using your markup it will be something like (also removed negative top-margin from your styles):
#wrapper-new {
width: 100%;
}
#record-section {
width: 100%;
float: left;
height: 100%;
border-bottom-width: 1px;
border-bottom-style: solid;
border-bottom-color: #333;
height: 80px;
}
#room-section {
width: 20%;
background-color: #E9E9E9;
display: inline-block;
float: left;
height: 80px;
}
.direction-image {
margin-top: 8px;
vertical-align: middle;
background-repeat: no-repeat;
background-image: url(https://s2.postimg.org/te7o9w9ix/orbitor_small_17.png);
}
.room-name {
font-family: Tahoma, Geneva, sans-serif;
font-size: 30px;
font-weight: bold;
color: #006;
/* remove display: inline-block; */
display: flex; /* new */
align-items: center; /* new */
/* remove margin-top: -60px; */
margin-left: 100px;
width: 200px;
float: left;
margin-right: 30px;
}
.floor-name {
font-family: Tahoma, Geneva, sans-serif;
font-size: 26px;
font-weight: bold;
color: #666;
}
<div id="wrapper-new">
<div id="record-section">
<div id="room-section">
<div class="direction-image">
<div class="room-name">Box
<div class="floor-name">Ground</div>
</div>
</div>
</div>
</div>
</div>
If you don't want to use flexbox (which is not supported by older browsers) you can add line-height: 100px (or some other number) (if parent has height 100px)
.box{
height:100px;
width:100px;
color:white;
background-color:red;
line-height:100px;
}
<div class="box">
Text inside box
</div>
Try flexbox: JSFiddle.
<div id="wrapper-new">
<div id="record-section">
<div id="room-section">
<div class="direction-image">
<div class="room-name">Box
<div class="floor-name">Ground</div>
</div>
</div>
</div>
</div>
.wrapper {
width: 200px;
height: 200px;
display: flex;
align-items: center;
background-color: #FFFFFF;
}
You can change how you vertically align inner items by changing the wrapper's align-items property to center.
I am trying to make a responsive tweet button with the twitter bird floated left, the text next to it and centered.
My code is:
.flex-rectangle {
float: left;
margin: 0 5px;
max-width: 500px;
text-align: center;
width: 200%;
background: #FFFFFF;
border: 7px solid #00A5EF;
}
/* Styles Twitter Bird png */
.image-wrapper {
padding-top: 10%;
position: relative;
width: 100%;
padding-bottom: 10%;
}
img .tweet {
float: left;
}
/* Tweet This: */
.span-content {
display: block;
color: #00A5EF;
}
.span {
display: block;
text-align: center;
font-family: OpenSans;
font-size: 36px;
color: #00A5EF;
}
<div class="flex-rectangle">
<div class="image-wrapper">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/281152/Twitter_bird_logo_2012.svg" class="tweet" />
</div>
</div>
<div id="buttons">
<div class="span-content">
<span>Tweet This</span>
</div>
</div>
CSS
I've tried pretty much everything under the sun.
I can't seem to get the rectangle to shrink and widen when I resize the page or go into Dev Tools and use the mobile device pane.
I understand CSS less than I do JavaScript at this point. Not sure if I should use flexbox in this instance or how I would do that.
Here is the CodePen
you can use quotes using pseudo element ::before and a::after
Thank you. This works for the most part. However I can't get the
twitter bird to float left and the text to be beside it. Any
suggestions?
I used flexbox the text will be next to the twitter button on desktop view, and below on mobile view.
#import url(https://fonts.googleapis.com/css?family=Open+Sans|Satisfy);
/*Styles for whole page */
img {
max-width: 100%;
border: 7px solid #00a5ef;
}
#page-wrap {
display: flex;
flex-wrap: wrap;
justify-content: center
}
h1 {
font-weight: bold;
text-transform: uppercase;
font-size: 30px;
margin-top: 50px;
width: 300px;
line-height: 1;
font-family: 'Open Sans', sans-serif;
color: #1485C7;
text-align: center;
letter-spacing: 0;
}
/* On: */
h1 .center {
text-transform: capitalize;
font-weight: normal;
font-family: "Satisfy";
vertical-align: text-bottom;
line-height: 10px;
color: #1485C7;
}
h1 .bigger {
font-size: 46px;
color: #1485C7;
display: block
}
/* Rectangle 1: */
.flex-rectangle {
background: #fff none repeat scroll 0 0;
flex: 1 15%;
margin: 0 15%;
max-width: 300px;
padding: 10px;
position: relative;
quotes: "\201C""\201D";
text-align: center;
top: 0;
}
.flex-rectangle::before {
color: #00a5ef;
content: open-quote;
font-family: Georgia;
font-size: 25vw;
left: -15vw;
position: absolute;
top: 50%;
}
.flex-rectangle::after {
color: #00a5ef;
content: close-quote;
font-family: Georgia;
font-size: 25vw;
position: absolute;
right: -15vw;
top: 50%;
}
.text {
align-self: flex-end
}
.span-content {
display: inline-block;
color: #00A5EF;
margin: 0 auto;
padding: 5px;
border: 3px solid #00A5EF;
}
<div id="page-wrap">
<div class="flex-rectangle">
<div class="heading">
<h1>Random Quotes<span class="center">On</span><span class="bigger">Design</span></h1>
</div>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/281152/Twitter_bird_logo_2012.svg" class="tweet" />
<div id="buttons">
<div class="span-content">
Tweet This
</div>
</div>
</div>
<div class="text">
<h1>Random Quotes</h1>
</div>
</div>
you have to place the bird and the text to one div and code for the image element in order to code for the image part you have to call first the first parent div and other div in one code where the image element is located .flex-rectangle .image-wrapper imgto edit the code for image. and also you have to insert the html code for <span>Tweet This</span> inside the .image-wrapper to make the image go left and your text go center.
CSS CODE :
.flex-rectangle {
float: left;
margin: 0 5px;
max-width: 500px;
text-align:center;
width: 200%;
background: #FFFFFF;
border: 7px solid #00A5EF;
}
/* Styles Twitter Bird png */
.image-wrapper {
padding-top: 10%;
position: relative;
margin: auto;
max-width: 125;
max-height: 50px;
width: 100%;
padding-bottom: 15%;
}
.flex-rectangle .image-wrapper img {
float: left;
max-width: 50px;
max-height: 50px;
width: 100%;
}
/* Tweet This: */
.span-content {
display: block;
text-align: center;
color: #00A5EF;
}
.span {
display: block;
text-align: center;
font-family: OpenSans;
font-size: 36px;
color: #00A5EF;
}
HTML Code:
<div class="flex-rectangle">
<div class="image-wrapper">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/281152/Twitter_bird_logo_2012.svg" class="tweet"/>
<div id="buttons">
<div class="span-content">
<span>Tweet This</span>
</div>
</div>
</div>
</div>