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

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/

Related

How to center my div in responsive design way?

I am trying to do a text-align center in my elements and I want everything inside that element has text-align left.
Here is my html
<div id='wrapper'>
<div id='inside-wrapper'>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
</div>
</div>
#inside-wrapper{
text-align:center;
}
#inside-wrapper div{
display:inline-block
text-align:left;
}
The above codes don't work and I need to center my elements like this
----------------------
|
| test test test //inside-wrapper is center but elements inside inside-wrapper is
| test //text-align left
|
I could use margin:0 auto and setup width for the inside-wrapper but I am also doing responsive design so I can't really set the width in my inside-wrapper. How do I resolve this? Thanks.
Regarding the alignment of elements:
The text-align propertly only applies to text or inline-block elements (like span or a), not block elements (like div or p).
I think this is what you're looking for:
HTML:
<div id="Outer">
<div id="Inner">
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
</div>
</div>
CSS:
#Outer {
width: 150px;
height: 150px;
background: yellow;
overflow: hidden;
}
#Inner {
width: 100px;
height: 100px;
background: red;
margin-top: 25px;
margin-left: 25px;
}
#Inner div {
display: inline-block;
}
Check the jsfiddle here.
As you may notice, you can use the auto property to align elements horizontally but you'll need to specify an specific margin-top size in order to center the inner element vertically; the overflow property must also be set to hidden, otherwise the margin will push your div down.
Update:
If you don't need to vertically center your inner div, you can stick with this (the inner-div's width is also relative), check this jsfiddle:
CSS:
#Outer {
width: 150px;
height: 150px;
background: yellow;
overflow: hidden;
}
#Inner {
width: 75%;
height: 75%;
background: red;
margin-left: auto;
margin-right: auto;
}
#Inner div {
display: inline-block;
}
By default, all div's widths are set to 100% and their heights to wrap their contents, so if you need to center your div without havning to set its width, you should do something like this:
CSS:
#Outer {
width: 150px;
height: 150px;
background: yellow;
text-align: center;
}
#Inner {
display: inline-block;
background: red;
margin-left: auto;
margin-right: auto;
}
#Inner div {
display: inline-block;
}
In this case you can center your inner div with the text-align property but you'll need to set its display property to inline-block. Check this jsfiddle.
Your code seems to be working, if you just fix the missing semicolon after display: inline-block
#inside-wrapper div{
display:inline-block; // Missing semicolon here.
text-align:left;
}
As for the last child being centered, you could easily use pseudo selector :last-child, to float the div left.
Your Working Fiddle

CSS fill remaining horizontal space and show an ellipsis

I'm trying to create a table-like-display containing 2 columns using div elements. One column is for an image URL while the other is for the file size. The file size column has a fixed width of 150px and the URL column should fill the remaining space. If there is insufficient space for the URL it should cut off and display an ellipsis. However what happens is the text just continues to the next line. I've also tried setting white-space: nowrap; but that causes the whole div to break onto a new line.
My HTML code look like this.
<div id="container>
<div class="row">
<div class="imageURL">http://mywebsite.com/image1.jpg</div>
<div class="fileSize">586 KB</div>
</div>
<div class="row">
<div class="imageURL">http://mywebsite.com/image2.jpg</div>
<div class="fileSize">785 KB</div>
</div>
<div class="row">
<div class="imageURL">http://mywebsite.com/image3.jpg</div>
<div class="fileSize">258 KB</div>
</div>
</div>
And here is the CSS.
#container {
width: 100%;
}
.row {
padding: 5px;
}
.imageURL {
display: inline;
text-overflow: ellipsis;
overflow: hidden;
word-break: break-all;
}
.fileSize {
display: inline-block;
width: 150px;
float: right;
}
I think you are looking for something more like this in the following JSFIDDLE. I have rearranged your CSS to look like the following:
#container {
width: 100%;
}
.imageURL {
display: inline-block;
text-overflow: ellipsis;
overflow: hidden;
width:auto;
min-width:100px;
max-width:65%;
white-space:nowrap;
}
.fileSize {
display: inline-block;
margin-left: 100px;
}
.row {
overflow: hidden;
white-space:nowrap;
}
Mainly, I have added the white-space nowrap property along with keeping both divs on the same line, I took out the float, which had made it a block level element, and just placed a margin to be a specific amount from it. The key comes from the width in the .imageURL class, where I added an auto width, a min width, and a max width, which allows the ellipsis to take place at certain points.
Change .imageURL to the following:
.imageURL {
white-space: nowrap;
text-overflow: ellipsis;
width: 400px;
display: block;
overflow: hidden
}
Hope This Helps!;)
JSFiddle Example

