Why won't this second item center in the div? - html

HTML
<div id="flexbox-container">
<h1 id="test1">test1</h1><h1 id="test2">test2</h1><h1 id="test3">test3</h1>
</div>
CSS
#flexbox-container {
display:inline-flex;
}
#test1 {
float:left;
}
#test2 {
justify-content:center;
align-items:center;
text-align:center;
align-self:center;
align-content:center;
}
#test3 {
position:relative;
left:1000px;
}
Why does test2 not center itself in the flex? I would prefer not to have to set px or margin to get it to centre. I tried all sorts of aligning stuff on it yet it still sticks to the left. I need the three items to be inline, so setting it to flex wouldn't work (though it does center align if I make it flex), PLEASE HELP IVE BEEN TRYING FOR DAYS
https://codepen.io/throwaway123/pen/mdpJJKY

Only this much code is enough. No need for all those styles for separate h1 tags. You have to give the aligning styles to the parent div.
#flexbox-container {
width: 100%;
display:inline-flex;
justify-content: space-between;
}
<div id="flexbox-container">
<h1 id="test1">test1</h1>
<h1 id="test2">test2</h1>
<h1 id="test3">test3</h1>
</div>

Basically that isn't how flex works.
You don't want the contents of the second item to be justified within itself, you want the container to have that element centered.
If you scrap all the positioning of the three items you can get flex to do the work for you. There are several ways of telling it how you want the items set out in the line. For example justify-content: space-between.
From MDN:
The items are evenly distributed within the alignment container along the main axis. The spacing between each pair of adjacent items is the same. The first item is flush with the main-start edge, and the last item is flush with the main-end edge.
#flexbox-container {
display: inline-flex;
justify-content: space-between;
width: 100vw;
}
<div id="flexbox-container">
<h1 id="test1">test1</h1>
<h1 id="test2">test2</h1>
<h1 id="test3">test3</h1>
</div>

Using IDs for css is bad practice. I'd suggest you to start using class selectors
Anyway, here is solution to your problem :
<style>
#flexbox-container {
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
}
</style>

If you want the h1 tags centered too you can wrap the h1 tag by a div. Then you can assign the div text-align: center CSS Property.
#flexbox-container {
background: green;
display:flex;
justify-content: space-between;
text-align: center;
}
#flexbox-container div {
width: 100%;
}
<div id="flexbox-container">
<div>
<h1 id="test1">test1</h1>
</div>
<div>
<h1 id="test2">test2</h1>
</div>
<div>
<h1 id="test3">test3</h1>
</div>
</div>

Related

Text is not being centered

