CSS expand <p> to fit text inside absolutely positioned parent? - html

Here is a codepen with my issue http://codepen.io/anon/pen/aNWEvQ.
HTML:
<div class="dialog-wrapper">
<div class="dialog">
<p>aaaaaaaaaaaaaaaaaaaaaaaaaaaa</p>
</div>
</div>
CSS:
* {
box-sizing: border-box;
}
.dialog-wrapper {
border: 1px solid red;
position: absolute;
}
.dialog {
min-width: 50px;
max-width: 80%;
border: 1px solid green;
margin: auto;
}
p {
border: 1px solid blue;
font-size: 16px;
font-family: Arial;
}
Why does the p tag not expand to the width of the text and force .dialog and .dialog-wrapper to expand as well?
Is there CSS that can make the paragraph expand as I desire?

Since .dialog-wrapper is absolutely positioned and all left, right and width are auto, its width will be determined by the shrink-to-fit algorithm. Basically, it will be the width of the text.
Then, if you use max-width: 80% on a child, that will be smaller than the width of the text. So the text will overflow, or it will break into multiple lines.
Instead, you should add some margin:
.dialog {
margin: 0 10%;
}
* {
box-sizing: border-box;
}
.dialog-wrapper {
border: 1px solid red;
position: absolute;
}
.dialog {
min-width: 50px;
border: 1px solid green;
margin: 0 10%;
}
p {
border: 1px solid blue;
font-size: 16px;
font-family: Arial;
}
.wrapper2 {
top: 100px;
}
.wrapper2 p {
width: 253px;
}
.wrapper3 {
top: 200px;
}
.wrapper3 p {
width: 315px;
}
<div class="dialog-wrapper">
<div class="dialog">
<p>aaaaaaaaaaaaaaaaaaaaaaaaaaaa</p>
</div>
</div>
<div class="dialog-wrapper wrapper2">
<div class="dialog">
<p>aaaaaaaaaaaaaaaaaaaaaaaaaaaa</p>
</div>
</div>
<div class="dialog-wrapper wrapper3">
<div class="dialog">
<p>aaaaaaaaaaaaaaaaaaaaaaaaaaaa</p>
</div>
</div>

Change the .dialog { max-width: 100%; }
and both they will expand.

Try this :
.dialog-wrapper {
display: table;
// add other properties
}
.dialog {
display: table-cell;
// add other properties
}
p {
display: table-cell;
// add other properties
}
Now the parent container dialog-wrapper will now adjust to the width of the child elements, i.e. dialog and p.

Use word-wrap:break-word on the style for p.
Remove fixed widths of p. You may also want to give your
.dialog-wrapper a fixed width.
codepen: http://codepen.io/anon/pen/WwjddX

Related

Why does block with text shift to bottom?

Why does block with text shift to the bottom? I know how to fix this issue (need to add "overflow: hidden" to the box), but I don't understand why it shift to the bottom, text inside the box is short, margins in browser-inspector are same as margins of example without text.
Example of the problem
HTML:
<div class="with-text">
<div class="box1">
SIMPLE TEXT
</div>
<div class="box2">
</div>
</div>
<div class="without-text">
<div class="box1">
</div>
<div class="box2">
</div>
</div>
CSS:
html, body {
font-size: 10px;
margin: 0;
height: 100%;
}
.box1 {
display: inline-block;
margin: 5px;
width: 50px;
height: 50px;
background: blue;
/* Fix the problem */
/* overflow: hidden; */
color: white;
}
.box2 {
display: inline-block;
margin: 5px;
width: 50px;
height: 50px;
background: red;
}
.with-text:before {
display: block;
content: "with-text";
text-transform: uppercase;
margin: 1rem;
}
.with-text {
box-sizing: border-box;
height: 50%;
border: 1px solid;
}
.without-text:before {
display: block;
content: "without text";
text-transform: uppercase;
margin: 1rem;
}
.without-text {
box-sizing: border-box;
height: 50%;
border: 2px solid black;
}
The problem is that by default vertical alignment of inline elements – baseline,
The text inside element affects it and pushes div to the bottom.
Use vertical-align: top to solve issue.
You can try to add vertical-align:
.box1 {
display: inline-block;
margin: 5px;
width: 50px;
height: 50px;
background: blue;
/* overflow: hidden; */
color: white;
vertical-align:top;
}

DIV inside DIV with 100% height minus margins

