How To Lay-out List Items Like A Grid With CSS And HTML?
Có thể bạn quan tâm
-
- Home
- Questions
- Tags
- Users
- Companies
- Labs
- Jobs
- Discussions
- Collectives
-
Communities for your favorite technologies. Explore all Collectives
- Teams
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Try Teams for free Explore Teams - Teams
-
Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams
Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about CollectivesTeams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about TeamsGet early access and see previews of new features.
Learn more about Labs How to lay-out list items like a grid with CSS and HTML? Ask Question Asked 11 years, 9 months ago Modified 3 years, 5 months ago Viewed 80k times 17I have a div block which does not have a fixed width.
Inside, I have an <ul> <li>..</li> block with 11 items. I would like these <li> items to be listed inside the div, all with equal widths like this:
##item## ##item## ##item## ##item## ##item## ##item## ##item## ##item## ##item## ##item## ##item##However, I can't sort it out at all.
I tried float left and right but the central 3 elements will not be centered.
What can I do to get this working?
Thanks!
Share Improve this question Follow asked Feb 14, 2013 at 22:13 PhilPhil 14.6k23 gold badges85 silver badges127 bronze badges 1- Could you include the relevant markup and CSS? You probably just need to specify a width for each LI. – Kevin Boucher Commented Feb 14, 2013 at 22:23
4 Answers
Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 32The "Inline Blocks" link that Jordan posted is a great resource, particularly when it comes to older browser support. For quick reference for others landing on this page off of google, the basic css for creating a centered, inline-block grid is:
ul { margin: 0 auto; text-align: center; } li { display: inline-block; vertical-align: top; } Share Improve this answer Follow edited Oct 30, 2018 at 15:22 Jeromy French 12.1k19 gold badges78 silver badges134 bronze badges answered Feb 10, 2014 at 13:40 FreedomManFreedomMan 4434 silver badges6 bronze badges Add a comment | 30The simplest solution is to use CSS columns:
http://jsfiddle.net/6tD2D/ (prefixes not included)
ul { columns: 3; } <ul> <li>a</li> <li>b</li> <li>c</li> <li>d</li> <li>e</li> <li>f</li> <li>g</li> <li>h</li> <li>i</li> <li>j</li> <li>k</li> </ul>This will equalize the columns as best it can. However, if there aren't enough elements to be perfectly equal, it will start removing them from the right instead of the center.
Share Improve this answer Follow edited Oct 30, 2018 at 15:25 Jeromy French 12.1k19 gold badges78 silver badges134 bronze badges answered Feb 14, 2013 at 22:23 cimmanoncimmanon 68.3k17 gold badges168 silver badges172 bronze badges 3- the jsfiddle example does not work in my chrome only 1 column – Dukeatcoding Commented Aug 19, 2013 at 15:56
- Nor does it work with IEOld. See bottom of developer.mozilla.org/en-US/docs/Web/CSS/columns for compatibility. – Jeromy French Commented Oct 30, 2018 at 15:25
- Is there a simple solution like this, but where you want the elements to go out right (horizontally) first, and not downwards? – Ela782 Commented May 5, 2020 at 21:20
According to this StackOverflow question, Inline Blocks may be just what you need.
Oh, and if you aren't implementing it already, be sure to look into CSS Grids, too. If you don't want to build a CSS grid yourself, this one is fantastic.
Share Improve this answer Follow edited May 23, 2017 at 12:01 CommunityBot 11 silver badge answered Feb 14, 2013 at 22:25 Jordan ThornquestJordan Thornquest 1,1362 gold badges12 silver badges28 bronze badges Add a comment | 1CSS
ul { display: grid; grid-auto-columns: 1fr; grid-auto-rows: 1fr; grid-template-columns: 1fr 1fr 1fr; grid-template-rows: 1fr 1fr 1fr 1fr; gap: 0px 0px; grid-template-areas: "col1-item1 col2-item1 col3-item1" "col1-item2 col2-item2 col3-item2" "col1-item3 col2-item3 col3-item3" "col1-item4 col2-item3 col3-item4"; } /* Assign a class to each li */ /* Column 1 */ .col1-item1 { grid-area: col1-item1; } .col1-item2 { grid-area: col1-item2; } .col1-item3 { grid-area: col1-item3; } .col1-item4 { grid-area: col1-item4; } /* Column 2 */ .col2-item1 { grid-area: col2-item1; } .col2-item2 { grid-area: col2-item2; } .col2-item3 { grid-area: col2-item3; } /* Column 3 */ .col3-item1 { grid-area: col3-item1; } .col3-item2 { grid-area: col3-item2; } .col3-item3 { grid-area: col3-item3; } .col3-item4 { grid-area: col3-item4; }HTML
<ul> <!-- Column 1 --> <li class="col1-item1">col1 item 1</li> <li class="col1-item2">col1 item 2</li> <li class="col1-item3">col1 item 3</li> <li class="col1-item4">col1 item 4</li> <!-- Column 2 --> <li class="col2-item1">col2 item 1</li> <li class="col2-item2">col2 item 2</li> <li class="col2-item3">col2 item 3</li> <!-- Column 3 --> <li class="col3-item1">col3 item 1</li> <li class="col3-item2">col3 item 2</li> <li class="col3-item3">col3 item 3</li> <li class="col3-item4">col3 item 4</li> </ul>Here is the fiddle: https://jsfiddle.net/omarjuvera/p3wajehs/2/ And also you can run the code here
ul { display: grid; grid-auto-columns: 1fr; grid-auto-rows: 1fr; grid-template-columns: 1fr 1fr 1fr; grid-template-rows: 1fr 1fr 1fr 1fr; gap: 0px 0px; grid-template-areas: "col1-item1 col2-item1 col3-item1" "col1-item2 col2-item2 col3-item2" "col1-item3 col2-item3 col3-item3" "col1-item4 col2-item3 col3-item4"; } /* Assign a class to each li */ /* Column 1 */ .col1-item1 { grid-area: col1-item1; } .col1-item2 { grid-area: col1-item2; } .col1-item3 { grid-area: col1-item3; } .col1-item4 { grid-area: col1-item4; } /* Column 2 */ .col2-item1 { grid-area: col2-item1; } .col2-item2 { grid-area: col2-item2; } .col2-item3 { grid-area: col2-item3; } /* Column 3 */ .col3-item1 { grid-area: col3-item1; } .col3-item2 { grid-area: col3-item2; } .col3-item3 { grid-area: col3-item3; } .col3-item4 { grid-area: col3-item4; } <ul> <!-- Column 1 --> <li class="col1-item1">col1 item 1</li> <li class="col1-item2">col1 item 2</li> <li class="col1-item3">col1 item 3</li> <li class="col1-item4">col1 item 4</li> <!-- Column 2 --> <li class="col2-item1">col2 item 1</li> <li class="col2-item2">col2 item 2</li> <li class="col2-item3">col2 item 3</li> <!-- Column 3 --> <li class="col3-item1">col3 item 1</li> <li class="col3-item2">col3 item 2</li> <li class="col3-item3">col3 item 3</li> <li class="col3-item4">col3 item 4</li> </ul>
Share Improve this answer Follow edited Jun 14, 2021 at 6:19 answered Jun 14, 2021 at 6:08 Omar JuveraOmar Juvera 12.2k21 gold badges88 silver badges115 bronze badges Add a comment |Your Answer
Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Draft saved Draft discardedSign up or log in
Sign up using Google Sign up using Email and Password SubmitPost as a guest
Name EmailRequired, but never shown
Post Your Answer DiscardBy clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Not the answer you're looking for? Browse other questions tagged
or ask your own question.- The Overflow Blog
- Your docs are your infrastructure
- Featured on Meta
- More network sites to see advertising test [updated with phase 2]
- We’re (finally!) going to the cloud!
- Call for testers for an early access release of a Stack Overflow extension...
Linked
16 UL+CSS for grid layout 3 CSS - Possible to reflow checkboxes in columns?Related
3 Layout a HTML list in evenly-spaced columns 0 HTML/CSS: column-like layout for list elements 0 css arrangement of differently sized list items in a neat row 8 HTML/CSS Making a list be in rows, 3 boxes per row? 0 create horizontal list in css 0 Make a list arrange in columns 2 How to create a list with three columns in a row by css and html? 3 displaying html lists in a tree-like row-like fashion 0 Basic CSS: making a horizontal list 3 How to create a sectioned grid/list with <li> and <hr> with html/css?Hot Network Questions
- Will a laptop battery that stays connected to its charger be damaged?
- Polynomial.java - a Java class for dealing with polynomials with BigDecimal coefficients
- What exactly is the cornerstone that Mark 12:10 speaks of?
- Problem with highlighting first row and column of a table
- Sorites paradox and emergence
- Angular orientation of exact solution of the Hydrogen Schrödinger Equation
- Why sand dunes appear dark in Sentinel-1 SAR Images but bright in optical images
- T47 to BSA bottom bracket adapter - good idea?
- What is small arch between two notes and how to play it?
- Wouldn't the ban of TikTok violate freedom of speech?
- What's a good way to append a nonce to ciphertext in Python for AES GCM in Python?
- How do I find out what kind of access a user has to a SharePoint Online site using PNP PowerShell?
- What is the polymorph reached by letting the chocolate cool down?
- Find the Smallest Data Type for a Number
- Is Isaiah's suffering servant the prophet Jeremiah?
- Is “thing” a good category?
- A novel about Earth crossing a toxic cloud of cosmic size
- Why hot refers to being closer and cold refers to moving away in the hotter/colder game?
- "Elegant" conditions on two quadratics (with positive real roots) to ensure that the larger root of one is less than the smaller root of the other
- Does length contraction "break the speed limit"?
- Calculate the sum of all the different real roots of the equation
- Saying something in the name of someone who never actually said it
- Non-Schengen flight without any passport control, any repercussion on a non-EU traveller
- Schrödinger's cat ++
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
lang-htmlTừ khóa » Html Ul Li Grid
-
The CSS Grid Methods - Elad Shechter - Medium
-
Auto-placement In CSS Grid Layout - MDN Web Docs
-
How To Create A List Grid View - W3Schools
-
List Items In Grid Columns - HTML & CSS - SitePoint Forums
-
4 CSS Grid Properties (and One Value) For Most Of Your Layout Needs
-
A Complete Guide To Grid - CSS-Tricks
-
Grid - List HTML - CSS - JQuery - Gists · GitHub
-
List Group - Bootstrap
-
Usage Examples Of CSS Grid Layout - Grid By Example
-
Thumbnail Grid Layout Instructions
-
How To Display Unordered List In Two Columns - W3docs
-
Using Position Sticky With CSS Grid - Ahmad Shadeed
-
Responsive Boxes
- Grid - CodePen
-
HTML And CSS Tutorial - Nanyang Technological University