You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Know More按钮置于HTML盒子(box)底部?

按钮位置调整方案

问题分析

核心问题有两个:一是按钮不在box容器内部,导致无法定位到盒子底部;二是box的高度设置与flex布局属性搭配不当,无法支撑元素的上下分布。

当前代码

CSS 代码

body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
    background-color: #24576c;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
}

#box {
    width: 80%;
    background-color: #D5C6EE;
    border: 1px solid #ccc;
    border-radius: 10px;
    padding: 20px;
    box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    height: auto;
    
}

#title {
    text-align: center;
    margin-bottom: 10px;
    font-weight: bold;
}

#underline {
    border: none;
    height: 2px;
    background-color: #131E62;
    width: 100%;
    margin-bottom: 20px;
}

#content {
    font-size: 20px;
    text-align: justify;
    line-height: 1.6;
    flex-grow: 1;
}

.button {
    display: flex;
    justify-content: center;
    margin-top: 20px;
}

#know-more-button {
    text-decoration: none;
    background-color: #AABFEF;
    color: white;
    padding: 10px 20px;
    border: none;
    border-radius: 5px;
    font-size: 16px;
    cursor: pointer;

}

#know-more-button:hover {
    background-color: #0056b3;
}

body {
    background-image: url("https://c.wallhere.com/photos/c6/5c/chemistry_science_laboratories-1697571.jpg!d"); 
    background-size: cover; 
    background-repeat: no-repeat; 
    background-position: center; 
    height: 100vh;
    margin: 0; 
}

HTML 代码

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Chem project</title>
    <link rel="stylesheet" href="style.css">
    <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap" rel="stylesheet">
</head>

<body>
    <div id="box">
        <h1 id="title">Brief Introduction</h1>
        <hr id="underline">
        <div id="content">
            <p>content</p>
        </div>
    </div>
    <br>
    <div id="button-container" class="button">
        <a id="know-more-button" href="secondpage.html">Know More</a>
    </div>

</body>
</html>

修正步骤及代码

1. 调整HTML结构:将按钮移入box内部

按钮必须作为box的子元素,才能被定位到盒子底部。修改后的HTML:

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Chem project</title>
    <link rel="stylesheet" href="style.css">
    <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap" rel="stylesheet">
</head>

<body>
    <div id="box">
        <h1 id="title">Brief Introduction</h1>
        <hr id="underline">
        <div id="content">
            <p>content</p>
        </div>
        <!-- 按钮容器移入box内部 -->
        <div id="button-container" class="button">
            <a id="know-more-button" href="secondpage.html">Know More</a>
        </div>
    </div>
</body>

2. 优化CSS:修复flex布局和样式冲突

  • 合并重复的body样式(原代码两次定义body,会导致样式覆盖)
  • 给#box设置最小高度,确保flex布局生效
  • 用margin-top: auto将按钮推到盒子底部

修改后的CSS:

body {
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 0;
    background-color: #24576c;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-image: url("https://c.wallhere.com/photos/c6/5c/chemistry_science_laboratories-1697571.jpg!d"); 
    background-size: cover; 
    background-repeat: no-repeat; 
    background-position: center; 
}

#box {
    width: 80%;
    background-color: #D5C6EE;
    border: 1px solid #ccc;
    border-radius: 10px;
    padding: 20px;
    box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
    display: flex;
    flex-direction: column;
    min-height: 60vh; /* 设置最小高度,保证容器有足够空间 */
}

#title {
    text-align: center;
    margin-bottom: 10px;
    font-weight: bold;
}

#underline {
    border: none;
    height: 2px;
    background-color: #131E62;
    width: 100%;
    margin-bottom: 20px;
}

#content {
    font-size: 20px;
    text-align: justify;
    line-height: 1.6;
    flex-grow: 1; /* 让内容区域占据盒子剩余空间 */
}

.button {
    display: flex;
    justify-content: center;
    margin-top: auto; /* 自动将按钮推到盒子底部 */
}

#know-more-button {
    text-decoration: none;
    background-color: #AABFEF;
    color: white;
    padding: 10px 20px;
    border: none;
    border-radius: 5px;
    font-size: 16px;
    cursor: pointer;
}

#know-more-button:hover {
    background-color: #0056b3;
}

效果说明

  • 按钮现在属于box的一部分,会随着盒子同步移动
  • flex-grow:1让内容区域自动填充盒子的剩余空间,避免内容过少时按钮紧贴文本
  • margin-top:auto利用flex布局特性,将按钮固定在盒子底部,不受内容多少影响

内容的提问来源于stack exchange,提问作者Srijan Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 08:17:04