Making a <div> that looks like a row in a table? - html

I'm having trouble creating a layout that looks like a row in a table, I need it to look like:
--------- ---------------------------
| | Line of text |
| | Line of text |
| | |
--------- ---------------------------
so I'm trying something like:
<div>
<img src="" />
<div float=left>Line of text</div>
<div float=left>Line of text</div>
</div>
it's not coming out right, it looks like the lines of text don't take up the full height, as high as the bottom of the img. I want to solid-color the entire row, how can I do this?
Thanks

I agree with Scobal's comment....if what you are trying to display is tabular data, then it would semantically be correct to display it in a table.
If not, you could theoretically set the div's img float property to left, and then wrap both of your text divs in an outer div and float that one as well.

looks like a comment with an avatar or user data with avatar if I'm not mistaken.
<div class="user">
<img class="avatar">
<div class="user-info">
<p>line of text</p>
<p>line of text</p>
</div>
</div>
css:
.avatar {
width: <width here>.px;
float: left;
background: #ccc;
}
.user-info {
float: left;
}
Of course remember to clear your floats.
You can also substitute lists for the divs if you want it more semantic :P

Related

HTML Table columns count responsive to window width

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>

How to create html page layout using DIVs

I've spend so many hours trying to figure out how to create html layout in the following format with no luck. Can someone guide me in the right direction? Thanks
-----------------------------------------------------------------
| headerDiv1 | hDiv2 &nbsp|
-----------------------------------------------------------------
| contentDiv1 | cDiv2 &nbsp|
| |
|
| |
&nbsp|
--------------------------------------------------------------
| footerDiv1 | fDiv2 &nbsp|
--------------------------------------------------------------
Start by using a grid system, like http://twitter.github.io/bootstrap/scaffolding.html#gridSystem or many other you around.
It would be much easier to design a layout.
This can also be accomplished pretty easy in just HTML & CSS. YOu can see a working example here - Grid Layout.
Twitter Bootstrap is of course another good option, if you're willing to use an external library. It gives you some other really nice features (such as icons, etc).
If you're doing something basic, I'd probably just go with the CSS & not have to worry about the external library. It'd be worth learning Bootstrap if you have larger projects, or plan on doing things like this often in the future.
HTML
<div id="columns">
<div class="col1">
<div id="headerDiv1">
<p>header div</p>
</div>
<div id="contentDiv1">
<p>content div</p>
</div>
<div id="footerDiv1">
<p>footer div</p>
</div>
</div>
<div class="col2">
<div id="hDiv2">
<p>header div right side</p>
</div>
<div id="cDiv2">
<p>content div right side</p>
</div>
<div id="fDiv2">
<p>footer div right side</p>
</div>
</div>
</div>
CSS
#columns{
display: table;
width: 500px;
}
.col1{
display: table-cell;
width: 80%;
padding: 1em;
position: relative;
left: auto;
}
.col2{
width: 20%;
display: table-cell;
padding: 1em;
position: relative;
}

CSS Header Layout

I am new to laying out webpages without the use of tables, so my apologies if this is a really simple question.
I am attempting to create a header for a page which I want to look something like this:
-------------------------------------------
| | Some big text |
| img | |
| | Some smaller text |
-------------------------------------------
Currently I have the following div, but it does not bottom align the small text like I want:
<div style="height:50px;">
<img src="img.jpg" style="vertical-align:middle; height:100%; float:left"/>
<div style="vertical-align:top;">BigText</>
<div style="vertical-align:bottom;">SmallText</div>
</div>
How should I do this?
Thanks!
My version: http://jsfiddle.net/gT6ze/
Alternatively, you can use position:relative for container div, set padding:60px for it, and position image within it with position:absolute; top:0; left:0;. This way div elements with text can also be positioned inside parent by setting position:absolute and top:0 and bottom:0.
use the padding or margin for the small text div
<div style="height:50px;">
<img src="img.jpg" style="vertical-align:middle; height:100%; float:left"/>
<div style="vertical-align:top;">BigText</>
<div style="vertical-align:bottom;padding-top:15px;">SmallText</div>
</div>​
demo

