'rogue' div is offsetting its siblings - html

Basically I have a set of divs that are pretty much identical in structure, then following them I have a div that is unique to the set. Basically the first divs are a bunch of different categories and the last div is a userprofile type square. What I can't figure out is why the user profile square is being rendered with a higher position than the other divs.
they all have the same css
cursor: pointer;
display: inline-block;
margin: 10px;
padding: 5px 5px 10px 5px;
width: 219px;
height: 219px;
background: #fff;
and the container has this css
display: block;
float: left;
width: 100%;
text-align: center;
padding: 15px;
white-space: nowrap;
overflow-x: auto;
overflow-y: hidden;
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-o-box-sizing: border-box;
-ms-box-sizing: border-box;
this is what it looks like
I'm guessing it's because the divs internal structure is different, but I'm not sure why it's doing this. I also noticed that if for example one of the category divs' images do not load it behaves the same way as my rogue div.
Any light there is to be shed on this issue is much appreciated.

With display: inline-block; it's best to always add vertical-align: top; to the children (and then format from there as needed), especially if you have different element types or images in your container. Even images inside of your child elements can mess up the layout of inline-block;. inline-block elements also suffer from the "whitespace problem", which can affect layout. To prevent that you can either put all child elements together or comment out the whitespace.
Demo: http://jsfiddle.net/ThinkingStiff/wwwkJ/
HTML:
<div id="container">
<div class="child">one</div>
<div class="child">two</div>
<img class="child" />
</div>
<div id="container-align">
<div class="child-align">one</div>
<div class="child-align">two</div>
<img class="child-align" />
</div>
<div id="container-align-whitespace">
<div class="child-align">one</div><!--
--><div class="child-align">two</div><!--
--><img class="child-align" />
</div>
CSS:
.child {
border: 1px solid red;
display: inline-block;
height: 50px;
width: 50px;
}
.child-align {
border: 1px solid red;
display: inline-block;
height: 50px;
vertical-align: top;
width: 50px;
}
#container, #container-align, #container-align-whitespace {
border: 1px solid black;
height: 100px;
width: 300px;
}
Output:

Related

Divide 3 divs in 3 columns without using float and flex

