I have a header which has an img for the logo (position: fixed) and a grid which is centered using margins, which contains whatever except for the logo is in the header. The HTML looks something like this:
<div id='header'>
<img id='logo' src='http://i65.tinypic.com/dw2nw9.png'>
<div id='header-grid'>
<div id='item-one'></div>
<div id='item-two'></div>
<div id='item-three'></div>
<div id='item-four'></div>
</div>
</div>
And the CSS looks like this:
#header {
position: fixed;
width: 100%;
height: var(--header-height);
background-color: var(--main-color);
z-index: 3141592;
}
#logo {
position: fixed;
max-width: 100px;
max-height: var(--header-height);
padding-left: 10px;
}
#header-grid {
width: 900px;
margin: 0 auto;
height: var(--header-height);
display: grid;
grid-template-columns: auto auto auto auto;
}
Now, when I zoom in, the #header-grid goes over the #logo. I want them to stay next to each other instead of one going on top of the other
Found a grid solution for you:
#header {
position: fixed;
width: 100%;
height: var(--header-height);
background-color: var(--main-color);
z-index: 3141592;
display: grid;
/* add the following */
grid-template-columns: 1fr repeat(2, auto) 1fr; /* not sure about grid but you need the repeat so the second column can be centred */
justify-items: center;
}
#logo {
max-width: 100px;
max-height: var(--header-height);
margin-left: 10px; /* change this to margin as you shouldn't add padding to images */
margin-right: auto; /* add this to push to the left */
}
#header-grid {
margin: 0 auto;
height: var(--header-height);
display: grid;
grid-template-columns: auto auto auto auto;
grid-column-start: 2; /* add this so it sits in centre */
/* for demo */
width: 300px;
border: 1px solid red;
}
<div id='header'>
<img id='logo' src='http://i65.tinypic.com/dw2nw9.png'>
<div id='header-grid'>
<div id='item-one'></div>
<div id='item-two'></div>
<div id='item-three'></div>
<div id='item-four'></div>
</div>
</div>
Related
This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 1 year ago.
In my header section, I would like the image logo centered, but I also want to keep my "X" icon aligned to the left. How do I accomplish this? Centering the logo using grid and margin: auto; cause the icon to be centered, and left aligning the icon also left aligns the logo.
This is the desired outcome:
body{
margin: 0;
padding: 0;
}
#signin-page{
display: grid;
place-items: center;
height: 100vh;
background-color: grey;
}
.signin-form{
display: grid;
grid-template-rows: 14% 75% auto;
height: 90vh;
width: 50vw;
margin: auto;
border-radius: 15px;
background-color:white;
}
.header{
display: grid;
grid-template-columns: 20% auto;
}
.bird-logo{
}
.input-pt1{
border: black solid 1px;
overflow-y: scroll;
}
.bottom{
}
i{
margin: 20px 20px;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons#1.7.2/font/bootstrap-icons.css">
<div id="signin-page">
<div class="signin-form">
<div class="header">
<i class="bi bi-x"></i>
<img src="" alt="bird" height="40px" width="40px" id="bird-logo"/>
</div>
<form class="input-pt1">
part one
</form>
<div class="bottom"></div>
</div>
</div>
There are several solutions for that issue. If you want the simpler, you can adjust your .header
body {
margin: 0;
padding: 0;
}
#signin-page {
display: grid;
place-items: center;
height: 100vh;
background-color: grey;
}
.signin-form {
display: grid;
grid-template-rows: 14% 75% auto;
height: 90vh;
width: 50vw;
margin: auto;
border-radius: 15px;
background-color: white;
}
.header {
display: grid;
grid-template-columns: 20% auto 20%;
place-items: center;
}
.bird-logo {}
.input-pt1 {
border: black solid 1px;
overflow-y: scroll;
}
.bottom {}
i {
margin: 20px 20px;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons#1.7.2/font/bootstrap-icons.css">
<div id="signin-page">
<div class="signin-form">
<div class="header">
<i class="bi bi-x-lg"></i>
<i class="bi bi-twitter"></i>
</div>
<form class="input-pt1">
part one
</form>
<div class="bottom"></div>
</div>
</div>
to this:
.header {
display: grid;
grid-template-columns: 20% auto 20%;
place-items: center;
}
I need to setup the following DIV structure (See image below. It tells more than a 1000 words)
The structure consists of 2 colums. The main column (left) has a variable width and 100% height.
The right colums has a FIXED width of 380px and 100% height.
Then inside the right column I need 3 DIVS.
The top DIV has a fixed height of 200px and must be aligned to the top.
The bottom DIV has a fixed height of 150px and must be aligned to the bottom.
The middle DIV has a variable height and must fill up the space vertically.
This is the DIV setup And the CSS I have:
.main-content {
width: 100%;
padding: 0px;
}
.col-1 {
width: calc(100% - 380px);
min-height: calc(var(--vh, 1vh)*100);
background-color: #2693FF;
float: left;
}
.col-2 {
width: 380px;
min-height: calc(var(--vh, 1vh)*100);
float: right;
}
.col-2-top {
height: 200px;
background-color: #00B200;
}
.col-2-middle {
height: 100%;
background-color: #FF8000;
}
.col-2-bottom {
height: 100px;
background-color: #B25900;
}
<div class="main-content">
<div class="col-1"></div>
<div class="col-2">
<div class="col-2-top"></div>
<div class="col-2-middle"></div>
<div class="col-2-bottom"></div>
</div>
</div>
Then... Column 1 and 2 should stack when the viewport width becomes less than 768px.
Column 1 on top and Column 2 below it.
Like this:
I think I'm almost there, but I'm having problems with the height of the Main DIV and the heights and aligning of the DIV col-2 middle DIV. I also need a bit helpt to get these divs stack nicely above each each other.
I would suggest that you use grid layout instead of floating around your <div>s, grid layout allows you to structure your layout and separate them in columns and rows, and areas using grid-template-areas.
for max-width:748 just add media query, here is how it might be implemented:
body {
padding: 0;
margin: 0;
}
.main-content {
display: grid;
background-color: #2196F3;
grid-template-areas:
'main fixed-top'
'main variable-mid-area'
'main fixed-bottom';
background-color: #2196F3;
height: 100vh;
grid-template-columns: 1fr 380px;
grid-template-rows: 200px 1fr 150px;
}
.main-content > div {
color: #fff;
font-size: 30px;
vertical-align: middle;
display: flex;
justify-content: center;
align-items: center;
}
.main {
grid-area: main;
background-color: #2693FC;
}
.variable-mid-area {
grid-area: variable-mid-area;
background-color: #FF8015;
}
.fixed-top {
grid-area: fixed-top;
background-color:#00B21F;
}
.fixed-bottom {
grid-area: fixed-bottom;
background-color: #B2590B;
}
#media only screen and (max-width: 768px) {
.main-content {
grid-template-areas:
'main'
'fixed-top'
'variable-mid-area'
'fixed-bottom';
grid-template-rows: 300px 200px 1fr 150px;
grid-template-columns: 100%;
height: auto;
}
}
<div class="main-content">
<div class="main"> main </div>
<div class="fixed-top"> 200 </div>
<div class="variable-mid-area"> auto </div>
<div class="fixed-bottom"> 150 </div>
</div>
If you have any questions how the css works, feel free to ask them in the comments.
I know the background-colors are irrelevant but they help to visualize it.
.container {
min-width: 768px;
width: 100%;
display: grid;
grid-template-columns: calc(100% - 380px) 1fr;
min-height: 100vh;
}
.col1 {
background-color: dodgerblue;
}
.col2 {
display: flex;
flex-direction: column;
}
.col2-row1 {
height: 200px;
background-color: orange;
}
.col2-row2 {
background-color: forestgreen;
height: 100%;
}
.col2-row3 {
height: 150px;
background-color: red;
}
#media (max-width: 768px) {
.container {
grid-template-columns: 1fr;
}
}
<div class="container">
<div class="col1">1</div>
<div class="col2">
<div class="col2-row1">2</div>
<div class="col2-row2">3</div>
<div class="col2-row3">4</div>
</div>
</div>
I'm learning CSS Grid layout and i have a problem about positioning.
What i want is to create a page layout composed by a left-side menu, top-bar menu and a main content page like the image below:
I have been able to achieve the goal, but now i want to fix the position of the top bar and sidebar while main content is scrolling.
I set position:sticky to both containers but it does not working.
How can i fix?
Here is my code:
* {
margin: 0 !important;
padding: 0 !important;
box-sizing: border-box !important;
}
.grid-container {
display: grid;
grid-template-columns: auto 1fr;
grid-template-rows: 10% 100vh;
grid-template-areas:
"LeftMenu TopMenu"
"LeftMenu Main";
}
.LeftMenu {
background-color: #a4a4a4;
grid-area: LeftMenu;
width: 200px;
height: 100%;
}
.TopMenu {
background-color: #d49494;
grid-area: TopMenu;
width: 100%;
height: 100%;
display: flex;
}
.Main {
background-color: #8990eb;
grid-area: Main;
width: 100%;
height: 100vh;
}
<div class="xdg-component-appnav-menu">
<div class="grid-container">
<div class="LeftMenu">left menu</div>
<div class="TopMenu">top menu</div>
<div class="Main">
<p style="padding-bottom: 1000px;">Content</p>
</div>
</div>
</div>
You don't need position: sticky. It's extra complication and still isn't fully supported by some browsers.
Just use overflow: auto on the main container.
.grid-container {
display: grid;
height: 100vh;
grid-template-columns: 200px 1fr;
grid-template-rows: 10% 90%;
grid-template-areas:
"LeftMenu TopMenu"
" LeftMenu Main ";
}
.LeftMenu {
grid-area: LeftMenu;
background-color: #a4a4a4;
}
.TopMenu {
grid-area: TopMenu;
background-color: #d49494;
}
.Main {
grid-area: Main;
overflow: auto; /* key adjustment */
background-color: #8990eb;
}
body {
margin: 0;
}
<div class="xdg-component-appnav-menu">
<div class="grid-container">
<div class="LeftMenu">left menu</div>
<div class="TopMenu">top menu</div>
<div class="Main">
<p style="height: 1000px;">Content</p>
</div>
</div>
</div>
im making a html css website however the high of the second div isnt equal with the first div
here is the code :
html part : here is have two divs the main image div and thirdDiv div
<div class="container">
<div class="header">
<div class="mainImage">
<img src="https://images.pexels.com/photos/3377538/pexels-photo-3377538.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940">
</div>
<div class="ThirdDiv">
<div class="firstImage">
<img src="https://images.pexels.com/photos/3377538/pexels-photo-3377538.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940">
</div>
<div class="secondImage">
<img src="https://images.pexels.com/photos/3377538/pexels-photo-3377538.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940">
</div>
</div>
</div>
</div>
css part :
.mainImage {
height: 100%;
width: 100%;
}
.mainImage img {
max-height:100% ;
width: 100%;
}
.header {
display: grid;
grid-template-columns: 70% 30%;
height: 400px;
grid-row-gap :0.5rem;
grid-column-gap: 0.5rem;
margin-left: 1rem;
margin-right: 1rem;
}
.ThirdDiv {
display: grid;
grid-template-rows: 50% 50%;
grid-row-gap :0.5rem;
}
.firstImage {
height: 100%;
width: 100%;
}
.firstImage img {
max-height:100% ;
width: 100%;
}
.secondImage {
height: 100%;
width: 100%;
}
.secondImage img {
max-height:100% ;
width: 100%;
}
https://codepen.io/belscode/pen/gObWMwz
Replace .header and .ThirdDiv styles with the next ones:
.header {
display: grid;
grid-template-columns: 70% 30%;
height: 400px;
grid-row-gap :2%;
grid-column-gap: 0.5rem;
margin-left: 1rem;
margin-right: 1rem;
}
.ThirdDiv {
display: grid;
grid-template-rows: 49% 49%;
grid-row-gap :2%;
}
I'm trying to get a fixed footer in my html which I know can be done with position absolute, and setting the bottom. When I do this, when my content gets bigger than the container, it overflows over the footer.
I tried setting a fixed height for my content. This works, but when viewing on other resolutions, these numbers of course wont work anymore.
This is the HTML:
<div class="container">
<div class="top">
</div>
<div class="content>
</div>
<div class="footer>
</div>
</div>
This is the SCSS:
.container {
position: absolute;
min-height: 90vh;
max-height: 90vh;
width: 90%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
margin: auto;
}
.container, .top {
color: $primary;
width: 100%;
display: grid;
grid-template-columns: auto auto 1fr 250px;
grid-template-rows: auto 1fr;
border-bottom: 0.1rem solid $contrast;
padding: 30px;
align-items: baseline;
overflow: auto;
}
.container, .content{
position: relative;
padding: 30px;
font-size: 1.2rem;
}
.container, .footer {
position: absolute;
bottom:20px;
width: 100%;
}
I just can seem to figure it out. Also tried flex and grid...
Thanks for the help!