How can I vertically align my text lines? - html

I'm having a problem with vertical alignment with my text lines. I have two text lines, <p>, and I want to put the second one in the bottom of my <div> so I tried to do vertical-align: bottom and text-bottom and the top and bottom ones in my <p> and that didn't work.
.header {
grid-area: header;
background-color: red;
display: grid;
grid-template-areas: 'headerLeft rest headerRight';
grid-template-columns: 10% 1fr 30%;
padding: 5px;
}
.header p {
margin: 0px;
}
.headerRight {
grid-area: headerRight;
}
.headerRight p {
float: right;
}
<div class="layout">
<div class="header">
<div class="headerRight">
<p style="margin: 0px 0px 0px 1px;"><img src="https://i.imgur.com/V2aWxOK.png" style="height: 13vh" /></p>
<p><img src="https://i.imgur.com/V2aWxOK.png" style="height: 13vh" /></p>
<p style="margin: 0px 5px 0px 0px">Ola</p>
<br />
<p style="margin: 0px 5px 0px 0px">Ola</p>
</div>
</div>
</div>

I'd suggest the following, which involves some minor updates to your HTML; explanations are in the comments to the code itself:
.header {
grid-area: header;
background-color: red;
display: grid;
grid-template-areas: 'headerLeft rest headerRight';
grid-template-columns: 10% 1fr 30%;
padding: 5px;
}
.header p {
margin: 0px;
}
.headerRight {
grid-area: headerRight;
/* here we specify the use of CSS Grid layout: */
display: grid;
/* define the top-and-bottom (0) and left-and-right (5px) grid
gap gutters: */
grid-gap: 0 5px;
/* define the named areas of the grid: */
grid-template-areas:
". paragraphAreaOne imageOne imageTwo"
". . imageOne imageTwo"
". paragraphAreaTwo imageOne imageTwo";
/* define the sizing of the columns; here we have the first column
taking up one fractional unit (1fr), with the other columns sized,
using the repeat() function, at min-content in order to have those
grid-columns sized to the minimum necessary to contain their content: */
grid-template-columns: 1fr repeat(3, min-content);
/* here we have three rows each sized, using the repeat() function,
to size each row to min-content: */
grid-template-rows: repeat(3, min-content);
}
/* positioning the various elements into the appropriate grid areas: */
.headerRight p:first-of-type {
grid-area: paragraphAreaOne;
}
.headerRight p:nth-of-type(2) {
grid-area: paragraphAreaTwo;
}
.headerRight img:first-of-type {
grid-area: imageOne;
}
.headerRight img:nth-of-type(2) {
grid-area: imageTwo;
}
<div class="layout">
<div class="header">
<div class="headerRight">
<!-- your img elements were wrapped in <p> elements, which were removed,
partially for semantic reasons, and partially because they were
simply unnecessary -->
<img src="https://i.imgur.com/V2aWxOK.png" style="height: 13vh" />
<img src="https://i.imgur.com/V2aWxOK.png" style="height: 13vh" />
<p>Ola</p>
<!-- there was a <br> element here which, when using a Grid layout
serves no purpose, so was removed -->
<p>Ola</p>
</div>
</div>
</div>
JS Fiddle demo.
References:
gap/ grid-gap
grid-template-areas.
grid-template-columns.
grid-template-rows.
min-content.
repeat().

you are using grid so u can use align-items:end to the header and it will align your elements to the bottom here take a look at it https://developer.mozilla.org/en-US/docs/Web/CSS/align-items

Related

CSS Grid Styling