I tried by using display inline-block to achieve 3 columns but 3rd column comes at separate row:
.wrapper {
width: 100%;
}
.column {
display: inline-block;
min-height: 150px;
width: 33.33%;
border: 1px solid black;
min-width: 300px;
margin-bottom: 8px;
}
<div class="wrapper">
<div class="column">abc</div>
<div class="column">def</div>
<div class="column">ghi</div>
</div>
Not able to figure out the reason.
I dont know this is exactly what you need , i have remove the default whitespace of the inline-block using font-size:0 and add box-size property you dont need to change the width 33.3% to 33% the width please check the snippet
.wrapper {
width: 100%;
font-size: 0;
}
.column {
display: inline-block;
min-height: 150px;
width: 33.33%;
border: 1px solid black;
margin-bottom: 8px;
box-sizing: border-box;
-webkit-box-sizing: border-box;
-ms-box-sizing: border-box;
-moz-box-sizing: border-box;
}
<div class="wrapper">
<div class="column">abc</div>
<div class="column">def</div>
<div class="column">ghi</div>
</div>
By Default inline-block count space as a element. You can do this in two different ways:
Method (Using Font Size)
*,*:after,*:before {
box-sizing: border-box;
}
.wrapper {
width: 100%;
font-size:0px;
}
.column {
display: inline-block;
min-height: 150px;
width: 33.33%;
border: 1px solid black;
/*min-width: 300px;*/
margin-bottom: 8px;
font-size:16px;
}
<div class="wrapper">
<div class="column">abc</div>
<div class="column">def</div>
<div class="column">ghi</div>
</div>
Method Removing extra Space
*,*:after,*:before {
box-sizing: border-box;
}
.wrapper {
width: 100%;
}
.column {
display: inline-block;
min-height: 150px;
width: 33.33%;
border: 1px solid black;
/*min-width: 300px;*/
margin-bottom: 8px;
}
<div class="wrapper">
<div class="column">abc</div><!--
--><div class="column">def</div><!--
--><div class="column">ghi</div>
</div>
As per your comment you want margin-right:5px and achieve same thing. for this you can use width in calc format. check below snippet
*,*:after,*:before {
box-sizing: border-box;
}
.wrapper {
width: 100%;
}
.column {
display: inline-block;
min-height: 150px;
width: calc(33.33% - 5px);
width: -moz-calc(33.33% - 5px);
width: -webkit-calc(33.33% - 5px);
border: 1px solid black;
/*min-width: 300px;*/
margin-bottom: 8px;
margin-right: 5px;
}
<div class="wrapper">
<div class="column">abc</div><!--
--><div class="column">def</div><!--
--><div class="column">ghi</div>
</div>
Use Table for it, Best Option
No width, No Float
table{table-layout:fixed;width:500px;border-collapse:collapse;text-align:center;}
<table border="1">
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
</table>
By default browser renders a gap between two div which are positioned inline.
Making the parent width:100%, and children width:33.33% will not make children fit in the parent, because DOM calculates the gap between child div.
To make the child div fit the parent, you need to modify the width smaller than 33.33%.
If you still want to use 33.33% width. Try this
Link For reference
Unexpected gap between div inline-block
hope this helps..
One simple way to solve this issue is to set the .wrapper to display: table; and set its children to display: table-cell;. See the example below for the outcome.
.wrapper {
width: 100%;
display: table;
}
.column {
display: table-cell;
min-height: 150px;
width: 33.33%;
border: 1px solid black;
min-width: 300px;
margin-bottom: 8px;
}
<div class="wrapper">
<div class="column">abc</div>
<div class="column">def</div>
<div class="column">ghi</div>
</div>
If you are working in bigger screen like (1600,1920) then please use media query. And nothing to do any changes in margin and other as well.
Please update this property
For < 1366 screen resolution use this css
width:32.90% //instead of 32.90% you may use width: 32%;
For > 1366 width:33% is working
in .column class
width: 33%;
So column class look like
.column {
display: inline-block;
min-height: 150px;
width: 33%;
border: 1px solid black;
min-width: 300px;
margin-bottom: 8px;
}
Because it's 1px border surrounded to div.And their is default Margin:8 to body tag.
Width:33% is not working in 1366 screen resolution. So here you must need to use width:32.90% either 32%
Do change in Width and add Box-Sizing will help you.
.column {
display: inline-block;
min-height: 150px;
width: 33.1%;
border: 1px solid black;
min-width: 300px;
margin-bottom: 8px;
box-sizing: border-box;
}
Try keeping the div in one line in your editor.
<div class="wrapper">
<div class="column">abc</div><!--
--><div class="column">def</div><!--
--><div class="column">ghi</div>
</div>
You can use flex, flex is very good to make responsive div. It's a simple example.
--- HTML ----
<div class="flex-wrapper">
<your-element class="item"> 1 </your-element>
<your-element class="item"> 2 </your-element>
<your-element class="item"> 3 </your-element>
</div>
--- CSS ---
.flex-wrapper {
display: flex;
flex-wrap: wrap; // if you want to lots of items, will be wrapped
}
.item {
width: 33.3%;
}
and if your items have margin, you can calculate that like this.
.item {
margin: 5px;
width: calc(33.3% - 10px);
}
Example :
I hope will be helped you.
Use the following properties to your column class
.column {
display: inline-block;
min-height: 150px;
width:32%;
float:left;
margin-left:4px;
border: 1px solid black;
min-width: 300px;
margin-bottom: 8px;
}
Reason
float:left; property will align your div's on left hand side
width:32%; property will give width of 32 % instead of 33.33% as
1px you are assigning to border, which needs to be accommodated from
the width itself.
margin-left:4px; property will add spacing between divs, this is
for nicety.
Your column class needs a couple of minor fixes, float and width changes:
.column {
display: inline-block;
float: left;
min-height: 150px;
width: 33%;
border: 1px solid black;
min-width: 300px;
margin-bottom: 8px;
}

Responsive full width input with button

I have the fiddle here: https://jsfiddle.net/ufLpqdtj/
My problem is trying to get my search box and button to always sit full width on the page regardless of the device it is running on.
In Javascript I could always make the text box width 100% minus the pixel width of the button (the button is always the same size) but I feel as if im missing something and that it can be done natively in CSS.
Any thoughts or suggestions?
#commonSearchContainer {
display: block;
clear: both;
width: 100%;
position: relative;
}
#commonSearchTerm {
width: 100%;
margin: 25px 0px 0px 0px;
padding: 5px;
border: 1px solid #999999;
height: 35px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.common-search-term-wrapper {
width: 90%;
display: inline-block;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.common-search-button {
background-color: #E9700D;
border: 1px solid #ccc;
display: inline-block;
margin: 25px 0px 0px 10px;
width: 80px;
color: #fff;
padding: 7px;
font-style: italic;
cursor: pointer;
}
<div id="searchSection" class="common-search-section">
<div class="common-search-term-wrapper">
<input id="commonSearchTerm" type="text" autocomplete="off" class="common-search-term">
</div>
<div id="commonSearchSubmit" class="common-search-button">
Search
<div style="clear: both;"></div>
</div>
</div>
What I typically do for that sort of layout is make a parent container around the elements (like you have) and give it position: relative and width: 100%.
Then I use position: absolute and display: inline-block on the inner elements. Set the width for the fixed-sized elements and use left or right to position all of the elements.
In your case, it would be something like this: https://jsfiddle.net/ufLpqdtj/1/
Well you shouldn't use the div as a button. There are html elements for that.
If correctly understood what you want to achieve...
form {
width: 100%;
display: inline-flex;
justify-content: center;
}
#commonSearchTerm {
width: 80%;
}
#searchButton {
width: 80px;
border-radius: 0;
background-color: red;
border: none;
padding: 2px;
color: white;
}
<form >
<input id="commonSearchTerm" type="text" autocomplete="off" class="common-search-term">
<input id="searchButton" type="submit">
</form>
This is using flexbox which is is more flexible when creating responsive stuff.

