Width 100% isn't working with overflow: auto; - html

I'm having issues having a line of text be hidden by it's parent element. I want it to be dynamic in it's width, but for the life of me I can't figure it out.
.container {
border: 6px solid black;
display: flex;
width: 100%;
}
.die_results {
border: 2px solid green;
display: flex;
width: 100%;
}
.low_rolls_container {
border: 2px solid blue;
width: 100%;
overflow: auto;
}
.low_rolls {
border: 2px solid red;
font-size: 5rem;
padding: 2px 5px 0 0;
white-space: nowrap;
}
<div class="container">
<div class="die_results">
<div class="low_rolls_container">
<div class="low_rolls">1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13</div>
</div>
<div class="high_roll">23</div>
</div>
<div class="die_results">
<div class="low_rolls_container">
<div class="low_rolls">1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13</div>
</div>
<div class="high_roll">23</div>
</div>
</div>
I'm trying to get the Red border box to scroll within the blue border box, all while having the width of "container" and "die_results" be dynamic. Is it possible to do this without using a fixed width somewhere?

The container represents any container whatsoever, the fixed width value of 100px doesn't really matter as the container's width can be fluid and this solution will still work.
I think you've overcomplicated your issue a bit, you've used the correct properties but there's probably been some complications with some of the other properties you've applied.
Using overflow: auto on the container thats holding the content that will overflow will only show a scrollbar if the content inside is actually overflowing, not a static scrollbar as if you were using overflow: scroll.
The border that you see is around the low_rolls-element so you can see when it's overflowing.
.container {
width: 100px;
}
.containertwo {
width: 100%;
}
/* flex for demo purpose, not necessary */
.die_results {
display: flex;
width: 100%;
}
/* force scroll on overflow */
.low_rolls_container {
overflow-x: auto;
}
.low_rolls {
border: 1px solid black;
font-size: .7rem;
padding: 2px 5px 0 0;
white-space: nowrap;
}
<div class="container">
<div class="die_results">
<div class="low_rolls_container">
<div class="low_rolls">1, 2, 3, 6, 2, 3, 6, 5, 5, 7, 9, 1, 2, 3, 6, 2, 3, 6, 5, 5, 7, 9</div>
</div>
<div class="high_roll">23</div>
</div>
</div>
<div class="container containertwo">
<div class="die_results">
<div class="low_rolls_container">
<div class="low_rolls">1, 2, 3, 6, 2, 3, 6, 5, 5, 7, 9, 1, 2, 3, 6, 2, 3, 6, 5, 5, 7, 9</div>
</div>
<div class="high_roll">23</div>
</div>
</div>

Found out the solution below. Needed overflow: hidden; on the container as well.
.container {
display: flex;
}
.die_results {
display: flex;
overflow: hidden;
}
.low_rolls_container {
border: 2px solid blue;
overflow: hidden;
}
.low_rolls {
border: 2px solid red;
font-size: 5rem;
padding: 2px 5px 0 0;
white-space: nowrap;
overflow: auto;
}
<div class="container">
<div class="die_results">
<div class="low_rolls_container">
<div class="low_rolls">1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13</div>
</div>
<div class="high_roll">23</div>
</div>
<div class="die_results">
<div class="low_rolls_container">
<div class="low_rolls">1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13</div>
</div>
<div class="high_roll">23</div>
</div>
</div>

Related

flexbox width inside horizontally scrollable container