I am trying to understand how CSS grids work. I've tried to make an example of a store item as practice, but I am at a loss.
Here's my how my CSS currently looks. Cut off at the top, weird spacing, and the right side is not coming together at all.
How's how it would ideally look
Here is my current CSS, I hope someone can help explain where I am misunderstanding the use of
CSS grids.
.store-currency {
height: 3vh;
}
.item {
display: flex;
align-items: center;
grid-row: 1 / span 2;
}
.currency {
display: flex;
align-items: center;
}
#num-bought-item0 {
display: flex;
align-items: center;
justify-content: right;
margin-right: 10px;
grid-column: 1 / span 2;
}
.store-item {
height: 15vh;
width: 100%;
display: grid;
grid-template-columns: 2fr;
grid-template-rows: 1fr 1fr;
font-size: 24px;
color: white;
border: 5px white solid;
justify-content: left;
align-items: center;
}
.store-item img {
margin: 10px;
height: 8vh;
}
.store-container {
display: flex;
height: 100%;
width: 30vw;
z-index: 0;
background-color: saddlebrown;
left: 0;
position: absolute;
text-align: center;
}
HTML:
<div class="store-container">
<div class="store-item" id="item0">
<div class ="item">
<img src="dumbell.png" alt="">
<span>Dumbbell</span>
</div>
<div id="num-bought-item0">
<span>Owned</span>
<span id="count-item0">0</span>
</div>
<div class="currency">
<img class="store-currency" src="coin.png" alt="">
<span>100000</span>
</div>
</div>
you did the first steps.
To get started you have to define a container element as a grid with display: grid, set the column and row sizes with grid-template-columns and grid-template-rows, and then place its child elements into the grid with grid-column and grid-row.
.store-container {
display: grid | inline-grid;
}
grid – generates a block-level grid
inline-grid – generates an inline-level grid
With grid-template-columns you can define how many columns will appear in your layout.
P.S Fr unit is a fractional unit and 1fr is for 1 part of the available space. In this example each column would take ~ 25% from the available space.
.container {
grid-template-columns: 1fr 1fr 1fr 1fr;
}
For your task, you can use grid-template-areas feature.
The grid-template-areas CSS property specifies named grid areas,
establishing the cells in the grid and assigning them names.
For example:
.item-a {
grid-area: header;
}
.item-b {
grid-area: main;
}
.item-c {
grid-area: sidebar;
}
.item-d {
grid-area: footer;
}
.container {
display: grid;
grid-template-columns: 50px 50px 50px 50px;
grid-template-rows: auto;
grid-template-areas:
"header header header header"
"main main . sidebar"
"footer footer footer footer";
}
This will generates something like that in modern browsers:
If you need more examples, take a look here:
https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-areas
https://css-tricks.com/snippets/css/complete-guide-grid/
Some of the examples are taken from the second site.
It looks like you are mixing flex and grid properties. grid-row and grid-column are only avalaible for a grid display (2D), not a flex display (1D).
You can try to play around with flex (worse choice since it is drawing a 1D layout) , you can use grid , which is made for this kind of layout.
Here a couple example with flex and grid
/* GRID make it simple*/
.grid {display:grid;}
#num-bought-item2 {grid-row:1/3;grid-column:2;}
#num-bought-item2 {display:grid;margin:auto;text-align:center}
/* layout done */
/* some reset for the demo*/
*{box-sizing:border-box;}
.store-container {display:grid;justify-content:center;}
.store-item {border:solid;}
.store-item>div {padding:0.5em;}
img{vertical-align:middle;}
[src="https://dummyimage.com/25/ff0"]{border-radius:50%}
big{color:darkgreen;background:lightyellow;}
/* FLEX make it a mess */
.flex {display:flex}
.column {flex-flow:column wrap;height:120px;}/* here an height is to be set so it wraps*/
/* since it is not made for this, we need to mess around */
.flex #num-bought-item1{order:2}/* reorder item */
.flex .item {height:0;min-height:60%;}/* hide it, then show it */
.flex .currency {height:0;min-height:40%;}/* hide it, then show it */
.flex #num-bought-item1{display:flex;flex-direction:column;justify-content:center;text-align:center;margin:auto;}
/* and flex did not do it */
<p>Let's try via flex</p>
<div class="store-container">
<div class="store-item flex column" id="item1">
<div class="item">
<img src="https://dummyimage.com/50" alt="">
<span>Dumbbell</span>
</div>
<div id="num-bought-item1" >
<span>Owned</span>
<span id="count-item1">0</span>
</div>
<div class="currency">
<img class="store-currency" src="https://dummyimage.com/25/ff0" alt="">
<span>100000</span>
</div>
</div>
</div>
<p>And via <big>grid</big> </p>
<div class="store-container">
<div class="store-item grid" id="item2">
<div class="item">
<img src="https://dummyimage.com/50" alt="">
<span>Dumbbell</span>
</div>
<div id="num-bought-item2" >
<span>Owned</span>
<span id="count-item1">0</span>
</div>
<div class="currency">
<img class="store-currency" src="https://dummyimage.com/25/ff0" alt="">
<span>100000</span>
</div>
</div>
</div>

