Vertically centre child in floated parent - html

Question:
Is it possible to vertically centre a child element in a floated div in relation to the dynamic height of a floated div along side it?
Desired result
Background:
I'm using a WordPress theme which has shortcodes for creating columns. The columns are created through floated divs. As such, I'd like to stick to using float, instead of using a table layout as has been suggested to similar questions (Vertically center content of floating div).
I'd like the result to be responsive. The image height changes with the screen width, and so I cannot set explicit height or margins on the p element.
The text in p is also more than one line, so I cannot set line-height as a solution (Vertically centre a div).
Example:
http://codepen.io/besiix/pen/rxdOWM
.wrapper {
background-color: #50C5B7;
padding: 5px;
}
.image {
width: 100%;
}
.clear {
clear: both;
}
.one-half {
float: left;
width:48%;
margin-right: 4%;
background-color: lightgrey;
position: relative;
display: inline;
box-sizing: border-box;
}
.last {
clear: right;
margin-right: 0;
}
<section class="wrapper">
<div class="one-half">
<p class="v-center"> This wants to be centered vertically in relation to the image on the right.</p>
</div>
<div class="one-half last">
<img class="image" src="http://www.premiercottages.co.uk/images/regions/Scotland.jpg">
</div>
<div class="clear"></div>
</section>

You could use flexbox, see jsfiddle https://jsfiddle.net/d5Lptwut/2/
<section class="wrapper">
<div class="one-half vertical-align">
<p> This wants to be centered vertically in relation to the image on the right.</p>
</div>
<div class="one-half last">
<img class="image" src="http://www.premiercottages.co.uk/images/regions/Scotland.jpg">
</div>
</section>
.wrapper {
background-color: #50C5B7;
padding: 5px;
display: flex;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
}
.image {
width: 100%;
height: auto;
}
.one-half {
width:48%;
margin-right: 4%;
background-color: lightgrey;
display: inline-block;
box-sizing: border-box;
}
.vertical-align {
flex-item-align: center;
-webkit-align-self: center;
-ms-flex-item-align: center;
align-self: center;
}
.last {
margin-right: 0;
}

you can try this one:
.wrapper {
background-color: #50C5B7;
padding: 5px;
display: flex;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
}
.image {
width: 100%;
height: auto;
}
.one-half {
width:48%;
margin-right: 4%;
background-color: lightgrey;
display: inline-block;
box-sizing: border-box;
}
.vertical-align {
flex-item-align: center;
-webkit-align-self: center;
-ms-flex-item-align: center;
align-self: center;
}
.last {
margin-right: 0;
clear:both;
}
DEMO HERE

you can use display: inline-block, this allow vertical-alignelements:
http://codepen.io/yukulele/pen/bEvYEZ?editors=1100

Related

Centering a div to the viewport with other divs on both sides

