2015-09-24 3 views
0

Я пытаюсь отобразить «ссылку» на верхней части div (ccontainer) с фоновым изображением. Он работает с положением: абсолютным, но с положением: относительное исчезает. Мне интересно, почему это так, как хотелось бы, чтобы кнопка была относительно div.Центрирование кнопки на фоновом изображении

Мой другой вопрос: как я могу сделать свой «критерий» div после (ниже) моего «контентного» div. Я сделал свое фоновое изображение прозрачным и может видеть, что серый цвет фона «совпадения» перекрывается сзади.

body 
 
{ 
 
\t margin: 0; 
 
\t padding: 0; 
 
\t background-color: #999; 
 
} 
 

 
.header 
 
{ 
 
\t z-index:3; 
 
\t position:fixed; 
 
\t background-color:#2B193E; 
 
\t border:0px solid #ffffff; 
 
\t height:70px; 
 
\t left:0; 
 
\t width:100%; 
 
\t top:0; 
 
\t /*Opacity start*/ 
 
    -ms-filter:    "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)"; 
 
    filter: alpha(opacity=80); 
 
    -moz-opacity: 0.80; 
 
    -khtml-opacity: 0.8; 
 
    opacity: 0.7; 
 
    /*Opacity end*/ 
 
} 
 

 
.hcontainer 
 
{ 
 
\t position: relative; 
 
\t width:100%; 
 
} 
 

 
.headingtext 
 
{ 
 
\t color: #ffffff; 
 
\t text-align: center; 
 
\t width: 100%; 
 
} 
 

 
.content 
 
{ 
 
\t z-index:1; 
 
\t position: absolute; 
 
\t top:0; 
 
\t left:0; 
 
\t height: 100%; 
 
\t width: 100%; 
 
\t padding:0px 0px; 
 
} 
 

 
.ccontainer 
 
{ 
 
\t height: 100%; 
 
\t width: 100%; 
 
\t background:url(my.jpg); 
 
\t background-position: center center; 
 
\t background-repeat: no-repeat; 
 
\t background-attachment: fixed; 
 
\t opacity: 0.9; 
 
\t background-size: 100% 100%; 
 
} 
 

 
.scontent 
 
{ 
 
\t width: 100%; 
 
\t height: 2000px; 
 
\t padding:0px 0px; 
 
} 
 
.sccontainer 
 
{ 
 
\t width: 100%; 
 
\t height: 100%; 
 
\t background-color: #444444; 
 
} 
 

 
.footer 
 
{ 
 
\t z-index:2; 
 
\t background: #2B193E; 
 
\t position: fixed; 
 
\t bottom:0; 
 
\t height:5em; 
 
\t width: 100%; 
 
\t padding: 0em 0em; 
 
\t /*Opacity start*/ 
 
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)"; 
 
    filter: alpha(opacity=80); 
 
    -moz-opacity: 0.80; 
 
    -khtml-opacity: 0.8; 
 
    opacity: 0.7; 
 
    /*Opacity end*/ 
 
} 
 

 
.fcontainer 
 
{ 
 
\t position:relative; 
 
\t color: #fff; 
 
\t text-align: center; 
 
\t top: 40%; 
 
} 
 

 
.btn 
 
{ 
 
\t position: absolute; 
 
\t width: 150px; \t 
 
    \t margin-left:-75px; 
 
    \t left: 50%; 
 
    \t bottom:200px; 
 
\t display: inline-block; 
 
    \t padding: 5px; 
 
\t color: #ffffff; 
 
\t border: 2px solid #fff; 
 
\t border-radius: 5px; 
 
\t text-align: center; 
 
\t outline: none; 
 
\t text-decoration: none; 
 
\t transition: background-color 0.2s ease-out, 
 
       \t color 0.2s ease-out; 
 
} 
 

 
.btn:hover, .btn:focus, .btn:active { 
 
\t background-color: #fff; 
 
\t color: #000; 
 
\t transition: background-color 0.3s ease-in, 
 
    \t \t \t color 0.3s ease-in; 
 
}
<body class="body"> 
 
    <div class="header"> 
 
    <div class="hcontainer"> 
 
     <h1 class="headingtext">Poise for victory!</h1> 
 
    </div> 
 
    </div> 
 
    
 
    <div class="content"> 
 
    <div class="ccontainer"> 
 
     <a href="http://www.google.com" class="btn"> Submit!</a> 
 
    </div> 
 
    </div> 
 
    
 
    <div class="scontent"> 
 
    <div class="sccontainer"></div> 
 
    </div> 
 

 
    <footer class="footer"> 
 
    <div class="fcontainer"> 
 
     MADE <em class="calluna">in</em> USA 
 
    </div> 
 
    </footer> 
 
    
 
</body>

* Edit: Вот некоторые изображения, иллюстрирующие то, что я имею в виду. Слева - это то, как я хочу иметь макет, а тот, который справа, - это то, как сейчас. На втором изображении показано, как серый фон перекрывает фоновое изображение сзади. Также я использую z-index, чтобы сохранить верхний и нижний колонтитулы над 2-мя содержимыми.

