--- title: 'position: מיקומים' slug: Web/CSS/position tags: - Position - css positions - מיקום - מיקומים ב-CSS translation_of: Web/CSS/position ---
{{CSSRef}}
מיקומים הינם חוקי CSS המאפשרים לסדר אלמנטים שונים ולשלוט באופן התצוגה שלהם באמצעות מאפיינים שונים:
{{Cssxref("top")}} ,{{Cssxref("right")}}, {{Cssxref("bottom")}} {{Cssxref("left")}}
 
{{EmbedInteractiveExample("pages/css/position.html")}}

סוגי מיקומים

אלמנט ממוקם הוא אלמנט שערכו המחושב הוא ערך יחסי (relative), מוחלט (absolute), קבוע (fixed) או דביק (sticky) וכל דבר שאינו סטטי (static).

{{csssyntax}}
static
זהו מאפיין ברירת המחדל הממקם את האלמנט בהתאם לכללים הסטנדרטים של הדף, כמו כן למאפיין {{cssxref("z-index")}} אין השפעה על סדר הדף.
relative
האלמנט ממוקם בהתאם לזרימה הרגילה של המסמך ולאחר מכן מזיז את עצמו באמצעות הערכים top, right, bottomleft. הזזתו אינה משפיע על מיקומם של מרכיבים אחרים ולכן, המרחב שניתן עבור האלמנט בפריסת הדף זהה לזה של המיקום הסטטי.
absolute
האלמנט מוסר מהזרימה הרגילה של המסמך, לא נוצר מרווח יחודי עבור האלמנט אותו הוא מייצג. האלמנט ממוקם ביחס לאלמנט relative הקרוב ביותר אליו, במידה ואין כזה הוא ימוקם באופן יחסי לאלמנט האב הראשון של המסמך. ניתן להזיז אותו באמצעות הערכים top, right, bottomleft.
fixed
האלמנט מוסר מהזרימה הרגילה של המסמך, לא נוצר מרווח יחודי עבור האלמנט אותו הוא מייצג. האלמנט ימוקם באופן קבוע ויחסי לאלמנט האב הראשון של המסמך. ניתן להזיז אותו באמצעות הערכים top, right, bottomleft.
sticky
האלמנט ממוקם בהתאם לזרימה הרגילה של המסמך ולאחר מכן מזיז את עצמו ביחס לאלמנט האב הקרוב ביותר אליו. ניתן להזיז אותו באמצעות הערכים top, right, bottom, left והתזוזה שלו אינה משפיעה על המיקום של שאר האלמנטים. כאשר משתמשים בתכונה זו יש לשים לב שבעת הגלילה האלמנט יסתיר אלמנטים אחרים שנמצאים מתחתיו.

דוגמאות

מיקום אלמנט באמצעות Relative 

HTML

<div class="box" id="one">One</div>
<div class="box" id="two">Two</div>
<div class="box" id="three">Three</div>
<div class="box" id="four">Four</div>

CSS

.box {
  display: inline-block;
  width: 100px;
  height: 100px;
  background: red;
  color: white;
}

#two {
  position: relative;
  top: 20px;
  left: 20px;
  background: blue;
}

{{ EmbedLiveSample('Relative_positioning', '600px', '200px') }}

מיקום אלמנט באמצעות Absolute

<h1>Absolute positioning</h1>

<p>I am a basic block level element. My adjacent block level elements sit on new lines below me.</p>

<p class="positioned">By default we span 100% of the width of our parent element, and we are as tall as our child content. Our total width and height is our content + padding + border width/height.</p>

<p>We are separated by our margins. Because of margin collapsing, we are separated by the width of one of our margins, not both.</p>

<p>inline elements <span>like this one</span> and <span>this one</span> sit on the same line as one another, and adjacent text nodes, if there is space on the same line. Overflowing inline elements <span>wrap onto a new line if possible — like this one containing text</span>, or just go on to a new line if not, much like this image will do: <img src="https://mdn.mozillademos.org/files/13360/long.jpg"></p>
body {
  width: 500px;
  margin: 0 auto;
}

p {
  background: aqua;
  border: 3px solid blue;
  padding: 10px;
  margin: 10px;
}

span {
  background: red;
  border: 1px solid black;
}

.positioned {
  position: absolute;
  background: yellow;
  top: 30px;
  left: 30px;
}

