How to left-align elements in centered div - html

Problem
I am currently trying to left-align blocks within a centered wrapper with dynamic width. I can't get this to work using only HTML/CSS.
Here is a JSFiddle: https://jsfiddle.net/hudxtL8L/
Examples
So currently, it looks like this:
| _____ _____ |
| | | | | |
| | | | | |
| |_____| |_____| |
| _____ _____ |
| | | | | |
| | | | | |
| |_____| |_____| |
| _____ |
| | | |
| | | |
| |_____| |
| |
And I want it to look like this:
| _____ _____ |
| | | | | |
| | | | | |
| |_____| |_____| |
| _____ _____ |
| | | | | |
| | | | | |
| |_____| |_____| |
| _____ |
| | | |
| | | |
| |_____| |
| |
or, on a bigger device, something like this:
| _____ _____ _____ |
| | | | | | | |
| | | | | | | |
| |_____| |_____| |_____| |
| _____ _____ _____ |
| | | | | | | |
| | | | | | | |
| |_____| |_____| |_____| |
| _____ |
| | | |
| | | |
| |_____| |
| |
The important thing here is that the last row is not centered, but left-aligned within the centered parent. Can this be done, and if so, how? I tried different approaches but all of them failed.
Tried approaches
margin: 0 auto will not work since it requires a fixed width, but I want as many .element's as possible per row.
Using a table seems difficult as well, since I don't know how many .element's will fit in one row on the current device.
Using javascript will of course work, but I have the feeling that there is a CSS-only solution to this.

Use can use CVS flex to realize this layout.
One approach is to define a container, #content that has a fixed width and center it with margin: 0 auto.
Apply the flex properties to #content, use justify-content: space-between to get the child elements in the desired positions.
The child .elements need flex-basis: 100px to specify the width within the flex container context.
You can control the spacing between elements using margins.
Reference: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout
#viewport {
border: 1px solid black;
}
#content {
width: 250px;
margin: 0 auto;
border: 1px dashed blue;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.element {
border: 1px dotted black;
flex-basis: 100px;
height: 100px;
margin: 10px; /* optinal, gives you some control on spacing */
}
<div id="viewport">
<div id="content">
<div class="element">1</div>
<div class="element">2</div>
<div class="element">3</div>
<div class="element">4</div>
<div class="element">5</div>
</div>
</div>

Is this what you want:
#content {
width: 300px;
border: 1px solid red;
}
#outer {
text-align: center;
border: 1px solid green;
}
#inner {
display: inline-block;
text-align: left;
border: 1px solid blue;
padding: 20px;
}
.element {
display: inline-block;
border: 1px solid black;
width: 100px;
height: 100px;
margin-left: 15px;
}

#inner {
display: block;
text-align: left;
border: 1px solid blue;
text-align:center;
-moz-text-align-last: left;
text-align-last: left;
}
.element:last-child {
margin-left:20px;
}
Use text-align-last: left

Related

Div Positioning IOS mobile different from other browsers

