首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在HTML5上对齐文章

如何在HTML5上对齐文章
EN

Stack Overflow用户
提问于 2021-05-11 16:45:35
回答 1查看 77关注 0票数 1

我正在使用Adobe Dreamweaver为我的学校编码班级创建一个网站,但我遇到了一个问题。

我有两篇文章,正在试着把它们内联起来。它们都被设置为block,我知道它们应该是内联块元素,但将其设置为block会导致问题。

我在这两篇文章上面有一个导航栏,如果我让这两篇文章内嵌块元素,导航栏就会消失。我不知道为什么会发生这种情况,我试着向我的老师和同学寻求帮助,但找不到解决方案。下面是这两篇文章作为块元素时的样子:

这是当它们是内联块元素时的样子:

我希望文章放在一起,如第二张图片所示,但我仍然希望保留我的导航栏。请注意,导航栏的样式为“position:fixed”,因此它始终位于页面顶部。我也想保留这个,但我觉得这可能是我问题的原因。这是我做的代码片段(如果它不能正常工作,图像也不能工作,我很抱歉)

代码语言:javascript
复制
@import url('https://fonts.googleapis.com/css2?family=Limelight&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Zen+Dots&display=swap');
nav {
  height: 120px;
  background-color: black;
  width: 100%;
  display: block;
  margin: -130px 0 0 -10px;
  position: fixed;
}

.dropdown {
  padding-right: 5px;
  margin-right: 5px;
}

.tasa {
  text-transform: uppercase;
  font-family: 'Limelight', cursive;
}

.nav-bar-image {
  width: 110px;
  height: 100px;
  margin: 10px -10px 10px 10px;
  display: inline;
}

nav>ul>li>a {
  text-decoration: none;
  color: white;
}

nav>ul>li {
  font-size: 20px;
  font-family: Zen Dots;
  display: inline;
  padding: 10px;
}

nav>ul>li:hover {
  background-color: darkgrey;
}

nav>ul>li>ul {
  display: none;
  margin-top: 10px;
  font-size: 14px;
  margin-left: 165px;
  list-style-type: none;
  position: absolute;
}

nav>ul>li:hover>ul {
  display: block;
}

nav>ul>li>ul>li {
  padding-left: 10px;
  padding-right: 69.5px;
}

nav>ul>li>ul>li:hover {
  background-color: darkgrey;
}

nav>ul>li>ul>li>a {
  color: white;
  text-decoration: none;
}

nav>ul {
  display: inline-block;
  vertical-align: top;
  margin-top: 20px;
  list-style-type: none;
}

body {
  background-image: url(background.jpg);
}

article {
  background-color: rgba(255, 231, 179, 0.80);
  width: 400px;
  margin: 130px 0 0 10px;
  /*130px 0 0 10px*/
  padding: 5px 10px 10px;
  height: 200px;
  text-align: justify;
  height: 750px;
  display: block;
  verticle-align: top;
}

.left {
  border-top-left-radius: 50px;
  border-bottom-left-radius: 50px;
  height: 200px;
}

.centre {
  margin-top: 0;
  height: 200px;
}

h1 {
  color: white;
  font-family: Limelight;
  font-size: 40px;
  text-align: center;
  margin: 10px 0 0;
}

p {
  text-align: jusify;
  color: white;
}

article>ul>li>a {
  color: white;
  text-decoration: none;
}

article>ul {
  list-style-type: none;
}

::-webkit-scrollbar {
  width: 16px;
}

::-webkit-scrollbar-thumb {
  background: #ffe7b3;
  border-radius: 8px;
}

::-webkit-scrollbar-track {
  background: #ffffff;
  box-shadow: inset 0 0 5px grey;
  border-radius: 8px;
}


/*

::-webkit-scrollbar-button:single-button {
  background-color: #ffffff;
  display: block;
  border-style: solid;
  height: 13px;
  width: 16px;
}

*/

::-webkit-scrollbar-thumb:hover {
  background: #d6c39a;
}


/*

::-webkit-scrollbar-button:single-button:vertical:decrement {
  border-width: 0 8px 8px 8px;
  border-color: transparent transparent #000000 transparent;
}

::-webkit-scrollbar-button:single-button:vertical:decrement:hover {
  border-color: transparent transparent #5e5e5e transparent;
}

::-webkit-scrollbar-button:single-button:vertical:increment {
  border-width: 8px 8px 0 8px;
  border-color: #000000 transparent transparent transparent;
}

::-webkit-scrollbar-button:vertical:single-button:increment:hover {
  border-color: #5e5e5e transparent transparent transparent;
}

*/

html {
  cursor: url(tasa-cursor-medium.cur), default;
}

a {
  cursor: url(tasa-cursor-a-large.cur), default;
}
代码语言:javascript
复制
<header>
  <nav>
    <img src="logo.jpg" alt="TASA logo." class="nav-bar-image">
    <ul>
      <li><a href="Home.html">Home</a></li>
      <li><a href="#">Details</a></li>
      <li class="dropdown"><a href="#">Presentations</a>
        <ul>
          <li><a href="#">Introduction</a></li>
          <li><a href="#">Constellations</a></li>
          <li><a href="#">Lunar Events</a></li>
        </ul>
      </li>
      <li><a href="links.html">Links</a></li>
    </ul>
  </nav>
</header>
<article class="left">
  <h1>TASA</h1>
  <p>

  </p>
