Max-Width Paragraph in Absolute Container - html

I want a paragraph inside an absolutely positioned element to break the text at a specified max-width, but not fill the entire space if the content is smaller.
<p>Text
<span class="cite">Reference
<span class="citeBox">A long sentence that needs to have a line break.</span>
</span>.
Text
<span class="cite">Reference
<span class="citeBox">Short</span>
</span>
Text.
</p>
.cite {
position: relative;
}
.cite .citeBox {
position: absolute;
max-width: 400px;
left: 0;
/*...*/
}
http://jsfiddle.net/8ebsLc6L/1/
That is, the Short-Box should only be small and the Big-Box should break at 400px. All boxes need to dynamically move with the reference span.
The current code does not expand the Big-Box to 400px.
The solution should not require Javascript.
Would much appreciate your help!

I believe it is not possible to achieve what you want by only modifying your CSS.
However, I think I managed it by adding another <span> element.
The problem here is that the width of .cite is passed down to its children.
But you can work around that by adding another element between them with
position: absolute;
with: 400px;
Such an element will be invisible by default, but it will catch mouse events, so you might want to prevent that, using
pointer-events: none;
From the most inner element, you can now remove the position, left and bottom attributes, but instead add
display: inline-block;
This is necessary in order to display the box as a block in case the text requires more than one line, but still keep it at the lowest width and height possible.
And you might want to re-enable pointer events on the most inner element too:
pointer-events: all;
All the above packed into a fiddle: http://jsfiddle.net/8ebsLc6L/4/

Here is one way of doing it.
Wrap your text in a span element, .citeWrap, and assign the background color, border-radius and so on. Note that you do not need the max-width property anymore.
For .citeBox, set width: 400px.
What will happen is that the .citeWrap will use the 400px as the working width to lay out the text and the line box will be no longer than 400px.
However, if the text is shorter than 400px, the span, being an inline element, will have a shrink-to-fit width, so you will get the background color just covering the text.
.cite {
position: relative;
color: blue;
}
.cite .citeBox {
position: absolute;
width: 400px;
left: 0;
bottom: 20px;
}
.cite .citeWrap {
padding: 5px 15px;
background-color: rgba(0, 0, 0, .6);
border-radius: 5px;
color: #fff;
font-size: 1.2rem;
display: inline-block;
}
p {
margin-top: 4.0em; /* for demo only */
)
<p>To visualize an algorithm, we don’t merely fit data to a chart; there is no primary dataset. Instead there are logical rules that describe behavior. This may be why algorithm visualizations are so unusual, as designers experiment with novel forms to better communicate. Here is Brets wonderful
<span class="cite">Reference<span class="citeBox"><span class="citeWrap">A long sentence that needs to have a line break. Make it a bit longer and you see multiple lines.</span></span></span>.
To visualize an algorithm, we don’t merely fit data to a chart; there is no primary dataset. Instead there are logical rules that describe behavior. This may be why algorithm visualizations are so unusual, as designers experiment with
<span class="cite">Reference<span class="citeBox"><span class="citeWrap">Short</span></span></span>
novel forms to better communicate. Here is Brets.</p>

Related

Whitespace at end of span affects absolute positioning of after pseudo element

In styling a component on my site, I found that adding whitespace to the end of my span that's inside an h2 affects the absolute positioning of span:after in a weird way. The after element I use as a border. The border positions itself after the content without the whitespace, but positions itself over the content when there is whitespace. You can see an example here.
Any ideas on why this would render differently?
html:
<h2>
<span>A test</span> <!-- the border appears 'after' the content -->
</h2>
<h2>
<span>A test </span> <!-- the border appears 'over' the content -->
</h2>
css:
h2 {
position:relative;
height:1.625em/1;
}
h2 span {
font-size: 100%;
margin: 0;
padding: 0;
vertical-align: top;
display: inline-block;
1.625em/1 'MuseoSlab300','Times New Roman',serif;
}
span:after {
content: "";
border-bottom: 5px solid #7c61a0;
position: absolute;
bottom: 5px;
margin-left: 8px;
width: 100%;
}
I think the major difference between those two cases (with the trailing space vs. without) is that a browser will typically wrap content, if needed, at whitespace. So in the first case (with trailing space), the browser has no choice but to push out the width of that span, because the :after pseudo-element can't wrap. In the second case, the browser uses the position:absolute declaration on the pseudo-element as a key to preserve the width of the original span, and with the whitespace there before the pseudo-element, the browser can then wrap the pseudo-element onto the next line. You'd see this effect even if the width of the pseudo-element were set to something small like 10px instead of 100%.
Since this appears differently in Firefox vs. Chrome, I'm not sure which one is actually behaving according to standards, or if this case is even specified.

