【问题标题】:Trouble with CSS positioning for small screen小屏幕的 CSS 定位问题
【发布时间】:2017-05-05 08:00:31
【问题描述】:

我需要为我的网页添加 CSS 以使其更适合小屏幕和手机。我的@media 修改似乎不起作用。我必须让它看起来像这样:

nav ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}
nav ul li {
  margin: 0.2em;
  padding: 0.75em 1em;
  background: #689;
  background-image: linear-gradient(to bottom, #689, #245);
  border-radius: 1.25em;
}
nav a {
  color: #fff;
  text-decoration: none;
  font-weight: bold;
}

h1 {
  text-align: center;
}
nav {
   float: left;
   padding-right: 30px;
   width: 15%;
}
main {
   float: left;
   width: 40em;
}

@media (max-width: 480px) { 
  float: none;
  display: inline;
}
<h1 id="top">Protocols</h1>
 
<nav>
  <ul>
  <li><a href="#top">Top of Page</a></li>
  <li><a href="#email">Email</a></li>
  <li><a href="#http">HTTP</a></li>
  </ul>
</nav>


<main>
<p><dfn>Protocols</dfn> (or <dfn>communications protocols</dfn>) are agreed ways for to computers to communicate and exchange information. The Wikipedia page on <a href="https://en.wikipedia.org/wiki/Communications_protocol">communications protocols</a> contains much more information than we can cover here.</p>
  
<section id="email">
<h2>Email Protocols</h2>
<p>There are several protocols that make email work:</p>
<ul>
<li><p><abbr title="Internet Message Access Protocol">IMAP</abbr> is used to fetch mail from the server so you can read it in clients like Thunderbird or on your phone.</p></li>
<li><p><abbr title="Post Office Protocol">POP</abbr> is an older protocol that does the same job as IMAP, but is less flexible.</p></li>
<li><p>The <abbr title="Simple Mail Transfer Protocol">SMTP</abbr> protocol is used to send email, either from a client program you are using, or from one mail server to another. SMTP has existed since the 1980s, but has been extended several times to add user authentication, encryption, and other features.</p>
<p>SMTP is where most spam fighting happens: preventing fraudulent email from being sent the the ultimate goal.</p></li>
</ul>
</section>
  
<section id="http">
<h2>HTTP</h2>
<p>The  <a href="https://en.wikipedia.org/wiki/World_Wide_Web">World Wide Web</a> rests on the protocol <abbr title="Hypertext Transfer Protocol">HTTP</abbr>.</p>
<p>HTTP is a client-server protocol. That means that an HTTP client (like a web browser, also called a &ldquo;user agent&rdquo;) contacts an HTTP server and makes a request. The request is likely a request for a particular web page available on that server. The server will respond with a response: often the contents of the web page that was requested.</p>
  <section id="uses">
  <h3>Uses</h3>
  <p>HTTP is often used to transfer <abbr title="Hypertext Markup Language">HTML</abbr> documents which we generally think of as &ldquo;web pages&rdquo;. HTML contains markup like <code class="html">&lt;em&gt;</code> and <code class="html">&lt;a href="http://example.com/"&gt;</code>.</p>
  <p>Every piece of content available on the web is called a <dfn>resource</dfn> and is available at a URL like <code class="url">http://example.com/somepage.html</code>.</p>
  </section>
</section>
 
</main>

【问题讨论】:

  • 尝试将@media改为@media screen and (max-width: 480px) {..some styles here..}
  • 嗯..没用
  • 不要破坏您的问题,我们只会还原更改,如果您继续这样做,版主可能会介入并暂停您的帐户。

标签: html css css-float css-position positioning


【解决方案1】:

您也应该将它们放入一个类中以使其像这样工作:

@media (max-width: 480px) { 
  .some-class {
    float: none;
    display: inline;
  }

}

并在您想要这些属性的地方使用“某些类”。

这是你真正想要的:

注意按 F12 并使页面宽度小于 480 像素以查看实际效果

nav ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}
nav ul li {
  margin: 0.2em;
  padding: 0.75em 1em;
  background: #689;
  background-image: linear-gradient(to bottom, #689, #245);
  border-radius: 1.25em;
}
nav a {
  color: #fff;
  text-decoration: none;
  font-weight: bold;
}

h1 {
  text-align: center;
}
nav {
   float: left;
   padding-right: 30px;
   width: 15%;
}
main {
   float: left;
   width: 40em;
}

@media (max-width: 480px) { 
  
  li, ul, nav {
    float: none;
    display: inline;
  }
  
}
<h1 id="top">Protocols</h1>
 
<nav>
  <ul>
  <li><a href="#top">Top of Page</a></li>
  <li><a href="#email">Email</a></li>
  <li><a href="#http">HTTP</a></li>
  </ul>
</nav>


<main>
<p><dfn>Protocols</dfn> (or <dfn>communications protocols</dfn>) are agreed ways for to computers to communicate and exchange information. The Wikipedia page on <a href="https://en.wikipedia.org/wiki/Communications_protocol">communications protocols</a> contains much more information than we can cover here.</p>
  
<section id="email">
<h2>Email Protocols</h2>
<p>There are several protocols that make email work:</p>
<ul>
<li><p><abbr title="Internet Message Access Protocol">IMAP</abbr> is used to fetch mail from the server so you can read it in clients like Thunderbird or on your phone.</p></li>
<li><p><abbr title="Post Office Protocol">POP</abbr> is an older protocol that does the same job as IMAP, but is less flexible.</p></li>
<li><p>The <abbr title="Simple Mail Transfer Protocol">SMTP</abbr> protocol is used to send email, either from a client program you are using, or from one mail server to another. SMTP has existed since the 1980s, but has been extended several times to add user authentication, encryption, and other features.</p>
<p>SMTP is where most spam fighting happens: preventing fraudulent email from being sent the the ultimate goal.</p></li>
</ul>
</section>
  
<section id="http">
<h2>HTTP</h2>
<p>The  <a href="https://en.wikipedia.org/wiki/World_Wide_Web">World Wide Web</a> rests on the protocol <abbr title="Hypertext Transfer Protocol">HTTP</abbr>.</p>
<p>HTTP is a client-server protocol. That means that an HTTP client (like a web browser, also called a &ldquo;user agent&rdquo;) contacts an HTTP server and makes a request. The request is likely a request for a particular web page available on that server. The server will respond with a response: often the contents of the web page that was requested.</p>
  <section id="uses">
  <h3>Uses</h3>
  <p>HTTP is often used to transfer <abbr title="Hypertext Markup Language">HTML</abbr> documents which we generally think of as &ldquo;web pages&rdquo;. HTML contains markup like <code class="html">&lt;em&gt;</code> and <code class="html">&lt;a href="http://example.com/"&gt;</code>.</p>
  <p>Every piece of content available on the web is called a <dfn>resource</dfn> and is available at a URL like <code class="url">http://example.com/somepage.html</code>.</p>
  </section>
</section>
 
</main>

【讨论】:

  • 好吧,我尝试选择导航部分和主要部分而不是类。两种方法都行不通。
  • 好吧,我尝试选择导航部分而不是类,导航菜单现在位于主要部分的顶部,但每个菜单项仍然堆叠在一起,而不是像在我贴的图片
  • @JohnnySack 已更新,如未解决请查看并通知我再次更新
【解决方案2】:

试试这个

@media (max-width: 480px) { 
 nav {
    float: left;
    padding-right: 0;
    width: 100%;
 } 
 main {
    float: left;
    width: 100%;
  }
 nav ul li {
    display:inline-block;
    width:auto;
  }

}

【讨论】:

  • 导航菜单项仍然出现在彼此之上,而不是像我发布的图像中那样并排显示
  • 你把它应用到你的css了吗
  • 您是否将我的代码和元标记添加到您的 html 页面? &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
  • 我检查了你的源代码,你没有在你的样式表中添加这个媒体查询,在样式表的底部添加这个
【解决方案3】:

抱歉,我无法从办公室打开链接。但是,如果我正确理解了您的问题,那么您基本上需要让您的页面在较小的视点中看起来很好。

请将您的@media 修改为以下内容:

@media (max-width: 480px) { 
  nav{
    float: none;
    width: 100%;
    padding-right: 0;
  }
  main {
   float: left;
   width: auto;
  }
}

几点建议: - 建议使用 max-width 到 640px 以获得更好的视图。 - 您可以使用 max-width:40em 和宽度作为 100% 而不是分配给主元素的宽度,然后您不需要覆盖其他视口。

希望这会有所帮助!

【讨论】:

  • 导航菜单项仍然出现在彼此之上,而不是像我发布的图像中那样并排显示
【解决方案4】:

试试这个

nav ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}
nav ul li {
  margin: 0.2em;
  padding: 0.75em 1em;
  background: #689;
  background-image: linear-gradient(to bottom, #689, #245);
  border-radius: 1.25em;
}
nav a {
  color: #fff;
  text-decoration: none;
  font-weight: bold;
}

h1 {
  text-align: center;
}
nav {
   float: left;
   padding-right: 30px;
   width: 15%;
}
main {
   float: left;
   width: 40em;
}

@media (max-width: 480px) {
	nav ul li {
		display: inline;
		float: none;
		width: 100%;
	}
	main {
		float: left;
		width: 100%;
		margin: 1em;
	}

}
<h1 id="top">Protocols</h1>
 
<nav>
  <ul>
  <li><a href="#top">Top of Page</a></li>
  <li><a href="#email">Email</a></li>
  <li><a href="#http">HTTP</a></li>
  </ul>
</nav>


<main>
<p><dfn>Protocols</dfn> (or <dfn>communications protocols</dfn>) are agreed ways for to computers to communicate and exchange information. The Wikipedia page on <a href="https://en.wikipedia.org/wiki/Communications_protocol">communications protocols</a> contains much more information than we can cover here.</p>
  
<section id="email">
<h2>Email Protocols</h2>
<p>There are several protocols that make email work:</p>
<ul>
<li><p><abbr title="Internet Message Access Protocol">IMAP</abbr> is used to fetch mail from the server so you can read it in clients like Thunderbird or on your phone.</p></li>
<li><p><abbr title="Post Office Protocol">POP</abbr> is an older protocol that does the same job as IMAP, but is less flexible.</p></li>
<li><p>The <abbr title="Simple Mail Transfer Protocol">SMTP</abbr> protocol is used to send email, either from a client program you are using, or from one mail server to another. SMTP has existed since the 1980s, but has been extended several times to add user authentication, encryption, and other features.</p>
<p>SMTP is where most spam fighting happens: preventing fraudulent email from being sent the the ultimate goal.</p></li>
</ul>
</section>
  
<section id="http">
<h2>HTTP</h2>
<p>The  <a href="https://en.wikipedia.org/wiki/World_Wide_Web">World Wide Web</a> rests on the protocol <abbr title="Hypertext Transfer Protocol">HTTP</abbr>.</p>
<p>HTTP is a client-server protocol. That means that an HTTP client (like a web browser, also called a &ldquo;user agent&rdquo;) contacts an HTTP server and makes a request. The request is likely a request for a particular web page available on that server. The server will respond with a response: often the contents of the web page that was requested.</p>
  <section id="uses">
  <h3>Uses</h3>
  <p>HTTP is often used to transfer <abbr title="Hypertext Markup Language">HTML</abbr> documents which we generally think of as &ldquo;web pages&rdquo;. HTML contains markup like <code class="html">&lt;em&gt;</code> and <code class="html">&lt;a href="http://example.com/"&gt;</code>.</p>
  <p>Every piece of content available on the web is called a <dfn>resource</dfn> and is available at a URL like <code class="url">http://example.com/somepage.html</code>.</p>
  </section>
</section>
 
</main>

【讨论】:

    猜你喜欢
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多