Home >Web Front-end >HTML Tutorial >Create a text portrait using CSS?
Adding style to your website is the most important part of creating a website, as it serves as the draw for users when they first visit your website. We can create many types of designs and interactive experiences using CSS. Text portraits can be created using Illustrator or Photoshop to make the design more attractive.
In this article we are going to have a look at how can we generate and create a text portrait using CSS and some JavaScript function so as to achieve our text portrait.
Text portrait is an image that looks like it has text on it, culminating in what an object or person looks like. We will use CSS to implement this text portrait. Let's talk about the methods we're going to use for text portraits.
Here are the steps we follow when creating a text portrait:
Step One - We will first create a document with the required text that you want to add. Suppose we will add the text "hello" and repeat the word using JavaScript's repeat() function.
Step 2 − The second thing we are going to do will be making the background more appealing by setting its color to black and we will also set background image using the URL function. We will be using the property “background repeat” and set its value to “no repeat” so that the image does not get repeated.
Step 3 − Now we will print our text in the image, so we will clip the text and add other properties like changing font and resizing etc.
For using the JavaScript repeat function.
Let’s take a look at the syntax of the Javascript repeat function.
string.repeat(count);
The count variable used in the syntax is a value that will tell the function how many times to repeat the given string, and count ranges from 0 to infinity.
To understand this better let’s, look at an example of creating thee text portrait using CSS.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Example of a text-potrait using CSS</title> <style> p{ background: url( "https://cdn.pixabay.com/photo/2015/10/01/17/17/car-967387_960_720.png"); -webkit-background-clip: text; line-height: 16px; background-position: center; background-repeat: no-repeat; background-attachment: fixed; line-height: 16px; -webkit-text-fill-color: rgba(255, 255, 255, 0); background-size: 76vh; } body { overflow: hidden; background: rgb(236, 236, 236); font-family: "Segoe UI", sans; } h1{ text-align: center; } h2{ text-align: center; } </style> </head> <body> <h1> Welcome to tutorial's point </h1> <h2>This is an Example of a text portrait </h2> <p id="repeat"></p> <script> let str = "Yellow Car "; document.getElementById("repeat").innerHTML = str.repeat(487); </script> </body> </html>
In the above code, we create a paragraph and give it an id, then we write the string to be repeated using JavaScript, then we use the function getElementByid to repeat it 500 times, then We went into the CSS part, added the background image and cropped the text so that only the text in the foreground of the image was visible. We will get a car portrait with a filler text of "Hello, how are you". Let's take a look at the output of the following code.
In the above output you can see that the text is in the foreground and the image of the car is in background we got our output using the JavaScript repeat and setting the value to 500 which means the text will be repeated 500 time in the whole canvas.
The string.repeat() function is a built-in function that builds a new string using a specified number of copies of a string, as shown in the example below
<script> A = "hello"; a=A.repeat(2); document.write(a); </script>
The above code will produce the following output.
This is an example showing how we use the string.repeat property.
Let’s look at another example of a text portrait created using CSS
<!DOCTYPE html> <html lang="en"> <head> <title>Example of a text-potrait using CSS</title> <style> p{ background: url( "https://cdn.pixabay.com/photo/2016/02/23/17/42/orange-1218158__340.png"); -webkit-background-clip: text; line-height: 16px; background-position: center; background-repeat: no-repeat; background-attachment: fixed; line-height: 16px; -webkit-text-fill-color: rgba(255, 255, 255, 0); background-size: 76vh; } body { overflow: hidden; background: rgb(236, 236, 236); font-family: "Segoe UI", sans; } .tut{ text-align: center; background-color:green; color:white; } </style> </head> <body> <div class="tut"> <h1> Welcome to tutorial's point </h1> <h2>This is an Example of a text portrait </h2> </div> <p id="repeat"></p> <script> let str = "Orange is a fruit "; document.getElementById("repeat").innerHTML = str.repeat(600); </script> </body> </html>
In the code above we used several CSS properties to demonstrate how can we create a text portrait. The JavaScript in the above code uses the str.repeat function so as to print the text multiple times in the image.
You can see in the above output that the text has taken the shape of an orange which was our actual image. Now let’s head over to conclude this tutorial.
Creating a text portrait using CSS and a few lines of code, and using the JavaScript function string.repeat, we get our result, first we write the text we want to appear in the foreground and decide how many times the text is repeated. The output we will get also depends on the length of the characters in the text.
In this article, we saw how to create text using just a few lines of CSS code and JavaScript functions.
The above is the detailed content of Create a text portrait using CSS?. For more information, please follow other related articles on the PHP Chinese website!