CSS: Stop cell text from wrapping within cell - html

JSFiddle: http://jsfiddle.net/wTtsV/7/
In the case of overflow in the example, the text is taken newline. How to hide the text instead of being taken newline? I already tried with overflow: hidden, it does not work.

I had success by adding table-layout:fixed to the table.
Then I added overflow:hidden; white-space:nowrap; to the table cell.
I had to adjust the width percentage due to the way table-layout:fixed renders tables.
#table{
display: table;
width: 100%;
table-layout:fixed;
}
#t2{
display: table-cell;
background-color: green;
width:80%;
white-space: nowrap;
overflow:hidden;
}
Working Example - jsFiddle
EDIT:
Here is another method using floated elements rather than display:table.
A minimum width is set on the container to prevent wrapping when the window is very small.
Caveat:
Granted, this is not perfect as you have to specify a min-width for the container.
If text in the left and right divs can vary unpredictably, it will be difficult to determine what min-width is appropriate to prevent wrapping.
<div id="container">
<div id="t1">some text</div>
<div id="t3">some other text</div>
<div id="t2">aaaaaaaaaa...</div>
</div>
#container {
min-width:200px;
width:100% !important;
width:200px;
}
#t1 {
float:left;
background-color: red;
}
#t2 {
background-color: green;
white-space:nowrap;
overflow:hidden;
}
#t3 {
float:right;
}
http://jsfiddle.net/wTtsV/26/

Related

Give an HTML element a static width, so it cannot be shrinked