CSS: Stop cell text from wrapping within cell

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/

how to collapse width of parent element to total width of child elements

My parent width takes up the whole 100% width of parent element.
Instead, I want parent width to collapse to total width of child elements.
jsfiddle : http://jsfiddle.net/z9Unk/232/
Notice how red-bordered parent width is greater than total width of green-colore child elements. I want to collapse the parent width.
Please advise necessary changes.
HTML
<div class="item1">
<div class="item2">
item2
</div>
<div class="item2">
item2
</div>
</div>
CSS:
.item1 {
position:relative;
white-space: nowrap;
width:auto;
overflow: hidden;
border:2px solid red;
}
.item2 {
position:relative;
display:inline-block;
background-color: green;
width : 255px;
height : 205px;
margin-right:6px;
border:1px solid blue;
}
.item1 is a block-level element which takes 100% of width of its parent by default.
You could change type of display of .item1 element to table or inline-block:
.item1 {
/* other CSS declarations */
/* display: inline-block; */ /* Or */
display: table;
}
Here is the Fiddle.
Note: If Internet Explorer doesn't run in compatibility view, there would be no problem.
However to force IE to run in standard mode, you could add following <meta> tag in the <head> section:
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
More info: http://msdn.microsoft.com/en-us/library/cc288325%28VS.85%29.aspx
the Item1 will always expand to the width of its parent because it is set as display:block.
one way to fix this is to add to Item1:
float:left
Another way is to use the fit-content value for the CSS property width. But it is supported only by Firefox and Chrome…
Example :
.item1 {
position:relative
white-space: nowrap;
width: -moz-fit-content;
width: -webkit-fit-content;
width: fit-content;
overflow: hidden;
border:2px solid red;
}

CSS Table Styles - Get table row to fill entire content area

