【问题标题】:Responsive onesite website padding to centre响应式 onesite 网站填充到中心
【发布时间】:2020-12-28 01:04:10
【问题描述】:

我目前正在制作 onesie 滚动网站,但我遇到了这个问题。我希望我的文字位于第一个“页面”的中心,但我不能让它同时在手机和桌面上看起来不错。 使用 padding-top: 50% 它在移动设备上看起来不错,但在桌面上看起来很糟糕,另一方面 padding-top: 7 在桌面上看起来不错,但在手机上却不多。有人可以帮我找到中庸之道吗?

我的html代码:

section {
  width: 100%;
  height: 100vh;
}

.main section.page1 {
  background: #81D4FA;
}

.main section.page2 {
  background: #F5F5F5;
}

.main section.page3 {
  background: #81D4FA;
}


h1 {
  font-size: 8vw;
  color: #efefef;
  text-align: top;
  padding-top: 50%;
  padding-left: 15%;
}

h2
{
  list-style-type: none;
  font-size: 4vw;
  color: #efefef;
  text-align: center;
  padding-top: 2%;
}

a.read-more{
  border: 2px solid #efefef;
  text-decoration: none;
  color: inherit;
}


.notnav {
  list-style-type: none;
  font-size: 3vw;
  padding-top: 2%;
  padding-left: 5%;
  padding-right: 15%;
}
<div class="main">
  <section class="page1"> 
    <h1>
      ABOUT ME
      <li class="notnav">
        I am John, 19 years old computer science student. Mainly coding in python, but I am quick lerner and flexible person. I am currently intrested in Artificial Intelligence, Big Data and Design 
      </li>
    </h1>
    <h2>
      <a class="read-more" href='/about'>Read More</a>
    </h2>
  </section>
  <section class="page2">        
     <h1>
      PROJECTS
    </h1>
  </section>
  <section class="page3"> 
    <h1>
      CONTACT
    </h1>
  </section>
</div>

【问题讨论】:

  • 您应该考虑对您的 css 使用媒体查询。否则,它永远不会响应您可以在没有 ul 或 ol 的情况下设置 li
  • 尝试做:.main section.page1 {display:flex; flex-direction: column; justify-content:center;} 删除 h1 中的padding-top
  • @Julio 感谢它的工作。还要感谢 MaxiGui,我肯定会尝试媒体查询,以前没听说过,但看起来很有希望
  • @bazzuk123 检查我的答案
  • @bazzuk123 了解 flexbox,我经常使用它!

标签: html css django


【解决方案1】:

试试这个(不使用媒体查询);抱歉迟到了

section {
width: 100%;
height: 100vh;
}

.main section.page1 {
background: #81D4FA;
  }

 .main section.page2 {
    background: #F5F5F5;
    }

   .main section.page3 {
   background: #81D4FA;
   }

   .page1{
  display:flex;
  min-height: 100%;
  align-items: center;
 text-align: center;
 flex-direction: column;
justify-content:center;
}

h1 {
font-size: 8vw;
color: #efefef;    
   }

    h2
   {
   list-style-type: none;
   font-size: 4vw;
   color: #efefef;
   text-align: center;
   }

  a.read-more{
  border: 2px solid #efefef;
  text-decoration: none;
  color: inherit;
  }
  .notnav {
    list-style-type: none;
    font-size: 3vw;
    clear: both;
   }

【讨论】:

  • 评论中有人发布了类似的回答。它非常适合我。
  • 使用媒体查询使代码更长,很高兴你喜欢它。
【解决方案2】:

根据您的代码,这里是使用 媒体查询 适应 section.page1 的示例。这就是你应该用来使你的代码适应和响应的东西。

@media screen and (min-width: 400px){
      section {
        width: auto;
        height: auto;
      }
      section.page1{
        padding: 20px;
      }
      h1 {
        font-size: 8vw;
        color: #efefef;
        text-align: top;
        padding-top: 10px;
        padding-left: 15%;
      }
    }

这是一个关于它的大解释:Mozilla Developper Using_media_queries

通过w3Schools获得更多实践和示例

您还可以使用 bootstrap 通过他们的网格系统获得简单的 css:Bootstrap Grid

section {
  width: 100%;
  height: 100vh;
}

.main section.page1 {
  background: #81D4FA;
}

.main section.page2 {
  background: #F5F5F5;
}

.main section.page3 {
  background: #81D4FA;
}


h1 {
  font-size: 8vw;
  color: #efefef;
  text-align: top;
  padding-top: 50%;
  padding-left: 15%;
}

h2
{
  list-style-type: none;
  font-size: 4vw;
  color: #efefef;
  text-align: center;
  padding-top: 2%;
}

a.read-more{
  border: 2px solid #efefef;
  text-decoration: none;
  color: inherit;
}


.notnav {
  list-style-type: none;
  font-size: 3vw;
  padding-top: 2%;
  padding-left: 5%;
  padding-right: 15%;
}

@media screen and (min-width: 400px){
  section {
    width: auto;
    height: auto;
  }
  section.page1{
    padding: 20px;
  }
  h1 {
    font-size: 8vw;
    color: #efefef;
    text-align: top;
    padding-top: 10px;
    padding-left: 15%;
  }
}
<div class="main">
  <section class="page1"> 
    <h1>
      ABOUT ME
    </h1>
    <div class="notnav">
        I am John, 19 years old computer science student. Mainly coding in python, but I am quick lerner and flexible person. I am currently intrested in Artificial Intelligence, Big Data and Design 
      </div>
    <h2>
      <a class="read-more" href='/about'>Read More</a>
    </h2>
  </section>
  <section class="page2">        
     <h1>
      PROJECTS
    </h1>
  </section>
  <section class="page3"> 
    <h1>
      CONTACT
    </h1>
  </section>
</div>

【讨论】:

  • 这会在我的代码中产生错误,“页面”除外,第一个的长度很短,但我看到了这个解决方案的概念。据我了解,这个 madia 查询类似于 if 语句,当您的屏幕宽度超过 640 像素时,它会更改代码。我想对了吗?
  • 是的,这里我先设置了 640px,但是你可以设置为 400px,或者任何你想要的宽度。您还可以创建超过 1 个媒体查询。我制作的示例从您的代码中非常基本(我不打算修复您的整个页面)。我想,你需要练习
  • @bazzuk123 这些媒体查询宽度称为断点。因为这是您的 css 将“打破”以优先考虑您希望的 css 的地方。我想你的问题已经回答了。
猜你喜欢
  • 1970-01-01
  • 2018-09-02
  • 1970-01-01
  • 2014-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-28
  • 2019-03-06
相关资源
最近更新 更多