Yogesh Chauhan's Blog

How to add transparent text on top of an image using CSS?

in CSS on March 14, 2021

The trick is how we set the position property of the elements and also how we create the transparent background.

HTML

Let’s start with the simple HTML container with the image and texts. To set the seperate style for the texts, we will put the texts in a sub-container.

That will just show an image and texts below the image. Like this:

CSS

Let’s set the HTML document’s box-sizing property to border-box. It will keep the texts sub-container inside the main container and will not let it spread out.

Also, set the container’s position property to relative and set a maximum width. You can adjust the width as per your requirements.

The last and the most important part is to add an appropriate style for the texts container. We need to set the texts container’s position property to absolute. You can adjust the position by changing the value of top property. Adjust the background-color and opacity by changing the values inside rgba().

Final result


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
The SELECT DISTINCT Statement in SQLSQL/MySQLThe SQL EXISTS OperatorSQL/MySQLHow to insert Bootstrap 4 in Angular 9 app?AngularFilling a button background from left to right using CSSCSSLearn to Establish Connection using MySQLi (object-oriented), MySQLi (procedural) and PDO with Example CodePHPHow destructuring works in React?ReactThe difference between 400(4xx) and 500(5xx) errorsMiscellaneousHow to Use the EXISTS and NOT EXISTS Operator with a Subquery in SQL and MySQL?SQL/MySQLthe box-sizing property in CSSCSSHow to create a Progress Bar using JavaScript?JavaScriptCommon Table Expressions (CTE) in PostgreSQLPostgresCreate a menu with a curtain falling animation using CSS and JavaScriptCSS