I'm trying to use the CSS table display to layout my page and I'm having trouble getting my main content area to take up the entire area (vertically) between the header and the footer. The main div contains some floated elements that don't necessarily extend the length of the screen. Basically, no matter what I do, the area of my main content is decided by the vertical height of these elements. Is there anything I can do about this? Thanks!
Html:
<div id="all-container">
<div id="header">
...
</div>
<div id="table-container">
<div id="content">
<div id="side-bar">
...
</div>
<div id="main">
... some content that's floated ...
</div>
</div>
</div>
<div id="footer"></div>
</div>
CSS:
#all-container {
margin:0px;
position:relative;
min-height:100%;
background-color:#E6DCD8;
}
#header {
height:60px;
padding-left:20px;
padding-right:20px;
background-color:#685642;
}
#table-container {
display:table;
height:100%;
}
#content {
display:table-row;
height:100%;
}
#side-bar {
display:table-cell;
vertical-align:top;
padding-right:100px;
height:100%;
padding-bottom:60px;
}
#main {
display:table-cell;
vertical-align:top;
border-left-style:solid;
border-left-width:normal;
border-left-color:#685642;
padding-bottom:60px;
height:100%;
}
#footer {
position:absolute;
width:100%;
height:50px;
bottom:0px;
background-color:#685642;
}
I'm going to take a shot in the dark at trying to answer. These are my suggestions, not necessarily the canonical correct answer you're looking for.
Not answering exactly to the question on table layout per say, but I'm offering other ways to achieve the same desired result.
This is your original code in the fiddle http://jsfiddle.net/CRzfS/
I think you have at least two design objectives here you want to achieve:
make a full screen height layout
make a 2-column layout
I'll have to put it forward first, that there are many ways to achieve the objective, but all has their limitations due to browser support. I also advise against table layouts unless it is necessary.
For me, display: table is only used for one reason mostly: Making vertical-align work in a fixed-height container, especially vertical-align: middle. There are also relevant uses for the auto calculation of table-cell widths from a fixed-width table, but it all depends on how you want to present data or information.
We'll face the issues one by one.
Full Height
First is the layout's height issue. Height flexibility has always been a sore point in web design layouts.
To fill screen height only, you can look at this sticker footer implementation:
http://www.cssstickyfooter.com/
Here's an example fiddle with full screen height, not taking footer implementation into account: http://jsfiddle.net/CRzfS/3/
CSS:
html, body {
height: 100%;
min-height: 100%;
}
For liquid height layout you can look at this: http://www.mightymeta.co.uk/superstretch-a-vertically-fluid-layout-using-css/
For a proper flexible height, you'll have to use CSS Flexbox.
http://caniuse.com/#feat=flexbox
You can try it out here http://flexiejs.com/playground/
Your example implemented using CSS Flexbox: http://jsfiddle.net/CRzfS/4/
CSS:
html, body {
height: 100%;
min-height: 100%;
margin: 0;
padding: 0;
}
#all-container {
display: -webkit-box;
display: -moz-box;
display: box;
-webkit-box-orient: vertical;
-moz-box-orient: vertical;
box-orient: vertical;
margin:0px;
position:relative;
min-height:100%;
height: 100%;
background-color:#E6DCD8;
}
#header {
height:60px;
padding-left:20px;
padding-right:20px;
background-color:#685642;
}
#table-container {
display: -webkit-box;
display: -moz-box;
display: box;
-webkit-box-flex: 1;
-moz-box-flex: 1;
box-flex: 1;
-webkit-box-orient: vertical;
-moz-box-orient: vertical;
box-orient: vertical;
}
#content {
display: -webkit-box;
display: -moz-box;
display: box;
-webkit-box-flex: 1;
-moz-box-flex: 1;
box-flex: 1;
overflow: hidden;
}
#side-bar {
vertical-align:top;
min-width: 150px;
}
#main {
-webkit-box-flex: 1;
-moz-box-flex: 1;
box-flex: 1;
vertical-align:top;
border-left-style:solid;
border-left-width:normal;
border-left-color:#685642;
}
#footer {
width:100%;
height:50px;
background-color:#685642;
}
Two-column Layout
There are many ways to achieve this. Considerations have to be made for the differing screen sizes you're supporting(which is a major headache). Each has their own drawbacks. And also it depends on your width requirements i.e. fixed width, flexible width.
semantic way by absolute positioning the sidebar and setting margin for main content
common method used by layout frameworks via floating sidebar and container so they are side by side
using display: inline-block to the same effect as #2.
The first method here, sets your #side-bar after the #main in the HTML. Then using CSS absolute positioning to set #side-bar to the left side, and setting margin-right for your #main. http://jsfiddle.net/CRzfS/2/
HTML:
<div id="table-container">
<div id="content">
<div id="main">
... some content that's floated ...
</div>
<div id="side-bar">
...
</div>
</div>
</div>
CSS:
#table-container {
position: relative;
}
#content {
height: 200px;
}
#side-bar {
width: 200px;
position: absolute;
left: 0;
top: 0;
background-color: rgba(255,255,255,0.5);
height: 100%;
}
#main {
margin-left: 200px;
height: 100%;
overflow: hidden; // for floated elements within
}
The second method here, using the original HTML, you'll only need to set the CSS. http://jsfiddle.net/CRzfS/5/
#table-container {
overflow: hidden;
}
#content {
width: 100%;
}
#side-bar {
width: 33%;
float:left;
}
#main {
width: 66%;
float: left;
vertical-align:top;
border-left-style:solid;
border-left-width:normal;
border-left-color:#685642;
padding-bottom:60px;
min-height: 100px;
overflow: hidden; // for floated elemnts
}
How to combine these 2 layout requirements together will be difficult if I'm not sure of what you exactly require for the vertical height part.
I'll need more information before I can give a relevant answer tailored to your question.
Resources
If you're open to layout grid systems framework, I'll suggest you take a look at: http://twitter.github.com/bootstrap/scaffolding.html#gridSystem
Even if you don't want to use it, just looking at the CSS implementation will yield you interesting insights.
I'll be adding other jsfiddle examples as more information on the question comes.
Edit: More information and explanations added.