css flexbox 3 children div, middle div should not change - html

i have 3 div for navbar and using flexbox i want to keep my middle div at center no matter how much i put content at the right div or left div
:- right now the content inside left and right div is just text
but i want to put logo and other stuff
but the middle div keep moving as my right div keeps growing
here is the code
<div class="header__div">
<!-- Left Button -->
<div class="left__header">
a
</div>
<!-- Left Button End -->
<!-- middle button -->
<div class="middle__header">
a
</div>
<!-- middle button end -->
<!-- Right Logo -->
<div class="right__header">
a
</div>
<!-- Right Logo end -->
</div>

Flexbox is designed to have even spacing between it's children depending on how you justify the content. If one side is bigger the centre child will move to keep even spacing between the children.
If you want to ensure that the centre child is always in the centre, I would suggest using grid as this will ensure they have their own columns and they stick to them.
.header__div {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}

Try doing this in CSS Grid:
.header__div{
display: grid;
grid-template-columns: repeat(3, 1fr);
}

One way to do this with flexbox is have 3 divs inside the header and give them all a flex value of 1 (You can change the proportions as you add content). Make the nested div in the center have "justify-content: center" and then you can place any other tags in the divs on the right and left of the center.
.header__div {
display: flex;
width: 100%
}
.div__1 {
flex: 1;
}
.div__2 { // Logo goes in this one
flex: 1;
justify-content: center;
align-items: center;
}
.div_3 {
flex: 1;
}

Related

How do I get HTML text-align center and right on the same line (without using float) [duplicate]

This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 2 years ago.
Right now I have
<p style="text-align: center">Hello world</p>
<p style="text-align: right">Hello world</p>
which gives me
Hello world
Hello world
I'd like them to be on the same line. I've tried using float such as
<div style="float: left">This is on the left </div><div style="float: right">This is on the right</div>
But they both end up on the left on top of each other
This is on the left
This is on the right
Is there a way to get them aligned where I want on the same line?
#dgknca you can use flex
.container {
display: flex;
justify-content: flex-end;
}
.item-center {
margin: auto;
}
<div class="container">
<div class="item-center">Item center</div>
<div>Item right</div>
</div>
You can use CSS Grid to divide a parent element evenly into columns and then flow content into each grid cell.
.grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}
.center {
text-align: center;
}
.right {
text-align: right;
}
<div class='grid'>
<p class='left'>This is on the left </p>
<p class='center'>This is in the center</p>
<p class='right'>This is on the right</p>
</div>
Following your first example, you have "center" and "right" so I'm inferring from that what you want, the rest of OP isn't quite clear to me.
The tricky part is actually having one be perfectly center, and one be on the right, and nothing on the left. Usually for stuff like this I'd use flexbox and put an extra container on the left to get the centering to work.
Here it is - just have your container be flex. Then set items to grow using 0 as basis (this is important so content width does not shift the centering and you get 3 equal width containers regardless of content). Then just wrap the one you want on the right in another div and justify-content: flex-end to get it on the right.
That's the best I know off the top - maybe some better ways...
body { display: flex; }
body div { display: flex; flex: 1 1 0 }
body .f-right { justify-content: flex-end }
/* center line/decoration */
.center-line {
position: absolute;
left: 50%;
transform: translateX(-50%);
height: 100%;
width: 1px;
background: blue;
opacity: .3
}
body { margin: 0 }
<div></div>
<p>Hai from center</p>
<div class="f-right">
<p>Hai from right</p>
</div>
<!-- just showing center -->
<div class="center-line"></div>
For this to work you have to put them both in the same p element. Using two separate p elements will cause them to be on different lines. The example code you gave works because both the left and right side are in the same block element. Separating them into two block elements will cause them to stack on each other.

CSS Grid layout with columns used as indentation borders not working as expected [duplicate]

