Div with absolute width is smaller than specified [duplicate] - html

This question already has an answer here:
flex items ignoring width
(1 answer)
Closed 1 year ago.
I am trying to have a number of columns with exact widths, and their heights split evenly between some number of elements. For some reason, despite my indicating an exact 200px width on each column, they are instead getting a computed width of 162px somehow.
Chrome dev tools is showing some weird arrow thing indicating that it it was shrunk from it's intended size for some reason. I've even tried removing all of the content from the div's as possible so as to rule out some weird interaction with the size of children.
The HTML content for the relevant area is this:
div {
background: rgba(255, 0, 0, .1);
}
<div style="display: flex;">
<div style="width: 200px; margin-right: 100px;">
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
</div>
<div style="width: 200px; margin-right: 100px;">
<div style="height: 100px;"></div>
<div style="height: 100px;"></div>
<div style="height: 100px;"></div>
<div style="height: 100px;"></div>
</div>
<div style="width: 200px; margin-right: 100px;">
<div style="height: 200px;"></div>
<div style="height: 200px;"></div>
</div>
<div style="width: 200px; margin-right: 100px;">
<div style="height: 400px;"></div>
</div>
</div>
Including some dev-tools highlighting (showing the arrow thing I described) it is rendering like this (the "round" labels at the top are not in the HTML content above but are properly 200px + 100px margin):
I have never seen anything like this before, especially those arrow things from the dev tools. Is there something obvious I'm missing or something I should look for to diagnose this?

Setting display: flex turns the sizing of child elements over to the flex container. If you don't want the individual elements to resize, set flex-grow: 0, flex-shrink: 0, and flex-basis: 200px. You can do all three using the flex shorthand:
flex: 0 0 200px;
.container {
display: flex;
}
.container > * {
flex: 0 0 200px;
margin-right: 100px;
}
div {
background: #cccccccc;
}
<div class="container">
<div style="width: 200px; margin-right: 100px;">
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
<div style="height: 50px;"></div>
</div>
<div style="width: 200px; margin-right: 100px;">
<div style="height: 100px;"></div>
<div style="height: 100px;"></div>
<div style="height: 100px;"></div>
<div style="height: 100px;"></div>
</div>
<div style="width: 200px; margin-right: 100px;">
<div style="height: 200px;"></div>
<div style="height: 200px;"></div>
</div>
<div style="width: 200px; margin-right: 100px;">
<div style="height: 400px;"></div>
</div>
</div>

This is the default behaviour for Flexbox. If you add up all your widths, so 200 width + the 100 margin, you get 300 * 4 = 1200px. If your viewport is smaller than 1200px then the browser will try to calculate the best width it can to fit all your div along the main axis. thus you are getting 162 + 100 * 4 is just shy of 1200. Try resize your viewport or the browser screen to bigger than this and you should get the expected behaviour.
The arrow you are seeing is Chrome dev tools way of telling you your original width has been made smaller to fit all content.

Related

Set Div to move below another Div as screen size shrinks

I have two Div's, the left one is a sidebar of sorts (but it does not extend the full height of the screen), and the right one is the content Div that should occupy as much of the screen as possible and collapse/move underneath the sidebar as the screen shrinks. Right now I have the sidebar set up how I expect it to be, but the Content Div begins to overlap the Sidebar as the page shrinks and does not move below.
I have tried different position: parameters (fixed,absolute,relative) different width/min-width values, as well as different values for float. I have everything in html styling right now for simplicity. The css files that were being used seemed to be interfering with functionality so I attempted to override them via styling directly within the HTML.
<body style="padding-top: 5px; padding-bottom: 20px; padding-left: 15px; padding-right: 15px; min-height: 80%; min-height: 50vh;">
<h3 class="text-center"> Title </h3>
<div class="SideContainer" style="background-color: green;position:absolute;left:0; top:80; bottom: 400;width: 20vw;min-width: 200px;max-width: 300px;padding: 3px;">
<div style="background-color: red;width: 100%;">Keys</div>
<div style="background-color: blue;">Error Box</div>
<div style="background-color: grey;border: none;width: 100%;border-radius: 3px;">File Menu Button</div>
<p> Enter Display Name</p>
<div style="background-color: yellow;">Name Box</div>
<p> Users Viewing </p>
<div style="background-color: orange;">User box</div></div>
<div class="ContentContainer" style="background-color: gold;position: relative;float: left;left:20vw; top: 25;width:80%;min-width: 400px;margin-right: 3px;padding: 8px;">
<div style="background-color: purple;border: 1px solid;border-radius: 4px;line-height: 2.6;padding: 3px;margin-bottom: 6px;">Doc Title</div>
<div style="background-color: maroon;min-height: 150px;border: 1px solid;border-radius: 4px;padding: 3px;">Doc Content</div></div><body>
Content div should shrink, and at a certain point, move below the Sidebar div.
<body style="padding-top: 5px; padding-bottom: 20px; padding-left: 15px; padding-right: 15px; min-height: 80%; min-height: 50vh;">
<h3 class="text-center"> Title </h3>
<div style="display: flex; flex-flow: row wrap;">
<div class="SideContainer" style="background-color: green;left:0; top:80; bottom: 400;width: 20vw;flex-grow: 1;padding: 3px;min-width: 200px;max-width: 300px">
<div style="background-color: red;width: 100%;">Keys</div>
<div style="background-color: blue;">Error Box</div>
<div style="background-color: grey;border: none;width: 100%;border-radius: 3px;">File Menu Button</div>
<p> Enter Display Name</p>
<div style="background-color: yellow;">Name Box</div>
<p> Users Viewing </p>
<div style="background-color: orange;">User box</div>
</div>
<div class="ContentContainer" style="background-color: gold;float: left;left:20vw; flex-grow: 4;top: 25;margin-right: 3px;padding: 8px;min-width: 200px;">
<div style="background-color: purple;border: 1px solid;border-radius: 4px;line-height: 2.6;padding: 3px;margin-bottom: 6px;">Doc Title</div>
<div style="background-color: maroon;min-height: 150px;border: 1px solid;border-radius: 4px;padding: 3px;">Doc Content</div>
</div>
</div>
<body>
I got your code working using Display:flex, it's a good idea to also separate your code into html and css and use classes references in the HTML. There is a lot of benefits to this including easier to maintain, cleaner, and also makes it easy to follow etc.

