Align Img Vertically within Div HTML and CSS - html

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;
}

Related

Display: table-cell; adding extra space on left of text

I am fairly new to html and css have been kind of struggling to get my text aligned vertically next to an image using css and html.
I came across a nice solution using "display: table-cell;" in the class and "display: table;" in it's container class.
This seemed to work until I came across a case where my text didn't spill over to a second line. For whatever reason, it seems my text which the shorter sentence is aligned to the center of the space between the image and the next column.
Html:
<h3 class="red-header">
Buttons Header
</h3>
<div class="row">
<div class="column divLink">
<img class="icon-img" src="http://www.immersion-3d.com/wp-content/uploads/2015/12/image-placeholder-500x500.jpg" />
<p class="icon-text">
<span class="red-text">Button Text pt 1</span>
<br /> short line extra space on the left</p>
<a class="feature" href="google.ca"></a>
</div>
<div class="column divLink">
<img class="icon-img" src="http://www.immersion-3d.com/wp-content/uploads/2015/12/image-placeholder-500x500.jpg" />
<p class="icon-text">
<span class="red-text">Button Text pt 2</span>
<br /> short setence describing button could possibly extend for a line or two or three. Would like this to be centered</p>
<a class="feature" href="google.ca"></a>
</div>
</div>
<div class="row">
<div class="column divLink">
<img class="icon-img" src="http://www.immersion-3d.com/wp-content/uploads/2015/12/image-placeholder-500x500.jpg" />
<p class="icon-text">
<span class="red-text">Button Text pt 3</span>
<br /> short setence describing button could possibly extend for a line or two or three. Would like this to be centered</p>
<a class="feature" href="google.ca"></a>
</div>
<div class="column divLink">
<img class="icon-img" src="http://www.immersion-3d.com/wp-content/uploads/2015/12/image-placeholder-500x500.jpg" />
<p class="icon-text">
<span class="red-text">Button Text pt 4</span>
<br /> short setence describing button could possibly extend for a line or two or three. Would like this to be centered </p>
<a class="feature" href="google.ca"></a>
</div>
</div>
CSS:
.red-header {
background-color: #be1e2d;
color: #FFF;
font-size: 1rem;
font-family: Lato, Helvetica, Arial, sans-serif;
padding: .6rem;
text-align: center;
}
.row {
width: 100%;
}
.icon-text {
display: table-cell;
height: 100%;
vertical-align: middle;
}
.column {
width: 50%;
display: table;
position: relative;
float: left;
}
.red-text {
color: #be1e2d;
margin-bottom: 0px;
margin-top: 10px;
line-height: 1;
}
.icon-img {
float: left;
width: 115px;
padding: 5px 16px 5px 0px;
}
.feature {
position: relative;
}
/* used to make entire div into a clickable element */
.feature {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
text-decoration: none;
/* No underlines on the link */
z-index: 10;
/* Places the link above everything else in the div */
background-color: #FFF;
/* Fix to make div clickable in IE */
opacity: 0;
/* Fix to make div clickable in IE */
filter: alpha(opacity=1);
/* Fix to make div clickable in IE */
}
/* visualization of the button using a hover */
.divLink:hover {
background-color: #b1e3e6;
}
My apologies for a terrible explanation, please see the following fiddle for clarity. Issue is in the "Button Text pt 1"
You can fix the spacing issue you are having with your current code by adding the following width: 100% to the .icon-text class:
.icon-text {
display: table-cell;
width: 100%;
height: 100%;
vertical-align: middle;
}
Of course I still recommend you go with flexbox instead.
Is this closer to what you were looking for? display: table should only be used for tables, which this isn't. You want Flexbox!
https://jsfiddle.net/o092e0y9/
Here's the solution with what you have now
https://jsfiddle.net/4Lp507cw/5/
HTML
<h3 class="red-header">
Buttons Header
</h3>
<div class="table">
<div class="row">
<div class="column divLink">
<img class="icon-img" src="http://www.immersion-3d.com/wp-content/uploads/2015/12/image-placeholder-500x500.jpg" />
<p class="icon-text">
<span class="red-text">Button Text pt 1</span>
<br /> short line extra space on the left</p>
<a class="feature" href="google.ca"></a>
</div>
<div class="column divLink">
<img class="icon-img" src="http://www.immersion-3d.com/wp-content/uploads/2015/12/image-placeholder-500x500.jpg" />
<p class="icon-text">
<span class="red-text">Button Text pt 2</span>
<br /> short setence describing button could possibly extend for a line or two or three. Would like this to be centered</p>
<a class="feature" href="google.ca"></a>
</div>
</div>
<div class="row">
<div class="column divLink">
<img class="icon-img" src="http://www.immersion-3d.com/wp-content/uploads/2015/12/image-placeholder-500x500.jpg" />
<p class="icon-text">
<span class="red-text">Button Text pt 3</span>
<br /> short setence describing button could possibly extend for a line or two or three. Would like this to be centered</p>
<a class="feature" href="google.ca"></a>
</div>
<div class="column divLink">
<img class="icon-img" src="http://www.immersion-3d.com/wp-content/uploads/2015/12/image-placeholder-500x500.jpg" />
<p class="icon-text">
<span class="red-text">Button Text pt 4</span>
<br /> short setence describing button could possibly extend for a line or two or three. Would like this to be centered </p>
<a class="feature" href="google.ca"></a>
</div>
</div>
</div>
CSS
.red-header {
background-color: #be1e2d;
color: #FFF;
font-size: 1rem;
font-family: Lato, Helvetica, Arial, sans-serif;
padding: .6rem;
text-align: center;
}
.table{width:100%; display:table;}
.row {
width: 100%;
display:table-row;
}
.icon-text {
height: 100%;
vertical-align: middle;
float:left;
width:70%;
}
.column {
width: 50%;
display: table-cell;
position: relative;
}
.red-text {
color: #be1e2d;
margin-bottom: 0px;
margin-top: 10px;
line-height: 1;
}
.icon-img {
float: left;
width: 25%;
padding: 5px 16px 5px 0px;
}
.feature {
position: relative;
}
.feature {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
text-decoration: none;
/* No underlines on the link */
z-index: 10;
/* Places the link above everything else in the div */
background-color: #FFF;
/* Fix to make div clickable in IE */
opacity: 0;
/* Fix to make div clickable in IE */
filter: alpha(opacity=1);
/* Fix to make div clickable in IE */
}
.divLink:hover {
background-color: #b1e3e6;
}

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>