Unequal rows in CSS grid

I am trying to create a CSS grid with unequal rows and I cannot make .feature-block-header take ONLY as much space as it actually needs. I don't want it to be equal height with .feature-block-text. Is there a way of having a flex-grow sort of way on .feature-block-text so that it takes whatever space header did not use?
I think I have tried almost every grid row property...
.feature-block {
display: grid;
grid-template-columns: 320px auto;
grid-column-gap: 10px;
grid-template-areas:
"feature-block-image feature-block-header"
"feature-block-image feature-block-text";
}
.feature-block-header {
grid-area: feature-block-header;
background: yellow;
margin-top: 0;
}
.feature-block-image {
grid-area: feature-block-image;
width: 320px;
height: 320px;
background: lightblue;
}
.feature-block-text {
grid-area: feature-block-text;
background: lightgreen;
}
<div class="feature-block">
<h2 class="feature-block-header">Header</h2>
<div class="feature-block-image"></div>
<div class="feature-block-text">
<p>Some text within text block</p>
</div>
</div>
By adjusting the grid-template-rowsproperty, you can adjust the row heights. I just added this property to your code to demonstrate the result.
EDIT: I changed 50px to auto to make it flexible to adjust row content height.
grid-template-rows: auto 1fr;
.feature-block {
display: grid;
grid-template-columns: 320px auto;
grid-column-gap: 10px;
grid-template-areas:
"feature-block-image feature-block-header"
"feature-block-image feature-block-text";
grid-template-rows: auto 1fr;
}
.feature-block-header {
grid-area: feature-block-header;
background: yellow;
margin-top: 0;
}
.feature-block-image {
grid-area: feature-block-image;
width: 320px;
height: 320px;
background: lightblue;
}
.feature-block-text {
grid-area: feature-block-text;
background: lightgreen;
}
<div class="feature-block">
<h2 class="feature-block-header">Header</h2>
<div class="feature-block-image"></div>
<div class="feature-block-text">
<p>Some text within text block</p>
</div>
</div>
You can set align-self: start; to .feature-block-header.
https://developer.mozilla.org/en-US/docs/Web/CSS/align-self

Make a child element take a third of the parent container (Parent is styled using grid-layout)