I'm currently designing a header bar for a site I'm working on. I would like the header to show a logo at the left, the title in the center, and the user account on the right.
This mockup is what I'm envisioning. Note that the dotted boxes denote a div.
I've made some progress on creating it in HTML/CSS, but I can't seem to get the title to center to the viewport.
As you can see, the title is centering itself between the logo and the account info divs, instead of centering itself on the viewport. This ends up making the page just look a little bit off, and so I would really like to center the title to the viewport.
Here's my code:
.headerBarArea {
background-color: #7785A2;
border: 0;
padding: 0;
width: 100%;
display: inline-block;
text-align: center;
}
.logoArea {
display: inline;
text-align: center;
float: left;
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.minesLogo {
width: 96px;
}
.titleArea {
display: inline-block;
text-align: center;
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.siteTitle {
color: white;
}
.pageTitle {
color: white;
}
.userAccountArea {
display: inline;
text-align: center;
float: right;
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.userAccountIcon {
float: left;
width: 35px;
}
.userAccountText {
float: right;
}
<div className="headerBarArea">
<div className="logoArea">
<img src="assets/mines_logo_stacked.png" className="minesLogo" />
</div>
<div className="titleArea">
<h2 className="siteTitle">This is my site Title</h2>
<h3 className="pageTitle">Page Title</h3>
</div>
<div className="userAccountArea">
<img src="assets/user_account.png" className="userAccountIcon" />
<span className="UserAccountText">John Smith (Student)</span>
</div>
</div>
Any ideas on what I could do to make the title div centered to the viewport, instead of centering between the two divs?
html code
<div class="flex-container">
<div class="flex-item">1</div>
<div class="align-self-center">
<span class="siteTitle">This is my site Title</span>
<br>
<div class="text-center ">Page Title</div>
</div>
<div class="flex-item align-self-end third-item">3</div>
</div>
CSS code
.flex-container {
/* We first create a flex layout context */
display: flex;
/* Then we define the flow direction
and if we allow the items to wrap
* Remember this is the same as:
* flex-direction: row;
* flex-wrap: wrap;
*/
flex-flow: row wrap;
/* Then we define how is distributed the remaining space */
justify-content: space-between;
}
.flex-item {
background: tomato;
padding: 5px;
width: 200px;
height: 150px;
margin-top: 10px;
line-height: 150px;
color: white;
font-weight: bold;
text-align: center;
}
.third-item {
height: 100px;
}
.text-center{
text-align: center;
}
.align-self-end{
align-self:end;
}
.align-self-center{
align-self:center;
}
code output
code solution
used flex to place the items used .flex-container as parent div where flex items are placed in .justify-content: space-between; is used to place space in between the items. align-self:center; is used to place Page Title at center
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
codepen
.headerBarArea{
display:flex;
justify-content: space-between;
align-items: center
}
It is an easy way to the layout.
you can try it.
Try adding margin-left:auto; and margin-right:auto; to the .titleArea class .
I would suggest using a flex-box though.
Replace your css code with this to make title div centered in all the viewport.
.headerBarArea {
background-color: #7785A2;
border: 0;
padding: 0;
width: 100%;
display:flex;
justify-content: space-between;
}
.logoArea , .titleArea , .userAccountArea {
border: lawngreen;
border-style: dashed;
border-width: 1px;
}
.minesLogo {
width: 96px;
}
.titleArea {
text-align: center;
}
.siteTitle , .pageTitle{
color: white;
}
.userAccountIcon {
float: left;
width: 35px;
}
.userAccountText {
float: right;
}

Why won't my images align horizontally on css

Hi I have 6 images that I have in my website that are appearing vertically stacked but I want them to be horizontally stacked and center aligned. 4 of the images have captions too that I need to appear right below each image... can someone please help thanks so much
Here is my html:
<div class="container-fluid">
<div class="container-of-images">
<img src="chick2.png">
<figure>
<img id="img1" src="dice6.png">
<figcaption>Testing</figcaption>
</figure>
<figure>
<img id="img2" src="dice6.png">
<figcaption>Testing</figcaption>
</figure>
<figure>
<img id="img3" class="threeChoices" src="dice6.png">
<figcaption>Testing</figcaption>
</figure>
<figure>
<img id="img4" class="fourChoices" src="dice6.png">
<figcaption>Testing</figcaption>
</figure>
<img src="chick1.png">
</div>
</div>
And here is my CSS:
body {
background-color: #b6e6bd;
line-height: 1.6;
}
.container-fluid {
padding: 1% 20%;
margin: auto;
text-align: center;
}
img {
width: 100px;
line-height: 0;
margin: 0 1%;
display: flex;
justify-content: center;
}
figcaption {
text-align: center;
width: 100px;
}
Try this CSS code, the problem I found was that display: inline-block should have been used instead of flex.
body {
background-color: #b6e6bd;
line-height: 1.6;
}
.container-fluid {
margin: auto;
text-align: center;
width:100%;
}
img {
width: 100px;
line-height: 0;
margin: 0 1%;
display: inline-block;
justify-content: center;
vertical-align: middle;
}
figcaption {
text-align:
center;
width: 100px;
}
figure {
display: inline-block;vertical-align: middle;
}
Make your image container a flex element that lays out the content in a row:
.container-of-images {
display: flex;
flex-flow: row nowrap;
justify-content: center;
align-items: center;
}
You need to add dispay: inline; or dispay: inline-block; to your css.
body {
background-color: #b6e6bd;
line-height: 1.6;
}
.container-fluid {
padding: 1% 20%;
margin: auto;
text-align: center;
}
img {
width: 100px;
line-height: 0;
margin: 0 1%;
display: flex;
justify-content: center;
display: inline;
}
figcaption {
text-align: center;
width: 100px;
}
Did you try to use flexbox on the container? Try this:
.container-of-images {
display: flex;
}
Based on your if you add the CSS property display:flex to container-of-images class it will solve your problem. But you solve be aware of the responsive design for it.
.container-of-images {
display: flex;
flex-direction: row; /* extra line flex alone can solve your problem */
justify-content: space-around; /* extra line to space images around */
}

Aligning divs in relation to other divs

I'm trying to replicate something like this: .
The red div (the container) is centered horizontaly and hasn't a fixed width. The "title" div inside it should set the width of the container by being the widest div inside it, and the "welcome to" and "subtitle" divs are attached to the "title" div and the left/right of the container.
I'm struggling to understand how I can put my "welcome to" div to the left, before having set the "title" div which gives the width of the container.
For the moment, I have this code:
.container {
text-align: center;
}
.welcome_to{
text-align: left;
}
.subtitle{
text-align:right;
}
.title{
}
It centers the title div, but put the other divs at the edges of the screen, and not of the container.
This should give you a start
.container {
border: 3px solid red;
display: flex;
flex-direction: column;
font-size: 2em;
}
.left,
.center,
.right {
border: 3px solid blue;
}
.full-width {
width: 100%;
}
.left {
display: flex;
align-self: flex-start;
}
.center {
display: flex;
align-self: center;
justify-content: center;
}
.right {
display: flex;
align-self: flex-end;
}
<div class="container">
<div class="left">Left</div>
<div class="center full-width">Center and full width</div>
<div class="right">right</div>
</div>
text-align aligns text, not block elements. The best way here is to use flexbox:
/* This is the actual code */
.container {
display: flex;
flex-direction: column;
}
.welcome-to {
align-self: flex-start;
width: 50%;
}
.title {
text-align: center;
}
.subtitle {
align-self: flex-end;
width: 50%;
text-align: right;
}
/* Decoration only */
.container {
border: 3px solid red;
}
.container > * {
box-sizing: border-box;
padding: 1rem;
border: 3px solid blue;
}
<div class="container">
<div class="welcome-to">Welcome to</div>
<div class="title">Title</div>
<div class="subtitle">Subtitle</div>
</div>

How can i center image in any div in HTML & CSS?

How can i center image in any div in HTML & CSS ?
I have a nav bar that uses images as links for other pages, the nav bar is multiple divs each has an image and anther div contains a words, how can I center this img in the div
The CSS:
#Nav
{
height: 150px;
margin-top: 50px;
}
#Nav ul
{
list-style-type: none;
padding: 50px;
padding-top: 0px;
width: 100%;
}
#Nav li
{
float: left;
font-size: 12px;
font-weight: bold;
letter-spacing: 0;
margin: 30px;
}
#Nav a
{
display: inline-block;
padding: 5px;
width: 70px;
height: 100px;
color: black;
text-decoration: none;
}
#Nav a:hover
{
border: 2px solid #ddd;
border-radius: 4px;
box-shadow: 0 0 2px 1px rgba (0, 140, 186, 0.5);
}
#img img
{
align-self: middle;
width: 50px;
height: 50px;
}
#desc
{
margin-top: 15px;
text-align: center;
}
The html:
<li> <a target="_blank" href="#">
<div id="img"> <img src="C:/Users/hp1/Desktop/Website/Pictures/Accessories.jpg" alt="Accessories">
<div id="desc"> Accessories </div>
</div>
</a> </li>
You need to change your CSS for image like below:-
#img img
{
display: block;
margin: 0 auto;
}
You can see Many Demos Here-
Note that align-self is for elements inside a flexbox container which is not your case, you can try with text-align: center; on img.
Or if you wish to go full flexbox, set the img container to:
.containerClass {
display: flex;
flex-flow: column wrap;
justify-content: center;
}
With this setup align-self on the img is not need since justify-content on it's container will do.
Flexbox to the rescue:
.parent {
width:200px; height:200px;
border:2px solid #000;
display: flex; /* Make it flex */
flex-direction: column; /* wrap children elements to columns */
align-items: center; /* Center children horizontally */
justify-content: center; /* Center children vertically */
}
<div class="parent ">
<img src="http://placehold.it/100x100/cf5">
<p>Green-ish</p>
</div>
<div class="parent ">
<img src="http://placehold.it/100x100/5fc">
<p>Blue-ish</p>
</div>
align-self: center; is valid, align-self: middle; is not valid.
However, you can probably achieve what you're wanting with text-align: center;. Despite it being an image, you can still center using text-align as images are, by default, inline elements just like text.
#img img
{
align-self: center;
width: 50px;
height: 50px;
}
OR
#img img
{
text-align: center;
width: 50px;
height: 50px;
}
First you have to change the nature of div to table then make its align to center like this
#img img
{
display:table;
align: center;
width: 50px;
height: 50px;
}

CSS to vertically align text to middle of div

What is the correct way to force text in a div to vertically align to the middle?
I have found a couple 'tricks', but I need something that works with a single line and multiple lines. Hopefully I am missing something stupid simple:
http://jsfiddle.net/rogerguess/DawFy/
.header {
display: inline-block;
text-align: center;
width: 123px;
background: green;
border-radius: 4px 4px 4px 4px;
height: 80px;
vertical-align: middle;
}
<div >
<div class="header">test1 </div>
<div class="header">some text that will wrap</div>
<div class="header">test3</div>
</div>
Change display: inline-block; to display: table-cell;
If your multi-line elements need to wrap for responsive reasons, then your best bet is with Flexbox. Due to the flakiness of Firefox's 2009 Flexbox implementation, it has to be handled slightly differently than you would do it for modern Flexbox implementations.
http://codepen.io/cimmanon/pen/mxuFa
<ul>
<li>One lorem ipsum dolor sit amet</li><!--
--><li>Two</li><!--
--><li>Three</li><!--
--><li>Four</li><!--
--><li>Five</li>
</ul>
li {
text-align: center;
vertical-align: middle;
/* fallback for non-Flexbox browsers */
display: inline-block;
/* Flexbox browsers */
display: -webkit-inline-box;
display: -moz-inline-box;
display: -ms-inline-flexbox;
display: -webkit-inline-flex;
display: inline-flex;
/* vertical centering for legacy, horizontal centering for modern */
-webkit-box-pack: center;
-moz-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
/* modern Flexbox only */
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
/* legacy Flexbox only */
-webkit-box-orient: vertical;
-moz-box-orient: vertical;
}
I've made a quick example for you, using display: table-cell property on the parent div.
CSS:
.outer {
width: 500px;
height: 500px;
display: table-cell;
background-color: red;
vertical-align: middle;
}
.inner {
display: block;
width: 300px;
height: 200px;
margin: 0px auto;
background-color: yellow;
}
HTML:
<div class="outer">
<div class="inner">
</div>
</div>
http://jsfiddle.net/aKT42/
Try this, it will work.
<div class="greenBorder" id="outer">
<div id="middle">
<div class="greenBorder" id="inner">
Your Text Here
</div>
</div>
</div>
Css Class
#outer {
height: 100%;
overflow: hidden;
position: relative;
width: 100%;
display: table;
position: static;
}
div.greenBorder {
background-color: #39B9FF;
text-align: center;
color: white;
}
#middle[id] {
display: table-cell;
position: static;
vertical-align: middle;
width: 100%;
}
#middle {
position: absolute;
top: 50%;
}
http://jsfiddle.net/DawFy/16/
.header {
display: block;
text-align: center;
width: 123px;
background: green;
border-radius: 4px 4px 4px 4px;
height: 80px;
margin:0px auto;
}
li
{
list-style-type:none;
}
<div >
<li>
<div class="header">test1 </div>
<li>
<div class="header">some text that will wrap</div>
<li>
<div class="header">test3</div>
Try this dont know if its "correct" but works
Most of these solutions wouldn't work for me because either the height was manually specified (which I couldn't do) or things like inline-block and float would remove the ability for the an inner div to inherit the parent div's height. I ended up creating a table with two columns each containing my divs and then I had no issues vertically centering text.