html - CSS 填充将右对齐对象推出页面

标签 html css

我正在为我的网站制作导航栏。我有一个 div,我想在其中包含 Logo ,并且与最右侧对齐的是一个下载按钮。我使用 CSS 属性“justify-content”和“space- Between”将按钮向右对齐。但是,当我向 div 左侧添加填充时,按钮会被推出页面。

这是我的代码:

  body, html {
            margin: 0;
            background-color: #000000;
        }
        #header {
            padding-top: 10px;
            padding-bottom: 10px;
            height: 30px;
            position: fixed;
            background-color: rgb(10, 98, 160, .5);
            width: 100%;
            border-bottom: 1px solid #808080;
            display: flex;
            justify-content: space-between;
            padding-left: 20px;
        }
    <div id="header">
        <img src="titanium_tsp.png" height="100%">
        <button>Download</button>
    </div>

最佳答案

你说标题是宽度的100%,但是使用content-box默认的box-sizing,这意味着标题的内容是100宽度的%,忽略填充。更改 box-sizingborder-box 以便同时考虑填充:

#header {
    box-sizing: border-box;
    . . .
}

关于html - CSS 填充将右对齐对象推出页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74964428/

相关文章:

php - 为什么我的页面不能在 Internet Explorer 中正确显示?

css - 启用发送网格的点击跟踪后保留链接的 css

html - 如何让我的色谱柱达到平衡?

html - 如何在自动高度计算中包括下降器高度?

html - 根据自身而不是父元素调整 H1、H2... 标签的大小

CSS 打包后搞砸了

css - 如何计算移动设备的宽度和高度

html - 使 div 高度近似恒定,但在 airbnb 中为 "responsive"

javascript - 响应式设计——根据媒体查询修改图像旋转

html - chrome 单选按钮上的白色背景