【发布时间】:2016-07-18 18:03:17
【问题描述】:
我做了一个简单的网站,但是 div 没有正确显示
- 我有一个 div
section-one即position: relative[green] 我还有一个名为silver-break-bar的 div 内部是position: absolute。
我在section-one 下方还有一个section-two div [红色]。 问题是三方面的。
-
section-two显示在绿色 div 上方。为什么不显示在绿色 div 下方?我什至做了body {height: auto}
第二个问题是我已经创建了我的
red-car imgpadding:0 margin:0,所以我没有超出页面,但是 div 仍然没有环绕图像?第三个问题是当我调整浏览器大小时,粉红色的 div 覆盖了标题?这是为什么呢?
更新:最后,我正在尝试创建显示在彼此下方的 div,即与此类似的第 1 节、第 2 节、第 3 节。
更新:这是代码,抱歉我忘记添加了!
HTML
<!DOCTYPE html>
<html>
<head>
<title>Responsive Navigation Demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/style.css">
<link href='http://fonts.googleapis.com/css?family=PT+Sans:400,700' rel='stylesheet' type='text/css'>
<script src="js/jquery-3.0.0.min.js"></script>
<script>
$(function() {
var pull = $('#pull');
menu = $('nav ul');
menuHeight = menu.height();
$(pull).on('click', function(e) {
e.preventDefault();
menu.slideToggle();
});
$(window).resize(function(){
var w = $(window).width();
if(w > 320 && menu.is(':hidden')) {
menu.removeAttr('style');
}
});
});
</script>
</head>
<body>
<div id='main-header'>
<nav class="clearfix">
<ul class="clearfix">
<li><a href="#">Home</a></li>
<li><a href="#">How ItS</a></li>
<li><a href="#">Why Us</a></li>
<li><a href="#">FAQ</a></li>
<li><a href="#">Contact</a></li>
</ul>
<a href="#" id="pull">Menu</a>
</nav>
</div>
<div id='section-one'>
<h1> Order anything from anywhere in Karachi instantly at your doorstep. </h1>
<div class='silver-line-break'></div>
<div id='fee-estimate-box'>
<form id='fee-estimate-form' action="#">
<legend>Delivery Fee Calculator</legend>
<span>First name: </span> <input type="text" name="firstname" value="Mickey">
<span>Last name: </span> <input type="text" name="lastname" value="Mouse">
<input type="submit" value="Submit">
</form>
</div> <div class='silver-break-bar'>
<img id='red-car' src="img/red-car.png" alt="" height="60%" width="30%;">
</div>
</div>
<div id='section-two'>
<p>section two div</p>
</div>
</body>
</html>
CSS
/* Basic Styles */
* {
margin-top: 0px;
padding: 0;
}
body {
margin: 0;
height: auto;
background-color: #ece8e5;
}
/***** NAVIGATION ***/
/* Clearfix */
.clearfix:before,
.clearfix:after {
content: " ";
display: table;
}
.clearfix:after {
clear: both;
}
.clearfix {
*zoom: 1;
}
nav {
height: 40px;
width: 100%;
background: white;
font-size: 11pt;
font-family: 'PT Sans', Arial, sans-serif;
font-weight: bold;
position: relative;
border-bottom: 1px solid #283744;
}
nav ul {
padding-right: :5px;
margin: 0 auto;
width: 500px;
height: 40px;
float: right;
margin-right: 0%;
}
nav li {
display: inline;
float: left;
}
nav a {
color: black;
display: inline-block;
width: 85px;
text-align: center;
text-decoration: none;
line-height: 40px;
/*text-shadow: 1px 1px 0px #283744;*/
}
nav li a {
/*border-right: 1px solid #576979;*/
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
}
nav li:last-child a {
border-right: 0;
}
nav a:hover, nav a:active {
background-color: #8c99a4;
}
nav a#pull {
display: none;
}
/*Styles for screen 600px and lower*/
@media screen and (max-width: 600px) {
nav {
height: auto;
}
nav ul {
width: 100%;
display: block;
height: auto;
float: none;
}
nav li {
width: 50%;
float: left;
position: relative;
}
nav li a {
border-bottom: 1px solid #576979;
border-right: 1px solid #576979;
}
nav a {
text-align: left;
width: 100%;
text-indent: 25px;
}
}
/*Styles for screen 515px and lower*/
@media only screen and (max-width : 480px) {
nav {
border-bottom: 0;
}
nav ul {
display: none;
height: auto;
}
nav a#pull {
display: block;
background-color: white;
width: 100%;
position: relative;
}
nav a#pull:after {
content:"";
background: url('../img/nav-icon.png') no-repeat;
width: 30px;
height: 30px;
display: inline-block;
position: absolute;
right: 15px;
top: 10px;
}
}
/*Smartphone*/
@media only screen and (max-width : 320px) {
nav ul {
float: none;
}
nav li {
display: block;
float: none;
width: 100%;
}
nav li a {
border-bottom: 1px solid #576979;
}
}
#section-one {
position: absolute;;
background-color: #80be05;
margin-top: 0 auto;
padding: 0;
height: 85%;
}
#section-one h1 {
position: relative;
margin: 0px;
padding: 2%;
font-family: 'TeX Gyre Adventor', 'sans-serif';
font-size: 57px;
text-align: center;
color: white;
text-shadow: 0px 1px;
}
#section-one .silver-line-break {
position: absolute;
width: 100%;
height: 10%;
top: 100%;
background-color: silver;
margin: 0 auto;
}
#section-two {
position: relative;
width: 100%;
height: 100%;
background-color: red;
}
#section-one #red-car {
position: absolute;
display: inline-block;
left: 75%;
top: 60%;
background-color: blue;
padding: 0;
margin: 0 auto;
}
#section-one #fee-estimate-box {
background-color: pink;
position: absolute;;
width: 70%;
height: 40%;
top: 50%;
left: 2%;
}
【问题讨论】:
-
请分享您的css和html(仅相关部分)
-
您尚未提供minimal reproducible example,但您所有三个问题的答案似乎都是“因为您使用了绝对定位”。它不是创建您在上一张图片中提供的布局的合适工具。
-
鉴于您遇到的问题,您应该检查引导程序。用它构建响应式页面非常容易。
-
对不起!我不小心按下了发布按钮。源代码已添加!
-
Bootstrap 可能值得用来了解响应式的工作原理,然后根据您从中学到的知识构建自己的 CSS
标签: html css css-position