Zoom inside a center of a picture instead of changing height

I have a gallery in the center of my website - it has a dynamic width (bootstrap column), and fixed height (80vh). I want the images inside to have 50% width of the container, and 350px height:
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6" style="height: 80vh;">
<div style="overflow-y: auto;">
<div style="width: 50%; height: 350px; float: left">
<img src="..." style="min-width: 100%; height: 350px;">
</div>
<div style="width: 50%; height: 350px; float: left">
<img src="..." style="min-width: 100%; height: 350px;">
</div>
<div style="width: 50%; height: 350px; float: left">
<img src="..." style="min-width: 100%; height: 350px;">
</div>
</div>
</div>
<div class="col-md-3"></div>
</div>
The thing is that when I resize the website, the width is changed, and the height stays the same - the image gets resized. Is there a better solution? I've been thinking about zooming on an image instead of resizing the whole thing - is this possible?
If you strictly have to use a 350px height for each gallery item and want images to maintain aspect ratio, you can use background-size: cover to zoom in and crop the images instead of resizing them. See: https://jsfiddle.net/nelonoel/c8zsscuL/

How to specify width in px for cell?

Sample code
<div class="container">
<div class="row" style=" display: flex;">
<div style="width: 200px; height: 50px; background-color: #847"></div>
<div class="container" style=" height: 50px; background-color: #147">
<div class="row" style=" height: 20px; background-color: #997">
<div class="col-xs-3 col-md-6 col-lg-2 " style=" height: 10px; background-color: #927"></div>
<div class="col-xs-3 col-md-6 col-lg-6" style=" height: 10px; background-color: #947"></div>
</div>
</div>
</div>
</div>
I need:
12 indented cells in the child rows(seems it works)
at the same time, have cell width in px for right cell (flex saves me, but i see some error when i reduce browser width until the end, in safari)
http://codepen.io/anon/pen/zKXaoV
Help please, maybe it is not good solution and exists better one or how to fix? Thanks

Two divs above and below each other stacked beside another similar pair of divs

