EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

CSS Overlay

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » CSS Tutorial » CSS Overlay

CSS Overlay

Introduction to CSS Overlay

There are many times, we click on a page and another page pops up on the screen and vanishes on clicking. These pop-ups can be advertisements, forms, notifications or any other thing, depending on the requirement of the page. This screen-over-screen effect is  called overlay. Its achieved using a combination of html, CSS and JavaScript.  This can be useful in creation of dynamic pages where the user can control of they want to see the popup screen or not. The concept of overlay will be better understood as we look into how to achieve it step by step.

Syntax

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The procedure to achieve an overlay is done in three steps. Firstly goes the CSS coding for the overlay. Here we can define the type of overlay that is needed and style it accordingly. Next, we call the styling the HTML page. Lastly, we use JavaScript function for the visibility of the overlay.

A sample CSS overlay styling can be as below:

#overlayCSS{
position: fixed;
display: none;
height: 50%;
width: 50%;
background-color: wheat;
opacity: 50%;
cursor: pointer;
}

Note: That additional features can be added in the styling of the overlay element. It totally depends upon the choice of the developers, how they want to design their page. Let’s look into some examples and see how overlay screens work.

Example to Implement CSS Overlay

Below are examples to implement:

1. Overlay Screen using JavaScript and External CSS

  • Since we are using external CSS, we will start by creating the CSS file first.
  • In the CSS file, we will just define the ID, which has the styling property for the overlay element.
  • The final CSS file should look like this:

Code:

#overlayCSS{
position: fixed;
display: none;
height: 50%;
width: 50%;
background-color: wheat;
opacity: 50%;
cursor: pointer;
}

  • Next, we will code the HTML page. Please note that all the magic happens in the HTML page, so we need to code it carefully.
  • First, in the head section we will call for the external Style sheet like this:

Code:

<head>
<title>Overlay Demo</title>
<link rel = "stylesheet" href = "overlay.css">
</head>

  • Next, in the body section, we will first define the JavaScript function, which will handle the appearance and vanishing of overlay screen. The script should look like this:

Code:

<script>
function up() {
document.getElementById("overlayCSS").style.display = "block";
}
function down() {
document.getElementById("overlayCSS").style.display = "none";
}
</script>

  • Now we will finally call the ID created in the CSS file, using an <div> element. We will define its on-click property, such that, clicking on the overlay screen will make it disappear.
  • Next, we will create a button, on click property of which, calls for the function which is responsible for the visibility of the overlay screen. The code for the above two points should look like this:

Code:

<div id="overlayCSS" onclick="down()"></div>
<h2>CLICK TO SEE OVERLAY</h2>
<button onclick="up()">Overlay effect UP</button>

  • The final HTML file should look like this:

Code:

<html>
<head>
<title>Overlay Demo</title>
<link rel = "stylesheet" href = "overlay.css">
</head>
<body>
<script>
function up() {
document.getElementById("overlayCSS").style.display = "block";
}
function down() {
document.getElementById("overlayCSS").style.display = "none";
}
</script>
<div id="overlayCSS" onclick="down()"></div>
<h2>CLICK TO SEE OVERLAY</h2>
<button onclick="up()">Overlay effect UP</button>
</body>
</html>

Popular Course in this category
CSS Training (9 Courses, 9+ Projects)9 Online Courses | 9 Hands-on Projects | 61+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,444 ratings)
Course Price

View Course

Related Courses
Bootstrap Training (2 Courses, 6+ Projects)jQuery Training (8 Courses, 5 Projects)
  • We will save this html page and open it through a browser. The initial page will look like this:

CSS Overlay - 1

  • Clicking on the button will give this overlay screen:

CSS Overlay - 2

  • Clicking anywhere on the overlay screen will make the overlay screen vanish.

2. Overlay through JavaScript using Internal CSS

  • We are using internal CSS in this example. So all the functionalities will be in the HTML file.
  • Firstly, in the head section of the page, we will define the style for the overlay screen by creating an ID. We will also create a class for styling the content that will be on the overlay screen. The style tag will be coded like this:

