The Wayback Machine - https://web.archive.org/web/20240717024750/https://www.geeksforgeeks.org/how-to-center-a-div-on-the-edge-of-another-div-in-sass/
Open In App

How to center a div on the edge of another div in SASS ?

Last Updated : 24 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to centre a div on the edge of another div using SASS. SASS is an extension of CSS that stands for Syntactically Awesome Style Sheets. It helps us to create things like variables, nested rules, etc. So that we can reuse the code. 

Centering a div on the edge of another div is not a difficult task. We can achieve it by using position property and bottom, top left and right properties. What we need to see is how Sass can reduce our work here. 

Example: HTML Code: In HTML, we will create two div elements. One that is outside will be called outer div and another will be called inner div.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>
        How to centre a div on the
        edge of another div
    </title>
 
    <link rel="stylesheet"
        type="text/css" href="style.css" />
</head>
 
<body>
    <div id="outer-div">
        <div id="inner-div"></div>
    </div>
</body>
 
</html>


SASS code: Sass is used to give general styling and to centering the inner div on the edge of the outer div. We create a mixin named prop with parameters for properties (height, width, background colour, and position ) so that we don’t need to write the same thing for both div elements. We can simply include this mixin and provide the value for properties.  We use position property and put the left property value to 50%  that will centre the inner div horizontally.  The value of the bottom property  is zero so that the inner div will be on the edge of the outer div. 

SASS




// For properties of both div
@mixin prop ($height, $width, $bg, $pos) {
    height: $height;
    width: $width;
    background-color: $bg;
    position: $pos;
}
 
#outer-div {
    @include prop(190px, 50%, #0057D9, relative);
}
 
// Inner div properties
#inner-div {
    @include prop(70px, 70px, #8ebf42, absolute);
    left: 50%;
    transform: translate(-50%);
    bottom: 0;
    padding: 3px;
}


Output:

Centre a Div on the edge of another div ( GFG)

Example 2: In this example, Centre a div on the side edge of another div. Everything in HTML will be the same. In Sass, we use the top property with a value 50% that will centre the inner div vertically and put the value of left property to zero so that the inner div will be on the side edge. Other than this piece of code rest will be the same. 

SASS




#inner-div {
    @include prop(70px, 70px, #8ebf42, absolute);
    left: 0;
    transform: translate(0, -50%);
    top: 50%;
    padding: 3px;
}


Output:

Centre div on the left edge of another div(GFG)

Example 3 : 

 HTML Code:  In this example, we create four div elements inside our outer div. We will see how to centre all the inner div elements on the edges of our outer div.  

HTML




<!DOCTYPE html>
<html>
 
<head>
    <link rel="stylesheet" type="text/css" href="style.css" />
</head>
 
<body>
    <div id="outer-div">
        <div id="top-div"></div>
        <div id="left-div"></div>
        <div id="right-div"></div>
        <div id="bottom-div"></div>
    </div>
</body>
 
</html>


Sass Code: In Sass, we create a mixin called prop that we include in all div elements. Similarly mixin small div is created for all div elements inside our outer div.  We create another two mixins one is verticalcentre(mixin) that is for centering the div vertically. For centering the div vertically we provide the top value to 50%. In the verticalcentre(mixin), there is an if and else condition, if the value of right is given true then the value of right property will be zero else value of the left property will be zero, which will decide on which side edge it will be on. horizontalcentre(mixin) also have the same logic, but it will centre horizontally. 

SASS




// Mixin for Common  properties of all div
@mixin prop($height, $width, $bg, $pos) {
    height: $height;
    width: $width;
    background-color: $bg;
    position: $pos;
}
 
// Div inside which we will centre another divs
#outer-div {
    @include prop(190px, 400px, #0057D9, relative);
}
 
