I'd like to position all my text vertically centered within the div.
Currently 'Chat to us online now' is at the top of the div.
How do I achieve this using my code?
Fiddle: http://jsfiddle.net/jL5bpmp1/
* {
margin:0
}
.box {
-webkit-border-radius: 20px;
-moz-border-radius: 20px;
border-radius: 20px;
background-color:#0f89cf;
height:55px;
width:100%;
padding:5px 0 0 0;
margin-bottom:30px
}
.box img, .box div {
float:left;
margin-left:10px
}
.box h1 {
color:#fff;
font-size:18px
}
.box h1 span {
display:block;
font-size:23px
}
<div class="is-mobile">
<div class="box">
<img src="http://placehold.it/50x50">
<div>
<h1>Chat to us online now</h1>
</div>
</div>
<div class="box">
<img src="http://placehold.it/50x50">
<div>
<h1>Call the Helpline
<span>101 2333 9302</span></h1>
</div>
</div>
<div class="box">
<img src="http://placehold.it/50x50">
<div>
<h1>Make a General Enquiry
<span>101 2333 9303</span></h1>
</div>
</div>
</div>
I suggest to use display:inline-block instead of float:left, then you can set up the vertical align value easily.
.box img, .box div {
display:inline-block;
vertical-align:middle;
}
jsfiddle
You can use this common vertical centering method:
.box div {
position: relative;
top: 50%;
transform: translateY(-50%);
}
* {
margin:0
}
.box {
-webkit-border-radius: 20px;
-moz-border-radius: 20px;
border-radius: 20px;
background-color:#0f89cf;
height:55px;
width:100%;
padding:5px 0 0 0;
margin-bottom:30px
}
.box img, .box div {
float:left;
margin-left:10px
}
.box h1 {
color:#fff;
font-size:18px
}
.box h1 span {
display:block;
font-size:23px
}
.box div {
position: relative;
top: 50%;
transform: translateY(-50%);
}
<div class="is-mobile">
<div class="box">
<img src="http://placehold.it/50x50">
<div>
<h1>Chat to us online now</h1>
</div>
</div>
<div class="box">
<img src="http://placehold.it/50x50">
<div>
<h1>Call the Helpline
<span>101 2333 9302</span></h1>
</div>
</div>
<div class="box">
<img src="http://placehold.it/50x50">
<div>
<h1>Make a General Enquiry
<span>101 2333 9303</span></h1>
</div>
</div>
</div>
If your browser support is IE10+ you can use flex-box. You just need to add 2 lines:
.box {
display: flex;
align-items: center;
padding: 0;
}
I also added padding: 0; as you don't need that now you're using flex-box. Welcome to the future. The best place to learn about flexbox is here:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Related
This is my sample html code:
.questions-table-main {
border-right: 3px solid rgb(242,244,247);
width:189px;
float:left;
padding-right:30px;
padding-top:10px;
display:table;
height: 200px;
}
.questions-table-main-category {
/* padding-top: 20px; */
text-align : center;
}
.questions-table-main-firm {
height:85px;
background-color:rgb(242,244,247);
text-align: center;
display: table-row;
vertical-align: bottom;
}
<div class="questions-table-main">
<div class="questions-table-main-category">
<img src="img/communication.png" alt="communication"/>
</div>
<div class="questions-table-main-firm">
<div class="questions-table-main-firm-image">
<span class="questions-table-firm-helper">
<img src="img/allianz.png" alt="allianz" /></span>
</div>
</div>
</div>
Let me clear this mess. I need to put <div class="questions-table-main-firm"> on the bottom of <div class="questions-table-main">. Next thing I need to do is that <div class="questions-table-main-category"> must be above <div class="questions-table-main-firm"> with 27px space between them. I was trying to do that with padding-top, but it's impossible because i have some different images with different dimensions. Now <div class="questions-table-main-category"> is on the top of parrent div. How to fix that ?
You can do it by using position: absolute; and top. For bottom div use top: 0 so that it says at top and for first div use top: 112px including height(85) and margin 27.
.questions-table-main {
border-right: 3px solid rgb(242,244,247);
width:189px;
padding-right:30px;
padding-top:10px;
display:block;
height: 200px;
position: relative;
}
.questions-table-main-category {
text-align : center;
height:85px;
top: 112px; /* height 85 + 27 margin*/
position: absolute;
}
.questions-table-main-firm {
height:85px;
background-color:rgb(242,244,247);
text-align: center;
top: 0px;
position: absolute;
}
<div class="questions-table-main-category">
<img src="https://placehold.it/189x85" alt="communication"/>
</div>
<div class="questions-table-main-firm">
<div class="questions-table-main-firm-image">
<span class="questions-table-firm-helper">
<img src="https://placehold.it/189x85" alt="allianz" /></span>
</div>
</div>
</div>
You need to remove the display table atributes, do it as follows:
.questions-table-main {
width:189px;
float:left;
padding-right:30px;
padding-top:10px;
height: 200px;
}
.questions-table-main-category {
/* padding-top: 20px; */
text-align : center;
margin-bottom: 27px;
}
.questions-table-main-firm {
background-color:rgb(242,244,247);
text-align: center;
vertical-align: bottom;
}
<div class="questions-table-main">
<div class="questions-table-main-category">
<img src="http://placehold.it/350x150" alt="communication"/>
</div>
<div class="questions-table-main-firm">
<div class="questions-table-main-firm-image">
<span class="questions-table-firm-helper">
<img src="http://placehold.it/350x150" alt="allianz" /></span>
</div>
</div>
</div>
So I'm making a pinterest-style page where there are four divs per row and there can be an unlimited number of rows. The problem that I've ran into is that if the height of the divs are not all the same, the divs will go all over the place.
This is what I mean:
What Should happen:
What Actually happens:
My code (I need to keep it simple and can't do four larger divs for each column because of some php stuff that I've added):
<div id="newsList">
<div>
<img src="#">
<p>Text.</p>
</div>
<div>
<img src="#">
<p>Text.</p>
</div>
<div>
<img src="#">
<p>Text.</p>
</div>
<div>
<img src="#">
<p>Text.</p>
</div>
<div>
<img src="#">
<p>Text.</p>
</div>
<div>
<img src="#">
<p>Text.</p>
</div>
<div>
<img src="#">
<p>Text.</p>
</div>
<div>
<img src="#">
<p>Text.</p>
</div>
</div>
CSS:
#newsList {
width: 1330px;
margin: 0 auto 0 auto;
display: table;
height: auto;
overflow: auto;
}
#newsList > div {
width: 313px;
height: auto;
float: left;
cursor: pointer;
margin-left: 15px;
position: relative;
overflow: auto;
border-radius: 2px;
background-color: white;
margin-bottom: 16px;
display: inline-block;
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.22);
transition: all 0.3s ease-in-out;
}
Place the an outside container div that contains the divs with the content div that you would like to place inside them that way they can all be different lengths that way that the website pinterest does it.
.container {
width:150px;
float:left;
}
.Blue {
width:140px;
padding:2px;
margin:5px;
background-color:blue;
}
.Green {
width:140px;
padding:2px;
margin:5px;
background-color:green;
}
Here is a fiddle link: http://jsfiddle.net/ztdgz24n/1/
I'm having issues with a button not displaying correctly, when the element is floated left or right the button displays fine, however if no float is applied the button doesn't display correctly.
I've prepared the following example to demonstrate:
http://codepen.io/anon/pen/xHvyt
My HTML and CSS are as follows:
<div class="section">
<div class="textbox">
<div class="text">
<h1>Woop, title</h1>
<p>content content content</p>
</div>
<div class="buttons">
<span>Find out more</span>
<div class="clear"></div>
</div>
</div>
</div>
<div class="section">
<div class="textbox">
<div class="text">
<h1>Woop, title</h1>
<p>content content content</p>
</div>
<div class="buttons">
<span>Find out more</span>
<div class="clear"></div>
</div>
</div>
</div>
<div class="section">
<div class="textbox">
<div class="text">
<h1>Woop, title</h1>
<p>content content content</p>
</div>
<div class="buttons">
<span>Find out more</span>
<div class="clear"></div>
</div>
</div>
</div>
CSS:
.section .textbox
{
width:25%;
z-index:2;
padding:2.5%;
text-align: center;
background: #4a8237;
}
.section .textbox h1
{
font-weight: 800;
font-size: 275%;
margin:0;
color:#FFF;
}
.section .textbox .text p
{
margin:10% 0;
color:#FFF;
}
.section .textbox .button
{
width: 45%;
text-align: center;
background: url('http://i.imgur.com/dUYP3sP.png');
border-radius: 5px;
display: block;
color:#FFF;
white-space: nowrap;
}
.section .textbox .button.left
{
float:left;
}
.section .textbox .button.right
{
float:right;
}
.section .textbox .button.middle
{
margin:0 auto;
}
.section .textbox .button span
{
display: block;
min-height: 40px;
padding:10%;
margin-bottom:10%;
background: url('http://i.imgur.com/NvJtuDL.png') no-repeat center bottom;
}
As you can see there are three .section divs. In each the ones where the button is floated left/right it displays properly, however when no float is applied it's almost as is the span is not displaying:block;
Thanks in advance for any help.
Changing from "display:block" to "display:inline-block" seems to fix your problem without concerns about the floats.
.section .textbox .button span
{
display: inline-block;
min-height: 40px;
padding:10%;
margin-bottom:10%;
background: url('http://i.imgur.com/NvJtuDL.png') no-repeat center bottom;
}
I have two image links that need to be centered with a little shifting.
My problem is that one link cause the other to be unclickable.
DEMO - The right one can't be clicked
HTML:
<div class="my_class" id="my_id1">
<a href="URL">
<img src="//placehold.it/200x150" />
</a>
</div>
<div class="my_class" id="my_id2">
<a href="URL2">
<img src="//placehold.it/200x150" />
</a>
</div>
css:
#my_id1
{
left: 120px;
}
#my_id2
{
right: 120px;
top: -157px;
}
.my_class
{
text-align:center;
position:relative;
display: block;
margin-left: auto;
margin-right: auto;
}
.my_class
{
text-align:center;
position:relative;
display: block;
margin: 0px, auto;
}
img{
border:1px solid red;
}
Here is the modified code:
<div class="my_class" id="my_id1"> <a href="URL">
<img src="//placehold.it/200x150" />
</a>
</div>
<div class="my_class" id="my_id2"> <a href="URL2">
<img src="//placehold.it/200x150" />
</a>
</div>
And the CSS:
#my_id1 {
float: left;
left: 150px;
}
#my_id2 {
float:right;
right: 150px;
}
.my_class {
text-align:center;
position:relative;
display: block;
margin-left: auto;
margin-right: auto;
}
.my_class {
text-align:center;
position:relative;
display: block;
margin: 0px, auto;
}
img {
border:1px solid red;
}
You need to float those containers : http://jsfiddle.net/GbzSQ/5/
Your first div overlaps over the other, so you need to float them and then use margins to position them properly.
.my_class{
float:left;
width:200px;
}
The divs of the links are just on top of each other.
So the mouse does not "see the bottom link".
Try using display inline in the divs with defined width.
I would use some floats or display:inline-block 's.
I made an update of your Fiddle with the Floats.
http://jsfiddle.net/cfknoop/GbzSQ/7/
#my_id1 {
float:left;
}
#my_id2 {
float:right;
}
The wrapper needs to be cleared with an clearfix or something.
Try not to use negative positioning, it's bad practice and will cause issues such as this. Try defining the size of the containing divide, position that, then float the divs within this.
I'll put together a quick fiddle to show you.
http://jsfiddle.net/GbzSQ/23/
And here's the HTML:
<div class="container" id="container">
<div class="my_class1" id="my_id1">
<a href="URL">
<img src="//placehold.it/200x150" alt="1" />
</a>
</div>
<div class="my_class2" id="my_id2">
<a href="URL2">
<img src="//placehold.it/200x150" alt="2" />
</a>
</div>
</div>
And the CSS:
.my_class2 {
text-align:center;
float: right;
position:relative;
display: block;
margin: 0 auto;
}
.my_class1 {
text-align:center;
float: left;
position:relative;
display: block;
margin: 0 auto;
margin-right: 20px;
}
img{
border:1px solid red;
}
.container {
width: 440px;
height: 200px;
display: block;
margin: 0 auto;
}
When i do my web design, meet a problem when i need to do
[LEFT LOGO] then [TITLE(CENTER)] then [RIGHT LOGO]
The problem appear at RIGHT LOGO there, it doesn't align with LEFT LOGO.
The right logo is under the line of TITLE(CENTER)
here is my code sample, thanks:
<div class="navigation">
<div id="left">
<a title="Multimedia" href="#">
<img src="images/logo.png"/>
</a>
</div>
<div id="title">Tutor Program</div>
<div id="right">
<a href="#" title="Inspire and Innovate">
<img src="images/tagline_alt.png"/>
</a>
</div>
css:
.navigation{
height:auto;
background-color:#666;
width:85%;
margin:auto;
min-width:800px;
}
#title{
text-align:center;
margin: auto;
font-family:‘Arial Black’, Gadget, sans-serif;
font-size: 20px;
color: #FFF;
}
#left {
float: left;text-align:left;
margin: auto;
}
#right {
float: right;text-align:right;
margin: auto;
}
a img { border: 0; }
simply move the right container above the left container check below
<div class="navigation">
<div id="right">
<a href="#" title="Inspire and Innovate">
<img src="images/tagline_alt.png"/>
</a>
</div>
<div id="left">
<a title="Multimedia" href="#">
<img src="images/logo.png"/>
</a>
</div>
<div id="title">Tutor Program</div>
</div>
I thing its work an another way i have some changes in style width depends of logo width. if it is not set in center then change the width %.
#title{
text-align:center;
margin: auto;
font-family:'Arial Black', Gadget, sans-serif;
font-size: 20px;
color: #FFF;
width:59%;
float:left;
}
.clear {
clear:both;
margin:0px;
font-size:0px;
}
and add a div after the last div with class clear.
<div id="right"> .... </div>
<div class="clear"> </div>
You could also display your divs as a table and table-cells, like this:
HTML
<div class="navigation">
<div id="left">
left
</div>
<div id="title">
title
</div>
<div id="right">
right
</div>
</div>
CSS
.navigation {
width: 100%;
display: table;
table-layout: fixed;
}
.navigation > div {
display: table-cell;
}
.navigation div:nth-child(1) {
background: lightgray;
}
.navigation div:nth-child(2) {
background: gray;
text-align: center;
}
.navigation div:nth-child(3) {
background: lightgray;
text-align: right;
}
Also check the JSFiddle Demo