Does anyone know how to fix the problem of iOS displaying elements in incorrect spots? I have a web app with two layers of divs superimposed on each other. It looks like the following on a desktop:
|--------------------------|
| |div1 - layer 1| |
| | | |
| | | |
| | |div2| | |
| | | |
| |--------------| |
| |div3 - layer 1| |
| | | |
| | | |
| | |div4| | |
| | | |
| |--------------| |
|--------------------------|
But on iOS, whether Safari/Firefox/Chrome, it looks like a less dramatic version of this:
|--------------------------|
| |div1 - layer 1| |
| | | |
| | |div2| | |
| | | |
| | | |
| |----|div4|----| |
| |div3 - layer 1| |
| | | |
| | | |
| | | |
| | | |
| |--------------| |
|--------------------------|
And it happens whether I use absolute positioning, relative positioning, or whether I use px or em, or whether I use margin-top vs top. Nothing seems to fix it. Only moves the divs around a bit but the problem still exists.
Is this something others have encountered?
Pics (excluding sensitive info):
Desktop:
iOS:
HTML:
<div id="cdiv" class="fdiv">
<label id="clabel"></label>
<label id="cdatelabel"></label>
<asp:TextBox runat="server" ID="txtcdate" onfocus="select()" class="txtfselect2" AutoPostBack="false" ReadOnly="false"></asp:TextBox>
</div>
<div id="fdiv" class="fdiv">
<label id="flabel"></label>
<button id="btnp" class="pbtn" onclick="return false;"></button>
<button id="btnx" class="xbtn" onclick="return false;">X</button>
<asp:TextBox runat="server" ID="txtfselect" onfocus="select()" class="txtfselect" AutoPostBack="false" ReadOnly="false"></asp:TextBox>
</div>
<asp:GridView ID="gtSLdsf" runat="server" CssClass="gcf" CellPadding="0" ForeColor="#333333" AutoGenerateColumns="False" Width="100%" ShowHeader="false">
<Columns>
</Columns>
</asp:GridView>
<asp:GridView ID="gtSLdsffixed" runat="server" CssClass="gcfixedt" CellPadding="0" ForeColor="#333333" AutoGenerateColumns="False" ShowHeader="false">
<Columns>
</Columns>
</asp:GridView>
CSS:
#gtSLdsf{
margin-top:2px;
}
#gtSLdsffixed {
position: absolute;
border-right: 4px solid;
border-top: 1px solid;
left: 0px;
top:356px;
z-index: 1;
}
.gcfixedt td{
padding-left: 2px;
padding-right: 2px;
padding-top:2px;
padding-bottom: 2px;
}
#cdiv{
margin-top:-2px
}
.fdiv {
width: 300%;
position: relative;
left: 0px;
display: inline-block;
margin-left: 0px !important;
background-color: #CECECE;
padding-left: 0px;
z-index: 1;
padding-top:3px;
min-height:30px;
}
#containdiv {
height: 100%;
position: relative;
pointer-events: all;
overflow: scroll;
margin-left: 28px;
padding-top: 4px;
}
.body {
height: 99vh;
position: relative;
overflow: hidden;
margin: 2px;
margin-bottom: 0px;
-moz-user-select: none;
-webkit-touch-callout: none;
}

How can I vertically center two spans on either side of a div, relative to that div?

Right now, it basically looks like this:
_______________________________________
| |
| ___________ |
| | | |
| ______ | div | ______ |
| |span| | | |span| |
| |____| |_________| |____| |
|_____________________________________|
And I want it to look like this:
_______________________________________
| |
| ___________ |
| ______ | | ______ |
| |span| | div | |span| |
| |____| | | |____| |
| |_________| |
|_____________________________________|
Flexbox can do that https://jsfiddle.net/2Lzo9vfc/220/
CSS
.content {
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-around;
}
.content div,
.content span {
padding: 50px;
border: 1px solid black;
}
.content div {
height: 300px;
line-height: 300px;
padding: 0 50px;
}
HTML
<div class="content">
<span>Span 1</span>
<div>Div </div>
<span>Span 2</span>
</div>
Will need to see your code to answer this more specifically, but try something like this
<div style="display:table-cell; vertical-align:middle"> ... </div>
If you put the div and spans in a table then you can simply add valign="middle" to the TD.
I would recommend that you use display:tablecell and then vertical-align:middle for your div. I am fairly new to html though, so I may be wrong.

div + scroll + fixed bottom + variable top