I want to display two DIVs in one line. Each DIV should have another DIV inside. Internal DIVs should have the same height as external DIVs minus margins.
I can't set proper height to inside DIVs (bottom margin is ignored). Can you help me with that? jsFiddle: https://jsfiddle.net/gf53e0on/
<body>
<div class="box"><div class="box-in"></div></div>
<div class="box"><div class="box-in"></div></div>
</body>
body {
position: fixed;
width: 100%;
border: none;
display: table;
table-layout: fixed;
padding: 0;
margin: 0;
top: 0;
left: 0;
}
.box {
border: none;
display: table-cell;
height: 100vh;
background-color: yellow;
}
.box-in {
border: solid 1px;
margin-top:10px;
margin-bottom:10px;
margin-left:10px;
margin-right:10px;
height: 100%;
}
You can add padding to the bottom of your outer boxes. You also have to set box-sizing: border-box; so that this additional padding doesn't add to the height of the outer box.
So your box class becomes:
.box {
border: none;
display: table-cell;
height: 100vh;
background-color: yellow;
box-sizing:border-box;
padding-bottom:20px;
}
updated fiddle here
edited to add:
If you don't actually need to use margins on the inner box, you can remove them completely and just set a padding of 10px on the outer box with box-sizing:border-box on it.
another fiddle
Another option would be to use CSS3's calc to calculate the height minus the margins.
body {
position: fixed;
width: 100%;
border: none;
display: table;
table-layout: fixed;
padding: 0;
margin: 0;
top: 0;
left: 0;
}
.box {
border: none;
display: table-cell;
height: 100vh;
background-color: yellow;
}
.box-in {
border: solid 1px;
margin:10px;
height: calc(100% - 20px);
}
<div class="box"><div class="box-in"></div></div>
<div class="box"><div class="box-in"></div></div>

How can I get these divs to expand to full width inside an scrolling div?

I am trying to make a file hierarchy in html/css and I can't get these labels or the divs they are in to expand to full width. They only expand to the width of the visible area but I want the width of what they are in. Here is the fiddle to see what I am talking about. The grey area needs to all line up on the right.
a = 3;
html,
body {
height: 100%;
width: 100%;
padding: 0;
margin: 0;
}
div.hierarchy {
position: relative;
overflow: auto;
border-right: 1px solid grey;
width: 150px;
left: 0;
top: 0;
bottom: 0;
height: 100%;
}
div.hierarchy label {
display: block;
min-width: 100%;
background: #eee;
white-space: nowrap;
}
div.directory {
padding-left: 20px;
width: 100%;
}
div.directory label {
border: 1px solid grey;
width: 100%;
cursor: pointer;
}
<div class="hierarchy">
<label>Hierarchy</label>
<div class="directory">
<label>src</label>
<div class="directory">
<div class="file"><label>test.txt</label></div>
<div class="file"><label>readme.txt</label></div>
<div class="file"><label>a really long filename.txt</label></div>
</div>
</div>
</div>
You need to change your div.directory CSS class as follows:
div.directory {
display:inline-block;
padding-left: 20px;
}
I made the following changes:
1) Added display:inline-block;
2) Removed the width:100%; rule.
Here is an example:
http://jsfiddle.net/nnd7jyj1/
(As a side note, it's generally bad practice in CSS to apply both a width and either a padding or margin rule to the same element. The reason for this is that some browsers interpret the width to include the padding/margin and some don't, which leads to inconsistent results)
Simply add display:inline-block; to div.directory
html,
body {
height: 100%;
width: 100%;
padding: 0;
margin: 0;
}
div.hierarchy {
position: relative;
overflow: auto;
border-right: 1px solid grey;
width: 150px;
left: 0;
top: 0;
bottom: 0;
height: 100%;
}
div.hierarchy label {
display: block;
min-width: 100%;
background: #eee;
white-space: nowrap;
}
div.directory {
padding-left: 20px;
/* width: 100%; */
/* added */
display: inline-block;
}
div.directory label {
border: 1px solid grey;
width: 100%;
cursor: pointer;
}
<div class="hierarchy">
<label>Hierarchy</label>
<div class="directory">
<label>src</label>
<div class="directory">
<div class="file">
<label>test.txt</label>
</div>
<div class="file">
<label>readme.txt</label>
</div>
<div class="file">
<label>a really long filename.txt</label>
</div>
</div>
</div>
</div>

How to display a horizontally scrolling list next to a fixed item?