Code:

<style>
#overlayCSS{
position: fixed;
display: none;
height: 50%;
width:50%;
background-color: cyan;
opacity: 50%;
cursor: default;
}
.content{
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
color: darkblue;
margin: 100px;
}
</style>

  • Next, in the body section, we will create the JavaScript function for visibility and vanishing of the overlay screen. The script tag should be coded as follows:

Code:

<script>
function up(){
document.getElementById("overlayCSS").style.display="block";
}
function down(){
document.getElementById("overlayCSS").style.display="none";
}
</script>

  • Finally, we will create the content, which uses the above defined style and script. We will call div elements and use the ID, class and functions to create the overlay effect. The coding should be as follows:

Code:

<div id="overlayCSS">
<div class="content">TEXT ON THE OVERLAY SCREEN
<br><button onclick="down()">Click to Close overlay</button>
</div>
</div>
<h2>CLICK TO SEE OVERLAY</h2>
<button onclick="up()">Overlay Up</button>

  • The final HTML page should look like this:

Code:

<html>
<head>
<title>Overlay Using Internal CSS</title>
<style>
#overlayCSS{
position: fixed;
display: none;
height: 50%;
width:50%;
background-color: cyan;
opacity: 50%;
cursor: default;
}
.content{
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
color: darkblue;
margin: 100px;
}
</style>
</head>
<body>
<script>
function up(){
document.getElementById("overlayCSS").style.display="block";
}
function down(){
document.getElementById("overlayCSS").style.display="none";
}
</script>
<div id="overlayCSS">
<div class="content">TEXT ON THE OVERLAY SCREEN
<br><button onclick="down()">Click to Close overlay</button>
</div>
</div>
<h2>CLICK TO SEE OVERLAY</h2>
<button onclick="up()">Overlay Up</button>
</body>
</html>

  • Saving this html page and opening it through a browser will fetch the following page as an outcome:

CSS Overlay - 3

  • On clicking the button, an overlay screen will pop-up like this:

overlay screen

  • Clicking on the second button will close the overlay screen and go back to the initial page.

Explanation: In the above example, we explored the basic usage of the overlay screen. This feature or property of HTML and CSS is very crucial. This can be used while creating pop-ups on clicking any element. This pop up can be an advertisement, a form to be filled by the user, any notification, or even a warning. Developers can use this basic code and tweak it accordingly to generate the overlay of their choice. There can be images and other content on the popup or overlay screen too. Make sure you have explored all the offered options.

Recommended Articles

This is a guide to CSS Overlay. Here we discuss an introduction to CSS Overlay with appropriate syntax and respective examples for better understanding. You can also go through our other related articles to learn more –

  1. CSS Border Style
  2. CSS Row
  3. CSS Inner Border
  4. CSS background-color

CSS Training (9 Courses, 9+ Projects)

9 Online Courses

9 Hands-on Projects

