CSS grid does not seem to be working, and I do not know why. Can anyone advise?
I have an index.html and css via Visual Studio. The html is basic. I made a container with a couple of items in.
This is the html
.container {
display: grid;
grid-template-columns: 1fr 200px;
}
<div class="container">
<div class="items">Lorem ipsum dolor sit </div>
<div class="items">Lorem ipsum dolor sit </div>
<div class="items">Lorem ipsum dolor, sit </div>
</div>
Can someone let me know as my take on it is the container is the parent so I use that class. I changed it to items and nothing.
I have tried changing the elements and that still did not work.
I don't have any other options.
It's really hard to understand what you are expecting😐. Please make your questions more specific in the future.
However, from what I understand, you are not too happy about the second element sitting at extreme right. Ok let's talk about that.
If you want to use the grid-template-columns property in your CSS, Ask yourself these:
How many columns do I want?
How much width do I want on each column?
CASE 1: If you want only ONE column, i.e. you want your list to look like this
Lorem ipsum dolor sit
Lorem ipsum dolor sit
Lorem ipsum dolor, sit
Use grid-template-columns: auto one auto for one column
.container {
display: grid;
grid-template-columns: auto;
}
<div class="container">
<div class="items">Lorem ipsum dolor sit </div>
<div class="items">Lorem ipsum dolor sit </div>
<div class="items">Lorem ipsum dolor, sit </div>
</div>
CASE 2: If you want TWO columns, i.e. you want your list to look like this
Lorem ipsum dolor sit Lorem ipsum dolor sit
Lorem ipsum dolor, sit
Use grid-template-columns: auto auto two autos for two columns
.container {
display: grid;
grid-template-columns: auto auto;
}
<div class="container">
<div class="items">Lorem ipsum dolor sit </div>
<div class="items">Lorem ipsum dolor sit </div>
<div class="items">Lorem ipsum dolor, sit </div>
</div>
CASE 3: If you want THREE columns, i.e. you want your list to look like this
Lorem ipsum dolor sit Lorem ipsum dolor sit Lorem ipsum dolor, sit
Use grid-template-columns: auto auto auto three autos for three columns
.container {
display: grid;
grid-template-columns: auto auto auto;
}
<div class="container">
<div class="items">Lorem ipsum dolor sit </div>
<div class="items">Lorem ipsum dolor sit </div>
<div class="items">Lorem ipsum dolor, sit </div>
</div>
Now that you know how many columns you want, next you'd want to decide the width you want on each column and width can be different for each column.
If you don't want too much trouble, keep using auto to automatically set the width for each column. But if you're ready to do a little bit of tinkering, start changing the values.
For example, you can say grid-template-columns: 1fr 200px 20%, this will generate a grid of three columns.
The first column will have width of 1fr.
The second column will have width of 200px.
The third column will have width of 20% of its parent, container.
.container {
display: grid;
grid-template-columns: 1fr 200px 20%;
}
<div class="container">
<div class="items">Lorem ipsum dolor sit </div>
<div class="items">Lorem ipsum dolor sit </div>
<div class="items">Lorem ipsum dolor, sit </div>
</div>
So, in your case, template-grid-columns: 1fr 200px means the first column is of 1fr width and the second is of 200px. Use your browsers dev tools to verify it.
Here are some useful links:
Play with grid-template-columns
Learn more about Grid
I hope that solves your problem. Happy Coding!😺
Related
I have a CSS column layout.
I find however, that there is an unnecessary gap at the bottom (indicated by the pink arrow in screengrab).
This gap disappears if I remove display: inline-block from the CSS, but then some of the <section> elements break in two. I've also tried using break-inside: avoid but it appears to do nothing at all.
Is there a fix for this?
<div class="feed-index">
<div class="feed">
<section>
Lorem ipsum dolor sit amet
</section>
<section>
Lorem ipsum dolor sit amet
</section>
<section>
Lorem ipsum dolor sit amet
</section>
<section>
Lorem ipsum dolor sit amet
</section>
<section>
Lorem ipsum dolor sit amet
</section>
</div>
</div>
.feed-index .feed {
column-count: 3;
column-gap: 40px;
}
.feed-index .feed > section {
display: inline-block;
margin-bottom: 50px;
overflow: hidden;
}
Add vertical-align:top; to your CSS for .feed-index .feed > section
The reason is your overflow: hidden; actually changes the block. See more info here: Why does x-overflow:hidden cause extra space below?
I am wondering if it is possible to left align text below center aligned text, so that both texts start from same position.
In the example figure. The TITLEs are center aligned and the copy below is left aligned.
How can you realise something like this with CSS?
Here is a JSFiddle to start from
https://jsfiddle.net/j5p7v8m9/
<div>
<p style="text-align: center;">TITLE</p>
<p style="text-align: left;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</p>
</div>
Here is the answer. I've updated your JSFiddle. You need to add some wrappers for each divs, then you can centerize the title as follows:
HTML:
<div class="container">
<div class="col">
<div class="contents">
<p class="title">TITLE</p>
<p class="text">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</p>
</div>
</div>
<div class="col">
<div class="contents">
<p class="title">TITLE</p>
<p class="text">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</p>
</div>
</div>
<div class="col">
<div class="contents">
<p class="title">TITLE</p>
<p class="text">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</p>
</div>
</div>
</div>
CSS:
.container{
display: block;
}
.col{
float: left;
width: 33.33%;
}
.contents{
margin-left: 20px;
margin-right: 20px;
}
.title{
text-align: center;
}
JSFiddle: https://jsfiddle.net/53oLpvqg/
Please try with bootstrap css it my help you for easy css.
<div class="col">
<p class="text-center">Title</p>
<p class="text-left">Test content Content</p>
</div>
Check Fiddle https://jsfiddle.net/0h2nmj3r/
*Look at other answers for directions on align text this is answer will help with getting that column effect
The best way to get three columns like that is to use a type of special display in css called "flex". Flex allows you to easily align elements within the parent html element.
.container{
display:flex;
flex-direction : row;
justify-content: space-around;
}
.text-box{
display: flex;
flex-direction:column;
align-items:flex-start;
width:20%;
}
p{
margin: 0 0 5px 0;
}
<div class="container">
<div class="text-box">
<p >TITLE</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</p>
</div>
<div class="text-box">
<p >TITLE</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</p>
</div>
<div class="text-box">
<p >TITLE</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</p>
</div>
</div>
So as you can see I have applied flex to both the container and the text-box's. Left align can be used like in these other answers but i opted for align-items:flex-start so any element within the text-box will be aligned to the left.
By setting justify-content:space-around it makes each element within the container have equal space around it so if you were to change the window size the spaces would be relative and change. I would recommend studying more about flex and grid to improve your css skills.
I have a CSS grid layout, and I would like to have one of the grid-items span two rows.
Normally I would do this via with the grid-row: span 2 property, or by using named grid areas.
In the example though, despite there being room for .news-item-5 to also span the row below (and effectively take over the space allocated to .news-item-7), I can't get this to work.
Is it not possible in this grid layout to have .news-item-5 span two rows?
As well as the included snippets I have a codepen: https://codepen.io/emilychews/pen/GzpBmO
Any help would be amazing.
/* ---- GRID */
.second-grid-wrapper {
display: grid;
grid-gap: 1rem;
grid-template-columns: 2fr 2fr 1fr;
grid-template-areas:
"news-1 news-2 news-3"
"news-1 news-4 news-5"
"news-6 news-4 news-5";
}
.news-item-1 {grid-area: news-1}
.news-item-4 {grid-area: news-4}
/* .news-item-5 {grid-area: news-5} */
.news-item-7 {
background: #fff;
}
.news{
background: lightblue;
box-sizing: border-box
}
<section class="second-grid-wrapper">
<article class="news news-item-1">
<div class="top-news-item-text-wrapper">
<h2 class="news-item-heading td">This is a headline - 1</h2>
<p class="bottom-text td">Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
</div>
</article>
<article class="news news-item-2">
<div class="top-news-item-text-wrapper">
<h3 class="news-item-heading td">A SMALLER HEADLINE - 2</h3>
<a target="_blank" class="bottom-text td">Link</a>
</div>
</article>
<article class="news news-item-3">
<div class="top-news-item-text-wrapper">
<h2 class="news-item-heading td">This is a headline -3</h2>
<p class="bottom-text td">Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
</div>
</article>
<article class="news news-item-4">
<div class="top-news-item-text-wrapper">
<h2 class="news-item-heading td">This is a headline - 4</h2>
<p class="bottom-text td">Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
</div>
</article>
<article class="news news-item-5">
<div class="top-news-item-text-wrapper">
<h2 class="news-item-heading td">This is a headline - 5</h2>
<p class="bottom-text td">Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
</div>
</article>
<article class="news news-item-6">
<div class="top-news-item-text-wrapper">
<h2 class="news-item-heading td">SOMETHING ELSE - 6</h2>
<p class="bottom-text td">Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
</div>
</article>
<article class="news news-item-7">
<div class="top-news-item-text-wrapper">
<h2 class="news-item-heading td">This is a headline - 7</h2>
<p class="bottom-text td">Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
</div>
</article>
</section>
Solution
Add grid-auto-rows: 1fr to your grid container.
/* ---- GRID */
.second-grid-wrapper {
display: grid;
grid-auto-rows: 1fr; /* new */
grid-template-columns: 2fr 2fr 1fr;
grid-gap: 1rem;
grid-template-areas:
"news-1 news-2 news-3"
"news-1 news-4 news-5"
"news-6 news-4 news-5";
}
.news-item-1 {grid-area: news-1}
.news-item-4 {grid-area: news-4}
.news-item-5 {grid-area: news-5}
.news-item-7 {background: #fff;}
.news{
background: lightblue;
box-sizing: border-box
}
<section class="second-grid-wrapper">
<article class="news news-item-1">
<div class="top-news-item-text-wrapper">
<h2 class="news-item-heading td">This is a headline - 1</h2>
<p class="bottom-text td">Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
</div>
</article>
<article class="news news-item-2">
<div class="top-news-item-text-wrapper">
<h3 class="news-item-heading td">A SMALLER HEADLINE - 2</h3>
<a target="_blank" class="bottom-text td">Link</a>
</div>
</article>
<article class="news news-item-3">
<div class="top-news-item-text-wrapper">
<h2 class="news-item-heading td">This is a headline -3</h2>
<p class="bottom-text td">Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
</div>
</article>
<article class="news news-item-4">
<div class="top-news-item-text-wrapper">
<h2 class="news-item-heading td">This is a headline - 4</h2>
<p class="bottom-text td">Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
</div>
</article>
<article class="news news-item-5">
<div class="top-news-item-text-wrapper">
<h2 class="news-item-heading td">This is a headline - 5</h2>
<p class="bottom-text td">Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
</div>
</article>
<article class="news news-item-6">
<div class="top-news-item-text-wrapper">
<h2 class="news-item-heading td">SOMETHING ELSE - 6</h2>
<p class="bottom-text td">Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
</div>
</article>
<article class="news news-item-7">
<div class="top-news-item-text-wrapper">
<h2 class="news-item-heading td">This is a headline - 7</h2>
<p class="bottom-text td">Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
</div>
</article>
</section>
Explanation
The element (.news-item-5) is actually expanding two rows in your code. Here's the view using Firefox's Grid outline tool:
You can see that the News Item 5 grid item is expanding across two rows. However, its content is confined to the first row.
This is most likely due to an absence of defined row lengths. As a result, grid-template-rows remains at its default setting: none, which means all rows will be implicitly created and sized by grid-auto-rows, who's default value is auto.
From the spec:
§ 7.2. Explicit Track Sizing: the grid-template-rows and
grid-template-columns properties
The none value.
Indicates that no explicit grid tracks are created by this property (though explicit grid tracks could still be created by grid-template-areas).
Note: In the absence of an explicit grid any rows/columns will be implicitly generated, and their size will be determined by the grid-auto-rows and grid-auto-columns properties.
Therefore, as a solution, give the grid some solid guidance by switching from grid-auto-rows: auto to grid-auto-rows: 1fr.
Based on your CSS code, your grid-template-areas indicate that you need to adjust the grid-template-columns: to read 2fr 2fr 2fr;. You have your style for .news-item-5 commented out, so make that visible and comment out the style for .news-item-7.
Then move down to your HTML and either remove (or comment out) the <article class="news news-item-7"> section that you want .news-item-5 to extend into.
We have a container with overflow-y:scroll that must have a footer that is sticky (bottom 0) unless the content inside the scrolling container + the height (which is dynamic) of the footer are bigger than the containers height.
HTML:
<div class="wrapper">
<div class="scroll">
<div class="content">
Lorem ipsum dolor sit amet
</div>
<div class="footer">
This must stick to the bottom until .content is too long, then go below it
</div>
</div>
</div>
.content and .footer can have more or less content.
If possible, we do not want to use JS for this.
I created a fiddle here with several states: http://jsfiddle.net/bqvtf1zo/1/
Removing position: absolute on .footer solves it for case "little content" (see fiddle), but breaks the other 2 cases.
You need to create a flex container. (Though there are other ways to hande this problem as well: https://css-tricks.com/couple-takes-sticky-footer/)
For the container, set the display to flex and flex-direction to column and give the scrollable content a flex value of 1. Remove positioning from footer, and there you have it.
This will cause the content to stretch to fill the height of the container if any is available, and it will cause the footer to be stuck to the bottom of the content.
For implementation: Be sure to follow up on all the cross-browser issues with flexbox, such as prefixes and bugs. https://github.com/philipwalton/flexbugs
.wrapper{
position: relative;
height: 205px;
width: 200px;
}
.scroll{
border: 1px solid red;
overflow-y: scroll;
height: 100%;
width: 100%;
display:flex;
flex-direction: column;
}
.content{
background-color: #ccc;
flex:1;
}
.footer{
background-color: #efefef;
}
<h1>
little content
</h1>
<div class="wrapper">
<div class="scroll">
<div class="content">
Lorem ipsum dolor sit amet
</div>
<div class="footer">
This must stick to the bottom until .content is too long, then go below it
</div>
</div>
</div>
<h1>
large content
</h1>
<div class="wrapper">
<div class="scroll">
<div class="content">
1. Lorem ipsum dolor sit<br>
2. Lorem ipsum dolor sit<br>
3. Lorem ipsum dolor sit<br>
4. Lorem ipsum dolor sit<br>
5. Lorem ipsum dolor sit<br>
6. Lorem ipsum dolor sit<br>
7. Lorem ipsum dolor sit<br>
8. Lorem ipsum dolor sit<br>
9. Lorem ipsum dolor sit<br>
10. Lorem ipsum dolor sit<br>
11. Lorem ipsum dolor sit<br>
12. Lorem ipsum dolor sit<br>
13. Lorem ipsum dolor sit<br>
</div>
<div class="footer">
This must stick to the bottom until .content is too long, then go below it
</div>
</div>
</div>
<h1>
large content with large footer
</h1>
<div class="wrapper">
<div class="scroll">
<div class="content">
1. Lorem ipsum dolor sit<br>
2. Lorem ipsum dolor sit<br>
3. Lorem ipsum dolor sit<br>
4. Lorem ipsum dolor sit<br>
5. Lorem ipsum dolor sit<br>
6. Lorem ipsum dolor sit<br>
7. Lorem ipsum dolor sit<br>
8. Lorem ipsum dolor sit<br>
9. Lorem ipsum dolor sit<br>
10. Lorem ipsum dolor sit<br>
11. Lorem ipsum dolor sit<br>
12. Lorem ipsum dolor sit<br>
13. Lorem ipsum dolor sit<br>
</div>
<div class="footer">
This must stick to the bottom until .content is too long, then go further down<br>
Some additional content
</div>
</div>
</div>
i must use css to alter the positions; the only thing that seems to be working is the right position nav bar and the liquid layout, but the "content" and "right navigation bar" is ot being positioned properly.
I want content to be in the middle, leftnavigation on the left, and right navigation on the right.
<title>CSS liquid layout</title>
<style type="text/css">
.due {
color: #ff0000;
font-weight: bold;
}
#leftnavigation{
position:absolute;
left:10px;
top:10px;
width:250px;
}
#rightnavigation {
float:right;
width:250px;
height:800px;
}
#content {
float:center;
}
</style>
</head>
<body leftmargin="0" topmargin="0" bgcolor="#ccff99">
<div id="app">
<div id="rightnavigation">
<h1>Right Navigation</h1>
link Instructor
Course <a href="http://www,google.com">
Resume
project
</a>
</div>
<div id="content">
<h1>Sample Content</h1>
<p>
This is the content section of the page. Use structural markup
like <p></p>
to keep the page valid in XHTML.
</p>
<h2>Lorem Ipsum</h2>
</div>
<div id="leftnavigation">
<h1>Left Navigation</h1>
<p>
Page 1 Page 2 <a href="http://www,google.com">
Page
3
</a> Page 4 Page 5 <br />
Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum. Lorem ipsum sit dolor
amum.
</p>
<h2>Lorem Ipsum</h2>
</div>
</div>
You could try this:
CSS
.app {
width: 100%
height: 100%;
}
.due {color: #ff0000;
font-weight: bold;
}
#rightnavigation {
float: left;
width: 33.333%
}
#leftnavigation{
float: left;
width: 33.333%
}
#content {
float: left;
width: 33.333%;
}
HTML
<div class="app">
<div id="leftnavigation">
<h1> Left Navigation </h1>
</div>
<div id="content"></div>
<div id="rightnavigation">
<h1>Right Navigation</h1>
</div>
</div>
Here's a live demo of the example - EXAMPLE
There are some errors in your HTML and CSS that need to be addressed before changing the styles to accomplish what you need.
In your HTML, there are still some unclosed tags. Especially the <div id="rightnavigation"> tag is never closed, so styles applied to #rightnavigation are actually applied to the entire page.
In your CSS, you apply a style to div.content. But that div has an id of content, not a class. The identifier should be div#content.
In your CSS, you give the div with id leftnavigation a position of "left". This should be "absolute" instead.
Once that is all cleaned up, the left nav is on the left, the content is in the center, and the right nav is on the right. But the center content overlaps the right nav (I assume that is unwanted behavior). To clean that up, without changing the HTML any more, you need to give your sections widths, and set their positions based on the width of their neighboring elements.
Your HTML:
<div id="rightnavigation">
<h1>Right Navigation</h1>
</div>
<div id="content">
<h1>Sample Content</h1>
<p>This is the content section of the page. Use structural markup
like <p></p>
to keep the page valid in XHTML.</p>
<p>The styled document should look like your printed version/screenshot.
Add styles to the left navigation links to give them borders and a
background color that changes when moused over (hint: Define navigation
links as display:block;). For the right side links, use a different
background color change and border as ashown.
Make the center column "liquid" or "elastic." Use an external (linked)
CSS file. Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum.
Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum. Lorem ipsum sit
dolor amum.
Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum. </p>
<h2>Lorem Ipsum</h2>
<p> Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum.
Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum.
Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum. </p>
<p><span class="due">Due Tuesday, September 22.</span> Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum.
Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum.
Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum. </p>
</div>
<div id="leftnavigation">
<h1>Left Navigation</h1>
<p><br>Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum. Lorem ipsum sit dolor amum. </p>
<h2>Lorem Ipsum</h2>
</div>
And your CSS:
#rightnavigation {
position: absolute;
top: 50px;
right: 0px;
width: 25%;
}
#content {
position: absolute;
top: 50px;
left: 25%;
width: 50%;
}
#leftnavigation{
position: absolute;
top: 50px;
left: 0px;
width: 25%;
}
.due {
color: #ff0000;
font-weight: bold;
}