2017-08-29 66 views
10

我猜这两个属性实际上并不共同努力,但我的情况:麻烦与NOWRAP和最大宽度的绝对定位的元素

我试图创建一个工具提示组件。我的工具提示是绝对定位的,因为我不知道内容的长度是多少,所以没有宽度。因此,与宽度相关的CSS,文本只是形成一个高大而瘦的列。我试过max-width,但是在它自己身上,那什么都不做。所以我决定尝试white-space: nowrap,虽然它很好地不包装文本,但它似乎并没有以一种有用的方式来承认最大宽度,文本反而超出了元素的边界。

我想不出如何解决我的问题,如果有一个干净的解决方案。我希望有一个绝对定位的div,可以扩展以适应其内容,直到达到最大值,然后进行包装。我看到的一个建议是将元素设置为flexbox,但从我所知道的来看,这对IE来说并不好,所以我认为在我的情况下不可行。有什么建议?

.wrapper { 
 
    position: relative; 
 
    display: inline; 
 
} 
 

 
.info { 
 
    position: absolute; 
 
    bottom: 1.2em; 
 
    left: 0; 
 
}
<div class="wrapper"> 
 
    <span>[ ? ]</span> 
 
    <div class="info">Any long text can go in here to test what goes wrong with wrapping.</div> 
 
</div>

+0

你能告诉我们任何代码指定的值?也许你试图解决这个问题的代码?会有帮助 –

+0

@OvidiuUnguru因为它的CSS我不明白的互动,我不知道我可以告诉你显示我试图“修复”它......如我所描述的,我尝试了一些不同的属性,但没有一个获得理想的效果。我发布了基本代码,但没有给出相同的代码3次,它们之间有一个属性差异,不知道要提供什么。 – RhoVisions

+0

https://codepen.io/anon/pen/yodpao看到这个,如果这确实能解决你的问题 –

回答

1

添加使用white-space:nowrap一个min-width:100%white-space: nowrap;

.wrapper { 
 
    position: relative; 
 
    display: inline; 
 
} 
 
    
 
.info { 
 
    position: absolute; 
 
    min-width: 100%; 
 
    white-space: nowrap; 
 
}
<div class="wrapper"> 
 
    <span>[ ? ]</span> 
 
    <div class="info">Any long text can go in here to test what goes wrong with wrapping.</div> 
 
</div>

+0

不幸的是,我不能让包装一个块,因为不是每个获得这种工具提示的元素都是一个块元素。我正在创建的组件是为了内联旁边的其他元素,最好是内联块。当我需要直接将这样的工具提示附加到元素时,这并没有帮助。 – RhoVisions

+0

你有没有试过用'display:flex;'玩弄?我会这样做,但我需要看到更多元素才能够重现您的错误 –

+0

正如我所提到的,我对flex的关注是IE兼容性。关注的不是它继续的元素(可以是任何),而是你所指出的包装的显示。 – RhoVisions

4

避免因为这将限制你的文字一行。 max-width应该使用绝对显示但不在内联元素内的块级元素。为了解决这个问题,我把工具提示放在你的包装块之外,并使用javascript将它放置在鼠标位置。

这是您的问题的一个简单的解决方案。点击“打开工具提示”以显示工具提示并移动滑块以更改max-width的值。

showContext = function() { 
 
    var e = window.event; 
 

 
    var posX = e.clientX; 
 
    var posY = e.clientY; 
 
    var info = document.getElementById("info"); 
 
    info.style.top = posY + "px"; 
 
    info.style.left = posX + "px"; 
 
    info.style.display = "block"; 
 
} 
 

 
changeWidth = function(value) { 
 
\t \t var info = document.getElementById("info"); 
 
    info.style.maxWidth = value + "px"; 
 
}
.wrapper { 
 
    position: relative; 
 
} 
 

 
.info { 
 
    position: absolute; 
 
    max-width:300px; 
 
    display:none; 
 
    border:1px solid black; 
 
    background-color:white; 
 
} 
 

 
.range { 
 
    margin:10px 0px 10px 0px; 
 
    display:block; 
 
}
<div class="wrapper"> 
 
    max-width slider 
 
    <input id="range" class="range" type="range" min="100" max="600" oninput="changeWidth(this.value)"/> 
 
    <input type="button" value="open tooltip" onclick="javascript:showContext();" /> 
 
</div> 
 
<div id="info" class="info">Any long text can go in here to test what goes wrong with wrapping.</div>

3

我不能完全确定你的目标是什么,因为有事情很多矛盾的事情。但我会尽力,希望你能引导我对你的期望的解决方案:

https://jsfiddle.net/q7dyf6xh/

.wrapper { 
    position: relative; 
    display: run-in; 
} 

.info { 
    position: absolute; 
    max-width: 200px; 
    white-space: pre-line; 
} 

看一看这个小提琴,你可以看到工具提示现在有一个最大宽度。看看我使用的是什么:

display: run-in;:显示一个元素作为阻止或内联,根据上下文

white-space: pre-line;:空白的序列会崩溃到一个单一的空白。文本将在必要时换行,并在换行符

为了更好地理解的事物如何运作这里看看:

