stacking divs -- mix of vertical and horizontal layout - html

I would like to accomplish a mix of horizontal and vertical layouts.
<div>
<div>
<span>A</span>
<span>B</span>
<span>C</span>
</div>
<div>
<span>D</span>
</div>
</div>
#1 It should look like this
A B C
<- D ->
#2 Instead I have the two div elements next to one another
A B C <- D ->
The real example has some very bad CSS that I recommend you not look at. I wanted to set the top three items to the same width of 25px and to be arranged next to one another. And a picture beneath the two control buttons.
For some reason, the div elements are not stacking on top of one another and instead sitting next to each other all in one row. Please help!
<div>
<div>
<button style="width: 25px; float: left;">-</button>
<span style="width: 25px; float: left; text-align: center;">0</span>
<button style="width: 25px; float: left;">+</button>
</div>
<div>
<img src="starry-night.jpg" width="500">
</div>
</div>

HTML:
<div>
<div>
<span>A</span>
<span>B</span>
<span>C</span>
</div>
<div>
<span>D</span>
</div>
</div>
CSS:
div {
width: 100%;
}
div > div {
display: flex; /* pertinent */
width: 100%;
}
div > div > span {
width: 33%; /* pertinent */
justify-content: center; /* pertinent */
margin: 1px auto; /* pertinent */
background: #333;
text-align:center;
color:#efefef;
font-family: sans-serif;
}
https://jsfiddle.net/16dqk7cL/1/

Give a width of 75 px to the parent div so that after placing 3, 25px elements the next element will be forced to be on the new line.
<div style="width: 75px;">
<div>
<button style="width: 25px; float: left;">-</button>
<span style="width: 25px; float: left; text-align: center;">0</span>
<button style="width: 25px; float: left;">+</button>
</div>
<div>
<img src="starry-night.jpg" width="500">
</div>
</div>

One of the possible solutions is to have a container Div and specify a width for it. Then adjust your other Divs width based on it.
<div style="width: 300px">
<div style="width=100%;">
<button style="width: 33%; float: left;">-</button>
<span style="width: 33%; float: left; text-align: center;">0</span>
<button style="width: 33%; float: left;">+</button>
</div>
<div style="width: 150px; margin:auto">
<img src="starry-night.jpg" style="width: 150px;">
</div>
</div>

Related

Divide page in two columns and align them in HTML