I want to center all the text in BodyAbout.js. Text-align isn't working nor is padding-left of left. I'm not repeating the class name and thats I can think is reason its not working.
function BodyAbout() {
return (
<div className='Body__About'>
<div className='Body__Header'>
<h2>About</h2>
</div>
<div className='Body__Paragraph'>
<p>Founded in 2010, we are a creative agency that</p>
<p>produces lasting results for our clients. We’ve</p>
<p>partnered with many startups, corporations, and</p>
<p>nonprofits alike to craft designs that make real</p>
<p>impact. We’re always looking forward to creating</p>
<p>brands, products, and digital experiences that</p>
<p>connect with our clients' audiences.</p>
</div>
</div>
BodyAbout CSS
.Body__About {
background-color: #e7816b;
height: 30em;
text-align: center;
}
BodyAbout is the component
function About() {
return (
<div>
<Navbar />
<AboutUsImage />
<BodyAbout/>
<WorldClassImg />
<MidPageDescAboutDesign Title='World-class talent'/>
<LandmarkImages Circle='/images/bg-pattern-small-circle.svg'/>
<RealDeal />
<MidPageDescAboutDesign Title='The real deal'/>
<AboveFooter />
<Footer />
</div>
)
}
text-align will only center the element's inline contents, it does not center the element itself.
If it is a block element (a div), you need to set margin: 0 auto;
What it does
The margin: 0 auto; will set top and bottom margin to 0 and left and right margin to auto, this will automatically put the elements content in the center. This only works if the block element in question has a known width (fixed or relative), otherwise it may not work.
If it is an inline-element, set text-align: center; on its parent element instead.
Alternatively you could just target the elements children in css, something like this may work:
.Body__About .Body__Paragraph p{
text-align: center;
}
You may not need the p tag but I included it just in case, that will center align all the p elements inside Body_Paragraph
p is commonly a block-level element and may have text-align: left assigned to it from another CSS. Try:
.Body__About {
background-color: #e7816b;
height: 30em;
}
.Body__Paragraph p {
text-align: center;
}
Try these
.Body__About {
display: flex;
justify-content:center;
}
Try adding these three style properties to the container
.Body__About{
display: flex;
align-items: center;
flex-direction: column;
}
For vertical and horizontal alignment, please use the below code
.Body__About {
display: flex;
align-items: center;
justify-content: center;
}

Using flex, one element centrally aligned and others spaced between the centre and right

I'm having a bit of trouble to produce the below with flex box. I'd like a centrally aligned "title" with some buttons to the right (2,3,4).
The code below gets me close, but it's not perfectly aligned and loses it when the window resizes.
Any suggestions?
.header {
display: flex;
height: 50px;
align-items: center;
justify-content: center;
}
.title {
width: 250px;
margin-left: auto;
margin-right: 15%;
}
.btn-group {
margin-right: 15%;
}
<div class="header">
<h1 class="title"></h1>
<div class="btn-group">
<button id="btn_1" class="selected">2</button>
<button id="btn_2">3</button>
<button id="btn_3">4</button>
</div>
</div>
Here's a clean and simple process to get you to your layout:
First, note that CSS pseudo-elements (i.e., ::before and ::after), when applied to flex containers, are treated as flex items.
Create a pseudo-element to serve as the first flex item in the container.
Make the pseudo consume all available space (i.e., set it to flex: 1)
Do the same with your button group (.btn-group) on the opposite end (i.e., set it to flex: 1)
Now, with the outer items pressuring from both sides, the title is pinned to the middle of the container.
Make the button group container a flex container.
Set that container to justify-content: center.
Now, the individual buttons are horizontally centered on the right side of the already centered title.
.header {
display: flex;
height: 50px;
align-items: center;
}
.header::before {
content: "";
flex: 1;
}
.btn-group {
flex: 1;
display: flex;
justify-content: center;
}
<div class="header">
<h1 class="title">1</h1>
<div class="btn-group">
<button id="btn_1" class="selected">2</button>
<button id="btn_2">3</button>
<button id="btn_3">4</button>
</div>
</div>
To better understand the concepts and methodology at work here, see this post:
Center and right align flexbox elements
Here are my suggestions when using flexbox layout. You do not need to set the width on the element because the width will resize dynamically. When you set display as flex in the container, the x-axis would change to row by default then use flex property for 'title' class to expand the width to double the width of 'btn-group'. As the result, the second div will push all the way to the right and you can add the width of margin-right as how much you want it to be. Also, I would create another div after header and give it a class name as 'title' instead of giving it on h1. That way you would have two children that allow you to control it. See below how I fixed it:
h1 {
text-align: center;
}
.header {
border: 1px solid red;
display: flex;
align-items: center;
justify-content: center;
}
.title {
flex: 1;
}
<div class="header">
<div class="title">
<h1>This is a title</h1>
</div>
<div class="btn-group">
<button id="btn_1" class="selected">2</button>
<button id="btn_2">3</button>
<button id="btn_3">4</button>
</div>
</div>

Flexbox item within css grid cell appears to wrap

I'm new to using css grid, and I am trying to position two items within a single grid cell using flexbox to position the items. There is a logo on the left and a nav bar on the right, but the nav bar is not centered within cell "a", it appears to go below the lower boundary of cell "a" (I tried to upload a jpeg image, but Stack Overflow is having problems right now accepting image uploads, see Image upload fails with "imgur is rejecting the request").
Here is the html code:
<div class="a">
<div class="a_left">
Logo for Project
</div>
<div class="a_right">
<div class="topnav">
<a class="active" href="#home">Home</a>
News
Contact
About
</div>
</div>
</div>
Here is the css code:
.a{
display: grid;
font-family: sans-serif;
color: green;
font-size: 16pt;
}
.a_left{
display: flex;
text-align: left;
vertical-align: left;
flex-wrap: nowrap;
}
.a_right{
display: flex;
height: 100%;
flex-wrap: nowrap;
justify-content: right;
vertical-align: right;
}
.topnav {
align-content: right;
justify-content: center;
overflow: hidden;
background-color: #333;
height: 100%
}
The grid container is "a", and the nav bar is in the a_right flexbox. I have tried a lot of the likely height, width and centering properties without success, but I don't know if the property should be applied to a or to a_right.
Thanks for any help centering this nav bar.
If you want to have the logo on the left and menu on the right, you can simply do:
.a {
display: flex;
justify-content: space-between;
}
<div class="a">
<div class="a_left">
Logo for Project
</div>
<div class="a_right">
<div class="topnav">
<a class="active" href="#home">Home</a>
News
Contact
About
</div>
</div>
</div>
Flexbox is a container for centering its children - right now it looks like you're making the children into flexboxes, which won't do you a whole lot.
If you need to maintain the "grid" display property, add a container for both your logo and nav bar. This is the container that you will want with display:flex, and it will be the container that you apply your flex-related alignment properties to.
Also, to vertically align content in a flexbox, use align-items. Horizontal alignment requires justify-content.
So you set your element .a to be a grid container:
.a {
display: grid;
}
… and you want to know why the children (.a_left and .a-right) are stacking vertically instead of on the same row.
The reason is that you haven't defined explicit tracks with grid-template-columns or grid-template-areas. Because you haven't defined explicit columns, grid-auto-columns comes into play to create implicit columns.
The default value of grid-auto-columns is auto, which essentially allows each grid item to occupy an entire row. That's what you're seeing; it's like block elements stacking.
Try this instead:
.a {
display: grid;
grid-template-columns: auto 1fr; /* define explicit columns */
color: green;
font-size: 16pt;
}
.a_left {
display: flex;
}
.a_right {
display: flex;
justify-content: center;
}
.topnav {
background-color: yellow;
}
<div class="a">
<div class="a_left">Logo for Project</div>
<div class="a_right">
<div class="topnav">
<a class="active" href="#home">Home</a>
News
Contact
About
</div>
</div>
You may also want to read this post about centering elements on a row having other elements: Center and right align flexbox elements

html center align vertically not working

I am having this simple issue here, which worked before at a lot of places.
I am trying to align items inside a div vertically and at the center. Here in this code the margin-left works, but the margin top doesn't, I tried changing it to bigger values, still no effect at all.
.footer {
background-color: #2E7FB6;
color:white;
height:50px;
}
<div class="footer">
<section style="margin-left:15px; margin-top:10px;">FETCHED: {{ recordsFetched }} Work Order(s)</section>
</div>
Remove the inline styling, use flexbox with flex-direction: column; justify-content: center; and text-align center; on the footer.
.footer {
background-color: #2E7FB6;
color:white;
height:50px;
text-align: center;
display: flex;
flex-direction: column;
justify-content: center;
}
<div class="footer">
<section>FETCHED: {{ recordsFetched }} Work Order(s)</section>
</div>
Use flexbox tips just #rprm192 say. But if you want to make it more simple and support older browser, you can use line-height. Here's code for you
.footer {
height: 50px;
}
.footer section {
height: 100%;
line-height: 50px; //make it same as height value
}

align-self property in flexbox is not working?

I am trying to understand flex box:
I want to make the “first” block stretched to match the full width of the browser, and the ”second” block of fixed size and aligned to left.
So I used align-items: flex-end in the parent(<html>) and tried to stretch the first block using align-self: stretch in the ”first” block.
This is not working. Both of them are aligned to the left.
<html>
<head>
<style>
html {
display: flex;
flex-direction: column;
justify-content: flex-start;
align-items: flex-end;
}
#first {
align-self:stretch;
background-color: red;
border: 3px solid black;
}
#second {
background-color:green;
width: 300px;
height: 400px;
border: 3px solid yellow;
}
</style>
</head>
<body>
<div id="first">This is first</div>
<div id="second">This is second</div>
</body>
</html>
So the problem is that you've got the <html> node as a flex container, and the <body> node (its child) is the one and only flex item.
Currently, align-self:stretch on #first is ignored, because that property only applies to flex items, and #first is not a flex item (the way you have things set up right now).
To get what you want, you need to make the <body> node a flex container, not the <html> node. So, just change your first style rule to apply to body{ instead of html{
(Also, if you want #second to be aligned to the left, you need flex-start, not flex-end. The left edge is the flex-start horizontal edge, generally.)
Add flex:1 to #first
Remove justify-content: flex-start; and align-items: flex-end; from html
See example: http://codepen.io/anon/pen/vifIr