空白:如果您使用NOWRAP文本永远不会换到下一线。文本继续在同一行,直到遇到标签!

这就是说你的max-width仍然在工作,但是用nowrap你溢出现在你的元素。试着给你的元素background-color,你会发现它实际上只有你的max-width定义的宽度。

这里看看它是如何溢出的元素:https://jsfiddle.net/fcnh1qeo/

现在宽overflow: hidden只有你框内的文字会显示出来。其他一切都被切断了!在这里看到:https://jsfiddle.net/0qn4wxkg/

我现在使用的是display: run-in;white-space: pre-line;以及max-width: 200px,这将给你希望你的期望的解决方案。不知道使用它的上下文和代码更多的是猜测而不是答案。但也许我可以引导你找到适合你需求的解决方案

干杯!

+0

问题是'display:run-in'在firefox上根本不起作用 –

1

不是那样以前我自己有一个非常类似的问题。我使用flexbox修正了它在这里的评论中已经提到的内容。

我的代码如下所示:

.has-tooltip { 
    display: inline-flex; align-items: flex-start 
} 

.has-tooltip > .tooltip { 
    padding: 1em; 
    max-width: 300px; 
    background: #bdc3c7; 
    word-wrap: break-word; 
    transform: translate(-50%,-110%) 
} 

我也是为了以防万一复制到this fiddle这个要看看它。 (:。

1

你是正确的,这是行不通

这里的,如果你被允许使用BR标记,有效的解决方案我已经提示合作很多次,这是我的最佳解决方案。

Codepen: https://codepen.io/btn-ninja/pen/JNJrgp

它的工作原理是利用空白NOWRAP使用CSS转换:

<button type="button" class="btn btn-block hasTooltip"> 
    Tooltip on top 
    <i class="tip">Most tooltips are short<br>but you can add line breaks.</i> 
</button> 

<button type="button" class="btn btn-block hasTooltip right"> 
    Tooltip on the right. 
    <i class="tip">Tooltip on right<br>vertically centered.</i> 
</button>  

.hasTooltip .tip { 
    position: absolute; 
    bottom: 100%; left: 50%; 
    transform: translateX(-50%); } 

.hasTooltip.right .tip { 
    bottom: auto; left: 100%; top:50%; 
    transform: translateY(-50%); } 

翻译允许绝对定位的工具提示水平或垂直居中对内容。带有BR的空白空间实现了长内容的包装,同时允许较短的工具提示与工具提示文本的宽度相匹配。

下面是完整的CSS:

.hasTooltip { 
    position:relative; } 

.hasTooltip .tip { 
    display:block; 
    background: #333; color: #fff; 
    box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.5); 
    font-size:inherit; 
    font-style:normal; 
    line-height: 1rem; 
    text-align:center; 
    padding: 8px 16px; 
    border-radius:4px; 
    margin-bottom:5px; 
    pointer-events: none; 
    position: absolute; 
    bottom: 100%; left: 50%; transform: translateX(-50%); 
    opacity: 0; 
    transition: opacity .34s ease-in-out; 
    white-space:nowrap; 
    z-index:99; } 

.hasTooltip .tip:before { 
    content: ""; 
    display:block; position:absolute; left:0; bottom:-5px; 
    width:100%; height:5px; } 

.hasTooltip .tip:after { 
    border-left: solid transparent 6px; 
    border-right: solid transparent 6px; 
    border-top: solid #333 6px; 
    bottom: -4px; 
    content: ""; 
    height: 0; 
    left: 50%; 
    margin-left: -6px; 
    position: absolute; 
    width: 0; } 

.hasTooltip:focus .tip, 
.hasTooltip:hover .tip { 
    opacity: 1; 
    pointer-events: auto; } 

.hasTooltip.right .tip { 
    bottom: auto; left: 100%; top:50%; transform: translateY(-50%); 
    margin-bottom:0; 
    margin-left:5px; } 

.hasTooltip.right .tip:before { 
    left:-5px; bottom:auto; } 

.hasTooltip.right .tip:after { 
    border-left: 0; 
    border-top: solid transparent 6px; 
    border-bottom: solid transparent 6px; 
    border-right: solid #333 6px; 
    bottom:auto; 
    left: -4px; 
    top:50%; 
    margin-left: 0; 
    margin-top: -6px; } 
1

display="runin"元素生成运行框。磨合元素就像内联或块一样,取决于周围的元素。即: 如果运行框包含一个块框,与块相同。 如果一个块盒在引入框之后,引入框成为块框的第一个嵌入盒。 如果随后出现一个内联框,则导入框变成一个块框。

pre-line空白序列已折叠。线条以新行字符分隔,位于<br>,并根据需要填充线框。 下表总结了各种white-space值的行为:

max-width CSS属性设置元素的最大宽度。它可以防止width属性的使用值变得比max-width.

.wrapper { 
    position: relative; 
    display: run-in; 
    top: 100px; 
} 


.info { 
position: absolute; 
bottom: 1.2em; 
left: 0; 
max-width: 200px; 
white-space: pre-line; 
background-color: #ddd; 

}

相关问题