div 中两个 hr 标签之间的表表不会从 div 中获取所有属性

如何解决div 中两个 hr 标签之间的表表不会从 div 中获取所有属性

全新的 HTML/CSS 和实验。

我正在尝试在两条 hr 标记行(一个在顶部,一个底部)之间放置一张表格。我想让表格的宽度与线条的宽度相匹配,页面上的所有内容都水平居中,并具有粘性定位。

发生的事情是,线条遵循我设置的宽度,居中并具有我想要的定位。表格有定位,但没有宽度,并且与行左对齐(不与页面或任何东西)。

<div style="width:100%; max-width: 800px; margin:auto; position:sticky; top:0px; ">
  <hr>
  <table style="border:1px solid black; background-color:Coral">
    <tr>
      <th><u>Section 1</u></th>
      <th><u>Section 2</u></th>
      <th><u>Section 3</u></th>
      <th><u>Section 4</u></th>
      <th><u>Section 5</u></th>
      <th><u>Section 6</u></th>
    </tr>
    <tr style="text-align:center">
      <td><a href="S1WordFormatting.html">Word<br/>Formatting</a></td>
      <td><a href="S2Lists.html">Lists</a></td>
      <td><a href="S3Tables.html">Tables</a></td>
      <td><a href="S4ImagesandVideos.html">Images and<br/>Videos</a></td>
      <td><a href="S5Linking.html">Linking</a></td>
      <td><a href="S6InputTypes.html">Input Types<br/>(Text Boxes)</a></td>
    </tr>
  </table>
  <hr>
</div>

为什么表格会从 div 中获取定位,而不是宽度或边距?另外,为什么它会与线条左对齐?

我知道我可以通过向 table 标签添加宽度和边距来解决这个问题,但是它可以在 div 标签中修复吗?或者我对 div 标签的工作方式有误解/遗漏吗?我认为它会将任何样式应用于标签间的任何元素。

非常感谢您的帮助!

解决方法

而不是:

<div style="width:100%; max-width: 800px; margin:auto; position:sticky; top:0px; ">

做:

<div style="flex-direction: column;width:100%;display: flex;max-width: 800px;margin:auto;position:sticky;top:0px;">
,

默认情况下,表格不会占用 100% 的父宽度。默认情况下,表格的宽度将取决于 content。因此,只需将 width: 100%; 添加到表格样式,它将占用 100% 的父宽度。

<div style="width:100%; max-width: 800px; margin:auto; position:sticky; top:0px; ">
  <hr>
  <table style="width: 100%; border:1px solid black; background-color:Coral">
    <tr>
      <th><u>Section 1</u></th>
      <th><u>Section 2</u></th>
      <th><u>Section 3</u></th>
      <th><u>Section 4</u></th>
      <th><u>Section 5</u></th>
      <th><u>Section 6</u></th>
    </tr>
    <tr style="text-align:center">
      <td><a href="S1WordFormatting.html">Word<br/>Formatting</a></td>
      <td><a href="S2Lists.html">Lists</a></td>
      <td><a href="S3Tables.html">Tables</a></td>
      <td><a href="S4ImagesandVideos.html">Images and<br/>Videos</a></td>
      <td><a href="S5Linking.html">Linking</a></td>
      <td><a href="S6InputTypes.html">Input Types<br/>(Text Boxes)</a></td>
    </tr>
  </table>
  <hr>
</div>

,

内联样式(使用 style= 设置是一次性定义。

内联 CSS 用于将独特的样式应用于单个 HTML 元素 https://www.w3schools.com/html/html_css.asp

如果你想多次重用同一个样式,使用css类

<style>
.commonWidthAndMargin {
width:100%;
max-width: 800px;
margin:auto;
}
</style>
<div class="commonWidthAndMargin" style="position:sticky; top:0px; "> 
    <hr>
      <table class="commonWidthAndMargin" style="border:1px solid black; background-color:Coral">
        ...etc
      </table>
    <hr>
</div> 

此外,表的默认行为是不扩展到其父级。默认情况下,子 div 将占据其父级的 100%。见:link

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?