Heading elements appearing below content - html

I can't position the items correctly:
1 - The titles "App name", "Title 1" and "Title 2" (h1 tags) are located below the cells and not above.
2- The vertical spaces between the div are very large
I tried "playing" using the properties in several elements:
align-items
justify-item
align-self
justify-self
margin
padding
My code:
.grid-container {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-template-areas: "title1""title2""title3";
background-color: crimson;
grid-column-gap: 15px;
grid-row-gap: 15px;
}
.title1 {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
grid-template-areas: "title1-subtitle1 title1-subtitle2""title1-subtitle3 title1-subtitle4";
grid-area: title1;
background-color: yellow;
grid-column-gap: 15px;
grid-row-gap: 15px;
width: 95%;
align-items: center;
justify-items: center;
align-self: center;
justify-self: center;
margin: auto;
}
.title1>div {
background-color: orange;
}
.title1 div>h2,
p {
display: flex;
justify-content: center;
align-items: center;
}
.title1-subtitle1 {
grid-area: title1-subtitle1;
}
.title1-subtitle2 {
grid-area: title1-subtitle2;
}
.title1-subtitle3 {
grid-area: title1-subtitle3;
}
.title1-subtitle4 {
grid-area: title1-subtitle4;
}
.title2 {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr;
grid-template-areas: "title2-subtitle1 title2-subtitle2";
grid-area: title2;
background-color: green;
grid-column-gap: 15px;
grid-row-gap: 15px;
width: 95%;
align-items: center;
justify-items: center;
align-self: center;
justify-self: center;
margin: auto;
}
.title2>div {
background-color: lightblue;
}
.title2 div>h2,
p {
display: flex;
justify-content: center;
align-items: center;
}
.title2-subtitle1 {
grid-area: title2-subtitle1;
}
.title2-subtitle2 {
grid-area: title2-subtitle2;
}
.title3 {
grid-area: title3;
background-color: violet;
grid-column-gap: 15px;
grid-row-gap: 15px;
width: 95%;
align-items: center;
justify-items: center;
align-self: center;
justify-self: center;
margin: auto;
}
.title3>h1,
p {
display: flex;
justify-content: center;
align-items: center;
}
<div class="grid-container">
<h1>App name</h1>
<div class="title1">
<h1>Title 1</h1>
<div class="title1-subtitle1">
<h2>Title 1 - Subtitle 1</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Alias, nihil ducimus pariatur dolore voluptas repellendus et sed, debitis sit tempore nam blanditiis, accusantium aliquam iusto inventore officia aperiam quos repudiandae?
</p>
</div>
<div class="title1-subtitle2">
<h2>Title 1 - Subtitle 2</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Alias, nihil ducimus pariatur dolore voluptas repellendus et sed, debitis sit tempore nam blanditiis, accusantium aliquam iusto inventore officia aperiam quos repudiandae? Lorem ipsum, dolor
sit amet consectetur adipisicing elit. Eaque, odit aspernatur ea dolores dicta nisi, unde ut dolorem omnis iste blanditiis saepe, dolorum rerum quos consequuntur temporibus veritatis voluptatum accusantium? Lorem ipsum dolor sit amet consectetur
adipisicing elit. Quod qui, incidunt voluptas aperiam corrupti molestiae aut nostrum voluptatem ratione totam dicta, distinctio expedita quaerat magni repellendus aliquid, animi sapiente quo.
</p>
</div>
<div class="title1-subtitle3">
<h2>Title 1 - Subtitle 3</h2>
<p>Lorem
</p>
</div>
<div class="title1-subtitle4">
<h2>Title 1 - Subtitle 4</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Alias, nihil ducimus pariatur dolore voluptas repellendus et sed, debitis sit tempore nam blanditiis, accusantium aliquam iusto inventore officia aperiam quos repudiandae?
</p>
</div>
</div>
<div class="title2">
<h1>Title 2</h1>
<div class="title2-subtitle1">
<h2>Title 2 - Subtitle 1</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Alias, nihil ducimus pariatur dolore voluptas repellendus et sed, debitis sit tempore nam blanditiis, accusantium aliquam iusto inventore officia aperiam quos repudiandae?
</p>
</div>
<div class="title2-subtitle2">
<h2>Title 2 - Subtitle 2</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Alias, nihil ducimus pariatur dolore voluptas repellendus et sed, debitis sit tempore nam blanditiis, accusantium aliquam iusto inventore officia aperiam quos repudiandae?
</p>
</div>
</div>
<div class="title3">
<h1>Title 3</h1>
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. A velit in aperiam, expedita nesciunt nemo ea pariatur explicabo laudantium quasi commodi sed laboriosam obcaecati, libero nobis voluptas? Aliquam, possimus. Nemo.
</p>
</div>
</div>
This is how I want it to look like:

The main problem with your title placement was that you didn't define a grid-area name for them, and then list it in grid-template-areas.
Once in a grid container, a heading (h1, h2, etc.) is just another grid item.
Because it was being ignored by grid-template-areas it was handled by the grid auto-placement algorithm, which was rendering it last.

How about moving the <h1>Title 1</h1> out of the div element? and while your at it, upload your code to jsfiddle or something, it's very convenient

Related

HTML Flex. Center elements

I need to center the elements of the column (like this:)
My actual code:
.education_flexColumn {
flex-direction: column;
display: flex;
align-items: center;
justify-content: center;
flex: 1;
flex-grow: 1;
}
.education_flexRow {
display: flex;
flex: 1;
justify-content: center;
align-items: stretch;
flex-grow: 1;
}
.divfh {
display: flex;
align-self: center;
flex: 1;
justify-content: center;
height: 100%;
flex-grow: 1;
}
<main>
<div class="education_flexRow">
<div class="education_flexColumn">
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Placeat voluptatum accusamus ipsum nulla, facere perspiciatis, quos quod, sint atque illum maiores iure. Excepturi harum quisquam iste ut esse ea non.
</div>
<div class="education_flexColumn">
<div class="divfh">Lorem ipsum dolor, sit amet consectetur adipisicing elit. Nesciunt id at dolore veniam illo ut et hic incidunt sit nostrum, laboriosam ipsa porro recusandae facere dolores. Laudantium amet placeat molestiae!</div>
<div class="divfh">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Minus, voluptatem reprehenderit explicabo magnam totam animi neque aliquam? Iure, minima aut ducimus placeat aliquam mollitia ad tempora rerum ex commodi sunt!</div>
</div>
</div>
</main>
On my actual result, the 2nd column content is not centered align.
You need an align-items: center; on the .divfh. The align-self does nothing since you want to align the children:
html, body, main, .education_flexRow {
height: 100%;
}
.education_flexColumn {
flex-direction: column;
display: flex;
align-items: center;
justify-content: center;
border-right: 1px solid black;
}
.education_flexColumn > .divfh:first-child {
border-bottom: 1px solid red;
}
.education_flexRow {
display: flex;
justify-content: center;
align-items: stretch;
border: 1px solid black;
}
.divfh {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
<main>
<div class="education_flexRow">
<div class="education_flexColumn">
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Placeat voluptatum accusamus ipsum nulla, facere perspiciatis, quos quod, sint atque illum maiores iure. Excepturi harum quisquam iste ut esse ea non.
</div>
<div class="education_flexColumn">
<div class="divfh">Lorem ipsum dolor, sit amet consectetur adipisicing elit. Nesciunt id at dolore veniam illo ut et hic incidunt sit nostrum, laboriosam ipsa porro recusandae facere dolores. Laudantium amet placeat molestiae!</div>
<div class="divfh">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Minus, voluptatem reprehenderit explicabo magnam totam animi neque aliquam? Iure, minima aut ducimus placeat aliquam mollitia ad tempora rerum ex commodi sunt!</div>
</div>
</div>
</main>
If you don't mind the opportunity to slightly change your approach, I would suggest you to use the grid template layout.
Your current layout has problems when reducing the box contents and they don't align correctly anymore if their width didn't grow because of their content too small to fit the whole remaining horizontal space.
In this demo I just flattened the html structure so that the second column doesn't need anymore to be its own parent element and gave to the first .area item the class wide so that it will span 2 rows.
The content of the boxes will be centered both on the horizontal and vertical axes because of display: flex.
.areas {
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-rows: 4rem; /*row min height*/
grid-gap: 5px; /*distance between boxes*/
}
.wide {
grid-row: span 2;
}
.area {
outline: solid 1px lightgray;
display: flex;
align-items: center; /*content centered vertically*/
justify-content: center; /*content centered horizontally*/
}
<main>
<div class="areas">
<div class="area wide">Lorem, ipsum dolor sit ame...</div>
<div class="area">Lorem ipsum dolotiae!...</div>
<div class="area">Lorem ipsum ex commodi sunt!...</div>
</div>
</main>

How to wrap a long text while first line of it remain at top but the other lines break

I've made a comment section used grid-box like this:
.grid {
display: grid;
align-items: center;
justify-items: center;
grid-template: 70px auto / 70px auto auto;
}
.profile-picture {
width: 42px;
height: 42px;
background-color: #aaa;
border-radius: 50%;
}
.long-text {
grid-column: 2 / -1;
grid-row: 2 / -1;
}
<div class="grid">
<div class="profile-picture"></div>
<span style="font-weight: 700;">Amogus</span>
<span class="long-text">Lorem ipsum dolor sit amet consectetur adipisicing elit. Sequi dolor est error aspernatur. Obcaecati fuga inventore ab cumque enim ipsam quisquam delectus ad, dolor numquam molestias necessitatibus ratione quis facere! Lorem ipsum dolor sit amet consectetur adipisicing elit. Sequi dolor est error aspernatur. Obcaecati fuga inventore ab cumque enim ipsam quisquam delectus ad, dolor numquam molestias necessitatibus ratione quis facere!</span>
</div>
The long text is either completely separated from the title and photo or is completely attached by switching between
.long-text {
/* separated one */
grid-column: 2 / -1;
grid-row: 2 / -1;
}
And
.comment-text {
/* attached one */
grid-column: 3 / -1;
grid-row: 1 / -1;
}
But I want something like this:
Wrap long text in paragraph element and place it in second row. Also you don't need to place long text in span, just move it's class to <p> tag.
.grid {
display: grid;
align-items: center;
justify-items: center;
grid-template: 70px auto / 70px auto auto;
}
.profile-picture {
width: 42px;
height: 42px;
background-color: #aaa;
border-radius: 50%;
}
.long-text {
grid-column: 2 / -1;
grid-row: 1 / -1;
}
<div class="grid">
<div class="profile-picture"></div>
<p class="long-text">
<span style="font-weight: 700;">Amogus</span>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Sequi dolor est error aspernatur. Obcaecati fuga inventore ab cumque enim ipsam quisquam delectus ad, dolor numquam molestias necessitatibus ratione quis facere! Lorem ipsum dolor sit amet consectetur adipisicing elit. Sequi dolor est error aspernatur. Obcaecati fuga inventore ab cumque enim ipsam quisquam delectus ad, dolor numquam molestias necessitatibus ratione quis facere!
</p>
</div>
I should've added a div or p element as a parent of two span tags (username, long-text) and it fixed :)

Is there a way to set my h1 to go on top of the paragraph because I've used display flex to set them vertically

As you can see I've used the display flex to vertically align my items, but I want the h1 to go on top of the paragraph.
[In the image you can see how the text are aligned in center.][1]
[1]: https://i.stack.imgur.com/YHj02.png
section{
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
color: white;
scroll-snap-align: start;
}
.one{
text-align: center;
}
<section id="about" class="one">
<h1>Lorem ipsum dolor sit amet consectetur.</h1>
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Placeat quo accusamus dolor, commodi repudiandae animi esse delectus ipsa <br>magni sed at expedita sint, beatae nulla? Adipisci quasi sint earum quo illo facere voluptatibus, praesentium est <br>neque tempore vitae quas aliquid molestias sit et ut, blanditiis <br>necessitatibus labore ducimus asperiores obcaecati!</p>
<img src="./img/undraw_New_message_re_fp03.png" alt="img">
</section>
Apply the styling flex-direction: column; to your section css selector like so:
section{
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
/* color: white; */
scroll-snap-align: start;
}
.one{
text-align: center;
}
<section id="about" class="one">
<h1>Lorem ipsum dolor sit amet consectetur.</h1>
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Placeat quo accusamus dolor, commodi repudiandae animi esse delectus ipsa <br>magni sed at expedita sint, beatae nulla? Adipisci quasi sint earum quo illo facere voluptatibus, praesentium est <br>neque tempore vitae quas aliquid molestias sit et ut, blanditiis <br>necessitatibus labore ducimus asperiores obcaecati!</p>
<img src="./img/undraw_New_message_re_fp03.png" alt="img">
</section>

empty space in the second fraction of grid-template-column in big screen layout

enter image description here
So, i am trying to create my layout for big screens (min-width:768px). Yet, when I set grid-template-columns as 1fr 1fr, there is empty space in the second column for both divs.
/* about */
.about-container{
padding: 3rem;
max-width: 90vw;
margin: 2rem auto;
display: grid;
grid-row-gap: 2rem;
}
#media screen and (min-width:768px){
.about{
display: grid;
grid-template-columns: 1fr 1fr;
}
}
.about_picture_1{
width: 100%;
display: block;
height: 100%;
border-radius: 0.25rem;
}
<div class="about-container">
<div class="about">
<div class="column_about_1">
<h2>Our Story</h2>
<p class="about_text">Lorem ipsum dolor sit amet consectetur adipisicing elit. Excepturi, minus. Animi adipisci perspiciatis, quae aspernatur fugiat odit incidunt officia nemo enim consequuntur quas cupiditate obcaecati hic, doloremque corrupti labore quidem?</p>
<div class="about_picture">
<img src="img/bravo.jpg" class="about_picture_1" alt="">
</div>
<div class="column_about_2">
<h2>Our Story</h2>
<p class="about_text">Lorem ipsum dolor sit amet consectetur adipisicing elit. Excepturi, minus. Animi adipisci perspiciatis, quae aspernatur fugiat odit incidunt officia nemo enim consequuntur quas cupiditate obcaecati hic, doloremque corrupti labore quidem?</p>
<img src="img/pizzafork.jpg" class="about_picture_1" alt="">
</div>
</div>
</div>
I think you should move the display:grid css rule in "about" class outside of media query and set the grid areas outside then for 768px and up for big screens set the grid template areas for 2 columns. Here's my sample css code that I made:
.about-container{
padding: 3rem;
max-width: 90vw;
margin: 2rem auto;
display: grid;
grid-row-gap: 2rem;
}
.about {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: repeat(2, 1fr);
grid-template-areas:
"col1 col1"
"col2 col2";
}
#media screen and (min-width:768px){
.about{
grid-template-areas:
"col1 col2"
"col1 col2";
}
}
.about_picture_1{
width: 100%;
display: block;
height: 100%;
border-radius: 0.25rem;
}
.column_about_1 {
grid-area: col1;
}
.column_about_2 {
grid-area: col2;
}
Here's your html structure that I updated/fixed.
<div class="about-container">
<div class="about">
<div class="column_about_1">
<h2>Our Story</h2>
<p class="about_text">Lorem ipsum dolor sit amet consectetur adipisicing elit. Excepturi, minus. Animi adipisci perspiciatis, quae aspernatur fugiat odit incidunt officia nemo enim consequuntur quas cupiditate obcaecati hic, doloremque corrupti labore quidem?</p>
<div class="about_picture">
<img src="img/bravo.jpg" class="about_picture_1" alt="">
</div>
</div>
<div class="column_about_2">
<h2>Our Story</h2>
<p class="about_text">Lorem ipsum dolor sit amet consectetur adipisicing elit. Excepturi, minus. Animi adipisci perspiciatis, quae aspernatur fugiat odit incidunt officia nemo enim consequuntur quas cupiditate obcaecati hic, doloremque corrupti labore quidem?</p>
<div>
<img src="img/pizzafork.jpg" class="about_picture_1" alt="">
</div>
</div>
</div>
</div>

CSS Grids and Fraction Unit

I'm trying to understand the basics of CSS grids but have a problem with fraction grid-template: repeat(4, 1fr) / repeat(3, 1fr); unit.
.grid-container {
display: grid;
grid-template: repeat(4, 1fr) / repeat(3, 1fr);
grid-gap: 20px;
grid-template-areas: 'header header header' 'sidebar content content' 'sidebar content content' 'footer footer footer';
}
.item-1 {
grid-area: content;
background: #000;
color: #ffffff;
text-align: justify;
padding: 10px;
}
.item-2 {
grid-area: sidebar;
background: #852021;
color: #ffffff;
padding: 10px;
}
.item-3 {
grid-area: header;
background: yellow;
text-align: center;
padding: 10px;
}
.item-4 {
grid-area: footer;
background: gray;
color: #fff;
text-align: center;
padding: 10px;
}
<div class="grid-container">
<div class="item-1">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Non nulla sapiente cumque ratione quas consequatur, quaerat qui possimus et voluptatibus expedita tempore porro fuga aliquam, cum aut rerum perspiciatis blanditiis.
</div>
<div class="item-2">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Non nulla sapiente cumque ratione quas consequatur, quaerat qui possimus et voluptatibus expedita tempore porro fuga aliquam, cum aut rerum perspiciatis blanditiis.
</div>
<div class="item-3">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Non nulla sapiente cumque ratione quas consequatur, quaerat qui possimus et voluptatibus expedita tempore porro fuga aliquam, cum aut rerum perspiciatis blanditiis.
</div>
<div class="item-4">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Non nulla sapiente cumque ratione quas consequatur, quaerat qui possimus et voluptatibus expedita tempore porro fuga aliquam, cum aut rerum perspiciatis blanditiis.
</div>
</div>
It allows you to define a ratio. So if you use "1fr 1fr 1fr 1fr" this means 4 columns, each with 1 share of the width, so they will each be 25% of the width. If however you use "1fr 2fr 1fr" then there will be 3 columns, where the first and last will both be 25% but the middle one will be 50%.
It makes adding another column easier, without having to do the maths to work out what all the percentages should be now.
See this article for more information.
The first part defines 4 rows, each with 1 fr, so each row will get 25% of the total height
The second part defines 3 columns, each with 1 fr, so each column will get 33% of the total width.
THis agrees with the following definition of areas, where there are 4 strings, one for each row, with 3 names in every string, one for each column.