ImageI would like to display the page in two columns below the heading and center align the buttons the way it is.
I do not understand if I have missed any divs here?
I have been trying to do it but its not showing up the way i expected.
<div>
<div class="slds-p-top--x-large slds-text-align--center slds-p-bottom_small" width="100%">
<span>Help us improve Customer Support</span>
</div>
<table>
<tr>
<td width="50%" class="divText slds-p-bottom--medium leftBtn" >Tell us what you think<br/>
<div class="slds-text-align--center slds-p-top--small slds-p-bottom--large slds-p-top--medium">
<button class="slds-button slds-button--neutral">
<span class="btn-feedback">Provide feedback</span>
</button>
</div>
</td>
<td width="50%" class="vertical-liness divText slds-p-bottom--medium rightBtn" >Collaborate with our team<br/>
<div class="slds-text-align--center slds-p-top--small slds-p-bottom--large slds-p-top--medium">
<button class="slds-button slds-button--neutral">
<span class="btn-feedback">Get Involved</span>
</button>
</div>
</td>
</tr>
</table>
</div>
.THIS .vertical-liness
{
border-left: 2px solid red;
}
.THIS .leftBtn{
padding-left:115px;
float:right;
}
.THIS .rightBtn{
padding-left:175px;
}
.THIS {
background-color: #f4f7f7; /* ibm-cool-white-3; */
border-top: 1px solid #d0dada; /* ibm-gray-10 */
font-size: 20px;
font-weight: normal;
color: #272727; /* ibm-gray-90 */
}
.THIS .divText{
font-size: 16px;
color: #272727; /* ibm-gray-90 */
}
I simplified your markup to show an example. You can create a couple of flex parents and use their centering properties to recreate this.
.flex {
display: flex;
justify-content: center;
align-items: center;
}
.col {
flex-direction: column;
}
.button {
padding: 0 3em;
text-align: center;
}
.button1 {
border-right: 1px solid #ccc;
}
header {
margin: 0 0 .5em;
}
<div class="flex col">
<header>
Help us improve Customer Support
</header>
<div class="buttons flex">
<div class="button button1">
<div>
Tell us what you think
</div>
<button>Provide feedback</button>
</div>
<div class="button button2">
<div>
Tell us what you think
</div>
<button>Provide feedback</button>
</div>
</div>
</div>
You need to create two divs in your body and place the content inside each of them.
body {
width: 500px;
height: 500px;
}
.left, .right {
float: left;
width: 50%;
height: 100%;
}
.left {
background-color: red;
}
.right {
background-color: blue;
}
<div class="left">
<p>Left column</p>
</div>
<div class="right">
<p>Right column</p>
</div>
https://codepen.io/anon/pen/QgLKzQ
float: left; with width: 50%; is the key here.
Be careful that you need either content inside your div like the below example, either give them an height (eg. CSS: .left-float { height: 500px; }
Be careful either that floating elements will break the flow.
You will need to clear float with the next elements:
https://css-tricks.com/almanac/properties/c/clear/
CSS :
.left-float {
width:50%;
float:left;
}
HTML :
<div>
<div class="left-float" style="background-color:green;">
left div
</div>
<div class="left-float" style="background-color:red;">
right div
</div>
</div>
So, with your code... Ugh, put me off this <table> element please !
They are designed to display tabular data and not to provide a way of positioning elements. In your case you do not display tabular data.
An example of how it could be :
https://jsfiddle.net/12fd30bf/
HTML:
<div>
<div class="slds-p-top--x-large slds-text-align--center slds-p-bottom_small" width="100%">
<span>Help us improve Customer Support</span>
</div>
<div class="left-float">
Tell us what you think<br/>
<div class="slds-text-align--center slds-p-top--small slds-p-bottom--large slds-p-top--medium">
<button class="slds-button slds-button--neutral">
<span class="btn-feedback">Provide feedback</span>
</button>
</div>
</div>
<div class="left-float">
Collaborate with our team<br/>
<div class="slds-text-align--center slds-p-top--small slds-p-bottom--large slds-p-top--medium">
<button class="slds-button slds-button--neutral">
<span class="btn-feedback">Get Involved</span>
</button>
</div>
</div>
</div>
CSS :
.left-float {
width:50%;
float:left;
}

Center items vertically without setting height

I'm trying to vertically center certain items within a table cell. I've tried most solutions on stackoverflow and several other sites without any luck.
In this cell, the image is stuck at the top of the table cell, while the text is properly centered vertically:
<tr>
<td class='sidebar-middle'> <!--sets a left and right border-->
<a target="_blank" href="data/Standards.pdf">
<div style='width: 100%;text-align: center;overflow: hidden;'>
<div style='float: left;width: 34%; text-align: center;height: 100%;'>
<img src='images/logo.jpg' alt='Standards' style='width: 80px;vertical-align: middle;'/>
</div>
<p style='float: right; vertical-align: middle;width: 64%;'>Local Facility Standards to be Followed</p>
</div>
</a>
</td>
</tr>
However, using the same method, this DOES seem to work:
<tr>
<td class='sidebar-bottom'> <!--sets a left, right, and bottom border-->
<a target="_blank" href="Policies.html">
<div style='width: 100%;text-align: center;overflow: hidden;'>
<div style='float: left;width: 35%; text-align: center;height: 100%;'>
<img src='images/patch.png' alt='Policies' style='height: 80px;vertical-align: middle;'/>
</div>
<p style='float: right; vertical-align: middle;width: 64%;'>Policies</p>
</div>
</a>
</td>
In the first (frustrating) example, the image is 112 pixels in height, scaled down to 30. In the second (working) example, the image is 122 pixels in height, scaled down to 80. I suspect that image height has something to do with it, but can't get any further in resolving the problem.
While assigning classes to the elements I didn't see a change. When I replaced the <tr> and <td> with <div> and <section> it didn't change. It just works like the way you wanted it to. There's no style info provided for classes, .sidebar-middle and .sidebar-bottom so that might be your problem (or the rest of the code you neglected to post). Note: I didn't need to modify the div.C or the <section>s I added, so table components may have not been needed and the floats were sufficient.
When using inline styling heavily, your HTML gets cluttered and there's no easy way of fixing it should you have many lines of that coding disaster. As Paulie_D and hidanielle already stated, your vertical-align does not function on floated elements, and HTML table -layouts are so 90s. In the 21st century we use table-* CSS properties.
SNIPPET
.A {
width: 100%;
text-align: center;
overflow: hidden;
}
.B {
float: left;
width: 34%;
text-align: center;
height: 100%;
}
.img {
width: 80px;
height: 80px;
}
.note {
float: right;
width: 64%;
}
<div class='C'>
<section class='sidebar-middle'>
<a target="_blank" href="http://www.orimi.com/pdf-test.pdf">
<div class='A'>
<div class='B'>
<img src='https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png' alt='Lenna' class='img' />
</div>
<p class='note'>Local Facility Standards to be Followed</p>
</div>
</a>
</section>
</div>
<div class='C'>
<section class='sidebar-bottom'>
<a target="_blank" href="http://www.example.com">
<div class='A'>
<div class='B'>
<img src='https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png' alt='Lenna' class='img'>
</div>
<p class='note'>Policies</p>
</div>
</a>
</section>
</div>
Instead of floats, use CSS Tables (since you started with an actual table for layout).
* {
margin: 0;
padding: 0;
}
.inner {
display: table;
table-layout: fixed;
width: 100%;
text-align: center;
overflow: hidden;
}
.left {
display: table-cell;
width: 34%;
text-align: center;
background: pink;
}
img {
width: 80px;
}
.right {
display: table-cell;
width: 64%;
vertical-align: middle;
background: lightblue;
}
<a target="_blank" href="data/Standards.pdf">
<div class="inner">
<div class="left">
<img src='http://www.fillmurray.com/80/80' alt='Standards' />
</div>
<p class="right">Local Facility Standards to be Followed</p>
</div>
</a>

Center-align text within DIV with "display: table-cell"

Overview
I have an image with variable width aligned to the left, with text (having variable length) to the right of it.
I am trying to make the width of the right-most div element (containing the text) to fill the remaining width, centering the text both horizontally and vertically.
However, I cannot find a way to center align the text horizontally.
Code
As you can see by the code below, the text will only be centered in the rightmost div when the length of text flows over one line - however, in many instances in my project, the code is much smaller (this example is just "Centered Text") and the text is left-aligned:
<hr>
<div style="float:left; height: 75px;">
<img src="//i.imgur.com/rJD1cRy.png" style="margin:auto;display:block;">
</div>
<div style="width: 100%;">
<div style="display: table; height: 75px; overflow: hidden;">
<div style="font-style: italic; color: #777; display: table-cell; vertical-align: middle; text-align: center;">
Centered Text
</div>
</div>
</div>
<div style="clear: both;"></div>
<hr>
for centering the right-most div horizontally ,
adding margin:0 auto to the div should fix the issue:
<hr>
<div style="float:left; height: 75px;">
<img src="//i.imgur.com/rJD1cRy.png" style="margin:auto;display:block;">
</div>
<div style="width: 100%;">
<div style="display: table; height: 75px; overflow: hidden; margin:0 auto;">
<div style="font-style: italic; color: #777; display: table-cell; vertical-align: middle; text-align: center;">
Centered Text
</div>
</div>
</div>
<div style="clear: both;"></div>
<hr>
<hr>
<div style="float:left; height: 75px;">
<img src="//i.imgur.com/rJD1cRy.png" style="margin:auto;display:block;">
</div>
<div style="width: 100%;">
<div style="display: table; height: 75px; overflow: hidden; width: 60%;">
<div style="font-style: italic; color: rgb(119, 119, 119); display: table-cell; vertical-align: middle; text-align: center;">
Centered Text
</div>
</div>
</div>
<div style="clear: both;"></div>
<hr>

Align Img Vertically within Div HTML and CSS

I have found the following solution for aligning an img vertically within a div
https://stackoverflow.com/a/7310398/626442
and this works great for a basic example. However, I have had to extend this and I want a row with two bootstrap col-md-6 columns in it. In the first column I want a 256px image, in the second I want a h1, p and a button. I have to following HTML:
<div class="home-costing container">
<div class="row">
<div class="frame">
<span class="helper"></span>
<div class="col-md-6">
<img src="http://www.nijmegenindialoog.nl/wp-content/uploads/in.ico" height="256" width="256" />
</div>
<div class="col-md-6">
<h2>Header</h2>
<p>
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA<br /><br/>
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
</p>
<a class="btn btn-default"
href='#Url.Action("Index", "Products")'
role="button">
Learn More
</a>
</div>
</div>
</div>
</div>
and the following CSS:
.home-costing {
color: #fff;
text-align: center;
padding: 50px 0;
border-bottom: 1px solid #ff6500;
}
.home-costing h2 {
text-transform: uppercase;
font-size: 60px;
}
.home-costing p {
font-size: 18px;
}
.home-costing .frame {
height: 256px;
width: 256px;
border: 0;
white-space: nowrap;
text-align: center;
margin: 1em 0;
}
.home-costing .helper {
display: inline-block;
height: 100%;
vertical-align: middle;
}
.home-costing img {
vertical-align: middle;
max-height: 256px;
max-width: 256px;
}
The problem is that now the second column is no longer contained and the text does not wrap and goes off to the right.
How can I center align my image in the first column with the text in the right column and still get the correct wrapping in the second column?
Fiddler: https://jsfiddle.net/Camuvingian/1sc40rm2/2/
Your HTML needed updated, in Bootstrap, the div order should ALWAYS go .container > .row > .col- * - *, your code however went .container > .row > .frame > .col- * - *. I have corrected your HTML and now your code works.
HTML:
<div class="home-costing container">
<div class="row">
<div class="col-md-6">
<img src="http://www.nijmegenindialoog.nl/wp-content/uploads/in.ico" height="256" width="256" class="center-block" />
</div>
<div class="col-md-6">
<h2>UserCost</h2>
<p>Hello, I'm a paragraph</p>
<a class="btn btn-default"
href='#Url.Action("Index", "Products")'
role="button">
Learn More
</a>
</div>
</div>
</div>
</div>
Link to finished code example:
Codepen - Updated & working code
This fixes the word wrap issue also on the p tag.
CSS:
p {
font-size: 18px;
word-wrap: break-word;
}

Text align left in a centered div with input fields

I have input fields which are supposed to be shown centered and then the texts to these input fields are supposed to be aligned left and "start" with the input fields.
http://jsfiddle.net/tfbatp5v/2/
.inputdes {
color: #9b9b9a;
font-size:20px;
height: 200px;
}
.blue {
height: 70px;
}
<div align="center" id="parent">
<div class="welcome">Welcome</div>
<div class="inputdes">
<div class="blue">text1<br><input id="inputfield1" /></div>
<div class="blue">text2<br><input id="inputfield2" /></div>
<div class="blue">text3<br><input id="inputfield3" /></div>
</div>
</div>
However, no matter what I do, every time when I use text-align: left; it automatically aligns the inputfields left as well. I tried to group the text areas together with class names but it doesn't work. Does anyone know the answer?
Thanks !
It's recommended to not use align="center", because align attribute is deprecated. You should use the CSS text-align property on the container.
The rule display: table; will make the element to "shrink-to-fit" the content inside, without need to specify the width value.
#parent {
display: table;
margin: 0 auto;
}
.welcome {
text-align: center;
}
.inputdes {
color: #9b9b9a;
font-size: 20px;
height: 200px;
}
.blue {
height: 70px;
}
<div id="parent">
<div class="welcome">Welcome</div>
<div class="inputdes">
<div class="blue">text1<br><input id="inputfield1" /></div>
<div class="blue">text2<br><input id="inputfield2" /></div>
<div class="blue">text3<br><input id="inputfield3" /></div>
</div>
</div>
Try something like the following. The idea is that we limit the width of the .inputdes div, then put the text in a nested div that has text-align: left. That way we can have the inputs centered but the text aligned left within its div.
.inputdes{
color: #9b9b9a;
font-size:20px;
height: 200px;
width: 200px;
}
.inputdes > div > div {
text-align: left;
margin: 0 15px;
}
.blue{
height: 70px;
}
<div align="center" id="parent">
<div class="welcome">Welcome</div>
<br>
<div class="inputdes">
<div class="blue" ><div>text1</div>
<input id="inputfield1"/></div>
<div class="blue" ><div>text2</div>
<input id="inputfield2" /></div>
<div class="blue" ><div>text3</div>
<input id="inputfield3" /></div>
</div>
</div>
You could give the input around the fields a fixed width and give the inputs a width: 100% to use text-align: left.
.inputdes{
color: #9b9b9a;
font-size:20px;
height: 200px;
width: 200px;
text-align: left;
}
input {
width: 100%
}
.blue{
height: 70px;
}
<div align="center" id="parent">
<div class="welcome">Welcome</div>
<div class="inputdes">
<div class="blue" >text1<br>
<input id="inputfield1"/></div>
<div class="blue" >text2<br>
<input id="inputfield2" /></div>
<div class="blue" >text3<br>
<input id="inputfield3" /></div>
</div>
</div>
Here's the updated Fiddle:
https://jsfiddle.net/tfbatp5v/11/