-1

I need to create a black "bar" on the top. The black stripe must be 10% of the maximum height the browser can reach. I need to lock the stripe to allow page scrolling and keep bar on the page.

This is something similar to youtube top bar.

I tried this:

.Header {
position:fixed;
display: flex;
top:0;
left:0;
width:100%;
height: 10%;
background-color:#333333;
}

but this let me have 10% of the actual maximum size, if I stretch the window the height size changes, I need to lock 10% of maximum size I can reach

user4789408
  • 1,196
  • 3
  • 14
  • 31

3 Answers3

1

I would recommend having fixed heights using media queries to avoid distortion across devices (see https://css-tricks.com/snippets/css/media-queries-for-standard-devices/).

However, if you insist on having it set to 10% of the height on page load, the easiest way would be to use jQuery:

$(document).ready(function () {
    $(".bar").height($(window).height()*0.1);
});

See here for example: https://jsfiddle.net/61hvr079/

user3495690
  • 576
  • 3
  • 15
  • I'm using dreamweaver with fluid and responsive implementations, I am a jQuery newbie, I would avoid using it if there is another solution – user4789408 Dec 01 '15 at 00:06
  • @user4789408 Hmm.. Well, I would still definitely recommend using media queries to avoid the problem all together (and likely avoid problems down the road when screens of all sizes try to visit your website and the contents of .bar are distorted due to the dynamic height setting). But if you're willing to settle for *restricting* the maximum height of the div, you could use the max-height property and set it to a certain number of **px** (or perhaps even **em** if you want it to change for mobile) – user3495690 Dec 01 '15 at 00:13
0

Have you looked at How to make the Navigation bar not move when window scrolled down CSS? ? They recommend position:absolute for better viewing on mobiles.

Community
  • 1
  • 1
kmwarren
  • 1
  • 1
0

If you have a <div class='bar'>, then the CSS to get a full-width, fixed-position black bar that's 10% of view height would be:

    .bar {
     width: 100vw;
     height: 10vh;
     background-color: black;
     position: fixed;
     top: 0;
     left: 0;
   }
Alex Pearson
  • 103
  • 7
  • It doesn't work, I need to have 10% of the size, if I stretch the window the black stripe changes his size, I don't want to – user4789408 Dec 01 '15 at 00:05
  • I'm not sure what you mean by 10% of the size, then, if not view height. Perhaps you're looking for different fixed heights across a few breakpoints? – Alex Pearson Dec 01 '15 at 00:55