CSS 子级填充正在拉伸以适合父级

问题描述 投票:0回答:1

我的问题是,在子菜单中,menubutton1 和 2 填充会拉伸以适合右侧的子菜单宽度,而不是像左侧和顶部那样仅在文本周围 10px,以及页面顶部的其他菜单按钮。我试图尽可能地解释这一点,但是如何让子菜单中的菜单按钮表现得像父菜单背景 div 中的菜单按钮一样?我怎样才能使填充不像这样向右延伸?此外,子菜单 div 中的菜单按钮似乎没有任何与子菜单相关的上边距。这是为什么?

我尝试使用

box-sizing: border-box;
,因为这是我读过的类似问题的解决方案,但它没有做任何事情。我是 css 新手,这个网站是一次学习经历,到目前为止我已经能够自己完成所有事情,但我完全陷入困境。

.container {
  height: 100vh;
  display: flex;
  align-items: center;
}

.subcontainer {
  height: 100vh;
}

.menubackground {
  width: 91%;
  height: auto;
  border-radius: 25px;
  margin: auto;
  background: rgba(255, 255, 255, 0.7);
  max-height: 90%;
  min-height: 90%;
  overflow-y: auto;
}

.submenu {
  width: 80%;
  height: auto;
  border-radius: 25px;
  margin: auto;
  background: white;
  min-height: 10%;
}

.title {
  margin: 20px;
  text-align: center;
}

.bodytextcss {
  margin-left: 20px;
  margin-right: 20px;
  margin-bottom: 20px;
  text-align: left;
}

.menubuttons {
  float: left;
}

#menubutton1 {
  border-radius: 25px;
  margin: 10px;
  padding: 10px;
  background-color: #451255;
  color: #ffffff;
}

#menubutton2 {
  border-radius: 25px;
  margin: 10px;
  padding: 10px;
  background-color: #451255;
  color: #ffffff;
}

body {
  background-image: url("background5.png");
  background-size: cover;
  background-color: #451255;
}

titletext {
  font-size: 22px;
  color: #000000;
  font-weight: bold;
}

bodytext {
  font-size: 20px;
  color: #000000;
}

buttontex {
  font-size: 20px;
}
<body>
  <div class="container">
    <div class="menubackground">
      <a href="home.html">
        <div id="menubutton1" class="menubuttons">
          <buttontext>home</buttontext>
        </div>
      </a>
      <a href="recipies.html">
        <div id="menubutton2" class="menubuttons">
          <buttontext>recipies</buttontext>
        </div>
      </a>
      <br><br><br><br>
      <div class="title">
        <titletext>Welcome to the stories section</titletext>
      </div>
      <div class="bodytextcss">
        <bodytext>This is where I will upload short stories that I write</bodytext>
      </div>
      <div class="submenu">
        <a href="test.html">
          <div id="menubutton1">
            <buttontext>story title 1</buttontext>
          </div>
        </a>
        <a href="test.html">
          <div id="menubutton2">
            <buttontext>story title 2</buttontext>
          </div>
        </a>
      </div>
    </div>
  </div>
</body>

html css
1个回答
0
投票

块元素默认占据其容器的宽度。 顶部附近的菜单项使用

float: left;
,因此它们“浮动”在容器中,而不占据整个宽度。

我不想过度使用

float
(或者根本不使用它,如果我诚实的话),但您可以将元素设置为
display: inline-block;
,它提供了
inline
block
样式的合理组合。 在这种情况下,它不会占据容器的整个宽度。

例如:

.container {
  height: 100vh;
  display: flex;
  align-items: center;
}

.subcontainer {
  height: 100vh;
}

.menubackground {
  width: 91%;
  height: auto;
  border-radius: 25px;
  margin: auto;
  background: rgba(255, 255, 255, 0.7);
  max-height: 90%;
  min-height: 90%;
  overflow-y: auto;
}

.submenu {
  width: 80%;
  height: auto;
  border-radius: 25px;
  margin: auto;
  background: white;
  min-height: 10%;
}

.title {
  margin: 20px;
  text-align: center;
}

.bodytextcss {
  margin-left: 20px;
  margin-right: 20px;
  margin-bottom: 20px;
  text-align: left;
}

.menubuttons {
  float: left;
}

#menubutton1 {
  border-radius: 25px;
  margin: 10px;
  padding: 10px;
  background-color: #451255;
  color: #ffffff;
  display: inline-block;
}

#menubutton2 {
  border-radius: 25px;
  margin: 10px;
  padding: 10px;
  background-color: #451255;
  color: #ffffff;
  display: inline-block;
}

body {
  background-image: url("background5.png");
  background-size: cover;
  background-color: #451255;
}

titletext {
  font-size: 22px;
  color: #000000;
  font-weight: bold;
}

bodytext {
  font-size: 20px;
  color: #000000;
}

buttontex {
  font-size: 20px;
}
<body>
  <div class="container">
    <div class="menubackground">
      <a href="home.html">
        <div id="menubutton1" class="menubuttons">
          <buttontext>home</buttontext>
        </div>
      </a>
      <a href="recipies.html">
        <div id="menubutton2" class="menubuttons">
          <buttontext>recipies</buttontext>
        </div>
      </a>
      <br><br><br><br>
      <div class="title">
        <titletext>Welcome to the stories section</titletext>
      </div>
      <div class="bodytextcss">
        <bodytext>This is where I will upload short stories that I write</bodytext>
      </div>
      <div class="submenu">
        <a href="test.html">
          <div id="menubutton1">
            <buttontext>story title 1</buttontext>
          </div>
        </a>
        <a href="test.html">
          <div id="menubutton2">
            <buttontext>story title 2</buttontext>
          </div>
        </a>
      </div>
    </div>
  </div>
</body>

您会注意到两者之间还有一条错误的下划线。 老实说,我建议简化您的结构和样式很多,远远超出本答案范围内解决的范围。 但对于这种特定情况,您所拥有的是围绕整个

<a>
<div>
,并且两者的大小不是很干净。

针对视觉样式的快速修复可以删除

<a>
元素的文本装饰:

text-decoration: none;

这将隐藏下划线作为纯粹的视觉“修复”。

© www.soinside.com 2019 - 2024. All rights reserved.