I am trying to create a layout for a virtual table.
A virtual table will be placed inside .table-body element.
Each table cell has the same flex layout styles as cells in .table-header so with the same parent element with they would look the same.
The problem is that the .table-cell elements do not stretch .table-header width, and .table-header element does not stretch the .table-container width.
I'm trying to get the .table-cell's to give the width for the .table-header and .table-container. And the .table-body would take up the remaining space in the .table-container
Here is Codesandbox to play with.
.table-wrapper {
width: 500px;
height: 300px;
overflow-x: scroll;
background: rgba(255, 255, 0, 0.2);
}
.table-container {
display: flex;
flex-flow: column nowrap;
width: 100%;
height: 100%;
/* If you uncomment next line, you see what I'm trying to achieve */
/* min-width: 1100px; */
background: rgba(0, 255, 0, 0.3);
}
.table-header {
display: flex;
flex-flow: row nowrap;
width: 100%;
flex: 0 0 auto;
background: rgba(255, 100, 0, 0.3);
}
.table-cell {
min-width: 100px;
display: flex;
flex: 1 1 auto;
height: 30px;
line-height: 30px;
border: 1px solid blue;
}
.table-body {
display: flex;
flex: 1 1 auto;
border: 1px solid red;
background: rgba(255, 0, 0, 0.3);
}
<div class="table-wrapper">
<div class="table-container">
<div class="table-header">
<div class="table-cell" style="flex: 0 0 180px;">header-cell 1</div>
<div class="table-cell">header-cell 2</div>
<div class="table-cell">header-cell 3</div>
<div class="table-cell">header-cell 4</div>
<div class="table-cell" style="flex: 0 0 200px;">header-cell 5</div>
<div class="table-cell">header-cell 6</div>
<div class="table-cell">header-cell 7</div>
</div>
<div class="table-body">virtual table here</div>
</div>
</div>
Consider the use of inline-flex instead of flex and define the width using width and not flex-basis
.table-wrapper {
width: 500px;
height: 300px;
overflow-x: scroll;
background: rgba(255, 255, 0, 0.2);
}
.table-container {
display: inline-flex; /* UPDATED */
flex-flow: column nowrap;
min-width: 100%; /* UPDATED */
height: 100%;
background: rgba(0, 255, 0, 0.3);
}
.table-header {
display: flex;
flex-flow: row nowrap;
width: 100%;
flex: 0 0 auto;
background: rgba(255, 100, 0, 0.3);
}
.table-cell {
min-width: 100px;
display: flex;
flex: 1 1 auto;
height: 30px;
line-height: 30px;
border: 1px solid blue;
}
.table-body {
display: flex;
flex: 1 1 auto;
border: 1px solid red;
background: rgba(255, 0, 0, 0.3);
}
<div class="table-wrapper">
<div class="table-container">
<div class="table-header">
<div class="table-cell" style="flex: 0 0 180px;width:180px;">header-cell 1</div>
<div class="table-cell">header-cell 2</div>
<div class="table-cell">header-cell 3</div>
<div class="table-cell">header-cell 4</div>
<div class="table-cell" style="flex: 0 0 200px;width:200px;">header-cell 5</div>
<div class="table-cell">header-cell 6</div>
<div class="table-cell">header-cell 7</div>
</div>
<div class="table-body">virtual table here</div>
</div>
</div>
Here's a workaround I found, use width: min-content; for both table-header and table-container.
This only work when you get rid of the inline style in the table-cell.
.table-wrapper {
width: 500px;
height: 300px;
overflow-x: scroll;
background: rgba(255, 255, 0, 0.2);
}
.table-container {
display: flex;
flex-flow: column nowrap;
width: min-content;
height: 100%;
/* If you uncomment next line, you see what I'm trying to achieve */
/* min-width: 1100px; */
background: rgba(0, 255, 0, 0.3);
}
.table-header {
display: flex;
flex-flow: row nowrap;
width: min-content;
flex: 0 0 auto;
background: rgba(255, 100, 0, 0.3);
}
.table-cell {
min-width: 100px;
display: flex;
flex: 1 1 auto;
height: 30px;
line-height: 30px;
border: 1px solid blue;
}
.table-body {
display: flex;
flex: 1 1 auto;
border: 1px solid red;
background: rgba(255, 0, 0, 0.3);
}
<div class="table-wrapper">
<div class="table-container">
<div class="table-header">
<div class="table-cell">header-cell 1</div>
<div class="table-cell">header-cell 2</div>
<div class="table-cell">header-cell 3</div>
<div class="table-cell">header-cell 4</div>
<div class="table-cell" >header-cell 5</div>
<div class="table-cell">header-cell 6</div>
<div class="table-cell">header-cell 7</div>
</div>
<div class="table-body">virtual table here</div>
</div>
</div>

CSS: How can I make the display 1, 2, or 4 columns but never 3 columns?