</article>
<article class=centre>
  <h1>Images</h1>
</article>

这段代码将文章上的display元素设置为block,而不是inline-block。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-11 19:16:24

问题:当您将类leftcentre设置为内联block时,nav的页边距顶部是-130px。这会让它从屏幕上消失。

更干净的解决方案是使用flex box,并具有一定的灵活性;)项目的对齐方式。在解决方案中,我删除了页边距并对其进行了更改,如下所示:

代码语言:javascript
复制
/* Wrap the class "left" and "centre" with a div (i named it "main_content") */

.main_content {
  display: flex;
  flex-direction: row;
}
nav {
  /* Removed this -> margin: -130px 0 0 -10px; */
  position: fixed;
}

.centre {
  /* Removed this --> margin-top: 0; */
}

nav>ul {
  /* Changed margin-top from 20px to 0px */ 
  margin-top: 0px;
}

代码语言:javascript
复制
@import url('https://fonts.googleapis.com/css2?family=Limelight&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Zen+Dots&display=swap');
nav {
  height: 120px;
  background-color: black;
  width: 100%;
  display: block;
  position: fixed;
}

.dropdown {
  padding-right: 5px;
  margin-right: 5px;
}

.tasa {
  text-transform: uppercase;
  font-family: 'Limelight', cursive;
}

.nav-bar-image {
  width: 110px;
  height: 100px;
  margin: 10px -10px 10px 10px;
  display: inline;
}

nav>ul>li>a {
  text-decoration: none;
  color: white;
}

nav>ul>li {
  font-size: 20px;
  font-family: Zen Dots;
  display: inline;
  padding: 10px;
}

nav>ul>li:hover {
  background-color: darkgrey;
}

nav>ul>li>ul {
  display: none;
  margin-top: 10px;
  font-size: 14px;
  margin-left: 165px;
  list-style-type: none;
  position: absolute;
}

nav>ul>li:hover>ul {
  display: block;
}

nav>ul>li>ul>li {
  padding-left: 10px;
  padding-right: 69.5px;
}

nav>ul>li>ul>li:hover {
  background-color: darkgrey;
}

nav>ul>li>ul>li>a {
  color: white;
  text-decoration: none;
}

nav>ul {
  display: inline-block;
  vertical-align: top;
  margin-top: 0px;
  list-style-type: none;
}

body {
  background-image: url(background.jpg);
}

article {
  background-color: rgba(255, 231, 179, 0.80);
  width: 400px;
  margin: 130px 0 0 10px;
  /*130px 0 0 10px*/
  padding: 5px 10px 10px;
  height: 200px;
  text-align: justify;
  height: 750px;
  display: block;
  vertical-align: top;
}

.main_content {
  display: flex;
  flex-direction: row;
}

.left {
  border-top-left-radius: 50px;
  border-bottom-left-radius: 50px;
  height: 200px;
}

.centre {
  height: 200px;
}

h1 {
  color: white;
  font-family: Limelight;
  font-size: 40px;
  text-align: center;
  margin: 10px 0 0;
}

p {
  text-align: jusify;
  color: white;
}

article>ul>li>a {
  color: white;
  text-decoration: none;
}

article>ul {
  list-style-type: none;
}

::-webkit-scrollbar {
  width: 16px;
}

::-webkit-scrollbar-thumb {
  background: #ffe7b3;
  border-radius: 8px;
}

::-webkit-scrollbar-track {
  background: #ffffff;
  box-shadow: inset 0 0 5px grey;
  border-radius: 8px;
}


/*

::-webkit-scrollbar-button:single-button {
  background-color: #ffffff;
  display: block;
  border-style: solid;
  height: 13px;
  width: 16px;
}

*/

::-webkit-scrollbar-thumb:hover {
  background: #d6c39a;
}


/*

::-webkit-scrollbar-button:single-button:vertical:decrement {
  border-width: 0 8px 8px 8px;
  border-color: transparent transparent #000000 transparent;
}

::-webkit-scrollbar-button:single-button:vertical:decrement:hover {
  border-color: transparent transparent #5e5e5e transparent;
}

::-webkit-scrollbar-button:single-button:vertical:increment {
  border-width: 8px 8px 0 8px;
  border-color: #000000 transparent transparent transparent;
}

::-webkit-scrollbar-button:vertical:single-button:increment:hover {
  border-color: #5e5e5e transparent transparent transparent;
}

*/

html {
  cursor: url(tasa-cursor-medium.cur), default;
}

a {
  cursor: url(tasa-cursor-a-large.cur), default;
}
代码语言:javascript
复制
<header>
  <nav>
    <img src="logo.jpg" alt="TASA logo." class="nav-bar-image">
    <ul>
      <li><a href="Home.html">Home</a></li>
      <li><a href="#">Details</a></li>
      <li class="dropdown"><a href="#">Presentations</a>
        <ul>
          <li><a href="#">Introduction</a></li>
          <li><a href="#">Constellations</a></li>
          <li><a href="#">Lunar Events</a></li>
        </ul>
      </li>
      <li><a href="links.html">Links</a></li>
    </ul>
  </nav>
</header>
<div class="main_content">
<article class="left">
  <h1>TASA</h1>
  <p>

  </p>
</article>
<article class="centre">
  <h1>Images</h1>
</article>
  </div>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67483381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档