Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can a centered <hr> be absolutely placed within a div, without losing its center alignment?

I need this


to be placed 20px from the bottom of the page, but every time I try to give it absolute positioning, it jumps halfway outside the div or it becomes longer.

It was centered using display:flex and justify-content:center. The exact same problem occurs with the socila icons, which need to be at the top on desktop and at the bottom on mobile, but if I give them an absolute position, then they are no longer centered.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is the entire page for you to see what I need, and thank you so much for your time:

<!DOCTYPE html>
<html>
<head>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.6.0.slim.min.js" integrity="sha256-u7e5khyithlIdTpu22PHhENmPcRdFiHRjhAuHcs05RI=" crossorigin="anonymous"></script>
<script src="https://kit.fontawesome.com/234b945d49.js" crossorigin="anonymous"></script>

<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
  font-family: "Lato", sans-serif;
  transition: 0.3s;
}

#main-menu-container{
  right:0;
}

.sidenav {
  padding:0;
  height: 100%;
  margin:0;
  width: 0px;
  position: fixed;
  z-index: 1;
  top: 0px;
  right: 0;
  background: radial-gradient(#3d77a7, #264c6d);
  overflow-x: hidden;
  transition: 0.5s ease-in-out;
  text-align: center;
}

.sidenav ul{
    list-style: none;
    padding: 150px 0 0 0;
    text-align:center;
    display: inline-block;
}

.sidenav ul a {
  padding: 8px 8px 8px 8px;
  text-decoration: none;
  font-size: 25px;
  text-transform: uppercase;
  color:white;
  font-weight:bold;
  display: block;
  transition: 0.3s;
}

.sidenav ul a:hover {
  color: #ffdc00;
}

.sidenav .closebtn {
  position: absolute;
  top: 0;
  right: 20px;
  font-size: 36px;
  margin-left: 50px;
  color:#ffdc00;
}

#main {
  transition: margin-right .5s;
  padding: 16px;
}

/* @media screen and (max-height: 450px) {
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
} */

#nav-icon3 {
  width: 40px;
  height: 30px;
  position: fixed;
  top:20px;
  right:20px;
  margin: 0;
  -webkit-transform: rotate(0deg);
  -moz-transform: rotate(0deg);
  -o-transform: rotate(0deg);
  transform: rotate(0deg);
  -webkit-transition: .5s ease-in-out;
  -moz-transition: .5s ease-in-out;
  -o-transition: .5s ease-in-out;
  transition: .5s ease-in-out;
  cursor: pointer;
}

#nav-icon3 span{ 
  display: block;
  position: absolute;
  height: 5px;
  width: 100%;
  background: #ffdc00;
  border-radius: 1px;
  opacity: 1;
  left: 0;
  -webkit-transform: rotate(0deg);
  -moz-transform: rotate(0deg);
  -o-transform: rotate(0deg);
  transform: rotate(0deg);
  -webkit-transition: .4s ease-in-out;
  -moz-transition: .4s ease-in-out;
  -o-transition: .4s ease-in-out;
  transition: .4s ease-in-out;
}

/* Icon 3 */

#nav-icon3 span:nth-child(1) {
  top: 0px;
}

#nav-icon3 span:nth-child(2),#nav-icon3 span:nth-child(3) {
  top: 12px;
}

#nav-icon3 span:nth-child(4) {
  top: 24px;
}

#nav-icon3.open span:nth-child(1) {
  top: 18px;
  width: 0%;
  left: 50%;
}

#nav-icon3.open span:nth-child(2) {
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
}

#nav-icon3.open span:nth-child(3) {
  -webkit-transform: rotate(-45deg);
  -moz-transform: rotate(-45deg);
  -o-transform: rotate(-45deg);
  transform: rotate(-45deg);
}

#nav-icon3.open span:nth-child(4) {
  top: 18px;
  width: 0%;
  left: 50%;
}

.social-icons {
    display: flex;
    justify-content: center;
    position:absolute;
    top: 20px;
}

.social-icons i{
    color:#ffdc00;
    padding:5px;
    transition: 0.3s;
}
.social-icons i:hover{
    color: white;
}
.menu-separator {
/*     position:relative;
    bottom:50px; */
    border: none;
    border-radius: 2px;
    height: 7px !important;
    opacity: 100%;
    color: #ffdc00; /* old IE */
    background: #ffdc00; /* Modern Browsers */
    width: 80%; 
    margin: 20px auto 20px auto;
}

.menu-additional-links{
    display: flex;
    justify-content: center;
    width: 80%;
    margin: auto;
}
.menu-additional-links a{
    text-decoration: none;
    padding: 20px 15px 20px 15px;
    color: white;
}
.menu-additional-links a:hover{
    color: #ffdc00;
}

/*MOBILE BREAKPOINTS*/
@media only screen and (max-width: 768px) {


}

</style>
</head>


<body>
<div id="main-menu-container">
    <div id="mySidenav" class="sidenav" role="navigation">
        <div id="nav-icon3">
            <span></span>
            <span></span>
            <span></span>
            <span></span>
        </div>
        <ul>
            <li><a href="#">About</a></li>
            <li><a href="#">Services</a></li>
            <li><a href="#">Clients</a></li>
            <li><a href="#">Contact</a></li>
        </ul>
        <div class="social-icons">
            <a href="#"><i class="fab fa-facebook-square fa-2xl"></i></a>
            <a href="#"><i class="fab fa-instagram-square fa-2xl"></i></a>
            <a href="#"><i class="fab fa-linkedin fa-2xl"></i></a>
        </div>
        <hr class="menu-separator">
        <div class="menu-additional-links">
            <a href="#">Datenschutzerklärung</a>
            <a href="#">Impressum</a>
        </div>
    </div>
</div>


<div id="main">
  <h2>Sidenav Push Example</h2>
  <p>Click on the element below to open the side navigation menu, and push this content to the right.</p>
  <span class="breakpoint"></span>
  
</div>

<script>
    //check screen width via javascript, and pass a diffrent width to the nav menu 
    const mediaQuery = window.matchMedia('(min-width: 768px)')
    let menuWidth = "";  
    if (mediaQuery.matches){menuWidth = "500px";} else {menuWidth = "100vw";}
    function handleScreenChange(e){
        if (mediaQuery.matches){
        menuWidth = "500px";
        } else {
            menuWidth = "100vw";
        }
    }      
    mediaQuery.addListener(handleScreenChange);

    //set the width and play the animation on hamburger click
    $("#nav-icon3").click(function() {            
        if (document.getElementById('nav-icon3').classList.contains("open")){
            document.getElementById("mySidenav").style.width = "0";
            document.body.style.backgroundColor = "rgba(0,0,0,0)";
        }
        else{
            document.getElementById("mySidenav").style.width = menuWidth;
            document.body.style.backgroundColor = "rgba(0,0,0,0.5)";
        }
        document.getElementById('nav-icon3').classList.toggle("open");
    });

</script>
   
</body>
</html> 

>Solution :

You can try with this styles to hr

position: absolute;
bottom: 20px;
margin-left: 10%;

You have 80% for the width, so a left of 10% makes hr centered

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading