Jump to content

First and last of a div with an assigned class...


Jim R
Go to solution Solved by kicken,

Recommended Posts

Each of those names with information are on .update_card

Since these lists will vary based on the user, I would like the first instance to have rounded corners at the top, and I'd like the last instance to have rounded corners at the bottom.  

CSS is below that I've tried...

image.thumb.png.4aa2d9377b2e122cfd93b3e37cf54405.png

 

I've also tried first-of-type, last-of-type.  Below is what I have now, which also doesn't work.  

.update_card {
	padding-left: 5px;
	margin-right: 10px !important;
	padding-bottom: 15px;
	background: #ccc;
}

.update_card:first-child() {
	border-radius:  10px 10px 0px 0px;

}

.update_card:last-child() {
	border-radius:  0px 0px 10px 10px;
}

 

 

Link to comment
Share on other sites

Other styles are working on them, and one of the changes I've made today in altering its look is removing the border-radius from all four corners of each instance.  

 

At first I tried nth-of-type(#), but it almost seemed like it was working in reverse.  That doesn't work though because the list in each class won't have the same names. 

Link to comment
Share on other sites

An image of code isn't code.  We can't copy and paste an image into our editor and experiment to help solve the issue.

It seems your HTML is not structured correctly for the nth/first/last pseudo selectors though.  Those selectors only count elements that are on the same level.  I can't tell for sure since your image is incomplete, but it looks like your update_card elements are wrapped by some other elements, meaning they are an only-child and thus both the first and last element.

If you can't make your update_card elements siblings of each other, then you may need to create a class that you apply to the first and last elements manually.

  • Like 1
Link to comment
Share on other sites

 

 

<div class="blw">

<div class="update_grade">Class of 2023</div>



<div class="update_card">

<div class="update_name"><span class="pName"><a href="/tag/jalen-hooks">Jalen Hooks</a></span>

</div>

<div class="update_information">6'7", <span class="update_position"><a href="/scout/player-rankings/?position=4&amp;grade=23">Power Forward</a></span>  ||  Warren Central</div>


</div>  // end update_card

</div> // end blw

 

Link to comment
Share on other sites

2 hours ago, maxxd said:

.update-card has the background color. Use border-radius on that.

I want the first instance to have a top border radius, and I want the last instance to have a bottom border radius.  The one(s) in the middle, I just want them to be flat/flush.  

Link to comment
Share on other sites

Then it's quite possible you're going to need to revisit the markup. What it comes down to is that whatever element(s) you want to have the rounded borders is where you need to put the background color.

Or you might be able to put the rounded borders on the parent element with an overflow set to hidden, but it's late on a Friday night for me so please don't quote me on that...

Link to comment
Share on other sites

If you wrap all your update card elements in a container, and have them all be siblings, then you can get the effect you want using :first-child and :last-child

<div class="blw">
  <div class="update_grade">Class of 2023</div>
  <div class="update-list">
    <div class="update_card">
      ... Jalen Hooks ...
    </div>
    <div class="update_card">
      ... Logan Imes ...
    </div>
    <div class="update_card">
      ... Spencer White ...
    </div>
  </div>
</div>

.update_card:first-child {
	border-radius:  10px 10px 0px 0px;

}

.update_card:last-child {
	border-radius:  0px 0px 10px 10px;
}

 

Link to comment
Share on other sites

2 hours ago, kicken said:

If you wrap all your update card elements in a container, and have them all be siblings, then you can get the effect you want using :first-child and :last-child

blw is the container.  

 

I have a few similar containers housing update_card instances.  

 

.blw .update_card:first-child {
	border-radius:  10px 10px 0px 0px;
}

.blw .update_card:last-child {
	border-radius:  0px 0px 10px 10px;
}

 

This is what it produced:

 

image.png.1e152d843c4ff1cff0e8296974e7f95b.png

Edited by Jim R
Link to comment
Share on other sites

  • Solution
2 hours ago, Jim R said:

blw is the container.

The container needs to contain only your .update_card elements, and those elements must be direct children of the container.

Your original HTML doesn't meet those requirements.  The .blw element also contains your .update_grade element (which is the :first-child).  Your screenshot suggests every .update-card is wrapped in some other parent element (hence, they all are :last-child of that container).

 

  • Like 1
Link to comment
Share on other sites

Using the markup in the thread and this CSS

.update_card{
	width: 300px;
	height: 100px;
	background-color: red;
}
.update-list{
	margin: 20px;
	border-radius: 10px;
	overflow: hidden;
	width: fit-content;
	display: flex;
	flex-direction: column;
	row-gap: 20px;
}

give me this

border-radius.png.26b9f6b923c860d6b7584f0b828dd49e.png

It looks like that's what you want?

  • Like 1
Link to comment
Share on other sites

5 hours ago, kicken said:

The container needs to contain only your .update_card elements, and those elements must be direct children of the container.

Your original HTML doesn't meet those requirements.  The .blw element also contains your .update_grade element (which is the :first-child).  Your screenshot suggests every .update-card is wrapped in some other parent element (hence, they all are :last-child of that container).

 

Makes sense, but it has to include update_grade because I'm cycling through multiple players in multiple grades.  In theory it's unavoidable because I don't control how many players my User bookmarks.  

I was wrong though on one point, the .blw isn't a container.  It's just a way to set update_card apart from other parts of the site. 

 

Seems like there would be something to the effect of .update_card:nth-of-type(first) / (last).

 

 

 

 

 

Link to comment
Share on other sites

2 hours ago, Jim R said:

Seems like there would be something to the effect of .update_card:nth-of-type(first) / (last).

*-of-type only consider the tag name as the type, not any classes or id's applied to an element.   The selector would be more accurately read as *:first-of-type.update_card {}.

So, using that you could have other elements in your container, so long as they used different tag names.  It's easier to just have a dedicated element and use :first-child / :last-child imo though.

1 hour ago, Jim R said:

Would love to get rid of those tiny gaps though.

You'll have to determine the source of them.  Browser's developer tools should help with that.  My guess would be some sort of margin.  Such gaps don't appear when using the example code from above.

If you can't find the issue, you'll need to provide updated HTML and CSS code, or a fiddle link, that re-creates the issue.

 

Edited by kicken
Link to comment
Share on other sites

7 hours ago, kicken said:

You'll have to determine the source of them.  Browser's developer tools should help with that.  My guess would be some sort of margin.  Such gaps don't appear when using the example code from above.

If you can't find the issue, you'll need to provide updated HTML and CSS code, or a fiddle link, that re-creates the issue.

 

For some reason the border-radius of the last instance is creating it.  When I turn it off it disappears. 

 

I did on the last instance, margin-top: -1px; 

 

It worked. 

Edited by Jim R
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.