如何使图像向下滑动

时间:2019-05-11 14:17:38

标签: html css media-queries

当页面宽度减小时,我想使导航栏中心的图像向下滑动。

这是在桌面上的样子:

enter image description here

在智能手机上,导航栏应如下所示:

enter image description here

如您所见,我希望导航栏保持原样,但图像会向下滑动第一行。

body {
  background-color: gray;
  color: white;
  font-family: Helvetica Neue;
}


/* Header */

header {
  background-color: black;
  background-image: url("images/spiaggia.jpg");
  background-size: 100%;
  background-position: center;
  padding: 2px;
  color: white;
  height: 200px;
  background-repeat: no-repeat
}

section {
  background-color: white;
  color: gray;
  padding: 20px;
  display: flex;
  flex-direction: row;
}

div {
  background-color: black;
  display: inline-block;
  width: 100px;
  margin: auto;
  color: white;
}

header ul {
  margin: 0px;
  padding: 0px;
}

header li {
  text-decoration: none;
  display: inline-block;
  margin-right: 20px;
}

header .mobile {
  display: none;
}

a {
  color: white;
  text-decoration: none;
}

.logo {
  background-image: url("images/città.jpg");
  background-size: 100px;
  background-repeat: no-repeat;
  display: inline-block;
  height: 50px;
  position: relative;
  text-indent: -999999px;
  top: 0px;
  width: 100px;
  border: solid lightblue;
}


/* Features */

.features {
  background: white;
  color: gray;
  padding: 20px;
  display: flex;
  flex-direction: row;
}

.features figure {
  margin: auto;
  text-align: center;
  text-transform: uppercase;
  width: 200px;
}

.features figure img {
  border: 1px solid white;
  border-radius: 10%;
  box-shadow: gray 0 0 10px;
  width: 200px;
}


/* Footer */

footer {
  background: black;
  padding: 10px 20px;
  color: gray;
  font-size: 12px;
  padding: 20px 20px;
  text-align: center;
}

@media (max-width: 600px) {
  .mobile {
    display: inline-block;
  }
  .desktop {
    display: none;
  }
}
<header>

  <ul>
    <li><a href="Home.html">Home</a></li>
    <li><a href="website/Menu.html">Menu</a></li>
    <li><a class="logo" href="Home.html">Cadice_foto</a></li>

    <li class="mobile"><a href="website/Locations.html">Locations</a></li>
    <li class="mobile"><a href="website/Contacts.html">Contacts</a></li>


    <li class="desktop"><a href="website/Locations.html">Locations</a></li>
    <li class="desktop"><a href="website/Contacts.html">Contacts</a></li>
  </ul>

</header>
<section class="features">
  <figure>
    <img src="images/porticciolo.jpg" alt="porticciolo Cadice">
    <figcaption>Porticciolo Cadice</figcaption>
  </figure>

  <figure>
    <img src="images/palme.jpg" alt="palme Cadice">
    <figcaption>palme Cadice</figcaption>
  </figure>

  <figure>
    <img src="images/sera.jpg" alt="sera Cadice">
    <figcaption>sera Cadice</figcaption>
  </figure>


</section>
<section>lower-section</section>
<footer>Via Condotti, Roma IT - numero: 02.123456 - email@email.com</footer>

3 个答案:

答案 0 :(得分:3)

这可以通过使用CSS flex-box来完成,并在移动视图媒体查询激活时重新排列flex元素。

.menu-container {
  display: flex;
  flex-flow: row wrap;
  text-align: center;
}

.menu-container>* {
  padding: 10px;
  flex: 1 20%;
}

@media all and (min-width: 600px) {
  .menu-container>* {
    flex: 1;
    counter-increment: menulink;
    order: counter(menulink);
  }
  .menu-left {
    order: 1
  }
 .menu-right {
    order: 3
  }
  .logo-menu {
    order: 2;
 }
}

body {
  background-color: gray;
  color: white;
  font-family: Helvetica Neue;
}

/* Header */

header {
  background-color: black;
  background-image: url("http://placekitten.com/1000/500?image=6");
  background-size: 100%;
  background-position: center;
  padding: 2px;
  color: white;
  height: 200px;
  background-repeat: no-repeat
}

section {
  background-color: white;
  color: gray;
  padding: 20px;
  display: flex;
  flex-direction: row;
}

a {
  color: white;
  text-decoration: none;
}

.logo {
  background-image: url("http://placekitten.com/200/100");
  background-size: 100px;
  background-repeat: no-repeat;
  display: inline-block;
  height: 50px;
  position: relative;
  text-indent: -999999px;
  top: 0px;
  width: 100px;
  border: solid lightblue;
}

/* Features */

.features {
  background: white;
  color: gray;
  padding: 20px;
  display: flex;
  flex-direction: row;
}

.features figure {
  margin: auto;
  text-align: center;
  text-transform: uppercase;
  width: 200px;
}

.features figure img {
  border: 1px solid white;
  border-radius: 10%;
  box-shadow: gray 0 0 10px;
  width: 200px;
}

