This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed last month.
I'm currently developing a web app using vue.js, and I'm having trouble vertically centering some elements.
This is what I currently have
I would like to have the div with the form(grey colored bit) to be vertically and horizontally centered.
This is the code for the form, the top navbar is a different component. Also, I'm using tailwindcss but am open to inline or internal styling options the same.
<template>
<div class="bg-gray-600">
<form action="">
<div>
<input type="email" name="email" id="email"/>
<label for="email">Email</label>
</div>
<div>
<input type="password" name="password" id="password"/>
<label for="password">Password</label>
</div>
</form>
</div>
</template>
The code below is in my base.css which was created when the vue project was created. I have no idea how much it affects the elements or what the former block of code(The one with *) does.
*,
*::before,
*::after {
box-sizing: border-box;
padding: 0;
margin: 0;
position: relative;
font-weight: normal;
}
body {
min-height: 100vh;
color: var(--color-text);
background: var(--color-background);
transition: color 0.5s, background-color 0.5s;
line-height: 1.6;
font-family: Inter, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu,
Cantarell, 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
font-size: 15px;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
I've tried most solutions I could find but they made little to no changes. I would like to avoid setting a fixed px height if possible because I would like it to be centered across viewports.
Here are a few methods on how to center an HTML element:
The oldest trick in the book:
.form {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%)
}
This works by moving the form element 50% to the left and 50% to the top of the container and move's it back 50% of its width and height.
flex
.form-container {
display: flex;
align-items: center;
justify-content: center;
}
Set the container of the form to flex and align it to the center vertically and horizontally.
grid
.form-container {
display: grid;
place-items: center;
}
Same thing as flex, but both justify-content and align-items are merged into the same line.
You can give the same CSS to your form using tailwindcss:
1.
<form class="absolute top-1/2 left-1/2 translate-x-1/2 translate-y-1/2">
<div class="bg-gray-600 flex items-center justify-center">
<div class="bg-gray-600 grid place-items-center">
Hope this helps!
Apply this css on the parent of your form, in this case, probably the bg-gray-600
.bg-gray-600 {
display:flex;
justify-content:center; /*horizontal center*/
align-items:center; /*vertical center*/
}
You might need to define width or height of the parent in order this to work. Depends on your CSS.
The * that you said you dont understand is used to select every element within the whole page, so the bg-gray-600 as well. In this case, none of its values effect the centering of your form.
Related
I am having a bit of an issue with aligning elements vertically, as can be seen in the picture:
Namely, when font-variant: smallcaps is in effect, the span containing the text is no longer "centered" with the icon beside it, this is more apparent the bigger the "font" due to icon being a sized by the font-size.
I tried converting the span to a display:block element but couldn't manage to get it to work with vertical-align: middle, i tried turning it into display: flex and using align-items: center. No success, seems that only the display is "out of touch" while internally the text is properly sized. How would you fix this?
Fiddle: https://jsfiddle.net/1nzhgymf/
.menu-item {
display: flex;
flex-flow: row nowrap;
justify-content: flex-start;
align-items: center;
overflow: hidden;
width: 100%;
height: 2.8em;
color: inherit;
text-decoration: none;
text-transform: uppercase;
font-family: Roboto;
font-size: 20px;
flex-grow: 0;
flex-shrink: 0;
padding: 0.4em;
}
.icon {
min-width: 1em;
min-height: 1em;
background: red;
margin-right: 0.4em;
}
<div>
<a class="menu-item">
<div class="icon"></div>
<span>Example</span>
</a>
<a class="menu-item" style="font-variant: all-small-caps">
<div class="icon"></div>
<span>Example</span>
</a>
</div>
The initial attempt at an answer here incorrectly concluded that the em size of a font was affected by all-small-caps in CSS font-variant. This is not so (and certainly should not be, an em being fixed for a font/font size) but I keep this answer here rather than deleting it because the questioner has made some useful comments.
Here is a further attempt at an explanation:
The problem as stated was an issue with aligning elements vertically. An example was given of an all uppercase word appearing to align differently from one which was set at all-small-caps. In the screenshot below you can see variations on this - with a line which is all lowercase or all small caps seeming to be further down than the uppercase example.
In some sense this is true - in the uppercase example the mid line goes through the middle of the uppercase E and X which makes it balanced about the line. In the all-small-caps example though the capitals' centres have dropped below the mid line. The final line in the image shows why this is necessary - mixed text would otherwise be waving up and down.
I'm working on a project where there is a row of controls, each of which is a button element. There is content inside of the buttons, and they are laid out in a row with flexbox. The button element centers its content vertically, and I can't figure out how to override it to vertically align it at the top of the button. The controls all need to be the same height and same width, and clicking anywhere in the borders must count as a click on the button.
This Codepen shows the problem clearly: http://codepen.io/anon/pen/RPpqdz
.wrapper {
display: flex;
flex-direction: row;
width: 80%;
}
button,
.object {
font-family: sans-serif;
font-size: 1em;
padding: 1em;
background: #fff;
flex: 1;
border: 1px solid red;
text-align: left;
}
<h1>What it looks like</h1>
<div class="wrapper">
<button>I am Content</button>
<button>I am Much Longer Content That Goes Here and Here</button>
<button>I am Content</button>
</div>
<h1>What I want it to look like</h1>
<div class="wrapper">
<div class="object">I am Content</div>
<div class="object">I am Much Longer Content That Goes Here and Here</div>
<div class="object">I am Content</div>
</div>
I realize this issue could be solved by not using button elements, but I also feel like I SHOULD be able to override this behavior of button elements. I'd like to figure this out for my own sanity!
Firstly, having h2 and p inside button is not valid HTML.
Secondly, there is no simple way to control the position of elements in a button, especially vertically.
But if you really really must use this BROKEN HTML, and only the top alignment is important, you can force the elements to take up fixed heights so that the button will align them at the top, like so:
button > h2 {
height: 48px;
}
button > p {
height: 16px;
}
I must say this is still not exactly the same as using <div>, so I don't know if this is sufficient.
In any case, do seriously try to convince those in charge of the "larger context of the project" to use proper valid HTML.
Edit:
If only inline elements are used inside the button, the problem becomes more manageable. The only caveat is: you need to know beforehand the height of the button. You can then simulate top-bottom flow using paddings.
button {
padding-top: 1em;
padding-right: 1em;
padding-bottom: 5em; /* make sure bottom + top padding == height */
padding-left: 1em;
height: 6em;
}
Still probably not ideal - feels like a heavily plastered hack solution.
*I'm not sure what do you want and what do you mean by "how to override it to vertically align it at the top of the button?"? but I hope this code is what you want.(same height, width and even with spaces also buttons matching with div (.objects).
.wrapper {
display: inline-flex;
display: -moz-inline-box;
width: 90%;
}
button, .object {
font-family: sans-serif;
font-size: 1em;
padding: 1em;
background: #fff;
border: 1px solid red;
text-align: left;
margin: 0em 0.3em 0em 0em;
}
Alright, so I'm trying to make a line of different text bits in html/css. This will be the precursor for a navbar. My HTML is:
<div id="navBar">
<p class="navBartext">About</p>
<p class= "navbartext">News</p>
<p class= "navbartext">Contact Us</p>
<p class= "navbartext">Jobs</p>
</div>
and the CSS:
.navBartext{
text-align: center;
color:black;
font-size: 20;
font-family: 'Poiret One', cursive;
display: inline;
padding-right: 20px;
}
#navbar{
width: 100%;
height: 50px;
}
Now, when I take the "display: inline;" out of the code, the text aligns vertically instead of horizontally, and then I can use text align to position it, but I want them all in one line. When I use display-inline though it seems to completely circumvent the text-align function (as anything put in here will be ignored). Is there something I'm missing? Perhaps I just don't know enough about the display function.
If you want to align the words horizontally, you have to use display:inline-block; so that the elements will be treated as text. Always use inline-block for the child elements and text-align:center; for the parent.
p{
color:black;
font-size: 20;
font-family: 'Poiret One', cursive;
display: inline-block;
padding-right: 20px;
}
#navbar{
width: 100%;
height: 50px;
text-align:center;
}
VIEW DEMO
Try this, you can use <ul> element instead of div, div is better as a wrapper if u need wrap navBar:
[http://jsfiddle.net/WT7qv][1]
http://jsfiddle.net/WT7qv
<div class="signup">
<div>Tenxian アカウントに必要な情報</div><br/>
</div>
<br/><br/><br/><br/><br/><br/>
<div align="center">#2009 Tenxian 利用規約
</div><br/>
<div align="center">Tenxian·English 腾闲·中国</div><br/><br/>
The code of .signup is:
.signup {
border: 1px solid #99CCFF;
position: absolute;
width: 700px;
height:450px;
left: 50px;
right: auto;
font-family: Arial, Helvetica, sans-serif;
font-size: 13px;
}
The problem is that
#2009 Tenxian 利用規約
Tenxian·English 腾闲·中国
is displayed in the box of <div class="signup"></div>, how to display it out of the box of <div class="signup"></div>?
I want to display
#2009 Tenxian 利用規約
Tenxian·English 腾闲·中国
at the bottom of a web page and outside of the box of <div class="signup"></div> . How to do it?
<div style="position:relative"><div align="center" >#2009 Tenxian 利用規約
</div><br/>
<div align="center">Tenxian·English 腾闲·中国</div><br/><br/>
</div>
does not work.
The problem is that your signup box is set to position:absolute. When you do this, the element is removed from the flow of the page.
The easiest solution is to simply not make it position:absolute. (If this is not possible, please revise your question so that more helpful answers can be posted.)
Your original code, simplified
<div class="signup">
<div>Tenxian アカウントに必要な情報</div>
</div>
<div class="footer">#2009 Tenxian 利用規約</div>
<div class="footer">Tenxian·English 腾闲·中国</div>
.footer {
text-align: center;
}
Note that my solution below does NOT require you to use this code. It works fine with your existing HTML markup. I'm posting this code so that future readers will be able to understand the markup more easily.
A possible solution
.signup {
border: 1px solid #99CCFF;
width: 700px;
height: 450px;
margin-left: 50px;
margin-right: auto;
font-family: Helvetica, Arial, sans-serif;
font-size: 13px;
}
This makes your signup box take up the proper amount of space in the flow of the page. Here are the changes:
Remove position:absolute: This causes the signup box to display as a regular statically positioned box
Change left and right to margin instead: When a block-level box is displayed statically, it does is not affected by top, left, and so on. Instead, we use margins to push the box over to where we want it to be.
{overflow:visible;} might work
When I change the size of a font in CSS, how do I make it so no matter what size the font is (from 12px to 200px), that the "Cap Height" (pic) of the text will always 'visually' have 10px padding on top?
Otherwise what I'm doing is every time I change the font size, I have to go back and reposition the top/margin-top etc.
Here's what I have:
CSS:
#header .statement {
position: relative;
background: white;
padding-top: 10px;
display: inline;
float: left;
margin-left: 0;
margin-right: 0;
width: 960px;
}
#header .statement h3 {
position: relative;
font-size: 160px;
letter-spacing: -10px;
font-weight: bold;
color: #141414;
font-family: Helvetica, sans-serif;
text-align: center;
}
HTML sample:
<div id='header'>
<div class='intro'>
Stuff before the statement
</div>
<div class='statement'>
<h3>
<p>A Statement</p>
</h3>
<div class='large_spacer'></div>
</div>
<div class='clearfix'></div>
</div>
This is what it looks like with line-height: 0:
alt text http://ilove4d.com/ex/css-typography.png
This is with line-height: 1:
alt text http://ilove4d.com/ex/css-typography-2.png
If I change the font-size from 160px to 20px, the white space proportionally gets smaller... How do I get around that?
Note: it's adding like 20px extra whitespace, even if margin:0;padding:0;...
If you really mean "on top" of the cap height, and not somehow inside the cap height margin, then you can apply the CSS padding to either the font element or its parent container:
<span class="something">Web Typography</span>
span.something {padding-top: 10px;}
OR...
<div class="something"><span>Web Typography</span></div>
.something {padding-top: 10px;}
One of the approaches will be suitable depending on what other styles you are applying.
Try adding padding-top:10px to #header .statement h3 {}
edit:
did you reset the values for #header .statement h3 p {}?
Otherwise what I'm doing is every time
I change the font size, I have to go
back and reposition the top/margin-top
etc.
Why don't you set bottom and margin-bottom of the elements above and below that text instead? In this way, you won't have to modify the text styling, the gap will be there always.
Also why in the world, you can touch a font-size of more than 36px? In any case, you could also use the line-height style for that like line-height:30px; or whatever value.