How to align three background images side by side

I'm trying to align three background images side by side, ideally with fluidity so that they re-position when my browser window resizes.
I've tried searching for an answer to this problem and thought using CSS properties suited to aligning regular 'img src' elements would work, however they haven't.
Essentially, I have a page with a gallery. Each image has a city name in it's center. Through research, I've decided to assign a background-image property to three separate divs and used the line-height property matching the height of each image so that the city name aligns itself in the center. The background-image technique assists in the alignment of the city name.
Where am I going wrong?
#jumbotron2 {
height: 500px;
width: 100%;
}
#city-container {
width: 100%;
height: 100%;
}
.london-square {
height: 400px;
width: 400px;
background-image: url("tombnb-images/london-400px.jpg")
}
.newyork-square {
height: 400px;
width: 400px;
background-image: url("tombnb-images/newyork-400px.jpg")
}
.sydney-square {
height: 400px;
width: 400px;
background-image: url("tombnb-images/sydney-400px.jpg")
}
.square p {
font-family: 'Slabo 27px', serif;
font-size: 32px;
color: #FFFFFF;
line-height: 400px;
text-align: center;
text-shadow: 0px 0px 10px black;
}
<div id="jumbotron2">
<div id="city-container">
<div class="london-square square">
<p id="text">London</p>
</div>
<div class="newyork-square square">
<p id="text">New York</p>
</div>
<div class="sydney-square square">
<p id="text">Sydney</p>
</div>
</div>
</div>
if you use a percentage width of your divs you have to float them too.
I recommand using this:
#city-container {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
flex-warp: wrap;
}
You can use bootstrap. you put your images inside divs.
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row">
<div class="col-sm-4">
<img class="img-thumbnail" src="http://www.nature.org/cs/groups/webcontent/#web/#giftplanning/documents/media/sample-cga-rates-splash-1.jpg">
</div>
<div class="col-sm-4">
<img class="img-thumbnail" src="http://sharedforfree.com/wp-content/uploads/2013/07/cropped-harrimanToday.jpg">
</div>
<div class="col-sm-4">
<img class="img-thumbnail" src="http://pre02.deviantart.net/f34e/th/pre/f/2015/182/4/f/croatia_nature_pack___sample__2___proref_org_by_proref-d8zgrc2.jpg">
</div>
</div>
Check out this fiddle:
jsfiddle example

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/