I'd want one child element to be 1/3 the size of its parent container and the other 2/3. The parent container is in a main container that uses grid-layout.
The parent container in question spans 2 columns of the main container it is contained in.
I've tried using margin, but it doesn't work: when I switch from a small screen to a large screen it moves to leave a gap.
<div class="main-container" style="display:grid; grid-template-columns: 1fr 2fr 1fr"> 1
<div></div>
<div class="parent-container" style="grid-column:span 2">
<div class="child-1"></div>
<div class="child-2"></div>
</div>
</div>
css code I've tried:
.child-1{
margin-left:-20%;
}
Child-1 must be 1/3 of parent-container and child-2 2/3 of parent-container
You can make use of the nested grid container. The dotted borders signify the width that the child elements take from the parent container. The solid borders are for the main container's child elements. Rest is explained in comments.
.main-container {
display: grid;
grid-template-columns: 1fr 2fr 1fr; /* 1/4th for Extra, 2/4th for Parent, 1/4th for Extra */
grid-auto-flow: column; /* Normal flow is row */
}
.parent-container {
display: grid; /* Nested Grid */
grid-template-columns: 1fr 2fr; /* 1/3rd for Child 1, 2/3rd for Child 2 of parent container*/
grid-auto-flow: column;
}
/* Extra styling for snippet, you just need the above logic */
.main-container {
font-size: 1.15em;
font-family: Sans-Serif;
}
.parent-container {
border: 2px solid black;
}
.parent-container>div {
background: #6660CE;
padding: 10px;
color: #fff;
text-align: center;
border: 2px dotted black;
}
.main-container>div {
background: #6660CE;
padding: 10px;
color: #fff;
text-align: center;
}
<div class="main-container">
<div class="extra">Extra</div>
<div class="parent-container">
<div class="child-1">Child 1 </div>
<div class="child-2">Child 2</div>
</div>
<div class="extra">Extra</div>
</div>
Simply give the parent element two columns with grid-template-columns, where the second column is twice the size of the first column (i.e. 1fr and 2fr).
This can be seen in the following:
.container {
display: grid;
grid-template-columns: 1fr 2fr;
}
.child-1 {
background: red;
}
.child-2 {
background: blue;
}
.child {
height: 50px;
}
<div class="container">
<div class="child child-1"></div>
<div class="child child-2"></div>
</div>
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.child:nth-child(1) {
grid-area: 1 / 1;
background-color: #9341aa;
}
.child:nth-child(2) {
grid-area: 2 / 1 / span 1 / span 2;
background-color: #ab41aa;
}
.child:nth-child(3) {
grid-area: 3 / 1 / span 1 / span 3;
background-color: #cab332;
}
<div class="container">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
</div>
Using calc, something like this should do the trick:
HTML:
<div class="main-container">
<div></div>
<div class="parent-container">
<div class="child-1"></div>
<div class="child-2"></div>
</div>
</div>
CSS:
.parent-container{
position: relative;
width: 100%;
height: 100%;
}
.parent-container .child-1,
.parent-container .child-2{
width: calc(100% / 3);
height: 100%;
float: left;
}

CSS grid and inline-block design issue