How can I lay out two <div>s on one line, and have one centre-aligned (relative to the entire line) and the other right-aligned?

I want to display 2 divs in a single line. I have a parent div and two child divs.I want to keep the width of first child div and parent div equal. So the header(label of first child div) displays always middle position of parent div and I want to display the second child div at the right side in the same line of parent div.(Condition is always label of first child div should display middle of parent div). Here is the jsfiddle.
If I were styling this header section for a website, and I wanted some flexibility in styling the various elements, here is out I would start.
For my HTML:
<div class="head">
<div class="innerfirst">
<h1>ABCDEF GHIJ</h1>
</div>
<div class="innersecond">
<label>RIGHT1</label>
<label>RIGHT2</label>
</div>
</div>
I would put the page title in a <h1> tag so that I can adjust font-size, padding, background color and so on. In fact, you could add a tag line below the title line and various background images. Having .innerfirst and h1 gives you quite a bit of flexibility.
The <label> tags don't make sense semantically in this context, but perhaps you will have have input fields later like a search box.
For the CSS:
.head {
background-color:#2191C0;
width: 100%;
height: 85px;
position: relative;
}
The above is fine, set position: relative so that you can use absolute positioning for one of the child elements. The fixed height is a good idea, makes it easier to adjust elements vertically.
.innerfirst {
}
.innerfirst h1 {
text-align: center;
color: #FCFCFC;
padding-top: 10px; /* You could also use a margin... */
}
By default, .innerfirst will have 100% width since it is an in-flow block element, same with the h1 element. You can center the text within h1, and adjust color, padding and margin as needed.
.innersecond {
border: 2px solid lightgray;
color: white;
position: absolute;
width: 25%; /* Set this or by default it will shrink-to-fit content */
height: 61px; /* Set this or by default it will shrink-to-fit content */
top: 5px;
right: 5px;
padding: 5px;
}
What you could do is create a box of text and absolutely position it to the right. It is a good idea
to set a height and width otherwise, as a result of the absolute positioning, the div will shrink to fit the content, which is sometimes useful. The top and right offsets will position the .innersecond to the top-right of the parent container because you set position: relative in .head.
.innersecond label {
display: block; /* optional if you want block behavior */
border: 1px dotted white;
}
Finally, if you want the label tags to behave like blocks, use display: block and style according to you design requirements.
For reference, demo fiddle: http://jsfiddle.net/audetwebdesign/qpb9P/
Here's an updated jsfiddle. Read up on the display property!

Text to Bottom of Div

