flexbox adding newline to clipboard

三世轮回 提交于 2019-11-29 06:22:13

As specified in the previous answer and this post :

In a flex container the child elements ("flex items") are automatically "blockified" ( more details )

depending on your use case, using display: contents can be helpful if you only want to copy / paste text,

see : how display contents works

The easiest way to understand what happens when display: contents is used is to imagine the element’s opening and closing tags being omitted from the markup.

and from the specification :

For the purposes of box generation and layout, the element must be treated as if it had been replaced in the element tree by its contents

( you might want to check the compatibility of this as it won't work in IE and Edge )

$('.toggleFlex').on('click', function() {
  $('.container').toggleClass('flex')
})
.container.flex {
  display: flex;
  color: red;
}

.container.flex span {
  display: contents;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="toggleFlex">toggle</span>
<hr>
<div class="container">
  <span class="label">Label</span>
  <span class="label">Message</span>
</div>
<hr>
<textarea></textarea>

this will override the display:block of the span caused by th flex container :

I think this is due to computed style of your elements. When you don't set flex your span are inline element:

But when you set display:flex they become block element:

Visually you will see them in one line due to the flex property but when doing copy/paste they are considered as block elements so there is a line break between them.

You may refer to the specification for more information


Unfortunately, I am not sure if there is a workaround to avoid this as we cannot control the computed styles in some cases even if we force it using !important.

.container{
    display: flex;
    color: red;
}
span {
  display:inline!important; /*will have no effect*/
}
<div class="container">
    <span class="label">Label</span>
    <span class="label">Message</span>
</div>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!