I have two p HTML elements, the second one must be shown completely. If the container cannot handle both elements, then any shrinks must be applied on the first element only.
The two HTML elements are inline. For example, Let us assume that the first HTML element represented by W and the second one is Y. "| |" represents the outer container.
|WWWWWWWWWWWW YYYY|, here the container can contain both HTML elements, so there is no problem
|WWWWWWW... YYYY|, here the container cannot contain both HTML elements, so the shrink is applied only on the first element.
Please note that the second element has a background color, so its width must be exactly match the text that it contains. For example, if you give him a 25% width and the text is only 11%, then there will be 14% extra color, which is a bad design.
I tried to use flex as the following:
First element: flex: 1 2 auto; where it shrinks more than the second element.
Second element: flex: 1 1 auto; where it shrinks normally
However, this solution have failed.
I believe this is what you were looking for:
HTML
<div id="div1">
<p>ABCDEFGHI</p>
<p>XYZ</p>
</div>
<div id="div2">
<p>ABCDEFGHI</p>
<p>XYZ</p>
</div>
CSS
div {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
align-items: center;
justify-content: center;
}
#div1 {
width: 100px;
}
#div2 {
width: 200px;
}
p:nth-child(1){
width: 65%;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
p:nth-child(2){
width: 25%;
}
try this,
HTML
<div id="outer-container">
<span class="shrink">
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
</span>
<span class="no-shrink">
<span class="content-width">YYYYYYY</span>
</span>
</div>
CSS
body,html{
padding:0px;
margin:0px;
}
* {
box-sizing:border-box;
}
#outer-container{
display:table;
width:100%;
background:#ddd;
margin:0px;
padding:0px;
}
.shrink, .no-shrink{
display:table-cell;
vertical-align:top;
}
.shrink{
max-width:100px; /* initial value, will be overwite by js */
min-width:100px;
overflow:hidden;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.no-shrink{
background:red;
color:#fff;
}
.content-width{
background:green;
display:inline-block;
max-width:200px; /* to limit the second element width growing */
word-break:break-all; /* to limit the second element width growing */
}
JS (JQuery)
$(document).ready(function(){
$(".content-width").css('min-width', $(".content-width").width());
$(".no-shrink").width($('.content-width').width());
$(".shrink").css('max-width', $(".outer-container").innerWidth() - $('.content-width').width());
$(window).resize(function(){
$(".content-width").css('min-width', $(".content-width").width());
$(".no-shrink").width($('.content-width').width());
$(".shrink").css('max-width', $(".outer-container").innerWidth() - $('.content-width').width());
});
});
Fiddle
https://jsfiddle.net/guruling/pkhf8any/

Expand child over parent padding

I've been trying this for a while and I don't seem to find a solution.
HTML:
<table>
<tr>
<td>
<div>this div has to expand over the td padding</div>
</td>
</tr>
</table>
CSS:
table {
height:100%;
}
td {
height:100%;
background: green;
padding:5px;
}
div {
min-width:100%;
height:100%;
background:yellow;
float:left;
white-space:nowrap;
}
I want the div to expand exactly as much as the td but to also expand over the td padding.
Moving the padding to the div element is not a solution since the div has to be 100% height and at least 100% width, the rest of the div's width is overflow:hidden and appears on hover but I try to keep the example as simple as possible so I didn't include that here.
Edit:
#codehorse I've tried your approach but now it appears that the div expands on the whole body so I guess Era is right, relative positioning might not work on td. I could use another wrapper between the td and div but I would like to avoid that if possible. I'm looking for a standard solution on this.
#Era Works perfect Thank you!
Although this is not the right way to do this but if it works for you then use this CSS for div:
div {
margin: -5px;
padding: 5px;
position: relative;
}
div {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
td {
position: relative;
}
If your table structure is not too complex,i'll suggest you use display:table to achieve your purpose.....this way, you'll avoid position attributes, which otherwise conflict with layout sometimes making a big mess of things.
Also, html table is not suggested these days, since you have css tables!!
here is a demo
HTML
<div class="table">
<div class="td">
<div class="inner">this div has to expand over the td padding</div>
</div>
</div>
CSS
.table {
height:100%;
display:table;
}
.td {
height:100%;
background: green;
padding:5px;
display:table-cell;
}
div.inner {
min-width:100%;
margin:-2px; /* change this to suit your need */
background:yellow;
float:left;
white-space:nowrap;
}

Height of outer div not expanding with inner div

I have a bodyMain div of 100% width. Inside it is a body div 800px with auto margin(can I use 'body' as id ?). Inside this are two divs bodyLeft and bodyRight 200px and 600px wide respectively. When I add content to inner divs neither bodyMain nor body expands in height . All heights are auto.
Here is the code: http://jsfiddle.net/TqxHq/18/
HTML:
<body>
<div id="bodyMain">
<div id="body">
<div id="bodyLeft"> left text goes here<br />
</div>
<div id="bodyRight">Right text goes here
</div>
</div>
</div>
</body>
CSS:
#bodyMain{
border:1px solid red;
width:100%;
height:auto;
}
#body{
border:1px solid green;
width:804px;
height:auto;
margin:auto;
}
#bodyLeft{
border:1px solid blue;
float:left;
width:200PX;
height:auto;
}
#bodyRight{
border:1px solid orange;
float:right;
width:600PX;
height:auto;
}
You must add
<div style="clear:both;"></div>
at the end of floating div to fix this issue. see
here
Problem happens when a floated element is within a container box and element does not automatically force the container’s height adjust to the floated element. When an element is floated, its parent no longer contains it because the float is removed from the flow. You can use 2 methods to fix it:
clear:both
clearfix
This is a common issue when working with floats. There are a couple of common solutions:
Add a div after the floats with clear: both
Add the two floats into a container with the CSS attribute overflow: auto
Make the parent element a float
Using the :after CSS pseudo element with the CSS: .clearfix:after {content: "."; display: block; height: 0; clear: both; visibility: hidden;}
Adding a set height to the parent element
See this article
The simple solution is to have outer div overflow:hidden (in style attribute).
Thank you
jsFiddle demo
*{
margin:0;
padding:0;
}
#bodyMain{
position:relative;
overflow:hidden; /*added*/
border:1px solid red;
/*removed height:auto;*/
/*removed width:100%;*/
}
#body{
display:table;/*added*/
border:1px solid green;
width:804px;
margin: 0 auto; /*improved*/
}
#bodyLeft{
border:1px solid blue;
float:left;
width:200px;
/*removed height:auto;*/
}
#bodyRight{
border:1px solid orange;
float:right;
width:600px;
/*removed height:auto;*/
}
To avoid confusion with predefined tag names, refrain from using body, html, or head as ID attribute values.
I agree with Muhammed Irfan's idea. I don't agree with his method though. Avoid inline styling except for small snippets. Especially in this case, because it is likely that there will be another case where clear: both is necessary. So, add a div, give it a meaningful class name and apply the additional CSS.
See this fiddle for an example.

Display text in the middle of box (vertically)