This question already has an answer here:
Why aren't my grid-template-areas with ASCII art not working?
(1 answer)
Closed 2 years ago.
I am creating a basic CSS Grid for my website. Right now, it has 3 rows. One for the logo and nav, another for a banner image, and a third for text content.
The grid has a total of 6 columns, two of which are a set size (25px) and I use them as borders to indent the content contained within certain css grid rows. As you can see in the attached images, the top-nav (logo+navigation) display as expected and are slightly indented using the 'left' and 'right' CSS grid border columns.
After that, the banner image appears on a new css grid row as expected, and takes up all 6 columns (no left or right border columns used).
Finally, the third row, that has text in it should be spanning over 4 columns, with the left and right css grid columns displaying. That is where everything turns into a mess.
Instead, the third row overlays the banner on the second row, does not follow the rule of using the left and right grid columns for indenting, and the navigation row (logo+nav), disappears.
I provide the code here, and also include a Codepen, along with pictures to illustrate the problem.
I believe the problem has to do with those left and right border columns because if I have the grid row spread across all 6 columns, the text displays properly.
Can you help me figure out what I am doing wrong?
Maybe there is a better approach to achieve the border without the extra columns?
Thank you!
Codepen
[https://codepen.io/BillRaymond/pen/vYOJEZY][1]
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="grid.css">
<title>Document</title>
</head>
<body>
<div class="container">
<div class="left"></div>
<div class="right"></div>
<div class="logo"><img src="https://via.placeholder.com/250x35?text=Logo" alt="" class="logo-image"></div>
<div class="nav">HOME ABOUT BLOG LINKEDIN TWITTER </div>
<div class="banner"><img src="https://via.placeholder.com/1366x500?text=banner" alt="" class="banner-image"></div>
<div class="title">This text should be below the banner with a 'left' and 'right' border. The logo and navigation
bar do not show.</div>
</div>
</body>
</html>
CSS/SCSS
// full size grid
.container {
margin: auto;
display: grid;
max-width: 1366px;
grid-template-areas:
"left logo nav nav nav right"
"banner banner banner banner banner banner"
"left title title title title right";
// ^^ comment the above line and add a semi-colon to the line above that. The logo and nav will appear again.
grid-template-columns: 25px auto auto auto auto 25px;
grid-template-rows: auto auto auto;
} // end full-size container
.left {
display: grid;
grid-area: left;
}
.right {
display: grid;
grid-area: right;
}
.logo {
display: grid;
grid-area: logo;
}
.nav {
display: grid;
grid-area: nav;
justify-content: end;
}
.banner {
display: grid;
grid-area: banner;
}
.title {
display: title;
grid-area: title;
}
Grid areas must be rectangles. You cannot spread a grid-area like you're trying to. Instead rename the areas left and right (and properly assign them to their respective CSS declarations):
grid-template-areas:
"leftTop logo nav nav nav rightTop"
"banner banner banner banner banner banner"
"leftBottom title title title title rightBottom";

Control size of images in nested grid layouts

I'm trying to get into the grid layout system and I'm really struggling with image-sizes inside grid containers. I just want to create a simple page with a navbar. The navbar should contain a logo, icons and text. It is divided into three parts:
A left part, containing the logo (aligned to the left side)
A center part, containing a title (aligned to the center)
A right part, containing an image and text (aligned to the right side)
Because I want to work with grid whenever possible my planned structure looks like this: https://codepen.io/Nicolas_V/pen/QWbvxoW
HTML:
<div class="site">
<div class="navbar">
<div class="navbar__area--left">
<div>Logo</div>
</div>
<div class="navbar__area--center">
<div>Admin Page</div>
</div>
<div class="navbar__area--right">
<div>Text</div>
<div>Image</div>
</div>
</div>
<div class="content">Content</div>
</div>
CSS:
html, body{
margin: 0;
padding: 0;
}
.site{
display: grid;
grid-template-rows: 1fr 9fr;
height: 100vh;
}
.navbar{
display: grid;
grid-template-columns: 1fr 1fr 1fr;
align-items: center;
background-color: green;
}
.navbar > div > *{
display: inline;
margin-right: 2rem;
margin-left: 2rem;
}
.navbar__area--left{
text-align: left;
}
.navbar__area--center{
text-align: center;
}
.navbar__area--right{
text-align: right;
}
I created three containers for each part, so I can align their contents separately. So far so good.
Now I added a test image to the left part and expected it to fit to the containers height. But it doesn't.
https://codepen.io/Nicolas_V/pen/XWbRYLR
What i don't understand is, that if I REMOVE the enclosing container from the left side, the image perfectly fits to the height of the navbar as expected previously.
https://codepen.io/Nicolas_V/pen/rNVmrNm
But I need to have this container, because for the right part I want multiple items in the container, all aligned to the right side.
I know, that I can set fixed heights for images and so on, but I want to dig into the grid system and I'm sure there is a way to solve my problem.

How to make child content vertically in center as respect to parent container in bootstrap 4

I have been trying to make my child content vertically centered as per the parent container. But the child container is always in relative to parent container. Below is the code I have tried:
HTML
<section id="welcome" class="bg-primary d-flex flex-column">
<h1>Positions</h1>
<div class="container text-center my-auto">
Centered content
</div>
</section>
CSS
#welcome{
min-height:150px;
width: 200px;
}
What I am looking is to make Centered Content text vertically centered as per the whole section.
Here is the codepen link: CodePen
Just make h1 position-absolute to remove it from relative positioning within the DOM...
https://codepen.io/anon/pen/EeVXbe
<section id="welcome" class="bg-primary d-flex flex-column">
<h1 class="position-absolute">Positions</h1>
<div class="container text-center my-auto">
Centered content
</div>
</section>
Your h1 element is what's causing it to mis-align. If you get rid of it (just to test) you'll see centered content move to the center.
display: flex; applies to all of that elements direct children, so you're aligning to aggregate height of both the h1 and the #welcome element. To have Centered Content in the center of a tall square, you could apply some of the styles you have on #welcome to .my-auto:
.my-auto {
min-height: 150px;
display: flex;
align-items: center;
justify-content: center;
background: blue;
}
align-items is what does vertical alignment when using display: flex. However, if you use flex-direction: column, the direction is "rotated", so you would use justify-content: center, which is normally for horizontal alignment.
A different approach is to use padding on top and bottom of the content you want to center:
.my-auto {
padding: 50px 0; /* 50px is an arbitrary number, just to demonstrate */
}
Note that this will cause centered content to push all other elements vertically away from it.

Bootstrap grid with variable width columns

Is there a way in Boostrap to make grid columns adjust according to the width of the viewport? I have the following page, where the first column is col-md-3 and the second col-md-9:
The right hand column is supposed to show a list of files when the user selects a folder in the left hand column. Yet the column widths stay fixed, and if I reduce the screen width just the tiny bit, the right-hand column is moved to below the left-hand column. The left hand column then fills the height and width of the screen, and the file list column is invisble below it, making for a very bad UX.
Is there some way, preferably in Bootstrap, to have the columns adjust their width in proportion to the screen width, or is there some alternative to Bootstrap with a grid that can work like that?
You can simply achieve this using css grid.
body{
margin:0;
}
.container{
display:grid;
grid-template: 100vh / repeat(12, 1fr);
color:#fff;
}
.container > div{
display: flex;
justify-content: center;
align-items:center;
font-size: 2em;
}
.left-panel{
grid-column: 1 / 4;
background: #F7A072;
}
.right-panel{
grid-column: 4 / -1;
background: #0FA3B1;
}
<body>
<div class="container">
<div class="left-panel">Left</div>
<div class="right-panel">Right</div>
</div>
</body>