Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 3905

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T07:03:07+00:00 2024-11-26T07:03:07+00:00

Two ways to add text over images

  • 61k

A great thing to learn if you're a beginner to CSS is how to add text over images, after all, you see this type of feature in almost every website. So I'll be showing you two ways to do this.

Standard Way

  1. create a div or any other element and style it your way
<div class="example">  </div>  
Enter fullscreen mode Exit fullscreen mode

.example {     width: 187px;     height: 347px;     border-radius: 10px;   font-family: Poppins; }  
Enter fullscreen mode Exit fullscreen mode


2. put an image in a div with a position of absolute and an object-fit of cover. You should also make sure that the image has the same width and height as the parent.

<div class="example">         <img src="https://images.unsplash.com/photo-1643678063167-1916c49968e6?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=387&q=80">     </div>  
Enter fullscreen mode Exit fullscreen mode

.example > img {     width: inherit;     height: inherit;     object-fit: cover;     position: absolute;     border-radius: inherit; } /*Note: the inherit property value is used  to give something the same value as its parent*/ 
Enter fullscreen mode Exit fullscreen mode


3. add a text container with the same width and height as the parent but a position of relative. Fill in that container with the text and headers you want. position your text by adding flex to your container, then use either flex-start, center, or flex-end to position at either the top center or bottom of the parent.

<div class="example">         <img src="https://images.unsplash.com/photo-1643678063167-1916c49968e6?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=387&q=80"> <!--text container-->       <div class="exampleInfo"> <!--I also put the text in one smaller container so i can center the header-->         <div class="info1">             <h4>The Florida Keys</h4>           <p>Plane arriving at May 15th, 6am</p>         </div>       </div>     </div>  
Enter fullscreen mode Exit fullscreen mode

.exampleInfo {     color: white;     width: inherit;     height: inherit;     position: relative;     border-radius: inherit;     background: linear-gradient(180deg, rgba(0, 0, 0, 0) 14.06%, #1E1E1E 100%); /*Placing the text at the bottom*/   display: flex;   align-items: flex-end;   text-align: center;   font-size: 20px }   .info1 > h4 {   display: flex;   align-items: center;   justify-content: center; }  
Enter fullscreen mode Exit fullscreen mode

Background Image

Another way to add text over an image is by setting the image as the background. Here's an example on how to do so.

  1. Create a div or any other element with the content and text you want, then style it.
       <div class="exampleTwo">       <h2>Climb the highest mountains</h2>          </div> 
Enter fullscreen mode Exit fullscreen mode

.exampleTwo {   width: 187px;   height: 347px;   border-radius: 10px;   text-align: center;   font-family: Aboreto; } 
Enter fullscreen mode Exit fullscreen mode


2. Give that element a background-image using the url() value. Make sure it fits using background-size: cover.

.exampleTwo {   width: 187px;   height: 347px;   border-radius: 10px;   text-align: center;   font-family: Aboreto; /*Adding the image*/     background-image: url('https://images.unsplash.com/photo-1683727610671-646dbf56aedf?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=387&q=80'); background-size: cover; }  
Enter fullscreen mode Exit fullscreen mode

Which is better

Even though the background image has way has less hTML and CSS needed, if you don't pick a big enough background image then that background image won't be responsive to large sized deives such as pc's. So whichever one is better is your choice, I usually use background image but for major projects i'll use the standard way from time to time.


Thank you for reading! It's been a while since the last time i've been going to the DEV community since I had state testing, but thank goodness that's over. You'll see me again talking about making a minimalistic website, have a great day/night 👋.

beginnerscsstutorialwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.