CSS Float Horizontal Overflow - html

I have the following jsfiddle: https://jsfiddle.net/CLight/1emh82cv/
This is a recursive structure where it consists of two twin cells sharing the top row, and one cell in the second/bottom row. The three cells will be recursively inserted into the bottom cell. I am trying to figure out how to make the cells expand horizontally when they overflow, without breaking the structure. Thanks a bunch.
Here's the html:
<div class="cell-main">
<div class="cell-left">
testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttestte
</div>
<div class="cell-right">
test
</div>
<div class="cell-bottom">
<div class="cell-left">
test
</div>
<div class="cell-right">
test
</div>
<div class="cell-bottom">
<div class="cell-left">
test
</div>
<div class="cell-right">
test
</div>
</div>
</div>
</div>
Here's the CSS:
*{
box-sizing: border-box;
}
div{
border: 1px solid black;
}
.cell-left {
height: 100%;
padding: 5px;
width: 50%;
float: left;
text-align:left
}
.cell-right {
height: 100%;
padding: 5px;
width: 50%;
float: left;
text-align:right
}
.cell-bottom {
height: 100%;
padding: 5px;
width: 94%;
margin-left: 3%;
float: left;
}
.cell-main {
height: 100%;
padding: 5px;
width: 100%;
float:left;
}
EDIT: Updated jsfiddle and title.

I think you are searching for the overflow-wrap property.
The overflow-wrap CSS property specifies whether or not the browser should insert line breaks within words to prevent text from overflowing its content box. (Mozilla MDN)
This property can have the following values, in case you need it recursive, the global values can be interesting for you:
/* Keyword values */
overflow-wrap: normal;
overflow-wrap: break-word;
/* Global values */
overflow-wrap: inherit;
overflow-wrap: initial;
overflow-wrap: unset;
If you want to keep the height of the parcel use overflow: auto on the main div's.
See my code snippet!
*{
box-sizing: border-box;
}
div{
border: 1px solid black;
overflow-wrap: break-word;
}
.cell-left {
height: 100%;
padding: 5px;
width: 50%;
float: left;
text-align:left
}
.cell-right {
height: 100%;
padding: 5px;
width: 50%;
float: left;
text-align:right
}
.cell-bottom {
height: 100%;
padding: 5px;
width: 94%;
margin-left: 3%;
float: left;
}
.cell-main {
height: 100%;
padding: 5px;
width: 100%;
float:left;
}
<div class="cell-main">
<div class="cell-left">
testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttestte
</div>
<div class="cell-right">
test
</div>
<div class="cell-bottom">
<div class="cell-left">
test
</div>
<div class="cell-right">
test
</div>
<div class="cell-bottom">
<div class="cell-left">
test
</div>
<div class="cell-right">
test
</div>
</div>
</div>
</div>

Related

How do I change the distance from the top till the text in a column?

I'm working on a HTML site where I need to make between 3 vertical columns, one horizontal header, and one footer. But when I'm writing something in the header/footer, the text won't fit in the column.
I have made an HTML document, with a stylesheet.css.
.title{
border-radius: 8px;
border-collapse: collapse;
border: 2px solid black;
float: left;
height: 15px;
padding-top:5px;
padding-bottom: 33px;
margin-left: 20px;
margin-bottom: 10px;
margin-top: 5px;
width: 95%;
}
<div class="rowTop">
<div class="columnTop">
<div class="title">
<h2>Header</h2>
</div>
</div>
I expect that the distance from the top to the text needs to get smaller, but when I change the px, they won't get smaller than this.
You forgot to reference your class names in your style sheet
.rowTop{
border-radius: 8px;
border-collapse: collapse;
border: 2px solid black;
float: left;
height: 15px;
padding-top:5px;
padding-bottom: 33px;
margin-left: 20px;
margin-bottom: 10px;
margin-top: 5px;
width: 95%;
}
<div class="rowTop">
<div class="columnTop">
<div class="title">
<h2>Header</h2>
</div>
</div>
I'm not sure I understand your question, but first off your HTML is missing a terminating div:
<div class="rowTop">
<div class="columnTop">
<div class="title">
<h2>Header</h2>
</div>
</div>
</div>
and your CSS has no selector:
.CSS-selector-here {
border-radius: 8px;
border-collapse: collapse;
border: 2px solid black;
float: left;
height: 15px;
padding-top:5px;
padding-bottom: 33px;
margin-left: 20px;
margin-bottom: 10px;
margin-top: 5px;
width: 95%;
}
For a simple header, footer, three-column layout, I would recommend trying to understand flexbox. A simple layout might look something like:
div {
padding: 1rem;
border: 1px solid black;
}
.header, .footer {
width: 90%;
height: 15vh;
}
.flex-container {
width: 90%;
display: flex;
justify-content: space-between;
}
.flex-child {
width: 30%;
height: 85vh;
}
<div class="header">
</div>
<div class="flex-container">
<div class="flex-child">
</div>
<div class="flex-child">
</div>
<div class="flex-child">
</div>
</div>
<div class="footer">
</div>

How to remove margin between div elements [duplicate]