I am trying to display a list of images (equal height) in a horizontally scrolling div. This much works, but when I want to have a fixed image - a "cover" image present leftmost inside container the layout gets screwed up.
Below is the CSS and HTML of my work. If you run the snippet you can see that the list jumps to next line, instead of staying adjacent to "cover" image and scrolling horizantally. Here is the jsfiddle - http://jsfiddle.net/6x66dLdy/
I can solve it using javascript by setting width of #list programmatically, but I want to do it with CSS alone if possible.
#container {
height: 120px;
background: #ccccff;
}
#cover {
height: 100px;
margin: 10px;
display: inline-block;
vertical-align: top;
position: relative;
}
#cover img {
border: 2px solid #cc0000;
}
#list {
overflow-x: scroll;
overflow-y: hidden;
white-space: nowrap;
height: 100px;
margin: 10px 0;
display: inline-block;
}
.item {
height: 80px;
margin: 10px 5px;
display: inline-block;
}
<div id="container">
<div id="cover">
<img src="http://placehold.it/160x100"/>
</div>
<div id="list">
<div class="item">
<img src="http://placehold.it/120x80"/>
</div>
<div class="item">
<img src="http://placehold.it/60x80"/>
</div>
<div class="item">
<img src="http://placehold.it/120x80"/>
</div>
<div class="item">
<img src="http://placehold.it/120x80"/>
</div>
<div class="item">
<img src="http://placehold.it/120x80"/>
</div>
</div>
</div>
This happening because you don't have widths specified. You have to provide widths for both of your inner divs and also to the container. Giving explicit width to container is advisable because you can then safely assign percent widths to children.
In you use-case, you have to calculate how much width is safer for your div#cover and then use the CSS calc to calculate the remainder of the width to assign to the list. Also, remember to account for the margins you have.
Relevant CSS:
width: calc(100% - 240px);
Your fiddle: http://jsfiddle.net/abhitalks/6x66dLdy/1
It is always better to specify a proper box-sizing. So include this at the top of your CSS:
* { box-sizing: border-box; }
.
Float the #cover left and remove the display: inline-block from #list.
This will allow the cover image and images in the list be any unknown width. Setting a fixed width on the containers like the other answers would not allow this.
Fiddle: http://jsfiddle.net/6x66dLdy/4/
#container {
height: 120px;
background: #ccccff;
}
#cover {
height: 100px;
margin: 10px;
float: left;
vertical-align: top;
position: relative;
}
#cover img {
border: 2px solid #cc0000;
}
#list {
overflow-x: scroll;
overflow-y: hidden;
white-space: nowrap;
height: 100px;
margin: 10px 0;
}
.item {
height: 80px;
margin: 10px 5px;
display: inline-block;
}
test this
http://jsfiddle.net/6x66dLdy/3/
#container {
height: 120px;
background: #ccccff;
width:1000px;
}
#cover {
height: 100px;
margin: 10px;
display: inline-block;
vertical-align: top;
position: relative;
width:200px;
float:left;
}
#cover img {
border: 2px solid #cc0000;
}
#list {
overflow-x: scroll;
overflow-y: hidden;
white-space: nowrap;
height: 100px;
margin: 10px 0;
width:600px;
float:left
}
.item {
height: 80px;
margin: 10px 5px;
display: inline-block;
}
To answer your question you can specify min-width:800px; for the id #container
so it does not jump down and stay beside the main picture
here is an example http://jsfiddle.net/6x66dLdy/5/

Vertical div expansion w/o fixed heights

Before you roll your eyes and move on, I know how to solve this problem by using a fixed height and absolution positioning with top: and bottom:, but I want to solve it without using fixed heights. I want to learn more about CSS so I'm trying to solve this a different way.
I have set up a typical navbar running across the top, and then a scrolling content div below.
However! How do I fit the bottom scrolling div container to the remaining space without using absolute coordinates? I can't do position: absolute, because then I'd need to know the height of the navbar to set "top:". And I can't do "bottom: 0" because I'd have to specify a height.
Here's the JS filddle:
http://jsfiddle.net/8dugffz4/1/
The class of interest is ".result". I currently have the height fixed, which I don't want.
Thanks, y'all.
PT
CSS:
* {
font-family: Helvetica, Sans;
border: 0px;
margin: 0px;
padding: 0px;
}
.navBar {
width: auto;
overflow: auto;
border-bottom: 1px solid #bbb;
}
.pageBar {
float: right;
}
.pager {
cursor: pointer;
float: left;
border: 1px solid #bbb;
width: 2em;
height: 2em;
line-height: 2em;
text-align: center;
margin: 5px;
margin-left: 0px;
background: #eee;
color: #bbb;
}
.pager:hover {
background: #777;
border: 1px solid black;
color: white;
}
.fliph {
-ms-transform:scale(-1,1); /* IE 9 */
-moz-transform:scale(-1,1); /* Firefox */
-webkit-transform:scale(-1,1); /* Safari and Chrome */
-o-transform:scale(-1,1); /* Opera */
}
.results {
background: gray;
width: 100%;
height: 200px;
overflow: scroll;
}
.line {
height: 10em;
line-height: 10em;
border: 1px solid red;
}
HTML:
<body>
<div class='navBar'>
<div class='pageBar'>
<div class='pager'>◁</div>
<div class='pager'>1</div>
<div class='pager fliph'>◁</div>
</div>
</div>
<div class='results'>
<div class='line'>Line1</div>
<div class='line'>Line2</div>
<div class='line'>Line3</div>
<div class='line'>Line4</div>
</div>
</body>
Here's a solution that uses display: table and can actually achieve fluid heights:
http://jsfiddle.net/8dugffz4/8/
And a minimalistic snippet in case you want to see specifically what I did:
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
}
#table {
display: table;
height: 100%;
width: 100%;
}
#table > div {
display: table-row;
}
#navbar {
height: 45px;
opacity: .5;
}
#navbar > div {
height: 100%;
background: black;
}
#results {
height: 100%;
}
#results > div {
height: 100%;
overflow: auto;
background: green;
}
<div id="table">
<div id="navbar">
<div></div>
</div>
<div id="results">
<div></div>
</div>
</div>
If you're just looking for an alternative to the position: absolute method, you could use the height: 100% method:
html, body { height: 100%; }
body { box-sizing: border-box; padding-top: 45px; }
.navBar { height: 45px; margin-top: -45px; }
.results { height: 100%; }
Like so: http://jsfiddle.net/8dugffz4/7/