Trouble with CSS positioning for small screen

468 views Asked by At

I need to add CSS for my webpage to make it better suitable for small screens and phones. My @media modification doesn't seem to work. I have to make it look like this:

Protocols Image

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>

4

There are 4 answers

4
Jishnu V S On BEST ANSWER

try with this

@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;
  }

}
3
Bahadir Tasdemir On

You should put those into a class too to make it work like this:

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

}

And use the "some-class" where you want those properties.

Here is what you really want:

Note: press F12 and make page's width smaller than 480px to see in-action

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>

1
Ashok Kumar Gupta On

Sorry I can not open link from office. However, If I understand your problem correctly then you basically need to make your page appear nicely for smaller viewpots.

Please modify your @media to following:

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

Couple of suggestions: - It is recommended to use max-width to 640px to get better views. - Instead of width assigned to main element you can use max-width:40em and width as 100% then you dont need to overwrite for other viewports.

Hope this helps!

0
MeowMeow On

Try this

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>