http://imgur.com/a/5uHlK

+0

Вы делаете некоторые шаткие вещи в вашем CSS, и я не совсем понимаю, что вы имеете в виду или то, что вы пытаетесь сделать. Если вы можете воссоздать проблему на http://codepen.io/pen и/или показать изображение того, что вы пытаетесь сделать или лучше объяснить, я могу помочь – Vall3y

+0

Где вы хотите, чтобы кнопка отображалась? –

+0

просто удалите все абсолютные позиции и z-индексы из кода выше, чтобы начать, чтобы вы могли попасть на дорожку. – imGaurav

ответ

0

Спасибо за помощь. Проблема заключалась в моем позиционировании элементов. Теперь я использую только «статическое» значение по умолчанию и относительное позиционирование. This сайт также помог не только с z-индексацией, но и с обычными макетами. Html остался прежним, но css приведен ниже для справки. Я также добавил 100% ширину и высоту к html и телу, которые очень помогли.

html, body { 
 
\t height: 100%; 
 
\t width: 100%; 
 
\t margin: 0; 
 
\t padding: 0; 
 
/* \t font: 1em/1.2 "Helvetica Neue", Helvetica, Arial, Geneva, sans-serif; 
 
*/ 
 
} 
 

 
.header { 
 
\t position:fixed; 
 
\t height:5em; 
 
\t width:100%; 
 
\t z-index:3; 
 
\t background-color:#2B193E; 
 
\t /*Opacity start*/ 
 
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)"; 
 
    filter: alpha(opacity=80); 
 
    -moz-opacity: 0.80; 
 
    -khtml-opacity: 0.8; 
 
    opacity: 0.7; 
 
    /*Opacity end*/ 
 
} 
 

 
.hcontainer { 
 
\t height: 100%; 
 
} 
 

 
.headingtext { 
 
\t position: relative; 
 
\t top: .5em; 
 
\t text-align: center; 
 
\t color: #ffffff; 
 
} 
 

 
.content { 
 
\t height: 100%; 
 
\t background:url(http://www.minimit.com/images/picjumbo.com_IMG_6648.jpg) no-repeat center center fixed; 
 
\t background-size: 100% 100%; 
 
} 
 

 
.ccontainer { 
 
\t border: 1px dashed #669966; 
 
\t position: relative; 
 
\t width:40%; 
 
\t height:40%; 
 
\t left:30%; 
 
\t top:50%; 
 
} 
 

 
.scontent { 
 
\t height: 1500px; 
 
\t background:url(mypicture.jpg) no-repeat center center fixed; 
 
\t opacity: 0.4; 
 
\t background-size: 100% 100%; 
 
} 
 

 
.sccontainer { 
 
\t height: 100%; 
 
\t width: 100%; 
 
} 
 

 
.footer { 
 
\t position: fixed; 
 
\t height:3em; 
 
\t width: 100%; 
 
\t bottom:0em; 
 
\t z-index:2; 
 
\t background: #2B193E; 
 
\t padding: 0em 0em; 
 
\t /*Opacity start*/ 
 
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)"; 
 
    filter: alpha(opacity=80); 
 
    -moz-opacity: 0.80; 
 
    -khtml-opacity: 0.8; 
 
    opacity: 0.7; 
 
    /*Opacity end*/ 
 
} 
 

 
.fcontainer { 
 
\t position: relative; 
 
\t top:1em; 
 
\t text-align: center; 
 
\t color: #ffffff; 
 
} 
 

 
/* ----------------------- 
 
Single styles 
 
------------------------*/ 
 
.infotable { 
 
\t position: relative; 
 
\t border: 1px dashed #ffffff; 
 
\t width: 70%; 
 
\t margin-bottom:0px; 
 
\t top: 50%; 
 
\t left:15%; 
 
\t 
 

 
} 
 

 
.btn { 
 
\t border: 20px dashed #000000; 
 
\t position: relative; 
 
\t left:50%; 
 
    \t top:-50%; 
 
\t width: 150px; \t 
 
    \t margin-left:-75px; 
 
    \t 
 
\t display: inline-block; 
 
    \t padding: 5px; 
 
\t color: #ffffff; 
 
\t border: 2px solid #ffffff; 
 
\t border-radius: 5px; 
 
\t text-align: center; 
 
/* \t outline: none;*/ 
 
\t text-decoration: none; 
 
\t transition: background-color 0.2s ease-out, 
 
       \t color 0.2s ease-out; 
 
} 
 

 
.btn:hover, .btn:focus, .btn:active { 
 
\t background-color: #fff; 
 
\t color: #000; 
 
\t transition: background-color 0.3s ease-in, 
 
    \t \t \t color 0.3s ease-in; 
 
}

0

Позиция абсолют, прислушивается к первому родительскому элементу, имея его позиция устанавливается на нечто иное, чем по умолчанию значение «статических».

Таким образом, если вы добавили фиксированную/абсолютную позицию относительно div, содержащего ссылку, это должно позволить вам использовать абсолютную позицию позиции в элементе link, чтобы расположить ее относительно родительского элемента.