I cannot seem to get these Google map divs to sit side by side. It was working fine until I decided to restructure it.
It consists of 4 divs, one above the other on the left and one above the other on the right. The two on the left are in their own container and the two on the right are in a container. Those two containers then sit side by side inside a bigger container.
It is all about being able to turn on and off the display of the individual sides (map vs pano) including the lat/long readout that sits above each map.
This is the earlier version that was working fine but was messy. To turn off the streetview pano for example I would have had to turn off two divs.
<div style="float:left; width: 512px;">
<table>
<tr><td><b>Lat/Lng:</b></td><td id="mappos">-33.1234,150.1234</td></tr>
</table>
</div>
<div style="float:left;">
<table>
<tr><td><b>Lat/Lng:</b></td><td id="svpos">-33.12345,150.12345</td>
<td><b>Hdg:</b></td><td id="hdg">90</td></tr>
</table>
</div>
<div style="clear:both;"></div>
<div style="width: 1024px; height: 512px">
<div id="map" style="float:left; width: 50%; height: 512px;"></div>
<div id="pano" style="float:left; width: 50%; height: 512px;"></div>
</div>
This is the new neater code that would supposedly allow easier switching on and off of each side but it does not work. The two pairs of divs sit on top of each other instead of side by side. It is no doubt an obvious error but I cannot spot it. I have looked at numerous other posts about side-by-side divs but cannot see what I am doing wrong.
Originally I had the left and right container divs specified with width 512px but when I tried to expand them to 1024px via Javascript it did not work. I therefore used 50%. When I change that to 100% followed by map resize it expands fine.
<div style="clear:both; width: 1024px;">
<div id="mapdiv" style="float:left; width: 50%"></div>
<div style="width: 512px;">
Lat/Lng: <span id="mappos">-33.1234,150.1234</span>
</div>
<div id="map" style="width: 512px; height: 512px;">
</div>
</div>
<div id="panodiv" style="float: left; width: 50%"></div>
<div style="width: 512px;">
Lat/Lng: <span id="svpos">-33.12345,150.12345</span> <span id="hdg">90</span>
</div>
<div id="pano" style="width: 512px; height: 512px;">
</div>
</div>
</div>
I thought it could be a padding issue. I tried increasing the width of the outer container to 1040 etc. but to no avail.
Here is a fiddle: http://jsfiddle.net/Lack10o0/
You just have to remove the unnecessary closing </div>s on line 2 and 9.
Edit : I updated your jsfiddle
You have too many closing tags. Take out the </div> after <div id="mapdiv" style="float:left; width: 50%"> and after <div id="panodiv" style="float:left; width: 50%">
Here is the corrected code:
<div style="clear:both; width: 1024px;">
<div id="mapdiv" style="float:left; width: 50%">
<div style="width: 512px;">
Lat/Lng: <span id="mappos">Top left div</span>
</div>
<div id="map" style="width: 512px; height: 512px;">Bottom left div
</div>
</div>
<div id="panodiv" style="float: left; width: 50%">
<div style="width: 512px;">
Lat/Lng: <span id="svpos">Top right div</span> <span id="hdg">90</span>
</div>
<div id="pano" style="width: 512px; height: 512px;">Bottom right div
</div>
</div>
</div>
Looks like you have a couple closing tags to many there. You have 7 opening divs and 9 closing divs. The faulty ones are probably the ones behind #mapdiv and #panodiv.

css columns and content

This probably is the most frequently asked question however I am not really able to find anything that seems to work for me - I am a newbie # styling/css.
I would like a cross browser compliant solution (IE9 or higher btw) where I can have as a header (first row) 3 columns:
Name_____DOB______Contact
underneath, there will be content. a row of data and I want the row of data (which is contained in a div) to be aligned correctly with the column headings.
Currently I have this:
<div style="width: 100%">
<div style="width: 300px; float:left">Name</div>
<div style="width: 200px; float:left">DOB</div>
<div style="width: 100px; float:left">Contact</div>
</div>
in terms of the data, I am using MVC4 with Razor so I am just using a for loop to go through a collection of data and spitting it out in a div i.e:
[for loop here]
<div id="refitem_#counter">
[data here]
</div>
[end for loop]
You can continue with the same approach.
<div>
<div style="width: 100%">
<div style="width: 300px; float:left">Name</div>
<div style="width: 200px; float:left">DOB</div>
<div style="width: 100px; float:left">Contact</div>
</div>
<div style="width: 100%">
<div style="width: 300px; float:left">Name 1</div>
<div style="width: 200px; float:left">DOB 1</div>
<div style="width: 100px; float:left">Contact 1</div>
</div>
<div style="width: 100%">
<div style="width: 300px; float:left">Name 2</div>
<div style="width: 200px; float:left">DOB 2</div>
<div style="width: 100px; float:left">Contact 2</div>
</div>
..
</div>
I'm strongly dis-advise you to use that. use a table instead.
also (as a BTY): don't mix your markup and CSS, use something like that.
HTML:
<div>
<div>
<div class="column col1">Name</div>
<div class="column col2">DOB</div>
<div class="column col3">Contact</div>
</div>
<div>
<div class="column col1">Name 1</div>
<div class="column col2">DOB 1</div>
<div class="column col3">Contact 1</div>
</div>
<div>
<div class="column col1">Name 2</div>
<div class="column col2">DOB 2</div>
<div class="column col3">Contact 2</div>
</div>
..
</div>
CSS:
.column
{
float: left;
}
.col1
{
width: 300px;
}
.col2
{
width: 200px;
}
.col3
{
width: 100px;
}
width: 100%; is the default behaviour for a block element (like a div) so you should omit it.