HTML Table columns count responsive to window width - html

I'm trying to achieve something like this:
I have a variable amount of data that is fetched from a database. I want to put each data received into a single cell and each cell side-by-side from other. At this point I could reach. You can think of a pokedex. Each pokemon info goes into a single cell (number, image, name).
My problem is: each cell has a fixed width, for example, 100 px. I want my table to create as many columns as possible according to window size. For example: if user window has 1345 px, my table will create 13 cells per row. But I want it to be responsive in realtime: if user resize his/her window to 890 px, my table would now have only 8 cells per row.
How can I achieve that?
Thanks!
EDIT: okay, some pointed me to a post I couldn't find on search. Following top voted answer from this post I kinda achieved what I wanted. It works when I'm using img inside my div. But in my case, I have div inside my div. Something like this:
<div class="wrapper">
<div>
<p>some text</p>
<img src="myimagepath.png" />
<p>more text</p>
</div>
</div>
In my example, wrapper makes just 1 column, not distributing to the whole page like it did when it was only img. How can I workaround this?
PS: I don't know if I should continue the thread on answer's post or continue here. Please let me know so we can close this thread and solve on the proper thread.

Maybe something like this?
.pokedex {
display: flex;
flex-wrap: wrap;
}
.pokedex div {
background: aquamarine;
height: 100px;
margin: 0 1px 1px 0;
text-align: center;
width: 100px;
}
p {
font-size: 12px;
margin: 3px;
}
img {
width: 50px;
}
<div class="pokedex">
<div>
<p>Pokemon 1</p>
<img src="https://assets.pokemon.com/assets/cms2/img/pokedex/detail/001.png">
<p>Bulbasaur</p>
</div>
<div>
<p>Pokemon 2</p>
<img src="https://assets.pokemon.com/assets/cms2/img/pokedex/detail/002.png">
<p>Ivysaur</p>
</div>
<div>
<p>Pokemon 3</p>
<img src="https://assets.pokemon.com/assets/cms2/img/pokedex/detail/003.png">
<p>Venusaur</p>
</div>
<div>
<p>Pokemon 4</p>
<img src="https://assets.pokemon.com/assets/cms2/img/pokedex/detail/004.png">
<p>Charmander</p>
</div>
<div>
<p>Pokemon 5</p>
<img src="https://assets.pokemon.com/assets/cms2/img/pokedex/detail/005.png">
<p>Charmeleon</p>
</div>
<div>
<p>Pokemon 6</p>
<img src="https://assets.pokemon.com/assets/cms2/img/pokedex/detail/006.png">
<p>Charizard</p>
</div>
<div>
<p>Pokemon 7</p>
<img src="https://assets.pokemon.com/assets/cms2/img/pokedex/detail/007.png">
<p>Squirtle</p>
</div>
</div>

Related

Escaping a CSS column with a fixed width

If you’re trapped in a page wrap - say all the contents of the page are in a div whose width is 900px, then you want one div WITHIN that that’s the full page width. What’s the easiest way to do this?
I know you can end the 900px div, do the full width div, and then start another 900px div, but is there a way to style the inner div so you don't have to escape it? 100vw works for making it the right size but doesn't position it in the right spot.
So simplistic example:
<div style="width:900px;margin-right:auto;margin-left:auto;display:block;">
<p>text text</p>
<div style="width:100vw;">
<p>I want this section to be the full page width and centered</p>
</div>
<p>text text</p>
</div>
Thanks!
You can use negative left margin (-50vw + half parent width).
body {margin: 0;}
#a {background: red;}
#b {background: green; margin-left: calc(-50vw + 200px)
<div id="a" style="width:400px;margin-right:auto;margin-left:auto;display:block;">
<p>text text</p>
<div style="width:100vw;" id="b">
<p>I want this section to be the full page width and centered</p>
</div>
<p>text text</p>
</div>
For this code example I've added IDs (for cleaner CSS styles) and change parent div to 400px (because there is smaller window).
I don't recommend trying to make a child div "escape" its parent because going with that approach will require pointlessly complicated CSS. You can accomplish what you want with a container div and a couple nested children which is a much simpler solution:
.narrow {
width: 300px;
margin: 0 auto;
background-color: tomato;
padding: 16px;
}
.full {
background-color: gold;
padding: 16px;
}
<div>
<div class="narrow">
<p>text text text</p>
</div>
<div class="full">
<p>more text or image or whatever</p>
</div>
<div class="narrow">
<p>text text text</p>
</div>
</div>
I would argue that the way you are trying to solve the issue is not very helpful for an actual website. Normally, you would have a container, your top div, which contains its lower elements. Making a child element go outside its parent div like you seem to want goes against that mentality.
Of course, sometimes you may want to put an element outside its parent, and you can use pavel's answer. For example, maybe you want to animate a line moving. You would then offset that element by -100% and then change that offset to give it the impression of movement. But that would be a special case.
To solve your problem, I would use the following structure:
Here is a link to the example too.
<div class='container'>
<div class='thin'>
<p>text text</p>
</div>
<div class='full-width'>
<p>I want this section to be the full page width and centered</p>
</div>
<div class='thin'>
<p>text text</p>
</div>
</div>
.container{
text-align:center;
padding: 0 5vw; //padding of 5vw to the left and right
}
.thin{
width:80vw;
margin: 0 auto;
background-color:yellow;
}
.full-width{
background-color:green;
}