I have made a blog design using CSS grid, having used inline-block to pack DIVs together.
In my blog I have 2 picture-DIVS of height 60 that I want to show next to a text-DIV of height 120. Only the first picture is shown next to the text.
Why is the second picture shown below the text, and please get some pointers on how I can fix this.
.GridCont {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-template-rows: auto auto auto;
grid-template-areas: "content content content content" "content content content content" "content content content content";
}
.PostContent {
grid-area: content;
background: #B8E986;
}
.Content {
background: #000000;
width: 35%;
color: white;
display: inline-block;
}
.box1 {
height: 120vh;
}
.PicContent {
background: blue;
color: white;
display: inline-block;
}
.pic1 {
height: 60vh;
width: 50%;
}
.pic2 {
height: 60vh;
width: 45%;
}
.cTextP {
padding: 20px;
}
<div class="GridCont">
<div class="PostContent">
<div class="PicContent pic1">
<div class="cTextP">Picture #1</div>
</div>
<div class="Content box1">
<div class="cTextP">Content #1</div>
</div>
<div class="PicContent pic2">
<div class="cTextP">Picture #2</div>
</div>
</div>
</div>
Code is at this JS-fiddle
Why would the second image show right beneath the first? There is no reason for that.
The second image is on the second row.
The second row goes right beneath the first row.
More specifically, the first row is occupied by two elements: image #1 and the content box. The height of the first row is defined by the tallest element. In this case, that would the content box.
So, because image #1 doesn't extend the full height of row #1, there will be a gap between images.
Here's an even more detailed explanation of the problem:
Is it possible for flex items to align tightly to the items above them?
(It's a flexbox-related post, but the logic applies here, as well.)
Instead of inline-block, use Grid properties to get the content box to span both rows:
.PostContent {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 60vh 60vh;
grid-gap: 1em;
grid-template-areas: " pic1 box1 "
" pic2 box1 ";
}
.box1 {
grid-area: box1;
}
.pic1 {
grid-area: pic1;
}
.pic2 {
grid-area: pic2;
}
.PostContent { background: #B8E986; }
.PicContent { background: blue; color: white; }
.Content { background: #000000; color: white; }
.cTextP { padding: 20px;}
<div class="GridCont">
<div class="PostContent">
<div class="PicContent pic1">
<div class="cTextP">Picture #1</div>
</div>
<div class="Content box1">
<div class="cTextP">Content #1</div>
</div>
<div class="PicContent pic2">
<div class="cTextP">Picture #2</div>
</div>
</div>
</div>
revised jsfiddle
Also note that grid properties work only between parent and child elements.
This will fix your problem:
<div class="grid-container">
<div class="image1"></div>
<div class="image2"></div>
<div class="text"></div>
</div>
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr;
grid-template-areas: "image1 image1 text text" "image2 image2 text text";
}
.image1 { grid-area: image1; }
.image2 { grid-area: image2; }
.text { grid-area: text; }
You can see the working example over here:
https://codepen.io/dennisperremans/pen/NeqNJp

Pre tag causing page to scroll horizontally in CSS Grid [duplicate]

This question already has answers here:
Prevent content from expanding grid items
(3 answers)
Closed 5 years ago.
I am working on switching my site to CSS Grid. I found an unexpected behavior.
I have a simple CSS-Grid layout header spanning two columns the next row has the side navigation with a fixed size, and the main content using the remaining space.
In the main element contains a pre tag with demo code. That pre is a rather long line, for some screen sizes it pushes the main area outside the browser view.
What I want is for the pre to have to scroll horizontally.
body {
margin: 40px;
}
pre {
overflow: auto;
background-color: #ffffff;
border: 1px solid #898989;
padding: 5px;
}
header {
background: #e1e1e1;
grid-area: header;
}
aside {
grid-area: aside;
background: #d7d7d7;
}
aside nav {
background: #e1e1e1;
}
aside nav ul {
list-style: none;
padding-top: 10px;
}
aside nav ul li {
margin-bottom: 10px;
}
main {
background: #c2c2c2;
grid-area: main;
}
.box {
border-radius: 5px;
padding: 20px;
}
.grid {
display: grid;
grid-gap: 10px;
grid-template-areas: "header header" "aside main";
grid-template-columns: 200px 1fr;
grid-template-rows: 110px 1fr;
}
<div class="grid">
<header class="box">
<h1>Site Name and Navigation Bar</h1>
</header>
<aside class="box">
<h1>Sidebar</h1>
<p>This is the sidebar. Might have secondary navigation elements.</p>
<nav>
<ul>
<li>Link to nowhere</li>
<li>Link to nowhere</li>
</ul>
</nav>
</aside>
<main class="box">
<h1>Main Content</h1>
<p>The following <code>pre</code> element will be too large for our grid. It will not have a horizontal scrollbar if it has lines that exceed its width.</p>
<h2>Darn Pre</h2>
<pre><code><html>
<body>
<p>I like long sentences and can not lie. Look at it it's so big. That make your page to be far to wide. I want to get horizontal with the freaky line. My homeboys tried to warn me that pre can make you layout funky.
</p>
</body>
</html>
</code></pre>
<main>
</div>
codepen with demo of this problem
A Pre tag will try and fill all available space. The fix is to limit it in some way.
I fixed this by adding another 1fr column and having main span that.
.grid {
display: grid;
grid-gap: 10px;
grid-template-areas: "header header header" "aside main main";
grid-template-columns: 200px 1fr 1fr;
grid-template-rows: 110px 1fr;
}
Codepen example showing my fix