Two resizeable divs , changing one changes the other

I'm trying to handle two resizeable divs where when I change one, the other div adjusts its size along the grid and vice versa.
I'm able achieve only the resize part. When I go beyond a certain length, the other div moves to the next line which I don't want to happen. Rather I want it to only stay in the same line.
I tried setting the position value to absolute and relative but still was not able to achieve what I want.
This is my Code:
.res {
border: 2px solid;
padding: 20px;
width: 200px;
resize: both;
overflow: auto;
}
.resright {
border: 2px solid;
padding: 20px;
width: 200px;
resize: both;
overflow: auto;
float: left;
position: relative;
}
<body>
<div class="container">
<div class="col-md-9">
<div class="col-md-2 res">
<p>Hello how are u?</p>
</div>
<div class="col-md-2 resright">
<p>ratatatatatata</p>
</div>
</div>
</div>
</body>
Please check this plunkr
Please try the below css
.res, .resright{
border: 2px solid;
box-sizing: border-box;
float: left;
overflow: auto;
max-width: 50%;
padding: 20px;
position: relative;
width: 200px;
resize: both;
}

How do I make this html two columns of equal width with a margin in between?

I have some HTML that I cannot change, but I can change the CSS as much as I want. I need to make these:
two columns of equal width
A margin in between of 2em
They have to take all the remaining width (parent width - 2em)
The boxes need to have a padding inside
HTML:
<div class="parent">
<a href="/page1" class="box">
<img class="pic" src="/images/image1.png">
<div class="description">the description</div>
</a>
<a href="/page2" class="box">
<img class="pic" src="/images/image2.png">
<div class="description">the description</div>
</a>
</div>
I'm able to do it without any spacing between them with: box-sizing: border-box; but if I add in a margin-right, they no longer fit.
Give this a try : It makes use of the Calc() function in css.
Note: The border throws off the calculation a bit, so you will have to adjust the calc slightly. I just did it to show you how the boxes were laid out.
.parent {
position: relative;
width: 600px;
height: 300px;
border: 1px solid red;
}
.box {
border: 1px solid black;
width: calc(50% - 1.25em);
display: inline-block;
}
.box:first-child {
margin-right: 2em;
}
Fiddle
I was able to solve it:
.parent .box
{
position: relative;
width: 50%;
margin: 0em;
padding: 1em;
float: left;
font-size: 1em;
overflow: hidden;
z-index: 0;
display: block;
text-decoration: none;
box-sizing: border-box;
}
.parent .box:nth-child(odd)
{
border-right: solid 1em #ffffff;
}
.parent .box:nth-child(even)
{
border-left: solid 1em #ffffff;
}
The n-th child lets me add the spacing between them

CSS anchor inline-block misaligned

I have this JSFiddle. Can someone explain, why is the anchor position misaligned relative to its siblings? I know I can correct it with position relative and negaitve top offset, but I don't understand, why it is like this in the first place.
HTML:
<div class="container">
<div class="left"></div>
Some link
<div class="right"></div>
</div>
CSS:
.container {
height: 25px;
white-space: nowrap;
}
.container .left {
border: 1px solid black;
display: inline-block;
margin: 0;
height: 25px;
width: 80px;
padding: 0;
}
.container .right {
border: 1px solid black;
display: inline-block;
margin: 0;
height: 25px;
width: 80px;
padding: 0;
}
.container a {
display: inline-block;
border: 1px solid black;
height: 25px;
width: 300px;
margin: 0;
}
The reason of this behaviour is due to the absence of text inside your .left and .right elements.
By default inline-block elements have vertical-align: baseline, but since you have empty elements there's no baseline, so they will be automatically aligned to the parent baseline (if you add some text inside them — even a — you would istantly solve the problem)
In order to prevent this behaviour you could also set a common vertical-align to all .container children.
You can add
vertical-align: top;
to .container a
This wil align the anchor with the divs.
You need to provide vertical-align property when you are declaring an inline-block.
Here you go.
WORKING DEMO
The CSS Change:
.container a {
display: inline-block;
border: 1px solid black;
height: 25px;
width: 300px;
margin: 0;
vertical-align:top;
}
You can use so many Option
1. Remove Display:inline-block and add float:left
Here the Demo
2. Use css vertical-align:top
Here demo