Vertical centering in CSS is relatively straight forward. This is the code I'm using.
position:relative;
top:50%;
transform:translateY(-50%);
1) This works great for centering multiple shapes next to each other.
2) It works great for centering multiple words next to each other.
However oddly enough when I place a centered shape next to a centered word it goes haywire. Is there an obvious, or not so obvious reason for this? How do I fix it?
I created a fiddle so you can see the result. https://jsfiddle.net/9h1pfpns/
Here is my code:
.container {
height: 200px;
margin: 10px;
border: 4px solid #754419;
}
.shape {
display: inline-block;
position: relative;
top: 50%;
transform: translateY(-50%);
width: 250px;
height: 100px;
margin-left: 10px;
background-color: aquamarine;
}
.text {
display: inline-block;
position: relative;
top: 50%;
transform: translateY(-50%);
font: bold 1.25em Arial, Helvetica, sans-serif;
margin-left: 10px;
border: 1px solid #754419;
}
<div class="container">
<div class="shape"></div>
<div class="text">first</div>
</div>
You can change the CSS for text to:-
.text {
display: inline-block;
position: relative;
margin: 0 auto;
font: bold 1.25em Arial, Helvetica, sans-serif;
margin-left: 10px;
border: 1px solid #754419;
}
Just to clarify I removed the following CSS:-
top: 50%;
transform: translateY(-50%);
And added:-
margin: 0 auto;
Check out the jsFiddle https://jsfiddle.net/01kkavf4/
* Update *
You can also replace:-
top: 50%;
transform: translateY(-50%);
With:-
top: 10%;
transform: translateY(-50%);
jsFiddle of both:-
https://jsfiddle.net/01kkavf4/
The second resolutions fits better with your box.
Just a few different ways.
It doesn't work because, before offsetting with relative positioning and transforms, the elements are not aligned to the top. The default is vertical-align: baseline.
Just add vertical-align: top.
.container {
height: 200px;
margin: 10px;
border: 4px solid #754419;
}
.item, .text {
display: inline-block;
position: relative;
top: 50%;
transform: translateY(-50%);
margin-left: 10px;
vertical-align: top;
}
.item {
width: 250px;
height: 100px;
background-color: aquamarine;
}
.text {
font: bold 1.25em Arial, Helvetica, sans-serif;
border: 1px solid #754419;
}
<div class="container">
<div class="item"></div>
<div class="text">first</div>
</div>
Anyways, I recommend against this approach because in case the container is shorter than the contents, they will overflow above and below. But you won't be able to scroll to see the above overflow.
Instead, I recommend flexbox with auto margins.
.container {
height: 200px;
margin: 10px;
border: 4px solid #754419;
display: flex;
}
.item, .text {
margin: auto 0 auto 10px;
}
.item {
width: 250px;
height: 100px;
background-color: aquamarine;
}
.text {
font: bold 1.25em Arial, Helvetica, sans-serif;
border: 1px solid #754419;
}
<div class="container">
<div class="item"></div>
<div class="text">first</div>
</div>
Related
How can I add a circle on top of two vertical div in HTML? I succeed in having 2 vertical boxes:
but I cannot figure out how to have a circle in the middle like the following:
The goal is to have a white circle with a blue line and being able to add a logo in the circle. I have the following code snippet:
http://jsfiddle.net/wL9xoad3/
.html {
height: 100%;
}
*,
*:before,
*:after {
box-sizing: border-box;
}
.body {
background-color: #000;
font-family: "Open Sans", sans-serif;
height: 100%;
}
.vidyard_padding {
height: 100%;
}
.vc {
display: table;
height: 100%;
width: 100%;
}
.vc-inner {
display: table-cell;
text-align: center;
vertical-align: middle;
}
.cta {
background-color: #fff;
height: 360px;
margin: 0 auto;
overflow: hidden;
position: relative;
width: 640px;
}
.cta-full {
height: 100%;
width: 100%;
}
.cta-half {
float: left;
height: 100%;
width: 50%;
}
.cta-block {
display: table;
height: 100%;
width: 100%;
}
.cta-block-inner {
display: table-cell;
padding: 20px;
text-align: center;
vertical-align: middle;
}
.cta-block p {
line-height: 1.4125;
margin: 0;
}
.cta-block p.white {
color: #FFFFFF;
}
.cta-block p+.btn {
margin-top: 10px;
}
.cta-block .btn {
background-color: #414142;
border-radius: 2px;
color: #FFFFFF;
display: inline-block;
font-size: 10px;
letter-spacing: 1px;
padding: 8px 12px;
text-decoration: none;
}
.cta-block .btn:hover {
background-color: #313132;
}
<div class="cta-half">
<div class="cta-block" style="background-color:#FFFFFF;">
<div class="cta-block-inner">
<p class="black">Watch our Quick Start</p>
<a class="btn" href="https://google.com">Quick Start</a> </div>
</div>
</div>
<div class="cta-half">
<div class="cta-block" style="background-color:#47b2ffff;">
<div class="cta-block-inner">
<p class="white">Start in the Cloud</p>
<a class="btn" href="https://google.com">Cloud</a> </div>
</div>
</div>
You can use a ::before or an ::after pseudo element with an empty content and some positioning. You can set the width and height of the new element and add some border-radius to make it a circle. Don't forget to set position: relative on the .cta-half element so you can move the circle relative to this.
You can add the following to your snippet on jsfiddle, it should work:
.cta-half {
position: relative;
}
.cta-half:last-of-type::after {
background-color: #fff;
border-radius: 50%;
border: 2px solid #47b2ff;
content: '';
height: 50px;
left: 0;
position: absolute;
top: 50%;
transform: translate(-50%, -50%);
width: 50px;
}
If you want to add a logo in the circle, you can update your content and add a url(). I'd probably grab the svg version of the logo and encode it using this tool. It will convert the image and use it like this:
content: url("data:image/svg+xml,%0A%3Csvg viewBox='0 0 533.5 544.3' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M533.5 278.4c0-18.5-1.5-37.1-4.7-55.3H272.1v104.8h147c-6.1 33.8-25.7 63.7-54.4 82.7v68h87.7c51.5-47.4 81.1-117.4 81.1-200.2z' fill='%234285f4'/%3E%3Cpath d='M272.1 544.3c73.4 0 135.3-24.1 180.4-65.7l-87.7-68c-24.4 16.6-55.9 26-92.6 26-71 0-131.2-47.9-152.8-112.3H28.9v70.1c46.2 91.9 140.3 149.9 243.2 149.9z' fill='%2334a853'/%3E%3Cpath d='M119.3 324.3c-11.4-33.8-11.4-70.4 0-104.2V150H28.9c-38.6 76.9-38.6 167.5 0 244.4l90.4-70.1z' fill='%23fbbc04'/%3E%3Cpath d='M272.1 107.7c38.8-.6 76.3 14 104.4 40.8l77.7-77.7C405 24.6 339.7-.8 272.1 0 169.2 0 75.1 58 28.9 150l90.4 70.1c21.5-64.5 81.8-112.4 152.8-112.4z' fill='%23ea4335'/%3E%3C/svg%3E");
You can also add some padding to make the logo smaller.
Result:
I am creating rows of objects that are currently in an HTML table as separate elements so that I have more flexibility when making the page responsive. However, I am unable to get the span text elements to center with the status div or button at all. The span text seems to have a larger bottom 'padding' than the other elements and for the life of me I can't figure out why.
I have to use floats for alignment (to my knowledge), unfortunately, as the website must fully support IE10+, otherwise I would use something like flex. Most of the things I've looked at and researched online deal with one line of text another element and many suggest editing the line height which seems to me like it wouldn't be the best solution for this case.
The codepen for all the code is here: https://codepen.io/stevennava/pen/WNrzzKo
and here is the code (per StackOverflow standards):
#outer-border {
border: 1px solid black;
border-radius: 5px;
padding: 1.3em 0;
position: relative;
}
#centered-container {
margin: 0;
padding: auto;
position: absolute;
top: 50%;
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
#status {
position: relative;
border: 1px solid lightgrey;
border-radius: 8px;
padding: 0 .2em 0 .2em
}
#status-message {
float: left;
margin-left: 1.2em;
}
#status-circle {
width: 12px;
height: 12px;
background-color: orange;
border-radius: 50%;
float: left;
margin: 0;
position: absolute;
top: 50%;
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
#button {
height: 1.5em;
width: 1.5em;
text-align: center;
}
.order-element {
padding: 0;
float: left;
margin-left: 15px;
}
.clearfix {
overflow: auto;
}
.clearfix::after {
content: "";
clear: both;
display: table;
}
<div id="outer-border" class="clearfix">
<div id="centered-container" class="clearfix">
<span name="user-name" class="order-element">John Smith</span>
<div id="status" class="order-element">
<div id="status-circle" class=""></div>
<span id="status-message" class="">Complete<span>
</div>
<div class="clearfix">
<span name="order-number" class="order-element">123456</span>
<span name="order-amount" class="order-element">$35.45</span>
<span name="fulfillment-date" class="order-element">07/07/2020 07:00 PM</span>
<button id="button" class="order-element">...</button>
</div>
</div>
and this is this the result looks like:
Two things, the height of the button is causing the .clearfix div's height to stretch, and the border on the #status is causing it's height to stretch.
If you don't want to completely change this up, you can add padding to match the thickness of the border on the spans, and set the button height to auto.
#outer-border {
border: 1px solid black;
border-radius: 5px;
padding: 1.3em 0;
position: relative;
}
#centered-container {
margin: 0;
padding: auto;
position: absolute;
top: 50%;
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
#status {
position: relative;
border: 1px solid lightgrey;
border-radius: 8px;
padding: 0 .2em 0 .2em
}
#status-message {
float: left;
margin-left: 1.2em;
}
#status-circle {
width: 12px;
height: 12px;
background-color: orange;
border-radius: 50%;
float: left;
margin: 0;
position: absolute;
top: 50%;
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
#button {
height: auto;
width: 1.5em;
text-align: center;
}
.order-element {
padding: 0;
float: left;
margin-left: 15px;
}
.clearfix {
overflow: auto;
}
.clearfix::after {
content: "";
clear: both;
display: table;
}
.pad-1 {
padding-top: 1px;
padding-bottom: 1px;
}
<div id="outer-border" class="clearfix">
<div id="centered-container" class="clearfix">
<span name="user-name" class="order-element pad-1">John Smith</span>
<div id="status" class="order-element">
<div id="status-circle" class=""></div>
<span id="status-message" class="">Complete<span>
</div>
<div class="clearfix">
<span name="order-number" class="order-element pad-1">123456</span>
<span name="order-amount" class="order-element pad-1">$35.45</span>
<span name="fulfillment-date" class="order-element pad-1">07/07/2020 07:00 PM</span>
<button id="button" class="order-element">...</button>
</div>
</div>
I'm trying to create a div, and then inside the div put the date on the left, and two icons on the right. Both should be vertically centered, have some margin to the edge, and margin between other elements in the parent div. But right now when I run it, the icons end up in strange positions, often clipping out of the div.
.c-project-bar {
width: 355px;
height: 30px;
display: table;
padding: 0px 10px;
background-color: #ffffff;
}
.c-project-date {
display: table-cell;
vertical-align: middle;
color: #828282;
font-size: 14px
}
.c-project-github-icon {
margin-left: 10px;
background-color: #000000;
height: 25px;
width: 25px;
float: right;
vertical-align: middle;
display: table-cell;
}
<div class="c-project-bar">
<p class="c-project-date">Aug, 2017</p>
<span class="c-project-github-icon"></span>
<span class="c-project-github-icon"></span>
</div>
https://jsfiddle.net/enyzhxyz/
Edit:
Everyone's answers are awesome, it seemed a major issue was the fact that the was inheriting { margin: 0px; }, and it was throwing a wrench in everyone's solutions. I solved this by including a margin: initial, before using any margin style later, so it first removed { margin: 0px; } then added the one I wanted.
When you give float: right you cannot vertically centre. Instead, use the inline-block technique:
.c-project-bar {
width: 355px;
padding: 0px 10px;
background: #ccf;
}
.c-project-date, .c-project-icons {
display: inline-block;
width: 48%;
vertical-align: middle;
}
.c-project-icons {
text-align: right;
}
.c-project-github-icon {
margin-left: 10px;
background-color: #000000;
height: 25px;
width:25px;
display: inline-block;
vertical-align: middle;
}
<div class="c-project-bar">
<p class="c-project-date">Aug, 2017</p>
<div class="c-project-icons">
<span class="c-project-github-icon"></span>
<span class="c-project-github-icon"></span>
</div>
</div>
I would have suggested you the transform and position centering technique, but it would be a too much of overkill for this solution. I have given some background colour to see it is perfectly vertical align middle. Although it uses slightly extra markup, this would be the right way that works on browsers that don't support flexbox too.
Here is a solution using css flexbox.
body {
background-color: #696969;
}
.c-project-bar {
display:flex;
align-items: center;
justify-content: space-between;
width: 355px;
height: 30px;
padding: 0px 10px;
background-color: #ffffff;
}
.c-project-date {
color: #828282;
font-size: 14px
}
.c-project-github-icon {
margin-left: 10px;
background-color: #000000;
height: 25px;
width:25px;
float: right;
}
<div class="c-project-bar">
<p class="c-project-date">Aug, 2017</p>
<div>
<span class="c-project-github-icon"></span>
<span class="c-project-github-icon"></span>
</div>
</div>
Another way to achieve this could be to use transform.
position: relative;
top: 50%;
transform: translateY(-50%);
.c-project-bar {
width: 355px;
height: 30px;
padding: 0px 10px;
background-color: #ccc;
}
.c-project-date {
margin: 0;
position: relative;
top: 50%;
transform: translateY(-50%);
float: left;
color: #828282;
font-size: 14px
}
.c-project-github-icon {
margin-left: 10px;
background-color: #000000;
height: 25px;
width:25px;
float: right;
position: relative;
top: 50%;
transform: translateY(-50%);
}
<div class="c-project-bar">
<p class="c-project-date">Aug, 2017</p>
<span class="c-project-github-icon"></span>
<span class="c-project-github-icon"></span>
</div>
You can do it easily with display: flexbox.
.c-project-bar {
display: flex; // power
align-items: center; // aligning
width: 355px;
height: 30px;
padding: 0px 10px;
background-color: #ffffff;
}
.c-project-date {
margin-right: auto; // let's push other stuff to right side
color: #828282;
font-size: 14px
}
.c-project-github-icon {
margin-left: 10px;
background-color: #000000;
height: 25px;
width:25px;
}
Fixed jsfiddle
As shown in the fiddle here with the following HTML:
<body>
<div class="main_container">
<div class="question">
<p>Test question here</p>
</div>
<input class="answer" type="text">
<input class="submit" type="submit">
</div>
</body>
And CSS:
#import 'https://fonts.googleapis.com/css?family=Open+Sans';
body {
text-align: center;
width: 100%;
font-family: 'Open Sans',sans-serif;
//background-color: rgba(0,150,250,0.75);
}
.question {
border-style: solid;
border-color: rgba(0,150,250,0.75);
border-width: 2em;
background-color: white;
border-radius: 1.618em;
margin: 5em auto;
width: 75%;
height: 10em;
}
.question>p {
border-radius: 1.618em;
position: relative;
top: 50%;
transform: translateY(-50%);
}
.answer {
font-size: 1.618em;
border: 0;
border-radius: 1.618em;
width: 50%;
margin: auto;
}
I am able to get the test question centered if I remove the border-style:solid property of question. However, I am wondering why with border-style it is not centered. I've tried using box-sizing:border-box to no avail.
Thanks
Your Vertical align is messed up because browser applied top bottom margin in p tag, if you removed it this will solve your problem
.question > p {
margin: 0;
}
or
p {
margin: 0;
}
see my updated fiddle here
There is by default margin on p elements, so when there is no border on parent element what happens is margin collapsing on parent-child and that margin doesn't affect position of p. But when you set border (it can be any border as you can see here DEMO) on parent element you prevent margin-collapsing and now you can see margin on p element.
So one solution is to remove margin from p
#import 'https://fonts.googleapis.com/css?family=Open+Sans';
body {
text-align: center;
width: 100%;
font-family: 'Open Sans', sans-serif;
//background-color: rgba(0,150,250,0.75);
}
.question {
border-style: solid;
border-color: rgba(0, 150, 250, 0.75);
border-width: 2em;
background-color: white;
border-radius: 1.618em;
margin: 5em auto;
width: 75%;
height: 10em;
}
.question>p {
background-color: red;
border-radius: 1.618em;
position: relative;
top: 50%;
transform: translateY(-50%);
}
.answer {
font-size: 1.618em;
border: 0;
border-radius: 1.618em;
width: 50%;
margin: auto;
}
p {
margin: 0;
}
<div class="main_container">
<div class="question">
<p>Test question here</p>
</div>
<input class="answer" type="text">
<input class="submit" type="submit">
</div>
Try having the parent div displayed as a table and the p displayed as a table-cell then use vertical-align.
See the below snippet.
#import 'https://fonts.googleapis.com/css?family=Open+Sans';
body {
text-align: center;
width: 100%;
font-family: 'Open Sans',sans-serif;
//background-color: rgba(0,150,250,0.75);
}
.question {
border: 2em solid rgba(0,150,250,.75);
background-color: white;
border-radius: 1.618em;
margin: 5em auto;
width: 75%;
height: 10em;
display: table;
}
.question p {
border-radius: 1.618em;
position: relative;
display: table-cell;
vertical-align: middle;
}
.answer {
font-size: 1.618em;
border: 0;
border-radius: 1.618em;
width: 50%;
margin: auto;
}
<body>
<div class="main_container">
<div class="question">
<p>Test question here</p>
</div>
<input class="answer" type="text">
<input class="submit" type="submit">
</div>
</body>
Just set margin 0px of p tag. Sample is
.question>p {
background-color:red;
border-radius: 1.618em;
position: relative;
top: 50%;
transform: translateY(-50%);
margin:0px;
}
You can use flexbox here:
1.) Add display: flex; and flex-direction: column; to .question
2.) Add margin: auto 0; to .question > p.
3.) Erase everything else except border-radius from .question > p
Here is a fiddle: https://jsfiddle.net/35jhjqcx/
Update inner paragraph positions to absolute and remove the margins
and update outer div of paragraph position to relative
see working fiddle link
.question {
border-style: solid;
border-color: rgba(0,150,250,0.75);
border-width: 2em;
background-color: white;
border-radius: 1.618em;
margin: 5em auto;
width: 75%;
height: 10em;
position: relative;
}
.question>p {
background-color: red;
border-radius: 1.618em;
top: 50%;
margin: 0;
transform: translateY(-50%);
position: absolute;
width: 100%;
}
http://samnorris.co.nz/backup/test/
In the section with the Pythagoras quote "There is geometry in the humming of the strings,
there is music in the spacing of the spheres" is a little experiment from Codepen I've tweaked a bit and have been playing around with (the rotating animated circles)
...not sure if my mind is just working a bit slow tonight or what but I'm having a great amount of difficulty positioning and centering the div that contains these elements underneath the div's that contain the quote and without overlaying the background...
any help would be appreciated :)
edit: updated code, I figured out why it was being pushed down (padding on the wrapper was causing it, so I've changed the background div to fixed height instead) now I just need to figure out why I am having to use left: 32% (roughly) to try and center it. Margin: 0 auto is failing me, and setting #spheres-block to width 100% messes up the bg.
Relevant CSS:
.spacesuit-bg {
background: transparent url("../img/spacesuit-bg.jpg") no-repeat 0 0;
color: #fff;
padding: 15% 0;
text-align: center;
text-transform: uppercase;
-webkit-transform: translate3d(0,0,0);
}
.quote-wrapper {
width: 100%;
display: block;
padding-top: 300px;
}
.quote-one {
font: 18px/1em "proxima_nova_rgbold", sans-serif;
font-weight: 700;
text-shadow: 0px 3px 3px rgba(0,0,0,0.3);
letter-spacing: 1px;
word-spacing: .06em;
width: 100%;
line-height: 1.25em;
}
.quote-two {
font: 36px/1em "proxima_nova_rgbold", sans-serif;
font-weight: 700;
text-shadow: 0px 3px 3px rgba(0,0,0,0.3);
letter-spacing: 1px;
word-spacing: .06em;
width: 90%;
line-height: 1.25em;
padding-top: 25px;
margin: 0 auto;
}
.spacesuit-wrapper:before, .missiongo, .golaunch {
display: inline-block;
vertical-align: middle;
}
#spheres-block {
overflow: hidden;
position: absolute;
display: block;
width: 600px;
height: 600px;
left: 32%;
}
.spheres {
width: 600px;
height: 600px;
margin: 0 auto;
position:absolute;
}
.rings {
border-radius: 50%;
border: 2px solid rgba(255,255,255, .5);
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
opacity: 0.6
}
Relevant HTML:
<div id="spacesuit-bg" class="section spacesuit-bg">
<div id="spheres-block">
<div class="spheres">
<div class="rings"></div>
<div class="rings"></div>
<div class="rings"></div>
<div class="rings"></div>
<div class="rings"></div>
<div class="rings"></div>
</div>
</div>
<div class="quote-wrapper">
<div class="quote-one wow flipInX">"There is geometry in the humming of the strings,</div>
<div class="quote-two wow flipInX">there is music in the spacing of the spheres."</div>
</div>
</div>
I am not a CSS expert (trying to learn, that's why I spend time here :)), so I may have missed other issues with my suggestion, but anyway I was able to achieve the desired effect by positioning the speres-block relatively, and positioning the quote-wrapper absolutely with a top offset of 0:
#spheres-block {
position: relative;
width: 650px;
height: 650px;
margin: 0 auto;
margin-top: 15px;
}
.quote-wrapper {
width: 100%;
display: block;
padding-top: 300px;
position: absolute;
top: 0px;
}
I hope I understood correctly.