Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent child's content from stretching parent's width [duplicate]

I have a component (Container) that contains an icon (marked with an X below), a title and a child component (Message) that contains a long message. I would like Container's width to wrap around the icon and the title so both are on one line as much as window's width allows for it.

Message component has a button that toggles display of a long text. This text should not stretch the parent Container and it should be aligned with title's width. The message content can be broken and wrapped at any point:

text toggle off

text toggle on

I used a flex-grow: 1; width: 0; style on a dummy div in Message as suggested here to prevent it from growing. This works well on all browsers except for MS Edge, where the message content stretches the parent:

edge

How can I fix this issue on MS Edge? Is there alternative way using only CSS that I can prevent the message content from stretching its parent?

Style.css:

.box {
  display: table;
  margin: auto;
  border: 1px solid black;
}

.container {
  display: flex;
  justify-content: center;
}

.icon {
  margin-right: 10px;
}

.message {
  display: flex;
}

.message > div {
  flex-grow: 1;
  width: 0;
  word-break: break-all;
}

Container.jsx:

export const Container = () => {
  return (
    <div className='box'>
      <div className='container'>
        <div className='icon'>
          X
        </div>
        <div className='content'>
          <div className='title'>
            Some title
          </div>
          <Message>
            Long message that should not make parent wider
          </Message>
        </div>
      </div>
    </div>
  );
}

Message.jsx:

export const Message = ({children}) => {
  const [isExpanded, setExpanded] = React.useState(false);

  const handleClick = () => setExpanded(!isExpanded);

  return (
    <div>
      <div>
        <button onClick={handleClick}>Click</button>
      </div>
      {isExpanded &&
        <div className='message'>
          <div>{children}</div>
        </div>
      }
    </div>
  );
}
like image 291
Pawel Laskowski Avatar asked Aug 21 '19 21:08

Pawel Laskowski


1 Answers

Try width:0;min-width:100%; on the message container:

.box {
  display: table;
  margin: auto;
  border: 1px solid black;
}

.container {
  display: flex;
  justify-content: center;
}

.icon {
  margin-right: 10px;
}

message {
  display:block;
  width:0;
  min-width:100%;
}
<div class='box'>
  <div class='container'>
    <div class='icon'>
      X
    </div>
    <div class='content'>
      <div class='title'>
        Some title
      </div>
      <message>
        <div>Long message that should not make parent wider</div>
      </message>
    </div>
  </div>
</div>

Or to the div inside the message:

.box {
  display: table;
  margin: auto;
  border: 1px solid black;
}

.container {
  display: flex;
  justify-content: center;
}

.icon {
  margin-right: 10px;
}

message {
  display:block;
}
message  > div {
  width:0;
  min-width:100%;
}
<div class='box'>
  <div class='container'>
    <div class='icon'>
      X
    </div>
    <div class='content'>
      <div class='title'>
        Some title
      </div>
      <message>
        <div>Long message that should not make parent wider</div>
      </message>
    </div>
  </div>
</div>
like image 166
Temani Afif Avatar answered Nov 15 '22 08:11

Temani Afif