Кроме того, если вы используете позицию position относительно элемента и не задаете никаких значений позиции, то для left/top это ничего не значит для его поведения.

для получения дополнительной информации: https://developer.mozilla.org/en-US/docs/Web/CSS/position

+0

Пожалуйста, не связывайтесь с w3schools.Они являются позорно неточными и недостаточно оценены по StackOverflow для этого и по другим причинам. – CodeMouse92

+0

спасибо, я не знал;) – DRGA

0

Является ли это то, что вы хотели ?????

Не знаете, где находится серый цвет.

Если вы хотите увидеть это правильно, нажмите «полная страница» после выбора фрагмента кода запуска. адаптировалось к размеру коробки меньшего он находится.

body 
 
{ 
 
\t margin: 0; 
 
\t padding: 0; 
 
\t background-color: red; 
 
} 
 

 
.header 
 
{ 
 
\t z-index:3; 
 
\t position:fixed; 
 
    
 
    /*Opacity Being done with RGBA */ 
 
\t background-color:RGBA(51, 37, 65, 0.7); 
 
    
 
\t border:0px solid #ffffff; 
 
\t height:70px; 
 
\t left:0; 
 
\t width:100%; 
 

 
} 
 

 
.hcontainer 
 
{ 
 
\t position: relative; 
 
\t width:100%; 
 
} 
 

 
.headingtext 
 
{ 
 
\t color: #ffffff; 
 
\t text-align: center; 
 
\t width: 100%; 
 
} 
 

 
.content 
 
{ 
 
\t z-index:1; 
 
\t position: absolute; 
 
\t top:0; 
 
\t left:0; 
 
\t height: 100%; 
 
\t width: 100%; 
 
\t padding:0px 0px; 
 
    background-image: url('http://www.liveurlifehere.com/wallpaper/uploads/liveurlifehere-wallpaper1431009496.jpg'); 
 
    background-size: 100% 100%; 
 
    
 
} 
 

 
.ccontainer 
 
{ 
 
\t height: 100%; 
 
\t width: 100%; 
 
\t background:url(my.jpg); 
 
\t background-position: center center; 
 
\t background-repeat: no-repeat; 
 
\t background-attachment: fixed; 
 
\t opacity: 0.9; 
 
\t background-size: 100% 100%; 
 
} 
 

 
.scontent 
 
{ 
 
\t width: 100%; 
 
\t height: 2000px; 
 
\t padding:0px 0px; 
 
} 
 
.sccontainer 
 
{ 
 
\t width: 100%; 
 
\t height: 100%; 
 
\t background-color: #444444; 
 
} 
 

 
.footer 
 
{ 
 
\t z-index:2; 
 
\t background: #2B193E; 
 
\t position: fixed; 
 
\t bottom:0; 
 
\t height:5em; 
 
\t width: 100%; 
 
\t padding: 0em 0em; 
 
\t /*Opacity start*/ 
 
    -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=50)"; 
 
    filter: alpha(opacity=80); 
 
    -moz-opacity: 0.80; 
 
    -khtml-opacity: 0.8; 
 
    opacity: 0.7; 
 
    /*Opacity end*/ 
 
} 
 

 
.fcontainer 
 
{ 
 
\t position:relative; 
 
\t color: #fff; 
 
\t text-align: center; 
 
\t top: 40%; 
 
} 
 

 
.btn 
 
{ 
 
\t position: absolute; 
 
\t width: 150px; \t 
 
    \t margin-left:-75px; 
 
    \t left: 50%; 
 
    \t bottom:200px; 
 
\t display: inline-block; 
 
    \t padding: 5px; 
 
\t color: #ffffff; 
 
\t border: 2px solid #fff; 
 
\t border-radius: 5px; 
 
\t text-align: center; 
 
\t outline: none; 
 
\t text-decoration: none; 
 
\t transition: background-color 0.2s ease-out, 
 
       \t color 0.2s ease-out; 
 
    z-index: 1; 
 
} 
 

 
.btn:hover, .btn:focus, .btn:active { 
 
\t background-color: #fff; 
 
\t color: #000; 
 
\t transition: background-color 0.3s ease-in, 
 
    \t \t \t color 0.3s ease-in; 
 
}
<body class="body"> 
 
    <div class="header"> 
 
    <div class="hcontainer"> 
 
     <h1 class="headingtext">Poise for victory!</h1> 
 
    </div> 
 
    </div> 
 
    
 
    <div class="content"> 
 
    <div class="ccontainer"> 
 
     <a href="http://www.google.com" class="btn"> Submit!</a> 
 
    </div> 
 
    </div> 
 
    
 
    <div class="scontent"> 
 
    <div class="sccontainer"></div> 
 
    </div> 
 

 
    <footer class="footer"> 
 
    <div class="fcontainer"> 
 
     MADE <em class="calluna">in</em> USA 
 
    </div> 
 
    </footer> 
 
    
 
</body>

Смежные вопросы