1

I am trying to horizontally center a fixed and rotated div (orange square). The div should be sticked to the navigation bar and should be responsive. I have tried for a long time but I can't seem to find the right solution. The code I have at the moment is provided below.

Hopefully some of you have the right answer. Thank you in advance!

(P.s. I just joined stackoverflow and everything is still new to me. :P )

HTML:

<!DOCTYPE html>
<html>
<head>
    <title>My test website!</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
    <section id="nav">
        <div class="navbar"> <!-- Navbar Section -->
            <ul id="navcontent">
                <li><a href="#" class="active">Home</a></li>
                <li><a href="#about">About</a></li>
                <a href="#" class="navbarlogo"><img src="img/logo001.png" width="auto" height="40px" alt="Logo"/></a>
                <li><a href="#portfolio">Portfolio</a></li>
                <li><a href="#contact">Contact</a>
            </ul>
            <div id="navsquare">
                <div>
                    <div class="navsquare"></div>
                </div>
            </div>
        </div>
    </section>
    <section id="header"> <!-- Header Section -->
        <div class="header">
            <img src="img/photo002.jpeg" width="100%" height="650" alt="Header image"/>
        </div>
    </section>

CSS:

    #nav {
        z-index: 30;
        position: fixed;
    }

    #header {
        z-index: 10;
        position: relative;
        height: 650px;
    }

    .navbar {
        background-color: #fb7507;
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 63px;
        text-align: center;
        box-shadow: 0px 5px 50px #222;
    }

    #navcontent {
        display: inline-block;
        list-style-type: none;
        margin: 0px 0px 0px 40px;
        padding: 0;
        text-align: center;
    }

    .navsquare {
        height: 115px;
        width: 115px;
        background-color: #fb7507;
        border-radius: 5px;
        -ms-transform: rotate(45deg); /* IE 9 */
        -webkit-transform: rotate(45deg); /* Chrome, Safari, Opera */
        transform: rotate(45deg);
        box-shadow: 0px 5px 50px #222;
        position: fixed;
        left: 50%;
        right: 50%
    }

Screenshot:

http://prntscr.com/b04gty

3 Answers3

0

use this css

 .navsquare {
        height: 115px;
        width: 115px;
        background-color: #fb7507;
        border-radius: 5px;
        -ms-transform: rotate(45deg); /* IE 9 */
        -webkit-transform: rotate(45deg); /* Chrome, Safari, Opera */
        transform: rotate(45deg);
        box-shadow: 0px 5px 50px #222;           
        display: inline-block; 

instead of your

.navsquare {
        height: 115px;
        width: 115px;
        background-color: #fb7507;
        border-radius: 5px;
        -ms-transform: rotate(45deg); /* IE 9 */
        -webkit-transform: rotate(45deg); /* Chrome, Safari, Opera */
        transform: rotate(45deg);
        box-shadow: 0px 5px 50px #222;
        position: fixed;
        left: 50%;
        right: 50%
    }
0

You just change your .navsquare class style

.navsquare{
   height: 115px;
   width: 115px;
   background-color: #fb7507;
   border-radius: 5px;
   -ms-transform: rotate(45deg);
   -webkit-transform: rotate(45deg);
   transform: rotate(45deg);
   box-shadow: 0px 5px 50px #222;
   position: fixed;
   left: 0;
   right: 0;
   margin: auto;

}

Mamun
  • 145
  • 11
0

This is a common problem, you can also find the answer here: Center a position:fixed element

The solution is to add a right: 0;, left: 0; and a margin: 0 auto to your .navsquare. This would end up as follows:

CSS

.navsquare {
    height: 115px;
    width: 115px;
    background-color: #fb7507;
    border-radius: 5px;
    -ms-transform: rotate(45deg); /* IE 9 */
    -webkit-transform: rotate(45deg); /* Chrome, Safari, Opera */
    transform: rotate(45deg);
    box-shadow: 0px 5px 50px #222;
    position: fixed;
    left: 0;
    right: 0;
    margin: 0 auto;
}

Furthermore, you can also use negative margin. Here you use the left: 50%; and a margin-left: half-of-div-width;. This is slightly more complicated in your situation because of the angle of your div, but it's possible with some simple calculation:

CSS

.navsquare {
    height: 115px;
    width: 115px;
    background-color: #fb7507;
    border-radius: 5px;
    -ms-transform: rotate(45deg); /* IE 9 */
    -webkit-transform: rotate(45deg); /* Chrome, Safari, Opera */
    transform: rotate(45deg);
    box-shadow: 0px 5px 50px #222;
    position: fixed;
    left: 50%;
    margin-left: -58px;
}
Community
  • 1
  • 1
Remco van Os
  • 150
  • 14