Center Align Multiple DIV under single main DIV causing overlapping issue

I want to print multiple DIV one after another in single line. Also i need to center the content to the page.
I am able do this with float style but its not Centered to the page. I searched google , it says use display: inline-block; instead of float.
Here is what i tried, but 2 divs are overlapping.
<div style="text-align:center">
<div style="display: inline-block;">
<a style="cursor:pointer;text-decoration:none;color:#000">
<div style="display: inline-block;cursor:pointer;position:absolute;margin-left:0px;margin-top:-1px;margin-right:0px;">
<img src="images/Calendar_blank.png" width="75" height="75">
</div>
<div style="display: inline-block;position:absolute;margin-left:0px;margin-top:-1px;margin-right:0px;width:75px; font-family:Verdana, Geneva, sans-serif; font-size:16px;color:#030">Aug</div>
<div id="date1" style="display: inline-block;position:absolute; margin-left:0px;margin-top:27px;margin-right:0px;width:75px; "><strong>25</strong></div>
</a>
</div>
<div style="display: inline-block;">
<a style="cursor:pointer;text-decoration:none;color:#000">
<div style="display: inline-block;cursor:pointer;position:absolute;margin-left:0px;margin-top:-1px;margin-right:0px;width:75px; ">
<img src="images/Calendar_blank.png" width="75" height="75">
</div>
<div style="display: inline-block;position:absolute;margin-left:0px;margin-top:-1px;margin-right:0px;width:75px; font-family:Verdana, Geneva, sans-serif; font-size:16px;color:#030">Aug</div>
<div id="date2" style="display: inline-block;position:absolute; margin-left:0px;margin-top:27px;margin-right:0px;width:75px; "><strong>25</strong></div>
</a>
</div>
</div>
Out Put is :
Expected is : The Content should be centered to the page & it should not overlap.
Note: I dont want to add hardcoded MARGIN to the style. as it will effective while zooming in and out & it will not be page centered.
Your code is too mired with inline styles for me to debug easily. Below is how you would rebuild your desired layout most basically from scratch.
The key component is adding display: inline-block to the light-gray calendar divs to allow them to sit side-by-side: https://developer.mozilla.org/en-US/docs/Web/CSS/display#Values
Also, setting those divs to position: relative and their child divs to position: absolute allows you to ensure the contained elements' origins are within those parent divs.
Inline styles (styles specified within your HTML) should be kept to an absolute minimum. HTML (markup) is for content primarily. Styles go in your CSS, and behaviors generally belong in JavaScript. This makes your code much easier to read and update (and debug).
A separate CSS file can be linked from your HTML, or it can included in your HTML if wrapped in <style> and </style> tags.
DEMO: http://jsbin.com/lifij/1/
HTML:
<body>
<div id="container">
<div class="calendar">
<div class="stripe">
<h3>Aug</h3>
</div>
<div class="number">
<h1>25</h1>
</div>
</div>
<div class="calendar">
<div class="stripe">
<h3>Aug</h3>
</div>
<div class="number">
<h1>25</h1>
</div>
</div>
</div>
</body>
CSS:
h1, h3 {
font-family: sans-serif;
text-align: center;
margin: 0;
}
h3 {
font-weight: normal;
}
.calendar {
background-color: lightgray;
width: 80px;
height: 80px;
border-radius: 6px;
display: inline-block;
position: relative;
}
.stripe {
background-color: red;
width: 100%;
height: 24px;
border-radius: 6px 6px 0 0;
position: absolute;
top: 0;
}
.number {
position: absolute;
top: 30px;
width: 100%;
}