This question already has answers here:
How can I position my div at the bottom of its container?
(25 answers)
Closed 10 months ago.
I want to keep the button at bottom previously which I was making it possible by writing more text to push it to bottom but it wasn't responsive for every device any way I can keep the button in a div at bottom?
:root {
--clr-primary: #651fff;
--clr-gray: #37474f;
--clr-gray-light: #b0bec5;
}
* {
box-sizing: border-box;
font-family: "Open Sans", sans-serif;
margin: 0;
padding: 0;
}
body {
color: var(--clr-gray);
margin: 2rem;
}
.wrapper-grid {
display: grid;
grid-template-columns: repeat(auto-fit, 20rem);
justify-content: center;
}
.container {
overflow: hidden;
box-shadow: 0px 2px 8px 0px var(--clr-gray-light);
background-color: white;
text-align: center;
border-radius: 1rem;
position: relative;
margin: 2rem 0.5rem;
}
.banner-img {
position: absolute;
background-image: url(https://gaito.000webhostapp.com/im/istockphoto-1307289824-640x640.jpg);
height: 10rem;
width: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.profile-img {
width: 8rem;
clip-path: circle(60px at center);
margin-top: 4.5rem;
height: 8rem;
}
.name {
font-weight: bold;
font-size: 1.5rem;
}
.description {
margin: 1rem 2rem;
font-size: 0.9rem;
}
.btn {
width: 100%;
border: none;
font-size: 1rem;
font-weight: bold;
color: white;
padding: 1rem;
background-color: var(--clr-primary);
}
<div class="wrapper-grid">
<div class="container">
<div class='banner-img'></div>
<img src='https://i.pinimg.com/originals/49/09/f9/4909f9e82c492b1e4d52c2bcd9daaf97.jpg' class="profile-img">
<h1 class="name">Slime</h1>
<p class="description">Slimes also commonly called ooze are common types of</p>
<button class='btn'>Attack this dungeon </button>
</div>
<div class="container">
<div class='banner-img'></div>
<img src='https://static.wikia.nocookie.net/kumo-desu-ga-nani-ka/images/4/4c/Mother_1.png/' alt='profile image' class="profile-img">
<h1 class="name">Gaint spider</h1>
<p class="description">This creature shoots sticky strands of webbing from its abdomen which are most commonly found underground, making their lairs on ceilings or in dark, web-filled crevices.</p>
<button class='btn'>Attack this dungeon </button>
</div>
</div>
Codepen
My solution would be:
.container {
display: flex;
flex-flow: column nowrap; /* Flex everything inside your cards vertically */
}
.description {
flex-grow: 1;
/*
When a card has more space (because another card is taller
with more info) - grow the description
*/
}
Here is a working snippet, click Full page top right to see it working:
:root {
--clr-primary: #651fff;
--clr-gray: #37474f;
--clr-gray-light: #b0bec5;
}
* {
box-sizing: border-box;
font-family: "Open Sans", sans-serif;
margin: 0;
padding: 0;
}
body {
color: var(--clr-gray);
margin: 2rem;
}
.wrapper-grid {
display: grid;
grid-template-columns: repeat(auto-fit, 20rem);
justify-content: center;
}
.container {
overflow: hidden;
display: flex;
flex-flow: column nowrap;
align-items: center;
box-shadow: 0px 2px 8px 0px var(--clr-gray-light);
background-color: white;
text-align: center;
border-radius: 1rem;
position: relative;
margin: 2rem 0.5rem;
}
.banner-img {
position: absolute;
background-image: url(https://gaito.000webhostapp.com/im/istockphoto-1307289824-640x640.jpg);
height: 10rem;
width: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.profile-img {
width: 8rem;
clip-path: circle(60px at center);
margin-top: 4.5rem;
height: 8rem;
}
.name {
font-weight: bold;
font-size: 1.5rem;
}
.description {
flex-grow: 1;
margin: 1rem 2rem;
font-size: 0.9rem;
}
.btn {
width: 100%;
border: none;
font-size: 1rem;
font-weight: bold;
color: white;
padding: 1rem;
background-color: var(--clr-primary);
}
<div class="wrapper-grid">
<div class="container">
<div class='banner-img'></div>
<img src='https://i.pinimg.com/originals/49/09/f9/4909f9e82c492b1e4d52c2bcd9daaf97.jpg' class="profile-img">
<h1 class="name">Slime</h1>
<p class="description">Slimes also commonly called ooze are common types of</p>
<button class='btn'>Attack this dungeon </button>
</div>
<div class="container">
<div class='banner-img'></div>
<img src='https://static.wikia.nocookie.net/kumo-desu-ga-nani-ka/images/4/4c/Mother_1.png/' alt='profile image' class="profile-img">
<h1 class="name">Gaint spider</h1>
<p class="description">This creature shoots sticky strands of webbing from its abdomen which are most commonly found underground, making their lairs on ceilings or in dark, web-filled crevices.</p>
<button class='btn'>Attack this dungeon </button>
</div>
</div>
Related
I'm trying to figure out how to make it look like this (the white box at the end of both sides is basically the continuation of the search-box):
I tried different ways to make it equally long on both left and right sides (to center it under my title) but unfortunately it always resulted in failure. The left side didn't move and the right side just became wider.
I'm looking as well for a way to add some space between the arrow icon and the right border of the search-box. I tried adding some padding but it didn't work.
.help-container {
padding: 5rem 0;
display: grid;
place-content: center;
background-color: #dadbf1;
}
h1 {
font-size: 5rem;
font-weight: 500;
padding-bottom: 2rem;
}
.search {
width: 100%;
position: relative;
display: flex;
}
.searchTerm {
width: 100%;
border: 1px solid #000000;
padding: 5px;
height: 45px;
border-radius: 0.25rem;
outline: none;
color: #9dbfaf;
font-size: 1rem;
background-image: url("https://svgur.com/i/qJh.svg");
background-repeat: no-repeat;
background-position: right center;
background-size: 30px;
}
<section>
<div class="help-container">
<div class="help">
<h1>How can we help?</h1>
<div class="search">
<input type="text" class="searchTerm" placeholder="Search" />
</div>
</div>
</div>
</section>
https://codepen.io/686579/pen/zYJYPWB
You are trying to make the search bigger than the nodes/elements that contain it.
Make sure to set the width of the containing components to something bigger.
To center using display: flex;, you can set a container to use flex, and then use margin: auto; in the child element to automatically center with the containing-flexed-element.
Here is updated html that gives a class to each container. Notice I added class="search-background" so I can give that element a width.
example.html
<section>
<div class="help-container">
<div class="help">
<h1>How can we help?</h1>
<div class="search-background" style="background-color: white;">
<div class="search">
<input type="text" class="searchTerm" placeholder="Search" />
</div>
</div>
</div>
</div>
</section>
Here is updated css with display flex on all containing elements (and flex direction of column so that things are displayed vertically, display flex defaults to horizontal)
Containers have display: flex;, centered items have margin: auto;
You can customize the actual widths of each element to your desire. Use margin to adjust the element right or left. margin "auto" just tells it to fill the difference between the parent and child element's width.
example.css
.help-container {
padding: 5rem 0;
display: grid;
place-content: center;
background-color: #dadbf1;
width: 100%;
display: flex;
border: 1px solid green;
}
.help {
width: 100%;
display: flex;
flex-direction: column;
border: 1px solid red;
}
h1 {
font-size: 5rem;
font-weight: 500;
padding-bottom: 2rem;
width: fit-content;
margin: auto;
}
.search-background {
width: 80%;
margin: auto;
display: flex;
flex-direction: column;
}
.search {
position: relative;
display: flex;
width: 100%;
margin: auto;
}
.searchTerm {
width: 100%;
border: 1px solid #000000;
padding: 5px;
height: 45px;
border-radius: 0.25rem;
outline: none;
color: #9dbfaf;
font-size: 1rem;
background-image: url("https://svgur.com/i/qJh.svg");
background-repeat: no-repeat;
background-position: right center;
background-size: 30px;
}
Here's a screenshot of it on my machine. The red border is only there to help visualize the container needed to be adjusted.
You may add this rule to the .search container:
.search::before {
position: absolute;
width: calc(100% - (2 * 3em));
height: var(--height);
padding: var(--padding);
content: '';
left: 50%;
transform: translateX(-50%);
background: white;
z-index: 1;
}
So that you have a pseudo element bound to the <input> container that will be positioned absolute while having width: 100% minus an arbitrary amount being your padding.
I also zeroed the padding/margin on html, body and used custom variables to hold the padding and height you are using on your input so that it will be replicated on the pseudo element.
This long route was required to have a separated element to style with different criteria using css alone.
html, body{
padding: 0;
margin: 0;
}
.help-container {
padding: 5rem 0;
display: grid;
place-content: center;
background-color: #dadbf1;
}
h1 {
font-size: 5rem;
font-weight: 500;
padding-bottom: 2rem;
}
.search {
--height: 45px;
--padding: 5px;
width: 100%;
/*position: relative;*/
display: flex;
}
.searchTerm {
height: var(--height);
padding: var(--padding);
z-index: 2;
width: 100%;
border: 1px solid #000000;
border-radius: 0.25rem;
outline: none;
color: #9dbfaf;
font-size: 1rem;
background-image: url("https://svgur.com/i/qJh.svg");
background-repeat: no-repeat;
background-position: right center;
background-size: 30px;
}
.search::before {
position: absolute;
width: calc(100% - (2 * 3em));
height: var(--height);
padding: var(--padding);
content: '';
left: 50%;
transform: translateX(-50%);
background: white;
z-index: 1;
}
<section>
<div class="help-container">
<div class="help">
<h1>How can we help?</h1>
<div class="search">
<input type="text" class="searchTerm" placeholder="Search" />
</div>
</div>
</div>
</section>
I added a div before searchbar with an inline CSS to let you see what I added, and deleted width: 100% in searchbar. That is why it was not centered
.help-container {
padding: 5rem 0;
display: grid;
place-content: center;
background-color: #dadbf1;
}
h1 {
font-size: 5rem;
font-weight: 500;
padding-bottom: 2rem;
}
.search {
padding: 0 50px;
position: relative;
display: flex;
}
.searchTerm {
width: 100%;
border: 1px solid #000000;
padding: 5px;
height: 45px;
border-radius: 0.25rem;
outline: none;
color: #9dbfaf;
font-size: 1rem;
background-image: url("https://svgur.com/i/qJh.svg");
background-repeat: no-repeat;
background-position: right center;
background-size: 30px;
}
<section>
<div class="help-container">
<div class="help">
<h1>How can we help?</h1>
<div style="background-color: white;">
<div class="search">
<input type="text" class="searchTerm" placeholder="Search" />
</div>
</div>
</div>
</div>
</section>
You can change your input width for 80% instead 100% and use justify-content to center the input and for adding a white space in your bg-input try using percentages instead right. If you want to know more about it check how to use background-position.
.help-container {
padding: 5rem 0;
display: grid;
place-content: center;
background-color: #dadbf1;
}
h1 {
font-size: 5rem;
font-weight: 500;
padding-bottom: 2rem;
}
.search {
width: 100%;
position: relative;
display: flex;
background-color: white;
justify-content: center;
}
.searchTerm {
width: 80%;
border: 1px solid #000000;
padding: 5px;
height: 45px;
border-radius: 0.25rem;
outline: none;
color: #9dbfaf;
font-size: 1rem;
background-image: url('https://svgur.com/i/qJh.svg');
background-repeat: no-repeat;
background-position: 95% center;
background-size: 30px;
}
<section>
<div class="help-container">
<div class="help">
<h1>How can we help?</h1>
<div class="search">
<input type="text" class="searchTerm" placeholder="Search" />
</div>
</div>
</div>
</section>
There's a challenge that i wanna solv to enhance on my front-end skills, but in this set of challenge i've been stuggling on trying to fit the image in the entire div box of its parent?!? Also i've been trying to set cart icon next to the icon and it's not going too well.
Here's the challenge that i need solve : -
As you can the image and icon is not really close to the picture above.
Here is the code :
* {
box-sizing: border-box;
margin: 0;
padding: 10px;
}
:root {
--font1: "Fraunces", sans-serif;
--font2: "Montserrat", sans-serif;
--fs-paragraph: 14px;
--fs-header1: 40px;
--primaryColor1: hsl(158, 36%, 37%);
--primaryColor2: hsl(30, 38%, 92%);
--neutralColor1: hsl(212, 21%, 14%);
--neutralColor2: hsl(228, 12%, 48%);
--neutralColor3: hsl(0, 0%, 100%);
}
body {
background-color: var(--primaryColor2);
display: flex;
flex-direction: column;
min-height: 100vh;
}
.container {
display: flex;
flex-direction: row;
justify-content: space-around;
margin: auto;
background-color: var(--neutralColor3);
width: 600px;
height: 100%;
border-radius: 1.5em;
}
.img-box {
margin: 0;
padding: 0;
}
.img-box img {
width: 100%;
height: 100%;
}
.details {
display: flex;
flex-direction: column;
}
.details h2 {
font-family: var(--font2);
font-weight: normal;
letter-spacing: 2px;
text-transform: uppercase;
font-size: var(--fs-paragraph);
}
.brand-title p {
font-family: var(--font1);
font-size: var(--fs-header1);
font-weight: bolder;
}
.brand-desc {}
.brand-desc p {
font-family: var(--font2);
font-size: var(--fs-paragraph);
color: var(--neutralColor2);
line-height: 2em;
}
.prices {
display: flex;
align-items: center;
}
.prices h1 {
font-family: var(--font1);
color: var(--primaryColor1);
}
.prices h4 {
font-family: var(--font2);
color: var(--neutralColor2);
text-decoration: line-through;
}
.addcart {
display: flex;
justify-content: center;
align-items: center;
}
.addcart button {
background: url(/images/icon-cart.svg) no-repeat left center;
padding-left: 20px;
background-color: var(--primaryColor1);
font-family: var(--font2);
font-weight: 600;
}
button {
border: none;
border-radius: 0.5em;
color: var(--neutralColor3);
width: 100%;
height: 50px;
}
.attribution {
font-size: 11px;
text-align: center;
}
.attribution a {
color: hsl(228, 45%, 44%);
}
<link href="https://fonts.googleapis.com/css2?family=Fraunces:opsz#9..144&family=Montserrat&family=Outfit&display=swap" rel="stylesheet" />
<div class="container">
<div class="img-box">
<img src="/images/image-product-desktop.jpg" alt="" />
</div>
<div class="details">
<div class="brand-title">
<h2>Perfume</h2>
<p>Gabrielle Essence Eau De Parfum</p>
</div>
<div class="brand-desc">
<p>
A floral, solar and voluptuous interpretation composed by Olivier Polge, Perfumer-Creator for the House of CHANEL.
</p>
</div>
<div class="prices">
<h1>$149.99</h1>
<h4>$169.99</h4>
</div>
<div class="addcart">
<span class="icon"></span>
<button>Add to Cart</button>
</div>
</div>
</div>
<div class="attribution">
Challenge by
<a href="https://www.frontendmentor.io?ref=challenge" target="_blank">Frontend Mentor</a
>. Coded by yes.
</div>
I did try to change the size of the image but it doesnt show the result that i want, for the icon i've been playing the position but it does not goes next to text of a button.
I would suggest to use display: grid; in your .container. In the image that you say is your challenge, the image takes half of the container. You can achieve that by creating two <div> inside your container, and then:
.container {
display: grid;
grid-template-columns: 1fr 1fr;
/*More properties*/
}
This will set each div to span half of your container. Your first div should have:
.your-div{
background-image: url('your-image-path')
background-size: cover;
background-position: center;
}
That way you can achieve the left side of your challenge. I would need more information about your problem with the right side. Hope this helped!
*Note: This is just one way of doing this. If you have to strictly use flex, play around with its properties.
As suggested By #aleyo742, grid is here your friend, but you will also need to reset some of your paddings, object-fit can be use too.
* {
box-sizing: border-box;
margin: 0;
padding: 10px;
}
:root {
--font1: "Fraunces", sans-serif;
--font2: "Montserrat", sans-serif;
--fs-paragraph: 14px;
--fs-header1: 40px;
--primaryColor1: hsl(158, 36%, 37%);
--primaryColor2: hsl(30, 38%, 92%);
--neutralColor1: hsl(212, 21%, 14%);
--neutralColor2: hsl(228, 12%, 48%);
--neutralColor3: hsl(0, 0%, 100%);
}
body {
background-color: var(--primaryColor2);
display: flex;
flex-direction: column;
min-height: 100vh;
}
.container {
display: grid;
grid-template-columns: 1fr 1fr;
padding: 0;
overflow: hidden;
margin: auto;
background-color: var(--neutralColor3);
max-width: 600px;
border-radius: 1.5em;
}
/* here comes why grid is your handy friend */
#media(max-width: 600px) {
.container {
grid-template-columns: 1fr;
grid-template-rows: 250px auto;
}
}
.img-box {
margin: 0;
padding: 0;
}
.img-box img {
width: 100%;
height: 100%;
padding: 0;
object-fit: cover;
}
.details {
display: flex;
flex-direction: column;
}
.details h2 {
font-family: var(--font2);
font-weight: normal;
letter-spacing: 2px;
text-transform: uppercase;
font-size: var(--fs-paragraph);
}
.brand-title p {
font-family: var(--font1);
font-size: var(--fs-header1);
font-weight: bolder;
}
.brand-desc {}
.brand-desc p {
font-family: var(--font2);
font-size: var(--fs-paragraph);
color: var(--neutralColor2);
line-height: 2em;
}
.prices {
display: flex;
align-items: center;
}
.prices h1 {
font-family: var(--font1);
color: var(--primaryColor1);
}
.prices h4 {
font-family: var(--font2);
color: var(--neutralColor2);
text-decoration: line-through;
}
.addcart {
display: flex;
justify-content: center;
align-items: center;
}
.addcart button {
background: url(/images/icon-cart.svg) no-repeat left center;
padding-left: 20px;
background-color: var(--primaryColor1);
font-family: var(--font2);
font-weight: 600;
}
button {
border: none;
border-radius: 0.5em;
color: var(--neutralColor3);
width: 100%;
height: 50px;
}
.attribution {
font-size: 11px;
text-align: center;
}
.attribution a {
color: hsl(228, 45%, 44%);
}
<link href="https://fonts.googleapis.com/css2?family=Fraunces:opsz#9..144&family=Montserrat&family=Outfit&display=swap" rel="stylesheet" />
<div class="container">
<div class="img-box">
<img src="https://i.stack.imgur.com/vloym.jpg" alt="" />
</div>
<div class="details">
<div class="brand-title">
<h2>Perfume</h2>
<p>Gabrielle Essence Eau De Parfum</p>
</div>
<div class="brand-desc">
<p>
A floral, solar and voluptuous interpretation composed by Olivier Polge, Perfumer-Creator for the House of CHANEL.
</p>
</div>
<div class="prices">
<h1>$149.99</h1>
<h4>$169.99</h4>
</div>
<div class="addcart">
<span class="icon"></span>
<button>Add to Cart</button>
</div>
</div>
</div>
<div class="attribution">
Challenge by
<a href="https://www.frontendmentor.io?ref=challenge" target="_blank">Frontend Mentor</a
>. Coded by yes.
</div>
Here is the JSfiddle complete code link:
CODE
my clock code output
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
background: #0b172a;
font-family: sans-serif;
}
#container {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
padding: 5rem;
color: white;
text-transform: uppercase;
}
.clock-ctr {
position: relative;
border: 2px solid white;
height: 80vh;
width: 80vw;
padding: 10px;
display: flex;
justify-content: center;
align-items: center;
}
.hour-ctr {
grid-area: hour;
}
.min-ctr {
grid-area: min;
}
.sec-ctr {
grid-area: sec;
}
.ampm {
grid-area: ampm;
background: #bc4123;
}
.time-ctr {
position: absolute;
height: 70%;
width: 90%;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: auto;
gap: 10px;
grid-template-areas: "hour min sec" "ampm ampm ampm";
}
h1 {
margin-bottom: 1rem;
letter-spacing: 8px;
font-size: 2.5rem;
}
.time-box {
background: #bc4123;
border-radius: 10px;
display: grid;
justify-items: center;
align-items: center;
height: auto;
text-align: center;
font-weight: bold;
font-size: 28px;
}
<div id="container">
<h1 class="clock-title">Clock</h1>
<div class="clock-ctr">
<div class="time-ctr">
<div class="hour-ctr time-box">
<p class="hour-value">00</p>
<p class="hour-title">Hour</p>
</div>
<div class="min-ctr time-box">
<p class="min-value">00</p>
<p class="min-title">Minute</p>
</div>
<div class="sec-ctr time-box">
<p class="sec-value">00</p>
<p class="sec-title">Second</p>
</div>
<p class="ampm time-box">AM</p>
</div>
</div>
</div>
Can any one tell me how to improve this code
I tried to make is completely responsive but it is not not working,
I tired to use flex to make the element appear in center of page.
Then I use grid to create the clock layout and i didn't knew how to align the cells so I used grid again in them. I was using rem and em to make responsive code but it didn't work out well. please review my code.
This is because of the font-size of the time-box div that is not responsive (28px whatever the device size), To make it responsive I added media queries to change the font depending on the device width, As presented in this example:
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
background: #0b172a;
font-family: sans-serif;
}
#container {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
padding: 5rem;
color: white;
text-transform: uppercase;
}
.clock-ctr {
position: relative;
border: 2px solid white;
height: 80vh;
width: 80vw;
padding: 10px;
display: flex;
justify-content: center;
align-items: center;
}
.hour-ctr {
grid-area: hour;
}
.min-ctr {
grid-area: min;
}
.sec-ctr {
grid-area: sec;
}
.ampm {
grid-area: ampm;
background: #bc4123;
}
.time-ctr {
position: absolute;
height: 70%;
width: 90%;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: auto;
gap: 10px;
grid-template-areas: "hour min sec" "ampm ampm ampm";
}
h1 {
margin-bottom: 1rem;
letter-spacing: 8px;
font-size: 2.5rem;
}
.time-box {
background: #bc4123;
border-radius: 10px;
display: grid;
justify-items: center;
align-items: center;
height: auto;
text-align: center;
font-weight: bold;
}
#media (min-width:768px) {
.time-box {
font-size: 18px;
}
}
#media (min-width:1024px) {
.time-box {
font-size: 22px;
}
}
#media (min-width:1280px) {
.time-box {
font-size: 28px;
}
}
<div id="container">
<h1 class="clock-title">Clock</h1>
<div class="clock-ctr">
<div class="time-ctr">
<div class="hour-ctr time-box">
<p class="hour-value">00</p>
<p class="hour-title">Hour</p>
</div>
<div class="min-ctr time-box">
<p class="min-value">00</p>
<p class="min-title">Minute</p>
</div>
<div class="sec-ctr time-box">
<p class="sec-value">00</p>
<p class="sec-title">Second</p>
</div>
<p class="ampm time-box">AM</p>
</div>
</div>
</div>
You can as well use calc() function, so you can calculate your font size relative to the screen width like this:
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
background: #0b172a;
font-family: sans-serif;
}
#container {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
padding: 5rem;
color: white;
text-transform: uppercase;
}
.clock-ctr {
position: relative;
border: 2px solid white;
height: 80vh;
width: 80vw;
padding: 10px;
display: flex;
justify-content: center;
align-items: center;
}
.hour-ctr {
grid-area: hour;
}
.min-ctr {
grid-area: min;
}
.sec-ctr {
grid-area: sec;
}
.ampm {
grid-area: ampm;
background: #bc4123;
}
.time-ctr {
position: absolute;
height: 70%;
width: 90%;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: auto;
gap: 10px;
grid-template-areas: "hour min sec" "ampm ampm ampm";
}
h1 {
margin-bottom: 1rem;
letter-spacing: 8px;
font-size: 2.5rem;
}
.time-box {
background: #bc4123;
border-radius: 10px;
display: grid;
justify-items: center;
align-items: center;
height: auto;
text-align: center;
font-weight: bold;
font-size: calc(18px + 0.390625vw);
}
<div id="container">
<h1 class="clock-title">Clock</h1>
<div class="clock-ctr">
<div class="time-ctr">
<div class="hour-ctr time-box">
<p class="hour-value">00</p>
<p class="hour-title">Hour</p>
</div>
<div class="min-ctr time-box">
<p class="min-value">00</p>
<p class="min-title">Minute</p>
</div>
<div class="sec-ctr time-box">
<p class="sec-value">00</p>
<p class="sec-title">Second</p>
</div>
<p class="ampm time-box">AM</p>
</div>
</div>
</div>
the issue with the CSS code in the Stack Overflow question is that the left and right values for the #nav element are set to 0. This causes the element to take up the full width of its parent element, which is likely not the intended behavior.
To fix this issue, you can try setting the left and right values to auto like this:
#nav {
position: fixed;
top: 0;
left: auto;
right: auto;
width: 100%;
height: 60px;
background-color: white;
box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2);
z-index: 1000;
}
With this change, the #nav element will no longer take up the full width of its parent element and will instead be positioned at the top of the page with its width set to 100%.
I have a Popular News section where I'm trying to display six news articles in a flex pattern. The problem I'm having is that I cannot get the flex items to be closer together. How do I do that?
EDIT: I've added the entire code for the bottom half.
This is how it currently looks:
This is how I want it to look:
.firstsection {
width: 100%;
height: 100;
}
.firstsection h1 {
color: rgb(255, 255, 255);
display: block;
font-size: 36px;
font-weight: 300;
line-height: 42.0001px;
padding-bottom: 14px;
text-align: center;
}
.firstsection {
display: block;
width: 100%;
height: 400px;
background-color: #414141;
float: left;
}
.bottomheader {
margin-top: 40px;
color: white;
font-size: 40px;
position: relative;
top: -20px;
}
.READMORE {
display: inline;
position: relative;
top: -40px;
left: 642px;
font-size: 16px;
text-align: center;
border: 1px solid white;
height: 50px;
padding: 20px;
}
.pop .READMORE a {
color: white;
text-decoration: none;
}
.h1,
.h2,
.h3,
.h4,
.h5,
.h6 {
color: black;
width: 30%;
border-top: 3px solid red;
background-color: white;
height: 80px;
}
.firstsection {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
}
.pop {
float: right;
height: 100px;
width: 100%;
text-align: center;
}
<section style="background-color: #293352" class="pop">
<h1 class="bottomheader">Popular News</h1>
<h4 class="READMORE">READ MORE</h4>
</section>
<section style="background-color: #293352" class="firstsection">
<h3 class="h1">content</h3>
<h3 class="h2">content</h3>
<h3 class="h3">content</h3>
<h3 class="h4">content</h3>
<h3 class="h5">content</h3>
<h3 class="h6">content</h3>
</section>
All you really need to do is add align-content: flex-start. This aligns wrapped lines to the start of the flex container and has similar options as align-items. See align-content
I created a fiddle
.firstsection {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
align-content: flex-start;
}
In this example I changed the 'cards' to divs and used a card class, and added a little padding. This may or may not be what you want but maybe it helps.
You can use the CSS GRID to align them properly.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.firstsection {
display: block;
width: 100%;
background-color: #293352;
padding: 50px 20px;
}
h3 {
color: black;
width: 100%;
border-top: 3px solid red;
background-color: white;
height: 130px;
margin: 0;
}
.firstsection {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
<section class="firstsection">
<h3>content</h3>
<h3>content</h3>
<h3>content</h3>
<h3>content</h3>
<h3>content</h3>
<h3>content</h3>
</section>
Codepen: https://codepen.io/manaskhandelwal1/pen/XWjobLx
here is another approach with grid , using pseudo elements to shrink the visible rows to the center, and sizing a few elements via width and max-width:
example below to run in fullpage mode then resize the window to see if the behavior is what you expect.
body {
margin: 0;
background-color: #293352;
}
.grid {
display: grid;
grid-template-rows: auto 1fr;
min-height: 100vh;
}
.pop {
color: white;
text-align: center;
position: relative;
padding: 1em 10em;
}
.pop h4 {
position: absolute;
right: 1rem;
top: 0.5rem;
border: solid 1px;
padding: 1em;
}
section.firstsection {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: 1fr repeat(auto-fill, max-content) 1fr;
align-items: center;
width: max-content;
max-width:100%;
margin: auto;
gap: 1em;
}
.firstsection:before,
.firstsection:after {
content: "";
grid-column: span 3;
}
.firstsection > * {
box-sizing: border-box;
min-width: 26%;
background: white;
height: 100%;
padding: 1em;
border-top: solid red;
max-width:30vw;
}
<div class="grid">
<section class="pop">
<h1 class="bottomheader">Popular News</h1>
<h4 class="READMORE">READ MORE</h4>
</section>
<section class="firstsection">
<h3 class="h1">content</h3>
<h3 class="h2">content</h3>
<h3 class="h3">content</h3>
<h3 class="h4">content <br> and an extra line</h3>
<h3 class="h5">content</h3>
<h3 class="h6">content or grow the column to my width if there's enough room.</h3>
</section>
</div>
here is a codepen with a grid of 9 boxes to play with : https://codepen.io/gc-nomade/pen/dypwYvY
This question already has answers here:
Understanding z-index stacking order
(1 answer)
Why can't an element with a z-index value cover its child?
(5 answers)
Closed 3 years ago.
attempting to make a simple layout and have hit a dead end. I am trying to make a page that perfectly fits the screen such that there is no scrolling what-so-ever. Basically, in the included code, I'd like to have the reddish title bar (at the top) display on top of the yellowish container. The height of the yellowish container is set to 100vh so as to span the height of the viewport. In this way, the page will be perfectly sized such that you will never need to scroll.
I think it has to do with the z-index...which I thought I understood up until this point. I've watched videos, read articles, and tried everything I could think of. My last resort is trying my luck online.
body,
html {
height: 100%;
margin: 0;
}
.bg {
background-color: rgb(171, 171, 175);
}
header h1 {
text-align: center;
position: relative;
margin: 0;
padding-top: 0.8rem;
background-color: coral;
}
.flex-container {
margin: 0 auto;
display: flex;
flex-direction: column;
justify-content: center;
}
.content-box {
border: solid 6px #e7c022;
border-radius: 0.8rem;
height: 45%;
background-color: rgba(128, 128, 128, 0.7);
}
.main-container {
position: relative;
z-index: 1;
height: 100vh;
width: 55vw;
max-width: 700px;
background-color: burlywood;
}
.code-container {
height: 80%;
align-items: center;
}
.key-container {
height: 20%;
align-items: center;
}
.key-code {
font-size: 20rem;
font-family: 'Yellowtail', cursive;
}
.key {
height: 30%;
width: 20%;
border: solid 4px #e7c022;
border-radius: 0.5rem;
text-align: center;
margin-bottom: 3rem;
font-size: 40px;
font-family: 'Share Tech Mono', monospace;
}
.key div {
margin-bottom: 0.2rem;
}
<div class="bg">
<header>
<h1>Titlebar</h1>
</header>
<div class="flex-container main-container">
<div class="content-box">
<div class="flex-container code-container">
<div class="key-code">
<span>65</span>
</div>
</div>
<div class="flex-container key-container">
<div class="flex-container key">
<div>a</div>
</div>
</div>
</div>
</div>
</div>
Do this if you want you navbar to always stick at the top.
body,
html {
height: 100%;
margin: 0;
}
.bg {
background-color: rgb(171, 171, 175);
}
header {
z-index: 11;
position: fixed;
width:100%
}
header h1 {
text-align: center;
position: relative;
margin: 0;
padding-top: 0.8rem;
background-color: coral;
}
.flex-container {
margin: 0 auto;
display: flex;
flex-direction: column;
justify-content: center;
}
.content-box {
border: solid 6px #e7c022;
border-radius: 0.8rem;
height: 45%;
background-color: rgba(128, 128, 128, 0.7);
}
.main-container {
position: relative;
z-index: 1;
height: 100vh;
width: 55vw;
max-width: 700px;
background-color: burlywood;
}
.code-container {
height: 80%;
align-items: center;
}
.key-container {
height: 20%;
align-items: center;
}
.key-code {
font-size: 20rem;
font-family: 'Yellowtail', cursive;
}
.key {
height: 30%;
width: 20%;
border: solid 4px #e7c022;
border-radius: 0.5rem;
text-align: center;
margin-bottom: 3rem;
font-size: 40px;
font-family: 'Share Tech Mono', monospace;
}
.key div {
margin-bottom: 0.2rem;
}
<div class="bg">
<header>
<h1>Titlebar</h1>
</header>
<div class="flex-container main-container">
<div class="content-box">
<div class="flex-container code-container">
<div class="key-code">
<span>65</span>
</div>
</div>
<div class="flex-container key-container">
<div class="flex-container key">
<div>a</div>
</div>
</div>
</div>
</div>
</div>
Welcome to SO!
Using position relative with z-index solved the issue on header
When you set position: relative on an element then you establish a new
containing block. All positioning inside that block is with respect to
it.
Setting z-index on an element inside that block will only alter its
layer with respect to other elements inside the same block.
body,
html {
height: 100%;
margin: 0;
}
.bg {
background-color: rgb(171, 171, 175);
}
header {
z-index: 11;
position: relative;
}
header h1 {
text-align: center;
position: relative;
margin: 0;
padding-top: 0.8rem;
background-color: coral;
}
.flex-container {
margin: 0 auto;
display: flex;
flex-direction: column;
justify-content: center;
}
.content-box {
border: solid 6px #e7c022;
border-radius: 0.8rem;
height: 45%;
background-color: rgba(128, 128, 128, 0.7);
}
.main-container {
position: relative;
z-index: 1;
height: 100vh;
width: 55vw;
max-width: 700px;
background-color: burlywood;
}
.code-container {
height: 80%;
align-items: center;
}
.key-container {
height: 20%;
align-items: center;
}
.key-code {
font-size: 20rem;
font-family: 'Yellowtail', cursive;
}
.key {
height: 30%;
width: 20%;
border: solid 4px #e7c022;
border-radius: 0.5rem;
text-align: center;
margin-bottom: 3rem;
font-size: 40px;
font-family: 'Share Tech Mono', monospace;
}
.key div {
margin-bottom: 0.2rem;
}
<div class="bg">
<header>
<h1>Titlebar</h1>
</header>
<div class="flex-container main-container">
<div class="content-box">
<div class="flex-container code-container">
<div class="key-code">
<span>65</span>
</div>
</div>
<div class="flex-container key-container">
<div class="flex-container key">
<div>a</div>
</div>
</div>
</div>
</div>
</div>
You need to set z-index for header. Ex:
header{
position: relative;
z-index: 9999
}
If you need your header should stick at the top of the screen. add positioning. ex:
header{
position: fixed;
top: 0;
width: 100%;
z-index: 9999
}