Sravya
Sravya

Reputation: 21

My header div disappearing when added position fixed

bellow are my html and css files where my header div disappearing when added position fixed. i even specified top and left but no luck. please help.

body { 
        margin: 0px;
        padding:0px;
        background-color:whiteSmoke;
    }

    .header {
        height: 60px;
        background-color: pink;
        box-shadow: 3px 3px 5px silver;
        position: fixed;
        top:0;
        left:0;
    }
<!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <link rel="stylesheet" href="style.css">
            <title>the title</title>
        </head>
        <body>
                <div class="header"></div>
        </body>
    </html>

Upvotes: 1

Views: 64

Answers (4)

Arpit Sharma
Arpit Sharma

Reputation: 83

Please add some content or width to header.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="style.css">
        <title>the title</title>
    </head>
    <body>
            <div class="header"></div>
    </body>
</html>


body { 
    margin: 0px;
    padding:0px;
    background-color:whiteSmoke;
}

.header {
    width:60px;
    background-color: red;
    height: 60px;
    background-color: pink;
    box-shadow: 3px 3px 5px silver;
    position: fixed;
    top:0;
    left:0;
}

Running Expample

Upvotes: 0

UncaughtTypeError
UncaughtTypeError

Reputation: 8712

As an alternative to declaring a specific width property to the fixed element, a right property with the value of 0 can also be declared, effectively "expanding" the fixed element width from left-to-right of the screen.

body {
  margin: 0;
  padding: 0;
  background-color: whiteSmoke;
}

.header {
  height: 60px;
  background-color: pink;
  box-shadow: 3px 3px 5px silver;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
}
<body>
  <div class="header"></div>
</body>

Upvotes: 1

KodFun
KodFun

Reputation: 423

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>the title</title>
    <style>
        body {
            margin: 0px;
            padding: 0px;
            background-color: whiteSmoke;
        }

        .header {
            height: 60px;
            width: 100%;
            background-color: pink;
            box-shadow: 3px 3px 5px silver;
            position: fixed;
            top: 0;
            left: 0;
        }
    </style>
</head>
<body>
    <div class="header"></div>
</body>
</html>

In your original code, there is no content in the div with the class "header". Therefore, its width is 0.

I've added "width: 100%" to the class header so that it can gain width and appear.

You can run the code to see the result. I hope it helps.

Upvotes: 1

Martijn
Martijn

Reputation: 16103

Because a fixed element doesn't have a width.
So it has a height of 60px and becaus eyou didnt specify a width, so it appears hidden. Adding content, or something along the lines of width: 100%; will show the div again.

Upvotes: 0

Related Questions