【问题标题】:inline style react element width内联样式反应元素宽度
【发布时间】:2018-05-10 01:53:54
【问题描述】:

我正在尝试创建一个具有固定宽度的圆形 div。它工作正常,但是一旦我添加了比屏幕上适合的更多的 div,圆圈的宽度就会被覆盖。父容器设置了overflow: scroll,并且 div 是可滚动的。

这是我的头像组件代码

 const Avatar= ({contact, email, circle, width, margin, fontSize, fontWeight}) => {
  let style = {}

  if (width) {
    style.width = `${width}px` || '32px'
    style.height = `${width}px` || '32px'
    style.margin = `${margin}px` || '4px'
  }

  if (circle) {
    style.borderRadius = `${width/2}px` || '16px'
  }

  let displayName;


  if (contact && contact.avatar) {
    return (
      <img style={style}
        src={contact.avatar}
        title={contact.FN || contact.wyreId || email}
        alt={contact.FN || contact.wyreId || email}
      />
    )
  } ... removed code that sets displayname

  // default display
  style.color = '#fff';
  style.background = 'orange';
  style.lineHeight = style.height;
  style.textAlign = 'center';
  style.fontSize = fontSize;
  style.fontWeight = fontWeight;
  return (
    <div style={style}>
        {
          displayName || '?'
        }
      </div>
  )
}

Here is an image of the components working as intended. Here is an image of the components not working as intended.

当我使用开发工具检查时,这是两种情况下添加到 div 的样式。 style=width: 32px; height: 32px; margin: 4px; border-radius: 16px; color: rgb(255, 255, 255); background: orange; line-height: 32px; text-align: center; font-size: 16px; font-weight: bold;

知道可能出了什么问题

也以防万一这里是在父 div 上设置的样式。

.horizontalList {
list-style-type: none;
margin-top: -20px;
margin-left: 2%;
margin-right: 2%;
padding-bottom: 10px;
display: inline-flex;
justify-content: center;
height: 60px;
overflow: scroll;}

【问题讨论】:

    标签: css reactjs width inline-styles


    【解决方案1】:

    我可以通过在 Avatar 组件中设置 style.minWidth =${width}px|| '32px'; 来解决这个问题。

    【讨论】:

      【解决方案2】:

      这不是反应或内联样式问题。从父容器中删除属性 display: inline-flex 并将其添加到圆形 div 中

      检查这个:working example

      【讨论】:

      • 从父级中移除 inline-flex 只会在单独的行中显示圆形 div。
      • 无论你有什么,你都必须将 display: inline-flex 设置为你的图像(或 li-item)。检查我添加到答案中的链接。
      猜你喜欢
      • 2020-09-05
      • 2013-06-14
      • 2010-11-08
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多