How can I use css to make sure the following code can display as 1, 2, or 4 columns but never as 3 columns?
.container {
display: flex;
flex-wrap: wrap;
border: 3px solid rgb(0, 0, 0);
}
.panel {
height: 100px;
width: 100px;
border: 3px solid rgb(255, 0, 0);
margin: 10px 0px 0px 10px;
}
<div class="container">
<div class="panel"></div>
<div class="panel"></div>
<div class="panel"></div>
<div class="panel"></div>
</div>
So it's all about the width since you are using flex-box. if you want an item to take the entire row give the container width of 100% and if you want 2 items next to each other give it width:50% if you want 4 items then make sure to give it 25% of the width.
I made this responsive example I used the names bootstrap uses to make it easier to understand in case you are using bootstrap.
Note that you should work inside the div with class wrap.
.container {
display: flex;
flex-wrap: wrap;
outline: 3px solid rgb(0, 0, 0);
}
.panel {
height: 100px;
outline: 2px solid rgb(255, 0, 0);
background-color: green;
}
.wrap {
margin: 0 1rem;
height: 100%;
background-color: blueviolet;
color: white;
}
.col-12 {
width: 100%;
}
#media (min-width: 768px) {
.col-mid-6 {
width: 50%;
}
}
#media (min-width: 1200px) {
.col-xl-4 {
width: 25%;
}
}
<div class="container">
<div class="panel col-12 col-mid-6 col-xl-4">
<div class="wrap">div1</div>
</div>
<div class="panel col-12 col-mid-6 col-xl-4">
<div class="wrap">div2</div>
</div>
<div class="panel col-12 col-mid-6 col-xl-4">
<div class="wrap">div3</div>
</div>
<div class="panel col-12 col-mid-6 col-xl-4">
<div class="wrap">div4</div>
</div>
</div>
if that didn't help let me know.

Center div elements like text

