css - 根据内容设置自动宽度

标签 css

我使用以下 CSS 代码创建了以下通知:

enter image description here

enter image description here

CSS

.notifications {
    position: fixed;
    z-index: 800;
    width: 20%;
    padding: 10px;
}

我正在尝试根据其中的文本/内容为其设置自动宽度

这是我目前尝试过的尝试:

CSS

.notifications {
    position: fixed;
    z-index: 800;
    width: auto;
    padding: 10px;
}

.notification .right {
    margin-left: 20%;
    white-space: nowrap;
    margin-left: 5%;
}

结果:

enter image description here

enter image description here

最佳答案

做这样的事情怎么样

.notifications {
    position: fixed;
    z-index: 800;
}

.notification .message {
  display: inline-block;
  border-radius: 5px;
  background: black;
  padding: 5px 10px;
  color: white;
}

.notification .message:before {
  content: ' ';
  display: inline-block;
  margin: 2px 5px 0 0;
  border-radius: 50%;
  width: 10px;
  height: 10px;
  background-color: lime;
}
<div class="notification"> 
  <div class="message"> 
    Hello
  </div>
</div>
<div class="notification"> 
  <div class="message"> 
    Hello, I'm much longer
  </div>
</div>

关于css - 根据内容设置自动宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35487852/

相关文章:

html - 半径不同颜色的CSS边框

html - 如何在 CSS 中将图片库居中?

css - 如何自定义 react-popper 箭头

CSS :focus border transition won't work when a border-bottom is already set

html - 如何使用 [position :absolute] inside a parent element with [position:relative]?] 更改子元素的大小

html - 如何让网格布局中的一行溢出顶部而不是底部

css - 减小 Bootstrap 3 中下拉菜单的大小

html - 在父 div 之外时子 div 内容不可见

javascript - 创建了一个选项卡但点击后无法正常工作 - jQuery 问题/新手

php - 如何在 table 上放一个粘头