html - 将对象附加到居中div的侧面

标签 html css

我想在内容的居中栏将徽标附加到其左上角的外部,如this fiddle所示。

HTML:

<div>
    I'm centered
    <div>
        I'm glued to the centered div
    </div>
</div>


CSS:

div
{
  background-color: pink;
  width: 300px;
  height: 400px;
  margin: 0 auto;
  position: relative;
}
div div
{
  width: 100px;
  height: 100px;
  background-color: cyan;
  left: -100px;
  top: 0px;
  position: absolute;
}


我对自己的解决方案不满意,因为似乎无法将内容和显示分离–在我的情况下,青色框是徽标,粉红色框是内容。我不能同时为内容和徽标创建包装div,因为它是我要居中的内容,而不是内容和徽标。

有没有更清洁的方法?

最佳答案

您可以使用css属性display: flex

像这样:

的HTML

<div class="container">
  <div class="content">
    <div class="logo"></div>
  </div>
</div>


的CSS

.container {
  display: flex;
  justify-content: center;
  flex-direction: row;
}
.content
{
  background-color: pink;
  width: 300px;
  height: 400px;
}
.logo
{
  width: 100px;
  height: 100px;
  background-color: cyan;
  left: -100px;
  position: relative;
}


除非我不明白是什么问题。

关于html - 将对象附加到居中div的侧面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45786685/

相关文章:

html - Chrome 的 `srcset` 属性对应于哪些设备尺寸/分辨率?

html - CSS/HTML : input bar width and button width not the same even though I'm setting it as the same

html - 如何设置使用 angularjs 创建的复选框的样式?

javascript - Canvas .fillText 方法似乎在 iPad 上不起作用

javascript - HTML5 : input type duration

css - 使用 Rails 的中型编辑器

css - IE CSS 错误 : IMG original height affecting parent height

javascript - 如何使用 jQuery 设置动态文本框的只读属性?

javascript - slideToggle() 动画期间出现跨文本换行问题

html - 响应式表重新排序