This question already has answers here:
How to remove the space between inline/inline-block elements?
(41 answers)
Closed 6 years ago.
Why is there a margin between divs? I tried to remove it by different methods but nothing worked. I had to reduce their width to stack them in rows.
*{
box-sizing: border-box;
}
.wrapper{
background-color: #ccc;
width: 500px;
margin: 5% auto;
padding: 0;
}
.box{
display: inline-block;
margin: 0px;
background-color: #f1f1f1;
width: 248px;
height: 250px;
border: 0 !important;
font-size: 0;
}
<div class="wrapper">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
Make the width of .box 250px and add an attribute of 'float: left' to .box
.box{
display: inline-block;
margin: 0px;
padding: 0;
background-color: #ff9900;
width: 250px;
height: 250px;
float: left;
}
Fiddle
Due to your display: inline-blocks, the white spaces appear in between your block elements.
There are many resolutions to the same, refer to David Walsh's blog
What I would prefer to do here is use float instead of display: inline-block.
Refer code:
*{
box-sizing: border-box;
}
.wrapper{
background-color: #ccc;
width: 500px;
margin: 5% auto;
padding: 0;
}
.box{
float: left;
margin: 0px;
background-color: #f1f1f1;
width: 248px;
height: 250px;
}
<div class="wrapper">
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
</div>
The problem is that there are spaces between the div's. Two possible solutions:
1:
<div class="wrapper">
<div class="box">
</div><div class="box">
</div><div class="box">
</div><div class="box">
</div>
</div>
-
.box { display: block; } // not multiple elements in one line, if you want this
2:
.wrapper { font-size: 0px; }
.box { display: block; } // not multiple elements in one line, if you want this
Its not margin what is causing space between two div its because of display:inline-block which you have added to box class, just add float: left; to same and it will go away.
*{
box-sizing: border-box;
}
.wrapper{
background-color: #ccc;
width: 500px;
margin: 5% auto;
padding: 0;
}
.box{
display: inline-block;
margin: 0px !important;
background-color: #f1f1f1;
width: 248px;
height: 250px;
border: 0 !important;
float: left;
}
<div class="wrapper">
<div class="box" style="background: rebeccapurple;">
</div>
<div class="box" style="background: orange;">
</div>
<div class="box" style="background: orange;">
</div>
<div class="box" style="background: rebeccapurple;">
</div>
</div>
Try setting border: 0 !important on all divs affected, once I had a similar problem and found that the divs were inheriting a 1px border that was breaking the width.
You are displaying them as inline blocks, so the white space between them in the formatting of your code is still being displayed just as it would had they been any other inline element.
You need to reformat your code, or set the wrapper to have a zero font size so they do not get rendered.
Try using
*{
box-sizing: border-box;
}
.wrapper{
background-color: #ccc;
width: 500px;
margin: auto;
padding: 0;
display: block;
background: green;
}
.box{
display: block;
margin: 0px;
width: 248px;
height: 250px;
background: red;
padding: 0;
float: left;
}
Display: inline-block creating that margin.
Or may be you could try
.wrapper{font-size: 0;}
.box{ display:inline-block;}

CSS - word-break changes element position

Im trying to do a little dashboard for some webservices i wrote, but whenever text within a widget breaks, the widget changes its position.
CSS looks like this:
#container {
text-align: center;
width: 100%;
height: auto;
}
.widget {
background-color: firebrick;
margin: 1px;
width: 200px;
height: 200px;
display: inline-block;
text-align: center;
word-break: break-all;
}
I reproduced that problem in a fiddle:
https://jsfiddle.net/vbb6fhz0/1/
If you want to align the box, then use float for better result. word-break has nothing to do with alignment.
But in your case, just vertical-align: middle solved the alignment issue
#container {
text-align: center;
width: 100%;
height: auto;
}
.widget {
background-color: firebrick;
margin: 1px;
width: 200px;
height: 200px;
display: inline-block;
text-align: center;
word-break: break-all;
vertical-align: middle;
}
<body>
<div style="width: 100%; height:100%;">
<div id="container">
<div class="widget">
<p>1dawdawdaegsergsergsrsrgrsddadawdadawdwdwbf</p>
</div>
<div class="widget">
2wdadawdad
</div>
<div class="widget">
3wadawdwdawda
</div>
<div class="widget">
4dawdawdaw
</div>
</div>
</div>
</body>
There is nothing bad with work-break you should try float
.widget{float:left;}

Can't center container div