{{ EmbedLiveSample('Absolute_positioning', '100%', 420) }}

 

מיקום אלמנט באמצעות Fixed

HTML

<div class="outer">
  <p>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam congue tortor eget pulvinar lobortis.
    Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Nam ac dolor augue.
    Pellentesque mi mi, laoreet et dolor sit amet, ultrices varius risus. Nam vitae iaculis elit.
    Aliquam mollis interdum libero. Sed sodales placerat egestas. Vestibulum ut arcu aliquam purus viverra dictum vel sit amet mi.
    Duis nisl mauris, aliquam sit amet luctus eget, dapibus in enim. Sed velit augue, pretium a sem aliquam, congue porttitor tortor.
    Sed tempor nisl a lorem consequat, id maximus erat aliquet. Sed sagittis porta libero sed condimentum.
    Aliquam finibus lectus nec ante congue rutrum. Curabitur quam quam, accumsan id ultrices ultrices, tempor et tellus.
  </p>
  <p>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam congue tortor eget pulvinar lobortis.
    Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Nam ac dolor augue.
    Pellentesque mi mi, laoreet et dolor sit amet, ultrices varius risus. Nam vitae iaculis elit.
    Aliquam mollis interdum libero. Sed sodales placerat egestas. Vestibulum ut arcu aliquam purus viverra dictum vel sit amet mi.
    Duis nisl mauris, aliquam sit amet luctus eget, dapibus in enim. Sed velit augue, pretium a sem aliquam, congue porttitor tortor.
    Sed tempor nisl a lorem consequat, id maximus erat aliquet. Sed sagittis porta libero sed condimentum.
    Aliquam finibus lectus nec ante congue rutrum. Curabitur quam quam, accumsan id ultrices ultrices, tempor et tellus.
  </p>
  <div class="box" id="one">One</div>
</div>

CSS

.box {
  width: 100px;
  height: 100px;
  background: red;
  color: white;
}

#one {
  position: fixed;
  top: 80px;
  left: 10px;
  background: blue;
}

.outer {
  width: 500px;
  height: 300px;
  overflow: scroll;
  padding-left: 150px;
}

{{ EmbedLiveSample('Fixed_positioning', '800px', '300px') }}

מיקום אלמנט באמצעות Sticky 

HTML

<dl>
  <div>
    <dt>A</dt>
    <dd>Andrew W.K.</dd>
    <dd>Apparat</dd>
    <dd>Arcade Fire</dd>
    <dd>At The Drive-In</dd>
    <dd>Aziz Ansari</dd>
  </div>
  <div>
    <dt>C</dt>
    <dd>Chromeo</dd>
    <dd>Common</dd>
    <dd>Converge</dd>
    <dd>Crystal Castles</dd>
    <dd>Cursive</dd>
  </div>
  <div>
    <dt>E</dt>
    <dd>Explosions In The Sky</dd>
  </div>
  <div>
    <dt>T</dt>
    <dd>Ted Leo &amp; The Pharmacists</dd>
    <dd>T-Pain</dd>
    <dd>Thrice</dd>
    <dd>TV On The Radio</dd>
    <dd>Two Gallants</dd>
  </div>
</dl>

CSS

* {
  box-sizing: border-box;
}

dl > div {
  background: #FFF;
  padding: 24px 0 0 0;
}

dt {
  background: #B8C1C8;
  border-bottom: 1px solid #989EA4;
  border-top: 1px solid #717D85;
  color: #FFF;
  font: bold 18px/21px Helvetica, Arial, sans-serif;
  margin: 0;
  padding: 2px 0 0 12px;
  position: -webkit-sticky;
  position: sticky;
  top: -1px;
}

dd {
  font: bold 20px/45px Helvetica, Arial, sans-serif;
  margin: 0;
  padding: 0 0 0 12px;
  white-space: nowrap;
}

dd + dd {
  border-top: 1px solid #CCC;
}

{{ EmbedLiveSample('Sticky_positioning', '500px', '300px') }}

מפרטים

Specification Status Comment
{{SpecName('CSS2.1', 'visuren.html#propdef-position', 'position')}} {{Spec2('CSS2.1')}}  
{{SpecName('CSS3 Positioning','#position-property','position')}} {{Spec2('CSS3 Positioning')}} Adds sticky property value.

תאימות לדפדפן

{{Compat("css.properties.position")}}