how to have equal height columns with equal size image in the column

i want to have equal column size with equal size image in the column, for example if the first column have less text and the second column have two or three paragraph text it should be equal, also the image size should equal with the second column image, if the image size is big or small than the first column but it should fit in both column in equal size.
<div class="container">
<div class="row">
<!-- this is one column -->
<div class="gold">
<div class="products owl-one owl-carousel">
<div class="pitem">
<img src="images/world.jpg">
<h3>this is title</h3>
<p>this is paragraph</p>
</div>
</div>
</div>
<!-- this second column -->
<div class="gold">
<div class="products owl-one owl-carousel">
<div class="pitem">
<img src="images/world.jpg">
<h3>this is title</h3>
<p>this is paragraph this is paragraph this is paragraph this is paragraph this is paragraph</p>
</div>
</div>
</div>
<!-- this third column -->
<div class="gold">
<div class="products owl-one owl-carousel">
<div class="pitem">
<img src="images/world.jpg">
<h3>this is title</h3>
<p>this is paragraph this is paragraph this is paragraph</p>
</div>
</div>
</div>
</div>
</div>
Css Code here.....
.gold{background: #d9edfb;}
.nopad{padding:0 !important;margin:0 !important;}
.products{position: relative; padding:25px 0px;}
.pitem {background: #fff; padding:12px; margin:5px; }
.pitem:hover{z-index: 50;border-radius: 3px;text-decoration: none;box-shadow: 0 7px 17px 0 rgba(23,84,116,0.18); border: solid 0.9px #f3f3f3;}
.pitem h3 {center; font-size: 15px}
.pitem img{width:100%; height: auto;}
.pitem p {margin-bottom: 20px; color:#444; text-align: justify;font-weight: 700;}
First: use columns inside of your rows if you are using bootstrap.
Once you have columns, don't mess around with setting heights of the columns themselves (that seems to go against the whole point of using columns that are reaponsive). Instead, try this:
Create a row with two columns.
Create a div in each column. Let's call the div "content". That div will hold images and text.
Then, with JavaScript, I think you can get the height of the 2 divs. Check out this question: How do you get the rendered height of an element?
So let's say the height of the div in column 1 is height1 = 20px and the height in column 2 is height2=50px. Get those values using Javascript, and set each one to a variable.
Use a conditional statement to tell which one is taller. Then set the height of either div to the height of the largest one.
I can't write the code right now, but hopefully this logic will help:
x = contentdiv1.getHeight()
y = contentdiv2.getHeight()
if x >= y --> y.setHeight(x.height)
else --> x.setHeight(y.height)
Use bootstrap class col it will automatically make your div responsive and give it height that you need
Div class=col

Columns with corresponding content

Let's say I have some structured content in HTML – for example text in paragraphs grouped into some sections. And let's say I have another instance of the same structure and I want to display both contents as two columns side by side using HTML and CSS. How to do that? The point is that I want that corresponding elements (paragraphs, sections) inside the columns are aligned so they start at the same height.
Examples of such structures may be a bilingual page, or a source code together with numbers of lines or with some side comments to individual lines.
The only idea I have is to use a table, but I'm not sure it is the best solution. I want to be able to select the content as if the column was an ordinary web page, but selecting in a table works in a way that cells in a row are selected first.
An example follows. Recall that I want the corresponding elements to start at the same height.
<!doctype html>
<html>
<head>
<title>Corresponding columns</title>
<meta charset="utf-8">
<style type="text/css">
.main {
margin: auto;
width: 500px;
}
.column {
float: left;
width: 50%;
}
.corresponding {
background-color: #FFFF99;
}
</style>
</head>
<body>
<div class="main">
<div class="column">
<h1>Section</h1>
<p>Some text</p>
<h2 class="corresponding">Subsection</h2>
<p>Some other text</p>
</div>
<div class="column">
<h1>Section</h1>
<p>The text translated to another language, which may be longer.</p>
<h2 class="corresponding">Subsection</h2>
<p>Some other text</p>
</div>
</div>
</body>
</html>
If you want each section/subsection to start at the same height I would suggest to do like this:
<div class="main">
<div class="row">
<div class="column">
<h1>Section</h1>
<p>Some text</p>
</div>
<div class="column">
<h1>Section</h1>
<p>The text translated to another language, which may be longer.</p>
</div>
</div>
<div class="row">
<div class="column">
<h2 class="corresponding">Subsection</h2>
<p>Some other text</p>
</div>
<div class="column">
<h2 class="corresponding">Subsection</h2>
<p>This text might also be longer so you need to push the next section's as well to start at the same height</p>
</div>
</div>
</div>
Same (almost) as a table but with div's.
I'm no "flex-box" expert so that might be a way, though with less broad browser support.
If you can't/don't want to use the "row" elements (and no/can't/don't want flex option) you will need a javascript snippet that iterate through your elements and compute margins to be set.
UPDATE
Check these 2 links, they will help you set this up as you like using flex:
- https://chriswrightdesign.com/experiments/using-flexbox-today/#card-layout
- http://codepen.io/imohkay/pen/PwPwWd/
A future proof way without using javascript.
UPDATE 2
And this one has some really cool grid solutions:
- https://philipwalton.github.io/solved-by-flexbox/demos/grids/
Well, I dont know what exactly You want... I thing that You might want two sections side-by-side, where You can place anything... Thats what I found:
make two div's <div id="first"> and <div id="second">
and place what You want in them. Now css:
#first {float:left;width:50%;}
#second {float:right;width:50%;}
Make sure You have body {padding:0; margin:0;}
If I understand your question correctly, you are searching for an HTML-structure which shows two items next to each other. Each of the properties of this item (i.e. the subsections) should have the same height. And, when the user selects the text, then the whole row (i.e. the same property from both items) should be selected.
I have the feeling that in order for the user to be able to select content the way you want, the structure needs to be correct, as I believe that the browser selects content according to the structure (not sure, this is always true though).
The question is, if you are free to use any HTML-structure you like?
When I try below example, it works for me. The solution is to use a list (ul with li) per "property" (row), making the lis display as inline-block. That way, they don't break and, as they are block elements, they always have the same height per "line". With vertical-align: top; all content starts at the beginning of the element.
I adjusted the content so we definitely have different line heights and wrapping, just to be sure it works.
Styling:
<style>
ul {
list-style: none;
}
ul li {
display: inline-block;
vertical-align: top;
width: 20%;
}
</style>
HTML:
<ul>
<li>Section 1</li>
<li>Section 2<br/>(with new line)</li>
</ul>
<ul>
<li>Some text</li>
<li>The text translated to another language, which may be longer.<br /><br />
The text translated to another language, which may be longer.
The text translated to another language, which may be longer.
The text translated to another language, which may be longer.
The text translated to another language, which may be longer.
The text translated to another language, which may be longer. </li>
</ul>
<ul>
<li>The text translated to another language, which may be longer.
The text translated to another language, which may be longer.</li>
<li>Some text</li>
</ul>
Thank to LGSon, I have learnt about flex. I tried to put together a solution. The following code somehow works, but there are some issues:
One has to add `order` attribute to all elements.
For some reason flex doesn't overlap margins like it is done in standard situation so all the vertical spaces are bigger.
It would be know hard to e.g. add a border around whole column.
<!doctype html>
<html>
<head>
<title>Corresponding columns</title>
<meta charset="utf-8">
<style type="text/css">
.container {
margin: auto;
width: 500px;
display: flex;
flex-wrap: wrap;
}
.container > * {
flex: 0 0 50%;
}
.corresponding {
background-color: #FFFF99;
}
</style>
</head>
<body>
<div class="container">
<h1 style="order: 1">Section</h1>
<p style="order: 2">Some text</p>
<h2 style="order: 3" class="corresponding">Subsection</h2>
<p style="order: 4">Some other text</p>
<h1 style="order: 1">Section</h1>
<p style="order: 2">The text translated to another language, which may be longer.</p>
<h2 style="order: 3" class="corresponding">Subsection</h2>
<p style="order: 4">Some other text</p>
</div>
</body>
</html>
Use css column-count CSS property: https://css-tricks.com/almanac/properties/c/columns/
Update
After re-reading the question, I think if you want both columns to start at the same height, you can use min-height on both columns, but be aware that the content will later push the height as it grows.
To maintain the height even with content, put a fixed height then have apply overflow-y:auto; or overflow-y:scroll. That way both boxes will have the same height, and can be scrollable in case content grows

Display p elements as block within an inline-block span

So I have some inline-block elements like so:
<span style="display: inline-block">
<img>
<p>Some text</p>
<p>Some more text</p>
<button>A button</button>
</span>
I want them all inline except I want the first p element positioned on top of the other one yet have both together inline with the rest of the span. From what I've been reading, it's bad practice to put a div inside a span, so what's the best way to do this?
It's not "bad practice", it's simply impossible. The browser will "correct" your HTML and it will not behave as expected.
Try using <div style="display:inline-block"> as your container instead.
Here's a wild guess at what you're after based on my comment above.
.table {
display: table;
width: 100%;
}
.row {
display: table-row;
}
.cell {
display: table-cell;
padding: 5px;
background: #ddd;
border: 1px solid #aaa;
vertical-align: top;
}
<div class="table">
<div class="row">
<div class="cell">
<img src="http://lorempixel.com/300/200" />
</div>
<div class="cell">
<div class="table">
<div class="row">
<div class="cell">
<p>Paragraph one. Paragraph one. Paragraph one.</p>
</div>
</div>
<div class="row">
<div class="cell">
<p>Paragraph two. Paragraph two. Paragraph two.</p>
</div>
</div>
</div>
</div>
<div class="cell">
<button>Button</button>
</div>
</div>
</div>
As #Neit pointed out, the browser will correct the DOM when you put block-level elements inside inline elements (see first example). A div, or maybe a section is definitely a better choice both for valid and semantic markup.
Using CSS to change display: does work, but it isn't best practice (for example an em in place of your span above will render exactly the same). Certain versions of browsers will also ignore some types of display: changes; thus, your code would fail. So using a better container is going to provide the fewest headaches.
See the code here:
https://jsfiddle.net/9mf91b1v/

make an image and a paragraph sit next to each other

I have a box with an image and some txt and I want the txt to be horizontal to the image. I know this may be a pretty easy question but I can't seem to find a good answer on the interwebs. thanks for any help.
<div id='container'>
<img src='someimage.jpg'/>
<p>some text</p>
</div>
Take a look at CSS's float property, for example:
<div id='container'>
<img src='someimage.jpg' style='float: left;'/>
<p>some text (that will now wrap around the image</p>
</div>
Since Rowland's 2010 answer, CSS has come a long way. Today we have Flexbox which is a part of CSS for controlling layout of elements in a row or column. It has a great deal of flexibility for handling alignment within the box and expanding or shrinking elements to fit.
Here is a simple example of how to style the HTML you provided (with the image URL changed to one that will render here).
#container {
display: flex;
align-items: center;
}
#container p {
margin-left: 1em;
}
<div id='container'>
<img src='http://placekitten.com/100/100' alt="" />
<p>some text</p>
</div>
The MDN tutorial linked above is a good place to learn more.
I recommend you to put the text and the image in a table, in the same row, the image would be in the first column, while the text goes to the second column in the first row . here's the code
<div id='container'>
<table>
<tr>
<td><img src='someimage.jpg'/></td>
<td><p>some text</p></td>
</tr>
</table>
</div>