html - 使用css定位img元素

标签 html css position margin

代码:

html -> https://pastebin.com/zNekXPLQ 
css -> https://pastebin.com/tifEY0A4

我想使 instagram 按钮距右侧 30 像素,距顶部 30 像素。但是当使用 margin-right: 30px; 时,没有任何反应,而当使用 right: 30px; 时,它就消失了。有人可以解释一下何时以及如何使用 margin-right ect 和 right, left, top, bottom 属性吗?我是 html/css 的新手,定位元素似乎是最难通过的瓶颈。

最佳答案

你有 position:fixed on .topbar 但它没有宽度。设置继承自容器的宽度,然后设置按钮的位置,我建议将class添加到a标签而不是img标签

HTML:

<a href="https://www.instagram.com/majic.photography/" target="_blank" class = "instagrambutton">
     <img src="instagram-logo-white.png" alt="majic.photography" />
</a>

CSS:

.topbar {
    position:fixed;
    width:inherit;
}

.instagrambutton {
    position: absolute;
    width: 50px;
    background-color: black;
    padding: 10px;
    top:30px;
    right:30px
}

关于html - 使用css定位img元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45626036/

相关文章:

javascript - 如何在ionic中的不同 View 中循环数据

css - 需要图像自动调整到 IE 8 中的浏览器窗口

css - 使用 css 样式化 Div

html - 单个 UL CSS 中的三个 LI 列

html - 悬停框位置问题

html - 使用 XSLT 将 XML 转换为 XML

javascript - 如何仅更改图像(html/css/js)的一部分的亮度?

html - “主页”和“联系我们”选项卡在我的下拉导航菜单中的悬停效果不是我想要的

html - CSS 使用 List-Style-Image 调整文本在 HTML 列表中的位置

html - 尽管绝对定位,元素位置在 webkit 浏览器中调整窗口大小时发生变化