简体   繁体   English

如何修复从DIV溢出的文本

[英]How to fix text overflowing from DIV

I've got 2 custom widgets, with HTML in each of them. 我有两个自定义小部件,每个小部件都有HTML。 When I'm viewing the site in desktop mode they're displayed fine, but on smaller resolutions (when the widgets are displayed below main content) the text overflows out of the parent div. 当我在桌面模式下查看网站时,它们显示正常,但在较小的分辨率上(当小部件显示在主要内容下方时)文本溢出父div。

You can see this here in the sidebar 您可以在侧栏中看到这一点

在此输入图像描述

Up to now, I have: 到目前为止,我有:

HTML: HTML:

<div id="cstmwdgt_background_image">
<img src="http://vetromar.com/beta/wp-content/uploads/2013/12/inspection_trip.png" />
<div id="cstmwdgt_text_overlay">
<p class="cstmwdgt_text_style_pink">Realiza tu Viaje de Inspección</p>
<br />
<a class="cstmwdgt_btn_pink" href="#">Click Here</a>
</div>
</div>

CSS: CSS:

/* CUSTOM WIDGET */
/*BG & Float text*/
#cstmwdgt_background_image {
width:100%;
position: relative;
overflow: visible;
white-space:normal
}
#cstmwdgt_text_overlay {
margin-left: 1px;
margin-top: 1px;
padding-right: 5px;
position: absolute;
top: 2px;
left: 10px;
text-align:left;
max-width: 280px !important;
display:block;
}
/*Text Style - Pink*/
.cstmwdgt_text_style_pink {
color: #e82c90;
font-weight:bold;
font-size: 16px;
font-family: Arial;
}
/*Btn Style - Pink*/
.cstmwdgt_btn_pink {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #fff !important;
padding: 5px 10px;
background: #e82c90;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
border: 3px solid #d42d89;
-moz-box-shadow:
    0px 1px 1px rgba(000,000,000,1),
    inset 0px 0px 0px rgba(255,255,255,0);
-webkit-box-shadow:
    0px 1px 1px rgba(000,000,000,1),
    inset 0px 0px 0px rgba(255,255,255,0);
box-shadow:
    0px 1px 1px rgba(000,000,000,1),
    inset 0px 0px 0px rgba(255,255,255,0);
text-shadow:
    0px -1px 0px rgba(000,000,000,0.2),
    0px 1px 0px rgba(255,255,255,0.3);
}
.cstmwdgt_btn_pink:hover{ border: 3px solid #8a1e5a; color: #fff !important;}

Here's a jsfiddle http://jsfiddle.net/breadadams/XaSv8/ 这是一个jsfiddle http://jsfiddle.net/breadadams/XaSv8/

I've also tried with word-wrap: breakword but no luck. 我也尝试过word-wrap: breakword但没有运气。

Just to be clear, there are 2 images (a green one and a pink one) - I'm going to be applying the same things to both of them 为了清楚起见,有两个图像(一个绿色和一个粉红色) - 我将对它们两个应用相同的东西

如果将display:inline-block添加到#cstmwdgt_background_image,它将导致div围绕内容折叠,并反过来强制文本的自动换行。

Remove the 5px padding from the inner text-overlay div, and add it to the outermost stmwdgt_background_image div instead. 从内部text-overlay div中删除5px填充,然后将其添加到最外层的stmwdgt_background_image div中。

Also set the max-width property to the outer widget, not on the text overlay. 同时将max-width属性设置为外部窗口小部件,而不是文本叠加层。 Set the text overlay to 100%. 将文本叠加设置为100%。

#cstmwdgt_background_image {
    width:100%;
    position: relative;
    overflow: visible;
    white-space:normal;
    padding-right:5px;
    max-width: 280px !important;
}
#cstmwdgt_text_overlay {
    margin-left: 1px;
    margin-top: 1px;
    position: absolute;
    top: 2px;
    left: 10px;
    text-align:left;
    width:100%;
    display:block;
}

You could always add a min-width property to the widget to prevent it from getting that small. 您始终可以向窗口小部件添加最小宽度属性,以防止它变小。

Ideally, you want to have the image to be placed as a background with CSS and not as an tag with a as an overlay, but since it seems that you are using Wordpress, I'm not sure how complicated it would be for you to do that. 理想情况下,您希望将图像放置为带有CSS的背景,而不是带有叠加的标记,但是因为看起来您使用的是Wordpress,所以我不确定它对您来说有多复杂去做。

In any case, what you want to do (in a purely CSS solution) would be the following: 在任何情况下,您想要做的(在纯CSS解决方案中)将是以下内容:

  1. Change the Max-width of #cstmwdgt_text_overlay. 更改#cstmwdgt_text_overlay的最大宽度。 I changed it t0 220px and that seem to give the text enough padding on the right to simulate the padding on the left and move all overflowing text to the next line. 我改为t0 220px,这似乎给文本足够的填充在右边模拟左边的填充,并将所有溢出的文本移动到下一行。

  2. Since your button is relative to the size of the text, you will want to give this element an absolute position. 由于您的按钮是相对于文本的大小,因此您需要为此元素提供绝对位置。

  3. Since you're giving a button a fixed position, you will want to take out (or at least control) any padding from the 由于你给了一个固定位置的按钮,你将需要取出(或至少控制)任何填充

    that the user agent is placing. 用户代理正在放置。

  4. If you're not familiar with absolute positioning, you want the element to be positioned absolute relative to the parent element. 如果您不熟悉绝对定位,则希望元素相对于父元素位于绝对位置。

The following code is what I added to your jsFiddle: 以下代码是我添加到你的jsFiddle的代码:

p.cstmwdgt_text_style_pink {
    margin-bottom: 0;
    padding-bottom: 0;
}

a.cstmwdgt_btn_pink {
    position: absolute;
    top: 70px;
    left: 5px;
}

#cstmwdgt_background_image {
    position: relative;
}

Here is the modified jsFiddle: http://jsfiddle.net/breadadams/XaSv8/ 这是修改后的jsFiddle: http//jsfiddle.net/breadadams/XaSv8/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM