Issue with space between two divs in one [duplicate] - html

This question already has answers here:
Display a div width 100% with margins
(6 answers)
Closed 1 year ago.
I have created two div in one line.
Below is code
.best-seller-new-products {
border: 1px solid #fff;
margin-top: 20px;
}
.float-child-best-new {
width: 48%;
float: left;
border: 1px solid red;
margin: 0 11px;
margin-top: 20px;
}
<!-- Start best seller and new products -->
<div class="best-seller-new-products">
<div class="float-child-best-new">
bestseller products
</div>
<div class="float-child-best-new">
new products</div>
</div>
</div>
<!-- End best seller and new products -->
The result:
Issue description
When I add: width: 50%;, the width of the elements is matched to the rest of the others at the top and bottom.
But when I add width 50%; to this code:
margin: 0 11px;
then they are not inline
I also tried:
width: 48%;
margin: 0 11px;
then is in one line but then these elements are too short to fit in with the rest of the page.
Question:
How to make spaces between these divs so that the left and right sides match the rest page and are not shortened? (first div truncate only on the right, second div truncate just on the left) to make spaces between.

Here is how you can do it with flexbox.
.best-seller-new-products {
border: 1px solid #fff;
margin-top: 20px;
display: flex;
/* Set display of parent block as flex*/
justify-content: space-between;
/* this will make sure any left over space is between the children */
}
.float-child-best-new {
width: 48%;
/*Set the width of the children according to your needs*/
border: 1px solid red;
margin-top: 20px;
}
<!-- Start best seller and new products -->
<div class="best-seller-new-products">
<div class="float-child-best-new">
bestseller products
</div>
<div class="float-child-best-new">
new products</div>
</div>
</div>
<!-- End best seller and new products -->

Related

Stop row height from dictating element height

I'm looking to create a 2 column grid in CSS where some of the column elements can have different heights. Currently I have made the grid but the height of the row is dictated by the height of the tallest element.
I want to remove the vertical spacing between each of the elements on the Y axis.
Some code below
<div class="home-block-cont">
<div class="home-block">
<div class="home-block-inner">
<h2 class="title">General</h2>
<div class="subtitle">Basic Information about the company:</div>
<ul>
<li class="child-list-item">
Address
Staff Contact Details
Office Security
VAT Number
</li>
</ul>
</div>
</div>
</div>
CSS
.home-block-cont {
margin-top: 20px;
width: 100%;
overflow: auto;
}
.home-block {
background: $LIGHTGREY;
border-radius: 6px;
width: 49%;
margin-bottom: 2%;
float: left;
}
.home-block:nth-of-type(odd) {
margin-right: 2%;
}
.home-block-inner {
padding: 10px 30px;
}
Any advice/guidance appreciated.

HTML 2 column layout always full height

I am trying to make a two column layout in HTML so that both columns always fill the entire screen no matter how much content. I want the left column to dictate the height of the right column (map column). So if the left column grows to 2000 pixels tall then the map column should do grow to 2000 also. If the left column is only 10 pixels worth of content then the map column should never be less than the entire browswer window height.
I have spent 8 hours trying divs, webflow, etc... and no luck. You would think this would take two seconds. Throw in some divs and make the 100% and done.
Here is what I have which does NOT work?
I have this html
<div class="table">
<div class="row">
<div class="cell1">
<div class="inner"> <!-- start left side -->
<strong>Transaction ID</strong>
<p>Nov 11th, 2015 at 2:44 PM</p>
</div> <!-- END start left side -->
</div> <!-- end 1st column -->
<div class="cell2">
<div class="inner">
<div id="map-canvas" style="position: relative; overflow: hidden; transform: translateZ(0px); background-color: rgb(229, 227, 223);"></div>
</div>
</div> <!-- end 2nd column -->
</div> <!-- end row -->
With this CSS
.table {
display: table;
border-spacing: 12px;
}
.row {
display: table-row;
margin-bottom: 0;
margin-top: 0;
width: 100%;
}
.cell1 {
display: table-cell;
width: 49%;
margin-right: 1%;
border: 1px solid #ccc;
margin 12px;
}
.cell2 {
display: table-cell;
width: 49%;
margin-right: 1%;
border: 1px solid #ccc;
margin 12px;
}
.inner {
padding-left: 12px;
padding-right: 12px;
}
Here is with the changes #mathew suggested below. Not sure why the margin on the left is even there. I even use cell2 css class for both columns.
I created a Fiddle with your html and css minus the working map.
All i did was add height: 100vh to your .cell1 class like so:
.cell1 {
display: table-cell;
width: 49%;
height: 100vh;
margin-right: 1%;
border: 1px solid #ccc;
margin 12px;
}
This seems to give the effect you are looking for. If this is not the result you are trying to get please let me know. Play around with it and delete a bunch of text to see if you have 1 sentence in there it is still the full height of the browser.
EDIT*: If you want it to look like the picture you need to remove the border-spacing from the .table class and set the body to margin: 0; like this:
body {
margin: 0;
}
.table {
display: table;
}
I have updated the Fiddle to reflect these changes.
Hope this helps!
It appears that you were on the right path. I simply modified your html by removing the .row div and adding the class .col to the .col1 and .col2 divs (so that you only have to style them once). In my testing, that seems to work. Try the CSS and HTML code below and see if it works for you as well.
.table {display: table;}
.table .col {
display: table-cell;
width: 49%;
margin-right: 1%;
border: 1px solid #ccc;
margin 12px;
}
.inner {
padding-left: 12px;
padding-right: 12px;
}
<div class="table">
<div class="cell1 col">
<div class="inner"> <!-- start left side -->
<strong>Transaction ID</strong>
<p>Nov 11th, 2015 at 2:44 PM</p>
</div> <!-- END start left side -->
</div> <!-- end 1st column -->
<div class="cell2 col">
<div class="inner">
<div id="map-canvas" style="position: relative; overflow: hidden; transform: translateZ(0px); background-color: rgb(229, 227, 223);"></div>
</div> <!-- end inner -->
</div> <!-- end 2nd column -->
</div> <!-- end table -->

How to force inline-block element to correctly wrap it's multiline child element?

I need to display left and right borders padded 10px away from left and right edges of the centered text. There's no problem when the all text fits into one line, but when text takes up multiple lines the wrapping inline-block element stretches to 100% of it's container width.
I need a pure CSS solution.
Here's JSFiddle with working demo of the problem: https://jsfiddle.net/k8wrbctr/
Here's HTML:
<div class="container">
<div class="borders-wrapper"><span>The title</span></div>
<div class="borders-wrapper"><span>The title that takes up multiple lines</span></div>
</div>
Here's CSS:
.container {
width: 200px;
text-align: center;
background-color: #ddd;
}
.borders-wrapper {
display: inline-block;
border-left: 2px solid black;
border-right: 2px solid black;
padding-left: 10px;
padding-right: 10px;
margin-bottom: 20px;
}
Here's the result:
| The title |
| The title that takes up |
| multiple lines |
And here's what I want to achieve:
| The title |
| The title that takes up |
| multiple lines |
I need to display left and right borders padded 10px away from left
and right edges
You need to give margins not padding for that.
when text takes up multiple lines the wrapping inline-block element
stretches to 100% of it's container width
That is because the content is long and the div will stretch as far as it can (upto parent width) to accommodate the content before it wraps to the next line.
There is another problem with your div being inline-block - if the content is less then the next div will start just right after the first one and not on its own line.
Solution (Keeping the div as inline-block):
Use a pseudo-element to break the line.
* { box-sizing: border-box; }
.container {
width: 200px;
text-align: center;
background-color: #ddd;
}
.borders-wrapper {
display: inline-block;
border-left: 2px solid black;
border-right: 2px solid black;
padding: 0px 10px; margin: 10px;
}
.borders-wrapper::after {
content:"\A"; white-space:pre;
}
<div class="container">
<div class="borders-wrapper"><span>The title</span></div>
<div class="borders-wrapper"><span>The title that</span></div>
<div class="borders-wrapper"><span>The title that takes up multiple lines</span></div>
</div>
Note:
Thanks #Kaiido for pointing it out. The pseudo-element trick won't work with its element being inline-block. In order for it to work, you do your padding/margin on the span, and float the divs. Then use transform trick to center it. A little more complicated.
Example:
* { box-sizing: border-box; }
.container {
width: 200px;
text-align: center;
background-color: #ddd;
}
.borders-wrapper {
float: left; clear: left;
position: relative; left: 50%;
transform: translateX(-50%);
margin: 0px auto;
}
.borders-wrapper > span {
display: inline-block;
padding: 0px 10px; margin: 10px;
border-left: 2px solid black;
border-right: 2px solid black;
}
.container:after { content:''; display:block; clear: both; }
.div2 { width: 400px; }
<div class="container">
<div class="borders-wrapper"><span>The title</span></div>
<div class="borders-wrapper"><span>The title that</span></div>
<div class="borders-wrapper"><span>The title that takes up multiple lines</span></div>
</div>
<br />
<div class="container div2">
<div class="borders-wrapper"><span>The title</span></div>
<div class="borders-wrapper"><span>The title that</span></div>
<div class="borders-wrapper"><span>The really long title that takes up multiple lines in a large width</span></div>
</div>

align five divs equally next to each other

I'm even finding it difficult to phrase my question right so bear with me here please.
I have one div that serves as the main container of my page. Inside that div I would like to have five other divs which have equal size and equal margins. However when I calculate everything right, the fifth div always jumps to the next line.
I hope this makes sense. This is my code:
CSS and HTML as follows
#content {
position: absolute;
width: 1000px;
height: 500px;
left: 50%;
top: 50%;
margin-left: -500px;
margin-top: -250px;
border: 2px solid #f9423a;
border-radius: 10px;
background-color: #3eb1c8;
overflow: hidden;
}
.bookmark {
display: inline-block;
width: 15%;
height: 20%;
margin-left: 2%;
margin-right: 2%;
margin-top: 2.5%;
border: 1px solid #f9423a;
border-radius: 10px;
background-color: #f9423a;
}
<div id="content">
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
<div class="bookmark"></div>
</div>
Note that I'm just working with color-filled divs to see if it's working.
As you can see it almost works, the online thing that bothers me is that there's a bit more margin on the right than there is on the left. I would like to have equal margins between the sides and the outer elements, and between the inner elements of course.
I hope someone understands my question!
This is because you are using: display: inline-block which reads the white space between your divs on your HTML code as a literal white space, like putting a space between words, that breaks the layout.
Try changing your sintax like this:
<div>content</div><div>
content2</div><div>
content3</div><div>
content4</div><div>
content5</div>
Then, for the CSS you could use calc(); to add borders, that would ruin your layout too.
Like this:
div {
width: calc(20% - 4px);//20*5 = 100 - 2px on each side each time
border: 2px solid black;
}
JS Fiddle
body {
margin: 0 0 0 0;
}
div {
text-align: center;
display: inline-block;
width: calc(20% - 4px);
/*20*5 = 100 - 2px on each side each time*/
border: 2px solid black;
background-color: red;
}
<div>content1</div><div>
content2</div><div>
content3</div><div>
content4</div><div>
content5</div>
check this example, if this is what you wanted
Here i have removed the right margin and increased the bookmark div with to 17%
.bookmark {
display: inline-block;
width: 17%;
height: 20%;
margin-left: 2%;
margin-top: 2.5%;
border: 1px solid #f9423a;
border-radius: 10px;
background-color: #f9423a;
}
https://jsfiddle.net/0gkfp7zr/
Aramil's answer is good and correct correct. There is no "nice" way to deal with this. Different people have different methods, but they are all a bit hackish. The way I prefer to do it is with comments as follows:
<div class="bookmark"></div><!--
--><div class="bookmark"></div><!--
--><div class="bookmark"></div><!--
--><div class="bookmark"></div><!--
--><div class="bookmark"></div>
Basically you don't want any white space between one closing div and the next opening div. Sometimes if my content is short enough I will put them all together on one line as you see below, but it makes it much harder to read.
<div class="bookmark"></div><div class="bookmark"></div><div class="bookmark"></div><div class="bookmark"></div><div class="bookmark"></div>
add this to .bookmark
float:left and the add .8 to the width of bookmark, your computation is not equal because in 1 bookmark div you have 15% plus the margin-left and right which is 4%, total of 1 div is 19 x 5 = 95 so I added .8 to fill the remaining white spaces. 19.8 x 5 = 99
.bookmark {
display: inline-block;
width: 15.8%;
height: 20%;
margin-left: 2%;
margin-right: 2%;
margin-top: 2.5%;
border: 1px solid #f9423a;
border-radius: 10px;
background-color: #f9423a;
float: left;
}

