Horizontally and Vertically Centering Text - html

I want to vertically and horizontally centre some text in a div that contains to images beside it. So far I've only got it horizontally centred and am having troubling getting it vertically centred. I know there a lots of posts like this and mentioning some absolute positioning but every time I try it everything just overlaps.
Heres the jsfiddle: https://jsfiddle.net/kkae9rzy/
If you expand the result screen horizontally so that everything is on the same line(baseline) you will see that the text is not vertically centred.
Heres the code for my css:
#titleheader{
margin:auto;
text-align: center;
padding:100px;
background:#f2f2f2;
}
#titleheader > h1 {
padding-left: 75px;
padding-right: 75px;
display:inline-block;
font-family: "Bariol_Regular", "Helvetica Neue", Helvetica, Arial, sans-serif;
color: #555;
letter-spacing: 3px;
font-size: 50px;
}
#wind{
display:inline-block;
}
#intel{
display:inline-block;
}

I didn't see that anything is "vertically centered" in your version, but I added a vertical-align: middle to the <img> tags (via css) and it looks like that's what you wanted. See the fiddle.

You can use the flexbox layout to achieve this, as well as a few other methods.
Here is the fiddle with the modifications to your CSS to utilize the flexbox method. Pay special attention to #titleheader. https://jsfiddle.net/kkae9rzy/2/
You'll see that I added a few flexbox rules to #titleheader:
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
display: flex; tells #titleheader to use the flexbox display layout.
(See http://dev.w3.org/csswg/css-flexbox/ and https://css-tricks.com/snippets/css/a-guide-to-flexbox/ for more info on flexbox)
align-items: center; is telling #titleheader to align all of its children to the center of the cross axis (for our demo, the X axis, so it's vertically centering them).
justify-content: center is telling #titleheader to align all of its children to the center of the main axis (for our demo, the Y axis, so it's horizontally centering them).
flex-wrap: wrap is telling #titleheader to wrap child elements onto a new line instead of forcing them to be all on the same line, regardless of how much screen space is available.
Flexbox is an extremely powerful tool. It's rapidly gaining in browser support and I suspect we will be able to start using it without too many polyfills soon.
Here is the current support for flexbox: http://caniuse.com/#feat=flexbox
To ensure the widest variety of support when using flexbox, make sure you include all vendor prefixed versions of the flexbox declarations I mentioned above. This can be achieved using a tool like autoprefixer (https://github.com/postcss/autoprefixer) which adds all the necessary vendor prefixes to your CSS when compiled.
Including all of the vendor prefixes for the attributes I mentioned above would look something like this:
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
-webkit-box-pack: center;
-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-flex-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;

This should work
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);

Related

Trying to center text vertically and horizontally next to an image

https://github.com/Yankysamurai/Personal-Website-P5
I am new to posting questions but I am trying to center my text "Welcome to my site" horizontally and vertically. I want it half the distance from the image and from the right margin. I tried display flex but it didn't work.
I just opened a git hub account so I hope you can see the code from the link.
Thank you.
So, the flex container has an img and h2. In order to vertically center the text (relative to the image), you need to set align-items: center;
.about_img_h2 {
display: flex;
justify-content: space-around;
align-items: center;
}
I would suggest using jsfiddle next time, because it makes it easier to review the problem. I created this one (using the GitHub repo you shared) with the fix:
https://jsfiddle.net/13sc4m76/
Let me know if I misunderstood your question and I can take a second look.
you can use the following ways to center elements in a block:
display: flex;
flex-direction: row;
justify-content: center
or
display: flex;
flex-direction: column;
align-items: center

how to place images in the same line inside a flexbox

so for responsiveness, I have created a flexbox and placed some images inside but for some reason, I am not able to place all the images in the same line. written below is my CSS code. the container-project is the class for the div in which I am placing all the images and the project-img class is the class for every image placed inside the div.
.container-projects{
display: flex;
flex-wrap: wrap;
justify-content: space-around;
}
.project-img{
width: 25%;
height: auto;
border-radius: 30px;
padding: 20px;
}
I see two issues.
One is that you've set flex-wrap to wrap which means that flex items will wrap onto multiple lines, from top to bottom. Try setting it to nowrap instead.
The second depends on how many images you're trying to put in the line. If it's 4 then you're fine, otherwise you want to change the width: 25%; to a different value as at the moment it will be dividing the width of the containing area in to 4. You might want to look into the flex-basis property instead. It defines the default size of an element before the remaining space is distributed.
Add align-items: center;
.container-projects{
display: flex;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
}

Flexbox in nav bar: Can't align-items vertically

I'm really new to html and css, so this is my first full attempt at making a nav bar, items in nav bar stuck to the top of the page, please tell me where things went wrong
https://codepen.io/galia-s/pen/GRojmwV
.flex-content {
display: flex;
margin: auto 3.25rem;
align-items: center;
}
Instead of align-content, you might be looking at align-items, depending on what you are trying to achieve. I assume you want to center these items.
So your .flex-content class could look like this:
.flex-content {
display: flex;
align-items: center;
justify-content: space-evenly;
height: 5rem; //set height here instead of header
}
Hope it helps.
You posted way too little information for a reliable answer, but anyway: To get flex-items to stack vertically, you have to add flex-direction: column; to the flex container.

anchor tag in my flex container breaks my layout

i want the second block quote element to look like the first one but with a clickable name and pic so i wrap them in an anchor tag it didn't behave like i wanted the text sits under the image. making the anchor tag a flex container fixes my problem i just don't understand why it behaves like that.
.review__user a {
display: flex;
align-items: center;
margin-right: auto;
}
demo is right here pen
.review__user, .review__user > a{
display: flex;
width:100%;
align-items: center;
}
Try this, it will solve your problem. Hope it helps.
Try this. Hope it helps.
.review__user a {
display: flex;
align-items: center;
align-content: center;
flex: 1;
}

Flexbox not working to center a Woocommerce Button

i'm trying to center a button in my woocommerce website but without sucess.
Heres what i already tried:
margin: auto;
float: none;
display: block;
display: flex;
align-items: center;
justify-content: center;
I made it work without flexbox but isnt responsive and i need to learn more flexbox.
This is the result using flexbox:
Can someone help? if needed, this is the website: http://svidro.recifeimage.com/
-you should have one <div> and inside it your <button>
-<div> should have this style
display: flex;
justify-content: center;
-this is because the content of tag that has
justify-content: center;
will center only it's content
- in this case you will have <div> and it's content is <button>
You could try using relative positioning combined with CSS transforms instead of flexbox to center the button:
position: relative;
/* moves button to the middle of its container */
left: 50%;
/* moves button 50% of its width to the left */
transform: translateX(-50%);