Фиксированная позиция относительно родительского элемента
Я относительно новичок в CSS. У меня возникла проблема, когда я пытаюсь исправить элемент рядом с его родительским элементом. Я могу сделать это со следующим кодом:
Родительский элемент:
#search_results{
position:relative;
}
Детский элемент:
.total {
position: fixed;
top:10px;
width:250px;
left: 75%;
/*overflow: hidden;*/
margin-left: -125px;
}
Это отлично работает, пока окно браузера не изменится. Когда это происходит, фиксированный элемент перекрывает свой родительский элемент. Вы можете увидеть мою проблему здесь:
Twittiment
Я пытаюсь установить дочерний элемент в начало страницы и правую часть родительского элемента. Любые идеи?
Ответы
Ответ 1
Редактирование:
Вы можете использовать position: sticky;
, который может быть относительно родительского элемента.
body > div {
height: 300px;
background-color: #ddd;
overflow: auto;
margin-top: 70px;
}
div > div {
height: 1000px;
position: relative;
}
span {
display: block;
height: 20px;
background-color: tomato;
position: sticky;
top: 0;
}
<div>
<div>
<span>This is a relatively sticky header</span>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Delectus voluptas pariatur ullam, dolores veritatis vero possimus nisi corrupti, provident aspernatur harum ab aliquam expedita assumenda, blanditiis aliquid id consequuntur distinctio.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Delectus voluptas pariatur ullam, dolores veritatis vero possimus nisi corrupti, provident aspernatur harum ab aliquam expedita assumenda, blanditiis aliquid id consequuntur distinctio.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Delectus voluptas pariatur ullam, dolores veritatis vero possimus nisi corrupti, provident aspernatur harum ab aliquam expedita assumenda, blanditiis aliquid id consequuntur distinctio.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Delectus voluptas pariatur ullam, dolores veritatis vero possimus nisi corrupti, provident aspernatur harum ab aliquam expedita assumenda, blanditiis aliquid id consequuntur distinctio.</p>
</div>
</div>
Ответ 2
Конечно, вы можете просто получить дополнительный div!
<div class="fixed-wrapper">
<div class="close-wrapper">
<div class="close"></div>
</div>
</div>
body
background: gray
height: 8000px
.fixed-wrapper
position: fixed
top: 20px
left: 0
right: 0
.close-wrapper
max-width: 1200px
position: relative
.close
background: #fff
width: 30px
height: 30px
position: absolute
right: 0
border: 1px solid #515151
&:before,&:after
width: 25px
height: 1px
background: #515151
content: ''
position: absolute
top: 50%
left: 50%
display: block
@include transform(translate(-50%, -50%) rotate(45deg))
&:after
transform: translate(-50%, -50%) rotate(-45deg)
Смотрите эту скрипку, которую я сделал для вас: -)
http://codepen.io/marinagallardo/pen/mJyqaN
Ответ 3
Лучший способ добиться этого - предоставить родительскому элементу преобразование css.
например:
.relative{
transform: translateX(0); // this will act like relative parent
}
.fixed{
position: fixed;
left:0;
top:0;
width:100%; // width will be relative to the width of .relative
}
Ответ 4
То, что вы хотите использовать, - позиция: абсолютная. Это помещает дочерний элемент в соответствие с его родительским элементом.
Некоторые показания здесь: http://www.w3schools.com/cssref/pr_class_position.asp