I want to have a centered grid of buttons that take up the full width, but I can't seem to get the container centered no matter what I try.
Here's the jsfiddle - https://jsfiddle.net/a6qo6tzL/
Thanks
<div class="Wrapper">
<div class="gridButton">
Test
</div>
<div class="gridButton">
Test
</div>
<div class="gridButton">
Test
</div>
<div class="gridButton">
Test
</div>
<div class="gridButton">
Test
</div>
<div class="gridButton">
Test
</div>
<div class="gridButton">
Test
</div>
</div>
<div style="clear: both;"></div>
CSS
.Wrapper {
display:block;
margin: 0 auto;
width: 100%;
}
.gridButton {
padding: 10px;
background-color: #ff5100;
color: #ffffff;
margin: 5px;
float: left;
width: 250px;
text-align: center;
text-transform: uppercase;
}
Your main problem is that your gridButton has
float: left;
Instead, use
display: inline-block;
Now your buttons can move freely next to one another and be centered. Your wrapper element is already full width but you'll need to tell it to center its content:
.Wrapper {
display:block;
width: 100%;
text-align: center;
}
You can get rid of margin: 0 auto because that will only affect blocks with a known width.
.Wrapper {
display: block;
text-align: center;
width: 100%;
}
.gridButton {
padding: 10px;
background-color: #ff5100;
color: #ffffff;
margin: 5px;
display: inline-block;
width: 250px;
text-align: center;
text-transform: uppercase;
}
<div class="Wrapper">
<div class="gridButton">
Test
</div>
<div class="gridButton">
Test
</div>
<div class="gridButton">
Test
</div>
<div class="gridButton">
Test
</div>
<div class="gridButton">
Test
</div>
<div class="gridButton">
Test
</div>
<div class="gridButton">
Test
</div>
</div>
<div style="clear: both;"></div>
You've got .Wrapper set to 100% width, so even though you have margin: auto, the container is full width and will not appear centered. Set it to a constant width at a higher breakpoint:
#media (min-width: 500px) {
.Wrapper {
width: 500px;
}
}
Then consider wrapping your buttons in a columns:
.cell {
float: left;
width: 50%;
}
Here is your updated fiddle.
Use flexbox:
.Wrapper {
display:flex;
flex-flow: row wrap;
justify-content: center;
align-content: center;
align-items: center;
margin: 0 auto;
width: 100vw;
}
DEMO
I also replaced the width to viewport units
have a look here if this is what you want
https://jsfiddle.net/Raider/744wv0o7/
.Wrapper {
display:block;
margin: 0 auto;
width: 100%;
border:1px solid;
}
.gridButton {
padding: 10px;
background-color: #ff5100;
color: #ffffff;
margin: 5px auto;
/*float: left;*/
width: 250px;
text-align: center;
text-transform: uppercase;
}
Is this what you need?
.Wrapper {
display:block;
margin: 0 auto;
width: 100%;
}
.gridButton {
padding: 10px;
background-color: #ff5100;
color: #ffffff;
margin: 5px;
width: 250px;
text-align: center;
margin:auto;
text-transform: uppercase;
}
You where using float:left; property which prevents the div's from being centered

Take padding in account for floating elements

I would like to have a main element, with side blocks floating to its right side. I don't know the number of side blocks, neither their final total height. But my main element should have the same height (see the following example for better understanding), without using columns.
(dashed areas are real contents)
To force my main (red) element to fit side blocks height, I use this trick:
padding-bottom: 5000px;
margin-bottom: -5000px;
This works well, but side blocks doesn't care of padding, they just ignore it.
How can I get them to take padding into account?
N.B: HTML markup should not be changed, and I'm not willing to use JS for layout purpose
.container {
width: 600px;
margin: 0 auto;
overflow: hidden;
}
.main {
float: left;
background: tomato;
width: 440px;
padding-bottom: 5000px;
margin-bottom: -5000px;
}
.side {
float: left;
background: forestgreen;
height: 50px;
width: 150px;
margin-left: 10px;
margin-bottom: 10px;
}
<div class="container">
<div class="main"> </div>
<div class="side"> </div>
<div class="side"> </div>
<div class="side"> </div>
</div>
How is this for an option?
No markup change and purely CSS with no change in absolute values already given.
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.container {
width: 600px;
margin: 0 auto;
overflow: hidden;
}
.main {
background: tomato;
width: 440px;
padding-bottom: 5000px;
margin-bottom: -5000px;
float: left;
}
.side {
background: forestgreen;
height: 50px;
width: 150px;
margin-left: 10px;
margin-bottom: 10px;
float: right;
clear: right;
}
.side:last-child {
margin-bottom: 0;
}
<div class="container">
<div class="main"> </div>
<div class="side"> </div>
<div class="side"> </div>
<div class="side"> </div>
</div>
The only way i can come up with a solution is this:
JS FIDDLE
I made a .wrapper div around the 3 (forest)green boxes, and centered that one to the right.
So now you have those 3 boxes floating right of the tomato colored div.
Don't forget to make a clear both under the floating divs, or else everything will overlap the divs. and in you CSS sheet: .clear{ clear: both; }
Hope it helps. :)
I found a solution, using margin-left instead of float: left:
.container {
width: 600px;
margin: 0 auto;
overflow: hidden;
}
.main {
float: left;
background: tomato;
width: 440px;
padding-bottom: 5000px;
margin-bottom: -5000px;
}
.side {
background: forestgreen;
height: 50px;
width: 150px;
margin-left: 450px;
margin-bottom: 10px;
}
<div class="container">
<div class="main"> </div>
<div class="side"> </div>
<div class="side"> </div>
<div class="side"> </div>
</div>
When you float an element, it's effectively taking it out of the document flow, so padding won't have an effect on it. You could use margin-top: 10px; on both of your inner divs.