/* Footer */

footer {
  background: black;
  padding: 10px 20px;
  color: gray;
  font-size: 12px;
  padding: 20px 20px;
  text-align: center;
}

.menu-container {
  display: flex;
  flex-flow: row wrap;
  text-align: center;
}

.menu-container>* {
  padding: 10px;
  flex: 1 20%;
}

@media all and (min-width: 600px) {
  .menu-container>* {
    flex: 1;
    counter-increment: menulink;
    order: counter(menulink);
  }
  .menu-left {
    order: 1
  }
  .menu-right {
    order: 3
  }
  .logo-menu {
    order: 2;
  }
}
<!DOCTYPE html>
<html>

  <head>
    <title></title>
    <link rel="stylesheet" href="11.css" type="text/css" />
  </head>

  <body>
    <header class="menu-container">
      <a class="menu-left" href="Home.html">Home</a>
      <a  class="menu-left" href="website/Menu.html">Menu</a>
      <a  class="menu-right"href="website/Locations.html">Locations</a>
      <a  class="menu-right" href="website/Contacts.html">Contacts</a>
      <div class="logo-menu"><a class="logo" href="Home.html">Cadice_foto</a></div>
    </header>
    <section class="features">
      <figure>
        <img src="http://placekitten.com/150/150?image=2" alt="porticciolo Cadice">
        <figcaption>Porticciolo Cadice</figcaption>
      </figure>

      <figure>
        <img src="http://placekitten.com/150/150?image=8" alt="palme Cadice">
        <figcaption>palme Cadice</figcaption>
      </figure>

      <figure>
        <img src="http://placekitten.com/150/150?image=4" alt="sera Cadice">
        <figcaption>sera Cadice</figcaption>
      </figure>
    </section>
    <section>lower-section</section>
    <footer>Via Lars, somewhere IT - numero: uno!- email@email.com</footer>
  </body>

</html>

答案 1 :(得分:3)

在下面的CSS媒体查询中添加以下内容:

.logo {
    position: absolute;
    top: 50px;
    left: 0;
    right: 0;
    margin: 0 auto;
  }

您可能想知道这段代码的作用。 position 属性指定元素使用的定位类型。 top 属性是已定位元素的垂直位置。写入属性以重置位置(默认情况下为“自动”),然后写入 margin (即“ 0自动”)以按中心对齐元素。 https://www.w3schools.com/css/css_margin.asp

您可能应该在这里看到有关媒体查询的一些信息:https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries

P.S。并记住干燥(请勿重复自己)。尽量避免写两次,尤其是这样的话:

            <li class="mobile"><a href="website/Locations.html">Locations</a></li>
            <li class="mobile"><a href="website/Contacts.html">Contacts</a></li>


            <li class="desktop"><a href="website/Locations.html">Locations</a></li>
            <li class="desktop"><a href="website/Contacts.html">Contacts</a></li>

您可以为每个媒体查询中的每个元素设置每个属性

答案 2 :(得分:1)

我已经对您的代码做了一些改动,以实现您想要的功能。这是Code

server.js
body {
  background-color: gray;
  color: white;
  font-family: Helvetica Neue;
}


/* Header */

header {
  background-color: black;
  background-image: url("https://www.hotelsantamargherita.it/wp-content/uploads/2017/12/caorle-dalla-spiaggia-di-pon-1440x500.jpg");
  background-size: 100%;
  background-position: center;
  padding: 2px;
  color: white;
  height: 200px;
  background-repeat: no-repeat;
}

section {
  background-color: white;
  color: gray;
  padding: 20px;
  display: flex;
  flex-direction: row;
}

div {
  background-color: black;
  display: inline-block;
  width: 100px;
  margin: auto;
  color: white;
}

.navbar {
  margin: 0px;
  padding: 0px;
  text-align: center;
  position: relative;
}

.navbar li {
  text-decoration: none;
  display: inline-block;
  margin-right: 20px;
}

.navbar li a {
  color: white;
  text-decoration: none;
}

.logo a {
  background-image: url("http://www.florenceholidays.com/image/66-05.jpg");
  background-size: 100px;
  background-repeat: no-repeat;
  display: inline-block;
  height: 50px;
  position: relative;
  text-indent: -9999px;
  top: 0px;
  width: 100px;
  border: solid lightblue;
}


/* Features */

.features {
  background: white;
  color: gray;
  padding: 20px;
  display: flex;
  flex-direction: row;
}

.features figure {
  margin: auto;
  text-align: center;
  text-transform: uppercase;
  width: 200px;
}

.features figure img {
  border: 1px solid white;
  border-radius: 10%;
  box-shadow: gray 0 0 10px;
  width: 200px;
}


/* Footer */

footer {
  background: black;
  padding: 10px 20px;
  color: gray;
  font-size: 12px;
  padding: 20px 20px;
  text-align: center;
}

@media (max-width: 600px) {
  .logo {
    position: absolute;
    top: 50px;
    left: 50%;
    transform: translatex(-50%)
  }
}