Can someone explain why my divs are indenting themselves after the first row?

Quite a weird problem here. I don't know if it's related, but I am using Shopify - which has been the cause of weird problems in the past.
Okay, so here is the page on which I am coding (not the final place it will be for the record) - http://stonedclassy.com/pages/page2
As you can see, there are 6 divs on the page. I am using Bootstrap, and they are all span6 (meaning they take up half the width of the page on a an averaged sized desktop). The first "row" of divs is perfectly fine, but starting on the second row, there is an indent that I cannot get rid of. It's causing all of the subsequent divs after the first row to be forced onto their own row since they can't fit two to one row.
If I change all the divs size to span5, they are able to fit two to a row, but there is still an indent starting on the second row.
Here is the html:
Please note that to save space, I am including just ONE of the SIX divs I am referring to. The full code is literally just this code repeated six times
<div class="span6 majorimgtesting">
<a href="">
<div class="ghostrowforcategories span6"> <button class="ghostbuttonforcategories">TESTING</button> </div>
<img src="//cdn.shopify.com/s/files/1/0654/2811/products/clear-hammer-bubbler-water-pipe-stonedclassy_large.jpg?v=1422320684" class="imgtesting" />
<img src="//cdn.shopify.com/s/files/1/0654/2811/products/Micro-Mini-Water-Pipe-Stonedclassy_5571a18d-b3ed-457d-b180-f36cf3acb2b3_large.jpg?v=1423018550" class="imgtesting" />
</a>
Here is the CSS:
.majorimgtesting
{
max-height:250px;
overflow:hidden;
border-left: 1px solid black;
border-right: 1px solid black;
margin-bottom: 50px;
opacity: .9;
transition: .5s ease;
float: left;
}
.majorimgtesting:hover
{
transition: .5s ease;
opacity: 1;
}
.imgtesting
{
max-width: 50%;
display: block;
float: left;
z-index:-1;
}
.ghostrowforcategories
{
width: 100%;
position: absolute;
display: inline-block;
padding-bottom: 10px;
text-align: center;
}
.ghostbuttonforcategories
{
display: inline-block;
border-radius: 2px;
border: none;
height: 45px;
padding-left: 15px;
padding-right: 15px;
width: 100%;
background-color: #666105;
color: white;
border: 1px solid #666105;
float: middle;
z-index: 5;
opacity: .8;
}
.ghostbuttonforcategories:hover
{
background-color: #666105;
color: white;
z-index: 5;
}
All of your divs are contained in a single <div class='row-fluid'> parent. The first child of every row-fluid gets a margin-left: 0 which is pulling the first element out of line. Each subsequent element gets the regular margin-left value so they all line up appropriately.
bootstrap removes the margin of the first span in a sections of spans so if you place the items like this(as it's on the page)
<div class="row-fluid">
<div class="span-12">
<div class="sapn-6">...
</div>
<div class="span-12">...</div>
<div class="span-12">...</div>
</div>
it would only remove the margin of the first span-12 and the first span-6 and due that the next span-6 within a span-12 that's not the firstone it'll have that margin, so there's why you have that margin problem. Now, to fix it you could remove the class span-12 form the "frame like" divs and your problem will be solved, and the responsive items should still work. Example:
<div class="row-fluid">
<div>
<div class="sapn-6">...
</div>
<div>...</div>
<div>...</div>
</div>