Is there any way to get the content text of a div to the bottom of it?
Here I've prepared an example.
http://jsfiddle.net/JGuP7/
This is the sample hierarchy:
<div class="button">Button Label</div>
I'm trying to achieve the result without adding additional spans or changing the hierarchy.
Three possible solutions, although each has its own caveats.
jsFiddle demo
The first solution relies on line-height: 220px; vertical-align: bottom; to align the text with the bottom of the DIV. The second solution creates a :before psuedo-element that pushes the text to the bottom of the DIV (this requires no additional markup). Both solutions will fail in IE7 or earlier, and both will have issues if:
The font size changes
The text wraps to a second line
The size of the containing element changes
The third solution relies on the display: box; property, which is a CSS property that is being phased out (more details at Quick Hits With the Flexible Box Model). This solution is only supported by Chrome since v4, Firefox since v2, and IE10 beta. However, a new Flexbox standard has been standardized, but browser support is minimal. This solution could be considered "too new" to be used effectively (html5please.com/#flexbox).
Generally speaking, you should consider a new wrapping element around your text which will allow for position: absolute; bottom: 0; to cozy the element to the bottom of the parent element. This has the benefit of growing the child element upwards as the font size or text length increases, and is agnostic to the parent container's dimensions. Depending on how mission critical your positioning is to your layout, you could add this new element using Javascript. This probably isn't ideal given the question you've posed, but browser support isn't less than ideal for the more wacky CSS solutions I've listed above :-p
Another way to to do this would be by using "display: table-cell" and "vertical-align: bottom".
http://jsfiddle.net/JGuP7/1/
.button {
display: table-cell;
background-color: darkred;
color: white;
width: 120px;
height: 120px;
text-align: center;
vertical-align: bottom;
}
If you use position:relative in the container, and use
position:absolute;
bottom:0;
in the content, you can achieve the desired result.
Your CSS might look like:
.button
{
display: block;
background-color: darkred;
color: white;
width: 120px;
height: 120px;
text-align: center;
position: relative;
}
.bottomContent
{
position:absolute;
bottom: 0;
}
and your HTML:
<div class="button"><p class="bottomContent">Button Label</p></div>
Source

Text overflow in table with variable size

I was looking to implement the following design to the HTML/CSS.
I have got problems with the text overflow in the column. Currently the table column width is given in the percentage format so that the column width will change depending on the screen size, but there is a minimum width too. In the first text column, you can see that the content is extending and produced a second line due to the long size. How to avoid this problem using the text overflow? Or any other solution? Also, you can see that a set of icons are appearing in the same row when the mouse hover takes place. At this time, the text below the icons should hide and it should be shortened as shown in the design. Can you advise me to get a solution to this problem? I have tried text-overflow: ellipsis. But I'm getting problem when the screen width changes. Since I don't have a minimum width due to the variable column width, how to cut short the text in this field? Also in the hover case ??
Please let me know if you want to know anything else.
If you don't want the text to split in multiple rows, add white-space:nowrap rule.
Then, set a max-width for the cell.
For the icons, position them in absolute to the right, with a z-index higher then the text. You'll have to add a relative position to the containing cell also.
To keep them visible over text, i've added a background color (and some left padding).
EDIT: Fix for Mozilla
Mozilla seems to ignore position:relative; for td elements.
To fix it, you've to wrap the td contents inside another div, and apply this style
.tables td {
font-weight: 400;
font-size: 13px;
border-bottom: 1px solid #E1E1E1;
line-height: 38px;
text-align: right;
white-space: nowrap;
max-width: 200px; /* just an example */
}
.tables td > div {
overflow: hidden;
width:100%;
position: relative;
}
.linkFunctions {
display: none;
padding-top: 14px;
position: absolute;
right: 0;
z-index: 999;
background-color: #FFF9DC;
padding-left: 3px;
width: 100%;
max-width: 120px; /* just an example */
text-overflow: ellipsis;
overflow: hidden;
}
It's not exactly what you want (regarding the elipsis) but comes very close.
For the <a> inside the <td> add
td a{
display:block;
overflow:hidden;
width:100%;
white-space:nowrap;
}
(You might need to add a class to them to more easily target them and not ALL <a> inside <td>s).
And regarding the hover. Float the div.linkFunctions right, add the yellow background to it and it will look like it cuts the text accordingly.
All of those require a width to be set, which doesn't make tables fluid as they are intended to be. Use this: http://jsfiddle.net/maruxa1j/
That allows you to automatically set widths on <td> as percentages and the :after automatically sizes the overflowed <span>

percentage width with percentage padding

I have the layout here: http://jsfiddle.net/chricholson/susXf/11/
In Chrome and Firefox you'll notice there is a 1 pixel gap between the edge of the red box and the image. In IE 8, this isn't there (as expected).
This does not happen if I specify the width and padding using pixels.
My guess, although I cannot be sure, is due to the calculations of percentages and how numbers are rounded. The 6% for the padding works out to be 20.94px, the width of the p works out as 328.06. Assuming both are rounded down (despite the fact the first should be rounded up), then the total width is 348px, which seems to be the cause of the problem. IE is maybe more intelligent and rounds up correctly?
Nevertheless, has anyone else come across the same situation and found a fix?
The error is actually caused because of your parent element. You have the width set at 349px. Some browsers, depending on available screen space, will round up or down by default.
It's generally a good practice to use nice widths when using percentages.
Solution: http://jsfiddle.net/vonkly/susXf/29/
This solution allows for an unset, arbitrary image width to define the wrapping element's width and thus, the child span and <p> widths. This achieves the goal of the OP on the fly, without having to manually enter custom classes or width declarations.
CSS
img {
width: 100%;
max-width: 100%;
}
.videoThumb {
position: relative;
display: inline-block;
padding: 0;
}
.videoThumb .details {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
color: #000;
background-color: red;
}
.details p {
display: block;
padding: 14px 4%;
margin: 0;
}
HTML
<div class="videoThumb">
<img src="http://www.chainreactiondisco.com/images/disco_panel_02_01.jpg">
<span class="details">
<p><!-- containing element for black box -->
Watch<br />
Signature Manager vs. Mail Dislcaimers
</p>
<span><!-- /details -->
</div><!-- /videoThumb -->
NOTE
If you are planning on using any <p> tags inside span.details, I'd suggest changing your css a bit. All you need to do is change .details p {} to .details span {}. Then go to your HTML and change the <p> tags to <span> tags.