I looked around but couldn't find something that works. I'd like to somehow center div elements that have class="box". Here is an example of how I would like it to be:
Can someone tell me how I would go about doing this? I have tried something that obviously wouldn't work but here is what I have so far:
body {
background-color: rgb(32, 32, 36);
height: 100%;
}
#main {
background-color: rgb(50, 50, 56);
width: 75%;
margin-left: auto;
margin-right: auto;
margin-bottom: auto;
margin-top: auto;
box-shadow: 10px 10px 20px rgb(0, 0, 0);
border-style: solid;
border-width: 3px;
border-color: rgb(20, 20, 26);
overflow: auto;
height: 95vh;
color: rgb(0, 0, 0);
}
.box {
margin: auto 0;
left: 25;
right: 25;
top: 25;
bottom: 25;
height: 160px;
width: 140px;
border-style: solid;
border-width: 3px;
border-color: rgb(20, 20, 26);
box-shadow: 10px 10px 20px rgb(0, 0, 0);
background-color: rgb(70, 70, 76);
}
<body>
<div id="main">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
</body>
P.S. Don't just post code, please. I want to learn so please explain how it all works
Use CSS Flexbox. Make your #main parent div display: flex & justify-content: space-between. And then accordingly give your width to box. Like:
#main {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.box {
width: 30%;
}
.box:nth-child(4),
.box:nth-child(5) {
width: 45%;
}
Have a look at the snippet below (use full view for better understanding):
body {
background-color: rgb(32, 32, 36);
height: 100%;
}
#main {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
background-color: rgb(50, 50, 56);
width: 75%;
margin-left: auto;
margin-right: auto;
margin-bottom: auto;
margin-top: auto;
box-shadow: 10px 10px 20px rgb(0, 0, 0);
border-style: solid;
border-width: 3px;
border-color: rgb(20, 20, 26);
overflow: auto;
height: 95vh;
color: rgb(0, 0, 0);
}
.box {
margin: 10px 0;
left: 25;
right: 25;
top: 25;
bottom: 25;
height: 160px;
width: 30%;
border-style: solid;
border-width: 3px;
border-color: rgb(20, 20, 26);
box-shadow: 10px 10px 20px rgb(0, 0, 0);
background-color: rgb(70, 70, 76);
}
.box:nth-child(4),
.box:nth-child(5) {
width: 45%;
}
<body>
<div id="main">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
</body>
Hope this helps!
With flexbox, you can justify-content: space-around; and flex-wrap: wrap; this means evenly space elements along a line and if the line has too much on it start a new line.
Here is some more resources and example about flexbox
I've made a simple example below.
body {
margin: 0;
}
#wall {
background: #bed6e2;
display: flex;
justify-content: space-around;
flex-wrap: wrap;
}
.brick {
width: calc(100%/9);
height: 65px;
background: #ab837b;
border: 1px solid #222;
border-radius: 3px;
margin: 2px 0;
}
.brick.wide {
width: calc((100% / 9) * 3);
}
<div id="wall">
<div class="brick wide"></div>
<div class="brick wide"></div>
<div class="brick"></div>
<div class="brick"></div>
<div class="brick"></div>
<div class="brick"></div>
<div class="brick wide"></div>
<div class="brick wide"></div>
<div class="brick wide"></div>
<div class="brick"></div>
<div class="brick wide"></div>
<div class="brick"></div>
</div>
Hope this is helpful.
Flexbox is great but it has its downsides, I prefer to stick to using float.
There a 2 new classes, box1 and box2 which have styles specific to the size of the box (you might want to look at using a grid system)
There is also a .clearfix class which is clears your floats.
I took the liberty of tidying various bits of your css that were not needed. If you want to know more let me know.
* {
box-sizing: border-box;
}
.clearfix:after {
content: "";
display: table;
clear: both;
}
body {
background-color: rgb(32, 32, 36);
}
#main {
background-color: rgb(50, 50, 56);
width: 75%;
margin: 0 auto;
box-shadow: 10px 10px 20px rgb(0, 0, 0);
border:3px solid rgb(20, 20, 26);
color: rgb(0, 0, 0);
}
.box {
margin: 25px 0;
height: 75px;
border: 3px solid rgb(20, 20, 26);
box-shadow: 10px 10px 20px rgb(0, 0, 0);
background-color: rgb(70, 70, 76);
float: left;
}
.box1 {
width: 20%;
margin-left: 10%;
}
.box1:nth-child(3) {
margin-right: 10%;
}
.box2 {
width: 35%;
margin-left: 10%;
}
.box2:nth-child(2) {
margin-right: 10%;
}
<body>
<div id="main" class="clearfix">
<div class="box box1"></div>
<div class="box box1"></div>
<div class="box box1"></div>
<div class="box box2"></div>
<div class="box box2"></div>
<div class="box box1"></div>
<div class="box box1"></div>
<div class="box box1"></div>
</div>
</body>
div elements have the property display: block by default. Block-level elements automatically begin on a new line, but adding the attribute display: inline-block on .box will let the elements exist on the same line. Applying text-align: center to the container holding your .boxes (which is #main) will center the inline-blocks inside.
Alternatively, you could apply display: flex to #main which will automatically manage the position of all elements inside (by default, in a row). You can customize this extensively without needing additional styles on .box. Read more here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Updated answer
Try to use display: flex to parent container and if you want wrap content to second line then use flex-wrap: wrap.
body {
background-color: rgb(32, 32, 36);
height: 100%;
}
#main {
background-color: rgb(50, 50, 56);
width: 75%;
margin-left: auto;
margin-right: auto;
margin-bottom: auto;
margin-top: auto;
box-shadow: 10px 10px 20px rgb(0, 0, 0);
border-style: solid;
border-width: 3px;
border-color: rgb(20, 20, 26);
overflow: auto;
height: 95vh;
color: rgb(0, 0, 0);
}
.container {
display: flex;
justify-content: space-between;
}
.container_second .box {
width: 180px;
}
.box {
margin: auto 0;
left: 25;
right: 25;
top: 25;
bottom: 25;
height: 160px;
width: 140px;
border-style: solid;
border-width: 3px;
border-color: rgb(20, 20, 26);
box-shadow: 10px 10px 20px rgb(0, 0, 0);
background-color: rgb(70, 70, 76);
}
<body>
<div id="main">
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
<div class="container container_second">
<div class="box"></div>
<div class="box"></div>
</div>
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
</div>
</body>
For complete reference on flex - https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Instead of using 0 margin in the left_right (the margin style of .box) try using 40%
use:
.box {
margin: auto 40%;
instead of:
.box {
margin: auto 0;
The percentage will keep 40% of the total width of the margin on each side. It is important to note though that if you change the width of the box class you will need to adjust the margins also.

Set same size for other elements with text cause different sizes

I have a parent element, then 3 children. Center element should be in the center, with 50 px width and other two should take the remaining space. Parent's size is unknown, because of responsive displays. If there is no text in these children, then it looks good, but when I put some text there... well, it is destroyed. Note that I want to make it in the pure CSS.
Problem: red one and blue one should have same width.
.parent {
display: table;
width: 100%;
height: 150px;
border: 1px solid black;
}
.left, .center, .right {
display: table-cell;
height: 150px;
}
.left {
background-color: rgb(199, 60, 60);
}
.center {
width: 50px;
background-color: rgb(60, 199, 96);
}
.right {
background-color: rgb(60, 122, 199);
}
<div class="parent">
<div class="left">Some text</div>
<div class="center"></div>
<div class="right">There is much more text</div>
</div>
You can use Flexbox and set flex: 1 on left and right div and flex: 0 0 50px on center div.
.parent {
display: flex;
height: 150px;
border: 1px solid black;
}
.left,
.center,
.right {
height: 150px;
}
.left {
background-color: rgb(199, 60, 60);
flex: 1;
}
.center {
flex: 0 0 50px;
background-color: rgb(60, 199, 96);
}
.right {
background-color: rgb(60, 122, 199);
flex: 1;
}
<div class="parent">
<div class="left">Some text</div>
<div class="center"></div>
<div class="right">There is much more text</div>
</div>

How do I align mixed-size divs within a container?

Updated based on comments
I'm trying to create div sections on a full sized page by making containers that are 30% of the width. Within those, I plan to have 2 or 3 div sizes aligned within them. I have a row with a large box that occupies 100% of the height, and a portion of the width, and then a box that's exactly half of the size. I'd like to have all of those half-size boxes be in the same row as the larger box to create a nice stack. I'm assuming it's an issue of size vs position, but I haven't had much luck and I'm over-thinking the issue.
Fiddle: https://jsfiddle.net/as9hud4k/10/
HTML:
<div class="content_section">
<div class="content_thirdsize">
<div class="content_thirdsize_inner_row">
<div class="content_thirdsize_inner_large"> </div>
<div class="content_thirdsize_inner_small"> </div>
<div class="content_thirdsize_inner_small"> </div>
<div class="content_thirdsize_inner_small"> </div>
<div class="content_thirdsize_inner_small"> </div>
<div class="content_thirdsize_inner_small"> </div>
<div class="content_thirdsize_inner_small"> </div>
<div class="content_thirdsize_inner_small"> </div>
<div class="content_thirdsize_inner_small"> </div>
</div>
</div>
</div>
CSS:
.content_thirdsize
{
width: 500px;
height: 500px;
display: inline-block;
text-align: center;
vertical-align: top;
background-color: rgba(83, 35, 128, 0.2);
}
.content_thirdsize_inner_row
{
width: 500px;
height: 105px;
display: inline-block;
background-color: rgba(83, 35, 128, 0.2);
margin: 2px;
}
.content_thirdsize_inner_large
{
position: relative;
width: 100px;
height: 100px;
display: inline-block;
background-color: rgba(83, 35, 128, 0.2);
border: 1px dashed #000;
vertical-align: left;
}
.content_thirdsize_inner_small
{
position: relative;
width: 50px;
height: 50px;
display: inline-block;
background-color: rgba(83, 35, 128, 0.2);
border: 1px dashed #000;
vertical-align: right;
}
I suspect the math may need to be tweaked to account for spacing but flexbox can do a lot of the work here.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
section {
display: flex;
margin: auto;
}
.content-wrap {
display: flex;
flex-wrap: nowrap;
padding: 5px;
background: orange;
}
.small-wrap {
display: flex;
flex-wrap: wrap;
width: 350px;
}
.large,
.small {
width: 100px;
height: 100px;
background: rebeccapurple;
border: 2px dotted white;
}
.small {
width: 50px;
height: 50px;
}
<section>
<div class="content-wrap">
<div class="large"></div>
<div class="small-wrap">
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div </div>
</div>
</section>
Codepen Demo
I would recommend using position and then align the divs using left, right, top, bottom. Pick a position setting that makes sense in your mind for moving the div and then fiddle with it's position until they line up as you would like.
As paulie_D said in the comments don't use IDs multiple times, they are supposed to be unique. You want to be using a class to apply the same style on multiple objects. My general rule is that classes are for applying style and IDs are for identifying a specific object on the page.