// Common properties of all small divs
@mixin smalldiv {
    @include prop(50px, 50px, #8ebf42, absolute);
    padding: 3px;
}
 
// Mixin for div that vertically centered
// on the edge
@mixin verticalcentre($right:false) {
    top: 50%;
    transform: translate(0, -50%);
    @if ($right) {
        right: 0;
    }
    @else {
        left: 0;
    }
}
 
// Mixin for div that are horizontally
// centered on the edge
@mixin horizontalcentre($top:false) {
    left: 50%;
    transform: translate(-50%);
    @if($top) {
        top: 0;
    }
    @else {
        bottom: 0;
    }
}
 
// Including mixin in each div
#top-div {
    @include smalldiv;
    @include horizontalcentre($top: true);
}
 
#bottom-div {
    @include smalldiv;
    @include horizontalcentre($top: false);
}
 
#right-div {
    @include smalldiv;
    @include verticalcentre($right: true);
}
 
#left-div {
    @include smalldiv;
    @include verticalcentre($right: false);
}


Output:

Centre div on the edges of another div (GFG)



Similar Reads

How to center a div within another div?
The div tag is used to construct a division or section of an HTML document in which other elements of HTML is placed and that division/section works like a container whose CSS styling can be done as a single unit or javascript can be used to perform various tasks on that container. Syntax: &lt;div&gt; &lt;h3&gt;'This is a div of the web page.&gt;/h
4 min read
How to overlay one div over another div using CSS
Creating an overlay effect simply means putting two div together at the same place but both the div appear when needed i.e while hovering or while clicking on one of the div to make the second one appear. Overlays are very clean and give the webpage a tidy look. It looks sophisticated and is simple to design. Overlays can create using two simple CS
2 min read
How to copy the content of a div into another div using jQuery ?
Given an HTML document containing some div element and the task is to copy a div content into another div as its child using jQuery. There are two approaches to solve this problem that are discussed below: Approach 1: First, select the div element which needs to be copied into another div element.Select the target element where the div element is c
2 min read
Sass sass:list Module
SASS provides many built-in modules that contain various useful functions and some mixins that makes it easy to use. All built-in modules begin with sass: to show that they are different from other modules and a part of SASS itself. One of the built-in modules is the sass:list module that makes it possible to access and modify values in a list. Not
4 min read
Sass sass:color Module
SASS provides many built-in modules that contain various useful functions and some mixins that makes it easy to use. All built-in modules begin with sass: to show that they are different from other modules and a part of SASS itself. One of the built-in modules is the sass:color module. This module is used in order to create new colors from the exis
3 min read
Sass | sass:map module
The sass:map module gives functions that makes it possible to look up the values associated with the key of a map. Some of the major functions are listed below: map.get(): This function returns the value associated with the given key in the map. In case, no value is associated then it returns null. Syntax: C/C++ Code map.get(map, key) map-get(map,
2 min read
SASS sass:string Module
The sass:string module makes it easy to combine, search, or split strings apart. The following are the list of functions that can be used using this module. 1. string.quote() function: This function returns a quoted string. Syntax: string.quote(string) quote(string) Example: @debug string.quote(GeeksForGeeks); @debug string.quote(&quot;GeeksForGeek
3 min read
Differences between HTML &lt;center&gt; Tag and CSS "text-align: center;" Property
If you are designing a simple web page, then not much difference is noticeable, but it is very necessary to understand the basic differences between these two. Since we are only concerned with the text, these elements do not have separate meanings.HTML &lt;center&gt; tag: The &lt;center&gt; tag in HTML is used to set to align of text into the cente
3 min read
How to center a &lt;div&gt; using Flexbox property of CSS ?
In this article, we will learn to center an HTML div element using flexbox property of CSS. Please refer this article to know more about flexbox. Approach: To center the &lt;div&gt; element horizontally using flexbox. We use the property of display set to flex i.e. display: flex;Align items to center using align-items: center;The last step is to se
1 min read
How to center a DIV on screen using jQuery ?
In this article, we are going to take a look at how to center a DIV on the screen using jQuery. There are a lot of times when while developing, you might want to center a div on the website, and using jQuery you can easily achieve the purpose. jQuery is a very popular javascript library that is fast, small, and full of features like HTML DOM traver
3 min read
three90RightbarBannerImg