What are some ways to create an HTML container which expands width with each added child - all in one row?
-
How to force parent to expand with children width. HTML: <div class="parent"> <div class="child"> 1 </div> <div class="child"> 2 </div> <div class="child"> 3 </div> </div> CSS: .parent { } .child { width: 200em; float: left; } .child:nth-child(odd) { background-color: red; } .child:nth-child(even) { background-color: green; } What I want to achieve: +Parent-----------------+ | child child child ... | +-----------------------+ What I do not want achieve but I am achieving :) +Parent-+ | child | | child | | child | +-------+ The best will be if it possible without tables and flex layout - if it possible.
-
Answer:
This can be achieved by styling divs to act as tables, rows and cells. Your parent div is set to display:table; and then you have a nested div setting display:table-row; and then inside you have your multiple divs set as display:table-cell; You are mimicking a table layout using divs and CSS. This will give you a nice horizontal layout that you're asking for if you want sample HTML and CSS, just ask and I'll write a codepen for you.
Dwayne Charrington at Quora Visit the source
Other answers
Try avoid using table elements for structural layouts. You can achieve better responsive and adaptive layouts using non-semantic elements like divs. Tables are great for tabular data, where rows and columns are necessary to represent comparative and relative data. They're your ally when it comes to newsletter layouts - but other than that, avoid using them. You'll need to declare your children divs as inline-block-elements first, since divs are naturally block-elements they'll behave like blocks unless you specify otherwise. HTML: <div class="parent"> <div>child #1</div> <div>child #2</div> <div>child #3</div> </div> CSS: .parent { display: block; width: 100%; /* setting the width value to 'auto' will have the parent element expand it's width to accommodate additional blocks, '100%' ensures .parent div utilizes 100% of its parent element's available space (which is the <body> in this case) */ } .parent div { display: inline-block; /* line-up your divs */ vertical-align: top; /* negate the step-down effect */ width: 32%; /* adjust width accordingly */ } Floating your children elements is another option you can experiment with. The general idea above, is lining up your children divs alongside each other. If you want four blocks in a line, you just need to adjust the width accordingly, otherwise, '<div>child #4</div>' will fall on a new line. Paste the code below in the url bar of your browser (all of it), and experiment with various styling solutions in-browser, using your developer tools/console. data:text/html, <html contenteditable> <head> <title>Inline Blocks</title> <style> .parent { display: block; width: 95%; border: 1px solid #ccc; margin: auto; height: 200px; } .parent div { display: inline-block; vertical-align: top; width: 30%; padding: 10px; background: #dddddd; height: 180px; text-align: center; margin-right: 2%; } .parent div:last-child { margin: 0px; } </style> </head> <body> <div class="parent"> <div>child #1</div> <div>child #2</div> <div>child #3</div> </div> </body> You sir, have now taken control back of your blocks, may all divs beware!
Nathan George Jean Shepherd
.parent{ display:inline-block; white-space:nowrap; border:1px solid red; } .child{ display:inline-block; border:1px solid blue; } Borders are just to see what happens.
Esger Jellema
Related Q & A:
- How do I create an HTML table, in jQuery, with JSON data?Best solution by Stack Overflow
- What are some ways to make extra money while in college?Best solution by Yahoo! Answers
- What are some ways to make money?Best solution by Yahoo! Answers
- How to create submenu HTML?
- Which of these two video cameras is the better one?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.