Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Position fixed changes container width

Tags:

html

css

I am trying to prevent the header element from scrolling, so I gave it position: fixed but that changes it size as per the second image below.
I am after a css to maintain the look of the first image and prevent the header from scrolling. Thanks

enter image description here

html, body {
    height: 100%;
    padding: 0;
    margin: 0;
}

button {
    height: 1.5em;
    width: 1.5em;
    font-size: 1.5em;
    top: 0;
}

label {
    display: inline-block;
    width: calc(100% - 5em);
    text-align: center;
}

header {
    border-bottom: 1px solid black;
    background-color: yellow;
    position: fixed;
}
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="index.css">
    <meta name="viewport" content="width=device-width" />
</head>

<body >
<header>
    <button class="menuLeft" type="button" >&#9776;</button>
    <label>Title of Page</label>
    <button class="menuRight" type="button">&#8942;</button>
</header>


</body>
</html>
like image 209
Fred J. Avatar asked Dec 18 '22 20:12

Fred J.


2 Answers

Just give width:100% to header.

html, body {
    height: 100%;
    padding: 0;
    margin: 0;
}

button {
    height: 1.5em;
    width: 1.5em;
    font-size: 1.5em;
    top: 0;
}

label {
    display: inline-block;
    width: calc(100% - 5em);
    text-align: center;
}

header {
    border-bottom: 1px solid black;
    background-color: yellow;
    position: fixed;
  width: 100%;
}
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="index.css">
    <meta name="viewport" content="width=device-width" />
</head>

<body >
<header>
    <button class="menuLeft" type="button" >&#9776;</button>
    <label>Title of Page</label>
    <button class="menuRight" type="button">&#8942;</button>
</header>


</body>
</html>
like image 136
ketan Avatar answered Dec 21 '22 08:12

ketan


Simply add width:100%; to the header, like the following solution:

html, body {
  height: 100%;
  padding: 0;
  margin: 0;
}
button {
  height: 1.5em;
  width: 1.5em;
  font-size: 1.5em;
  top: 0;
}
label {
  display: inline-block;
  width: calc(100% - 5em);
  text-align: center;
}
header {
  border-bottom: 1px solid black;
  background-color: yellow;
  position: fixed;
  width:100%;
}
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="index.css">
    <meta name="viewport" content="width=device-width" />
  </head>
  <body >
    <header>
      <button class="menuLeft" type="button" >&#9776;</button>
      <label>Title of Page</label>
      <button class="menuRight" type="button">&#8942;</button>
    </header>
  </body>
</html>
like image 34
Sebastian Brosch Avatar answered Dec 21 '22 09:12

Sebastian Brosch