I have a problem with sizing a scrolling div to fill the window, considering that I have one or more top divs and a footer div.
this is what I need
+------------------+ +------------------+ +------------------+
| top1 | | top1 | | top1 |
+------------------+ +------------------+ +------------------+
| top2 | | top2 | | |^|
| | +------------------+ | | |
+------------------+ | |^| | | |
| |^| | | | | scroll | |
| | | => | scroll | | => | | |
| scroll | | | | | | | |
| | | | | | | | |
| | | | | | | | |
| |v| | |v| | |v|
+------------------+ +------------------+ +------------------+
| footer | | footer | | footer |
+------------------+ +------------------+ +------------------+
Top1 has a fixed height.
Footer has a fixed height.
Top2 doesn't have a fixed height and sometimes doesn't even appear.
The only way I know to do that is by defining the container height, fixing its top and its bottom. But I cannot fix the top property since the top2 div has variable height...
Can someone help me?
html:
<body>
<div id='top1'>Top1</div>
<div id='top2'>Top2</div>
<div id='container'>
<ul id='data'>
<li>item1</li>
<li>item2</li>
<li>item3</li>
<li>item4</li>
<li>item5</li>
<li>item6</li>
<li>item7</li>
<li>item8</li>
<li>item9</li>
<li>item10</li>
</ul>
</div>
<div id="footer">footer</div>
</body>
css:
body, html {
height: 100%;
}
#top1 {
width: 100%;
height: 50px;
background-color: #EEE;
text-align: center;
line-height: 50px;
}
#top2 {
width: 100%;
height: 50px;
background-color: #DDD;
text-align: center;
line-height: 50px;
}
#footer {
width: 100%;
height: 50px;
background-color: #BBB;
text-align: center;
line-height: 50px;
position: fixed;
bottom: 0;
}
#container {
overflow: auto;
width: 100%;
position: absolute;
top: 100px;
bottom: 50px;
}
#data li {
font-size: 30px;
padding: 10px;
}
try it
first get windows height by this function
$( window ).height();
and make one more function for top2
$( document ).height();
pluse heights 1st header + 2nd footer +.3rd top2 ( get by this function $( document ).height();)
minus height in windows height
then will get new height and apply on scrlloing div
one more thing for make better scrlloing use resizing function

Set elements' height value to the max one in a line with pure CSS

I have 3 divs with display:inline-block style. I want to set their height value so that it matches the one with the highest value. I also want to set height values auto. I've tried to show visually what I want to get below. Is this possible with pure CSS?
----- ----- ----- ----- ----- -----
| | | | | | | | | | | |
----- | | | | ==> | | | | | |
----- | | | | | | | |
----- ----- ----- -----
With CSS you can set the inner divs to be display: table-cell and the outer one to be display: table
Demo: http://jsfiddle.net/maniator/C2dNu/
HTML:
<div id='out'>
<div class='red'></div>
<div class='blue'></div>
<div class='green'></div>
</div>
CSS:
.red {
background: red;
height: 60px;
}
.blue {
background: blue;
height: 160px;
}
.green {
background: green;
height: 80px;
}
#out {
display: table;
width: 500px;
}
#out > div {
display: table-cell;
width: 33%;
}
With JS, it can be done with Equalize.js. I don't know a pure CSS solution though.

how to reduce bottom of relatively positioned div to 100%?

If I have two divs, outer and inner, with following stylesheet:
#html,body{
height:100%;
margin:0;
padding: 0;
}
#outer{
position: relative;
top: 0;
left: 0;
height: 100%;
width: 100%;
background-color: red;
}
#inner{
position: relative;
top: 0;
left: 0;
width: 0;
height: 100%;
background-color: yellow;
}
At the same time, outer and inner div should grow both in width and height if content inside innerdiv grows dynamically.
+---------------------+
| +=================+ |
| | | |
| | | |
| | | |
| | | |
| | div id="inner" | | div id="outer"
| | | |
| | | |
| | | |
| +-----------------+ |
+---------------------+
But if I change the stylesheet to this:
#html,body{
height:100%;
margin:0;
padding: 0;
}
#outer{
position: relative;
top: 0;
left: 0;
height: 100%;
width: 100%;
background-color: red;
}
#inner{
position: relative;
top: 50px;
left: 0;
width: 0;
height: 100%;
background-color: yellow;
}
The inner div will go down to 50px from top and bottom of outer div as there is top:50px style on inner div like this:
+---------------------+
| |
| +=================+ |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
+-| |-+
| |
+-----------------+
But I want inner div will go down 50px from outer div, but remain bottom of outer div with position: relative like this:
+---------------------+
| |
| +=================+ |
| | | |
| | | |
| | | |
| | | |
| | | |
| +-----------------+ |
+---------------------+
How to do this?
You can achieve this using a jQuery
var newinnerdivheight=$("#innerdiv").height() + $("#innerdiv").innerHeight() +$("#innerdiv").outerHeight();
$("#innerdiv").height(newinnerdivheight);
$("#outerdiv").height(newinnerdivheight+50);
similarly for width also...