尝试通过媒体查询居中导航菜单但没有响应

时间:2016-02-04 23:52:27

标签: html css css-float media-queries

我正在响应式网站上创建一个横幅,并且在某个屏幕尺寸上我希望导航菜单(通常浮动到横幅的右侧)居中。导航栏似乎响应了媒体查询中的其他类型的更改(颜色,大小等),但我无法弄清楚如何将整个ul居中。



/* line 8, ../scss/header.scss */
body {
  margin: 0;
  padding: 0;
}

/* line 13, ../scss/header.scss */
.banner {
  background-color: blue;
  box-sizing: border-box;
  height: 80px;
}

/* line 19, ../scss/header.scss */
.container {
  max-width: 940px;
  margin: 0 auto;
}

/* line 24, ../scss/header.scss */
.left-col {
  float: left;
  width: 23%;
}

/* line 29, ../scss/header.scss */
.right-col {
  float: right;
  width: 77%;
  text-align: right;
}
/* line 34, ../scss/header.scss */
.right-col .site-nav li {
  list-style: none;
  float: right;
  border-left: 1.5px solid;
}
/* line 39, ../scss/header.scss */
.right-col .site-nav li:last-child {
  border: none;
}
/* line 42, ../scss/header.scss */
.right-col .site-nav a {
  display: block;
  text-decoration: none;
  padding: 5px 20px;
}

@media screen and (max-width: 700px) {
  /* line 51, ../scss/header.scss */
  .banner {
    height: 160;
    background-color: red;
  }

  /* line 55, ../scss/header.scss */
  .left-col, .right-col {
    width: auto;
    float: none;
    text-align: center;
  }

  /* line 60, ../scss/header.scss */
  .right-col {
    margin: 0 auto;
    max-width: 840px;
    padding: 0;
    height: 60px;
    text-align: center;
  }

  /* line 67, ../scss/header.scss */
  .right-col li {
    float: none;
    position: relative;
    display: inline-block;
    text-align: center;
    background: none;
  }

  /* line 74, ../scss/header.scss */
  .right-col ul {
    padding: 0px;
    text-align: center;
  }
}
/* This is the clear fix method */
/* line 91, ../scss/header.scss */
.group:before,
.group:after {
  content: "";
  display: table;
}

/* line 96, ../scss/header.scss */
.group:after {
  clear: both;
}

/* line 100, ../scss/header.scss */
.group {
  zoom: 1;
}

<head>
	<link rel="stylesheet" href="css/header.css">
	<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<html>
	<div class="banner">
		<div class="container">
			<div class="left-col">
				<p>HEADER</p>
			</div>
			<div class="right-col">
				<nav class="site-nav">
					<ul class="group">
						<li><a href="contact.html" class="last">Contact Us</a></li>
						<li><a href="#about">About</a></li>
						<li><a href="#methodology">Methodology</a></li>
						<li><a href="#">Products</a></li>
						<li><a href="draft1-1.html">Home</a></li>
					</ul>
				</nav>
			</div>
		</div>
	</div>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

Specificity可能会受到指责。

将媒体查询中的CSS声明从.right-col li更改为.right-col .site-nav li似乎可以解决问题。

此外,从float: right移除.right-col .site-nav li并将显示更改为display: inline-block将保持订单不变。

/* line 8, ../scss/header.scss */
body {
  margin: 0;
  padding: 0;
}

/* line 13, ../scss/header.scss */
.banner {
  background-color: blue;
  box-sizing: border-box;
  height: 80px;
}

/* line 19, ../scss/header.scss */
.container {
  max-width: 940px;
  margin: 0 auto;
}

/* line 24, ../scss/header.scss */
.left-col {
  float: left;
  width: 23%;
}

/* line 29, ../scss/header.scss */
.right-col {
  float: right;
  width: 77%;
  text-align: right;
}
/* line 34, ../scss/header.scss */
.right-col .site-nav li {
  list-style: none;
  display: inline-block;
  border-left: 1.5px solid;
}
/* line 39, ../scss/header.scss */
.right-col .site-nav li:last-child {
  border: none;
}
/* line 42, ../scss/header.scss */
.right-col .site-nav a {
  display: block;
  text-decoration: none;
  padding: 5px 20px;
}

@media screen and (max-width: 700px) {
  /* line 51, ../scss/header.scss */
  .banner {
    height: 160;
    background-color: red;
  }

  /* line 55, ../scss/header.scss */
  .left-col, .right-col {
    width: auto;
    float: none;
    text-align: center;
  }

  /* line 60, ../scss/header.scss */
  .right-col {
    margin: 0 auto;
    max-width: 840px;
    padding: 0;
    height: 60px;
    text-align: center;
  }

  /* line 67, ../scss/header.scss */
   .right-col .site-nav li {
    float: none;
    position: relative;
    display: inline-block;
    text-align: center;
    background: none;
  }

  /* line 74, ../scss/header.scss */
  .right-col ul {
    padding: 0px;
    text-align: center;
  }
}
/* This is the clear fix method */
/* line 91, ../scss/header.scss */
.group:before,
.group:after {
  content: "";
  display: table;
}

/* line 96, ../scss/header.scss */
.group:after {
  clear: both;
}

/* line 100, ../scss/header.scss */
.group {
  zoom: 1;
}
<head>
	<link rel="stylesheet" href="css/header.css">
	<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<html>
	<div class="banner">
		<div class="container">
			<div class="left-col">
				<p>HEADER</p>
			</div>
			<div class="right-col">
				<nav class="site-nav">
					<ul class="group">
						<li><a href="contact.html" class="last">Contact Us</a></li>
						<li><a href="#about">About</a></li>
						<li><a href="#methodology">Methodology</a></li>
						<li><a href="#">Products</a></li>
						<li><a href="draft1-1.html">Home</a></li>
					</ul>
				</nav>
			</div>
		</div>
	</div>
</html>