I have a box of fixed width and height, I have a link in it, i want to display the link in the center of box (vertically). Please see this jsfiddle to see the problem
Demo: http://jsfiddle.net/a5hP3/
Here's code anyway:
HTML:
<div class="box">
put it down, in center of box
</div>
CSS:
.box
{
width: 200px;
height: 300px;
border:1px solid green;
}
.box a{
vertical-align:middle; //doesnt work
}
You can set the line-height equal to the height:
.box
{
width: 200px;
height: 300px;
border:1px solid green;
line-height: 300px;
}
http://jsfiddle.net/a5hP3/3
There are two solutions:
First you can set the line-height of your div equal to its height. Unfortunately for this, you need to remember to update the line-height whenever you change the div's height dimension.
Another solution is to place your text within a div that's styled to be displayed as a table-cell with a vertical alignement. This would be similar to placing your text within a table and setting the vertical alignment on its cells:
<div style="outline:#000 thin solid; display:table-cell; height:300px; width:700px; vertical-align:middle">
Some Text
</div>
SEE DEMO
CSS:
.box
{
width: 200px;
height: 300px;
border:1px solid green;
position:relative;
}
.box a{
display:block;
position:absolute;
top:45%;
left:10% /* adjust based on link width */
}
Make the line-height the same as the container height...
http://jsfiddle.net/a5hP3/1/
Note: This solution only works when there is one line of text.
This is a problem better handled by javascript. (I'm using jQuery here):
http://jsfiddle.net/a5hP3/15/

Need 3 divs in a row with the center div filling the width of the page, regardless of content length

I am trying to create an html interface, where rows will be dynamically added to my web page.
The way I am currently doing it is by using nested DIVs with the CSS display style set to table.
Each row has 3 divs. The left div and the right div have a fixed width, while the middle div should expand to fit the page horizontally regardless of the length of it's content.
My problem is I'm not sure how to make that center div expand the entire remaining width of the page. With the code below, the center div is as small as the content.
I tried a solution that floated the left div left, and the right div right, however that would not let me select a row of text properly. i.e., if I started selecting the right div's content, then dragged towards the left, the left and center div would not be selected.
The solution only needs to target webkit based engines, as my code will only be used in a webkit based environment.
EDIT!
I forgot to mention that I also tried using tables. However I also need to avoid getting horizontal scroll bars appearing on the page when the screen is shrinking. When I use tables and shrink the page, the center div stops shrinking at a certain point (due to the fixed width percentages I guess).
My CSS code:
.chatarea
{
display: table;
height = 100%;
padding-top:50px;
margin: 0px;
}
.row
{
#display: table-row;
}
.nick
{
display: table-cell;
width: 140px;
border-style: solid;
text-align: right;
}
.timestamp
{
display: table-cell;
width 50px;
border-style: solid;
}
.message
{
display: table-cell;
border-style: solid;
}
And the relevant html
<div class="chatarea">
<div class="row">
<div class="nick">
<p>Some Nick</p>
</div>
<div class="message">
<p>Some Message</p>
</div>
<div class="timestamp">
<p>Some Timestamp</p>
</div>
</div>
</div>
I believe this is a solution:
Add border-collapse: collapse; to .chartarea to remove the double border width.
Add width: 100% to .chartarea to cover the entire width of the window.
Add width: 80%; to .message to have it grow as the window width changes.
Add white-space: nowrap; to .nick to control wrapping.
Add white-space: nowrap; to .timestamp to control wrapping.
Uncomment display: table-row in .row
Check out this fiddle.
Note: the fiddle page appears to have been removed by the server.
Remove the display styles. Then just use:
.nick {
width: 50px;
float: left;
}
.timestamp {
width: 50px;
float: right;
}
Then .message will take up the remaining width.
Alternatively, just use a <table> element.
If you can't do it fake it. Just put your .nick and .timestamp inside .message and position them absolutely.
.nick
{
width: 140px;
border-right-style: solid;
text-align: right;
height:100%;
position:absolute;
top:0;
left:0;
}
.timestamp
{
width: 50px;
border-left-style: solid;
position:absolute;
top:0;
right:0;
height:100%;
}
.message
{
border-style: solid;
padding:0 50px 0 140px;
overflow:hidden;
position:relative;
}
I've check the fiddle in recent chrome and safari (on windows) and had no problems with selecting text.