How to create a HTML container which will fill all available width between two floating elements?

Here is a schema of what I want to render, and as simple as it seems I can't find a way to code the container between the two floating elements...:
---------- ----------
| | Some text text text text text text text text | |
| | text (in a <p> element). | |
| float: | ------------------------------------------ | float: |
| left; | | The container I want to create | | right; |
| | ------------------------------------------ | |
| | Some other text text text text text text tex | |
---------- text text text text text text text text text | |
text text text (in another <p> element). | |
----------
The width of each of the two floating elements is unknown and may vary, so I have to code the container independently of them (as well as I can't change their code). And I want to have its left and right borders along the borders of the floating elements.
For instance, if I use a div element (with display:block) its left and right border are under the two floating elements... If I use a table element (or a div with display:table) it won't fill all available width if there isn't any full text line in it...
I bet there is a simple solution, but I simply can't find it! Thanks for any help!
You have to use a margin to get out of the floats as they are not within the context of the containing element. It can work with a fluid or fixed width design:
<div id="LeftColumn" style="float: left; width: 20%;">
<p>Left Column</p>
</div>
<div id="CenterColumn" style="margin: 0 25%;">
<p>Some text text text text text text text text text (in a <p> element).</p>
<div style="width: 100%; text-align:center;">
<p>The container I want to create</p>
</div>
<p>Some text (in another <p> element).</p>
</div>
<div id="RightColumn" style="float: right; width: 20%;">
<p>Right Column</p>
</div>
Based on the answer by #durilai (I believe this solves the problem more accurately, but that depends on what you really want to do):
<div style="float: left; width: 20%;">
<p>Left Column</p>
</div>
<p>Some text text text text text text text text text (in a <p> element).</p>
<div style="margin: 0 25%;">
<p>The container I want to create</p>
</div>
<p>Some text (in another <p> element).</p>
<div style="float: right; width: 20%;">
<p>Right Column</p>
</div>
This will, unlike #durilais solution, behave like with normal floats when text is "higher" than the floats. It will still require you to know the widths of the floats, and it will not behave correctly once the innermost <div> is below the two floats, but it's about as close to a solution you can get.
It's not possible. Here's why:
The width property can take any of these values:
a fixed length
a percentage
inherit (=100%)
auto (=100%-(margins+borders+paddings))
Neither of these will work with your prerequisites:
you need to know the page width and the floating containers' widths
you need to know the floating containers' widths as percentages
this will not leave the floating containers alone
you need to know the floating containers' widths and set the margins accordingly
I got it! Ok it's a kind of hack and it uses a table, but it works on every single browser (even IE6), and I may manage to clean it up a bit now I've got it working...
The key is to create a table with at least two cells, and to set the width of one of them to 100%; this way the browser will display this cell as large as possible in order to get its actual displayed size as close to 100% of the whole table as possible. And now I can put what I want in this cell.
Here is the corresponding HTML code:
<div style="float:left;">...</div>
<div style="float:right;">...</div>
<p>Some text...</p>
<table>
<tr>
<td style="width:100%;">
Some content
</td>
<td style="width:1px;"/>
</tr>
</table>
<p>Some other text...</p>
This way, the container will display correctly regardless of the size of whatever is in the two floating div. And if the 1px-wide empty cell on the right is a problem, I can always put another one on the left to get it symmetrical and less visually annoying (but as a matter of fact, with a 0-border-spacing on the table and a 0-padding on each cell, the 1px-wide empty cell is actually not visible...).
Now I've got to find a way, if possible, to get all this a bit cleaner (and maybe even without using a table element?).
<div id="Main" style="margin: 0 25%;">
<p>Hello</p>
</div>
<style type="text/css">
#Main
{
width:1000;
height:670;
background-color:White;
color:black;
}
</style>

Both columns same height as deepest column?

If I have a div layout like this:
<div id="stretchyheader"></div>
<div id="fixedwidthwide"><div>
<div id="fixednarrow></div>
<div id="footer"></div>
Which makes something like this:
-----------------------------------------------------
| stretchyheader |
-----------------------------------------------------
| | |
| | |
| fixedwidthwide | fixednarrow |
| | |
| | |
| | --------------
| |
| |
| | patterned
| | background
-----------------------
- footer -
How do I ensure that both columns are the same height as the deepest column? The column heights are flexible according to the amount of content and have a white background.
A very simple, common way to do this is using Faux Columns.
You would have a structure that looked something like this:
<div id="stretchyheader"></div>
<div id="container">
<div id="fixedwidthwide"></div>
<div id="fixednarrow></div>
</div>
<div id="footer"></div>
And you actually apply a background image to #container to add any background colors, borders, etc. to each of the 2 columns.
There are CSS techniques to do this without faking it, but they are much more complex:
http://matthewjamestaylor.com/blog/equal-height-columns-cross-browser-css-no-hacks
http://matthewjamestaylor.com/blog/ultimate-2-column-right-menu-pixels.htm
http://www.socialgeek.be/blog/read/flexible-equal-height-columns
Adapted from here:
Create a container around the two fixed columns, and have css something like this:
#container {
float:left;
width:[sum of the two columns width];
}
#fixedwidthwide {
float:left;
width:[whatever];
}
#fixednarrow {
float:left;
width:[whatever];
}
Note that this is only necessary if the columns need to be of equal height for some reason. If not, you can just follow philfreo's suggestion and use faux columns.
There are a number of solutions for this problem, including OneTrueLayout Technique, Faux Columns Technique and CSS Tabular Display Technique.
The best solution for equally height-ed columns is the CSS Tabular Display Technique that means to use the display:table feature.
It works for Firefox 2+, Safari 3+, Opera 9+ and IE8.
The code for the CSS Tabular Display:
The HTML
<div id="container">
<div id="rowWraper" class="row">
<div id="col1" class="col">
Column 1<br />Lorem ipsum<br />ipsum lorem
</div>
<div id="col2" class="col">
Column 2<br />Eco cologna duo est!
</div>
<div id="col3" class="col">
Column 3
</div>
</div>
</div>
The CSS
<style>
#container{
display:table;
background-color:#CCC;
margin:0 auto;
}
.row{
display:table-row;
}
.col{
display: table-cell;
}
#col1{
background-color:#0CC;
width:200px;
}
#col2{
background-color:#9F9;
width:300px;
}
#col3{
background-color:#699;
width:200px;
}
</style>
Even if there is a problem with the auto-expanding of the width of the table-cell it can be resolved easy by inserting another div withing the table-cell and giving it a fixed width. Anyway, the over-expanding of the width happens in the case of using extremely long words (which I doubt anyone would use a, let's say, 600px long word) or some div's who's width is greater than the table-cell's width.
The Faux Column Technique could be a solution to this problem, but it has some drawbacks such as, you have to resize the background tiled image if you want to resize the columns and it is also not an elegant solution.
The OneTrueLayout Technique consists of creating a padding-bottom of an extreme big height and cut it out by bringing the real border position to the "normal logical position" by applying a negative margin-bottom of the same huge value and hiding the extent created by the padding with overflow:hidden applied to the content wraper. A simplified example would be:
The HTML file:
<html><head>
<style>
.wraper{
background-color:#CCC;
overflow:hidden;
}
.floatLeft{
float:left;
}
.block{
padding-bottom:30000px;
margin-bottom:-30000px;
width:100px;
background-color:#06F;
border:#000 1px solid;
}
</style>
</head>
<body>
<div class="wraper">
<div class="block floatLeft">first col</div>
<div class="block floatLeft">
Second col<br />Break Line
</div>
<div class="block floatLeft">Third col</div>
</div>
</body>
</html>
In my opinion the unimplemented 100% height within an automated height container is a major drawback and the W3C should consider revising this attribute.
Other resources: link1, link2, link3, link4, link5 (important)