61+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
CSS Tutorial
  • CSS
    • Introduction To CSS
    • What is CSS?
    • Uses Of CSS
    • Advantages of CSS
    • Career In CSS
    • CSS Commands
    • Is Css Case Sensitive
    • CSS object-fit
    • Types of CSS Selectors
    • CSS Radio Button
    • CSS Attribute Selector
    • CSS first child of class
    • CSS Selector nth Child
    • CSS Parent Selector
    • CSS Child Selector
    • CSS Not Selector
    • CSS Descendant Selector
    • CSS Inline Style
    • Checkbox CSS
    • CSS Appearance
    • CSS Font Properties
    • CSS font-variant
    • CSS Pagination
    • CSS Table Styling
    • CSS Table Cell Padding
    • CSS Padding Color
    • CSS Text Formatting
    • CSS for Text-Shadow
    • CSS text-stroke
    • CSS text-indent
    • CSS Rotate Text
    • CSS Text Color
    • CSS Center Div
    • CSS Arrow
    • CSS Arrow Down
    • CSS offset
    • CSS Cursor
    • CSS Layout
    • CSS Grid Layout
    • Button in CSS
    • CSS Button Border
    • text-align in CSS
    • CSS Horizontal Align
    • CSS Position
    • CSS Box Sizing
    • CSS box-shadow
    • CSS Text Underline
    • CSS Text Outline
    • CSS Blinking Text
    • Text Decoration CSS
    • CSS Vertical Align
    • CSS Word Wrap
    • CSS Padding
    • CSS Font Color
    • CSS Color Generator
    • CSS Margin Right
    • CSS Margin Color
    • CSS Color Codes
    • CSS Color Transparent
    • CSS Color Chart
    • CSS Link Color
    • CSS z-index
    • CSS Curved Border
    • CSS Border Left
    • CSS left
    • CSS Gradient Generator
    • Radial Gradient in CSS
    • CSS Shape Generator
    • CSS Triangle Generator
    • CSS background-color
    • CSS Background Image
    • CSS background-clip
    • CSS background-blend-mode
    • CSS Drop Shadow
    • CSS line height
    • CSS line break
    • Sticky Footer CSS
    • CSS Header Design
    • CSS Border Style
    • CSS Border Generator
    • Sticky Sidebar CSS
    • CSS Transparent Border
    • CSS Border Radius
    • CSS translate
    • CSS transform
    • CSS 3D Transforms
    • CSS Text Transform
    • CSS Transition Effects
    • CSS Transition Property
    • CSS Animation Transition
    • Negative Margin CSS
    • CSS Navigation Bar
    • CSS Overflow
    • CSS overflow-wrap
    • CSS Lists
    • CSS list-style
    • CSS Order
    • CSS Box Model
    • CSS Inner Border
    • CSS Icon
    • Menu Icon CSS
    • CSS Multiple Borders
    • Opacity in CSS
    • CSS Float Right
    • CSS Clear Float
    • CSS clip
    • CSS disabled
    • CSS Border Padding
    • Border Images in CSS
    • CSS Visibility
    • CSS Validator
    • CSS Clearfix
    • CSS Counter
    • CSS Letter Spacing
    • CSS root
    • CSS zoom
    • CSS calc()
    • CSS.supports()
    • CSS Loader
    • Media Query CSS
    • CSS @keyframes
    • CSS @bottom
    • CSS page-break-after Property
    • CSS page-break
    • CSS Position Fixed
    • CSS skew()
    • CSS Row
    • CSS Masking
    • CSS Scrollbar
    • CSS Overlay
    • CSS Important
    • CSS Cursor Hand
    • CSS Inherit
    • CSS Position Relative
    • CSS Compressor
    • CSS tricks
    • CSS Outline Property
    • CSS Flexbox Properties
    • CSS flex-direction
    • CSS content property
    • CSS Typography
    • CSS Formatter
    • CSS nowrap
    • CSS Column
    • GridView CSS
    • CSS Viewport
    • CSS Minify
    • CSS Combinators
    • CSS in React
    • CSS Matrix 
    • CSS Pseudo Elements
    • CSS Pseudo Classes
    • CSS Pointer Events
    • CSS Resize
    • CSS Inheritance
    • CSS Interview Questions
    • Cheat Sheet CSS
  • CSS3
    • What is CSS3?
    • CSS3 Interview Questions
    • Cheat sheet CSS3
  • sass
    • How to Install SASS
    • SASS Interview Questions
    • What is Sass
    • SASS Comments
    • Sass Variables
    • SASS Import
    • SASS if else
    • SASS Nesting
    • SASS @each
    • SASS @at-root
    • SASS @extend
    • SASS @media
    • SASS @for
    • SASS Map
    • SASS Selectors
    • SASS Color Functions
    • SASS Mixins

Related Courses

CSS Training Course

Bootstrap Training Course

JQuery Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - CSS Training (9 Courses, 9+ Projects) Learn More