My understanding of table-cell is that each table-cell's height will expand to be the same height as the tallest table-cell; however, in this example, .col2's height extends below .col1 when the img within .col1 is set to display: block and the text within .col2 is pushed all the way to the bottom. If display: block is removed from the img, .col1 and .col2 line up with the text aligned to the middle. My question is why does the display: block on the img change how the table-cells are rendered and how the text is aligned. Toggle display: block on the img to see the difference.
https://codepen.io/norkuy/pen/EvQQrE
HTML
<div class="row1">
<div class="col1">
<img src="http://lorempixel.com/1000/800/" alt="">
</div>
<div class="col2">
<p><div>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Amet
mollitia est maiores temporibus ea, sint ex repellat ipsa eveniet nesciunt.
</div></p>
</div>
</div>
<p><div>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ab veniam
praesentium delectus aliquid ea nisi porro eius rem debitis architecto quas,
hic placeat ratione possimus voluptates perferendis at voluptatibus tenetur!
</div></p>
CSS
.col1, .col2 {
width: 50%;
display: table-cell;
}
.col2 {
background: black;
color: white;
}
.row1 {
display: table;
img {
display: block;
}
}
img {
max-width: 100%;
width: 100%;
}
If you want your table-cell elements work properly, your cells container has to have table-row display
.table {
display: table;
width: 100%;
}
.row {
display: table-row;
}
.cell {
display: table-cell;
width: 50%;
vertical-align: middle;
border-bottom: 1px solid #000;
}
<div class="table">
<div class="row">
<div class="cell">
<br/><br/>fdfsf
</div>
<div class="cell">
dffdsf
</div>
</div>
</div>
Related
I'm trying to recreate this but I'm stuck on getting the image and text to fit inside the innermost border. the final is supposed to have an outside border, an inside border, and a div that expands across the top. Then a picture that is 30% width of the top spanning div. Then text that is supposed to be width 70% of the top spanning div.
This is what I got so far HTML:
:root {
--winter-primary: #ffd110;
}
.outside-winter-border {
border: 2px solid var(--winter-primary);
margin: auto;
max-width: 1000px;
}
.insdie-winter-border {
border: 2px solid var(--winter-primary);
margin: 20px;
}
.wh {
background-color: var(--winter-primary);
padding: 30px;
}
.winter-image {
width: 30%;
float: left;
}
.wi {
width: 70%;
float: right;
}
<section class="winter">
<div class="outside-winter-border">
<div class="insdie-winter-border">
<div class="section-heading">
<div class="wh">
<h2>Winter</h2>
</div>
</div>
<div class="content-wrapper">
<div class="winter-image">
<div class="section-image">
<a href="winter.html">
<img src="assets/images/winter.jpg" alt="Winter Image">
</a>
</div>
</div>
<div class="wi">
<div class="section-content">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Impedit dolore enim sequi dignissimos vel fugit reiciendis minus voluptatem nostrum, at repellat odio libero cum eveniet officiis, cumque veritatis, qui eaque.
</p>
</div>
</div>
</div>
</div>
</div>
</section>
The exact desired result is unclear. However, here is a version using flex that gets both the image and text within the border. Don't use float.
.outside-winter-border {
border: 2px solid var(--winter-primary);
margin: auto;
max-width: 1000px;
}
.insdie-winter-border {
border: 2px solid var(--winter-primary);
}
img {
height: 100%;
}
.wh {
display: inline-flex;
}
.insdie-winter-border {
display: flex;
align-items: center;
}
:root {
--winter-primary: #ffd110;
}
p {
margin-left: 1em;
}
.section-heading {
width: 100%;
background-color: var(--winter-primary);
text-align: center;
}
.section-heading>h2 {
margin: 0;
}
<section class="winter">
<div class="outside-winter-border">
<div class="section-heading">
<h2>Winter</h2>
</div>
<div class="insdie-winter-border">
<div class="wh">
<a href="winter.html">
<img src="https://dummyimage.com/125/000/fff" alt="Winter Image">
</a>
</div>
<div class="content-wrapper">
<div class="winter-image">
</div>
<div class="wi">
<div class="section-content">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Impedit dolore enim sequi dignissimos vel fugit reiciendis minus voluptatem nostrum, at repellat odio libero cum eveniet officiis, cumque veritatis, qui eaque.
</p>
</div>
</div>
</div>
</div>
</div>
</section>
I think you can try this (insted of .winter-image and .wi ):
.content-wrapper {
display: flex;
}
.winter-image {
flex: 3;
}
.wi {
flex: 7;
}
I'm creating an 'About' section for a website, which is a table with three equal-width columns: a headshot, a paragraph, and another paragraph (see screenshots below).
I'd like to have the image automatically resize (keeping its aspect ratio) to be the height of the largest text- aligned left within the cell- without hardcoding any height/width values. However, I've played around a bunch and nothing seems to make the image resize.
/* an element that's one-third the width of its container */
.third-width {
width: 33%;
}
/* the headshot photo */
#headshot {
border-radius: 5px;
max-width: 100%;
max-height: 100%;
display: inline-block;
}
#about-table td {
background-color: pink;
padding: 1vw;
text-align: justify;
vertical-align: top;
font-family: var(--body-font);
font-weight: lighter;
}
<table id="about-table">
<tr>
<td class="third-width">
<img id="headshot" src="https://via.placeholder.com/80" alt="None">
</td>
<td class="third-width">
<p>[... SOME TEXT ...]</p>
</td>
<td class="third-width">
<p>[... SOME TEXT ...]</p>
</td>
</tr>
Current state:
What I would like:
Thank you!
Use CSS Flex instead of table
Make the cells flex: 1; position: relative;
Make the image position: absolute; with 100% W/H and object-fit: cover to not distort the image
/* QuickReset */
* { margin:0; box-sizing: border-box; }
/* About component */
.About {
display: flex;
}
.About > div {
position: relative;
flex: 1;
outline: 1px solid #000;
padding: 20px;
}
.About > div img {
position: absolute;
top:0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
object-fit: cover;
}
<div class="About">
<div><img src="https://placekitten.com/408/287" alt="Catz!"></div>
<div>Lorem ipsum</div>
<div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestiae sunt nisi nostrum sed, assumenda sequi doloribus excepturi quibusdam obcaecati tenetur tempora voluptatibus eligendi dolorem. Excepturi perspiciatis ipsa porro, minus ea.</div>
</div>
<div class="About">
<div>Lorem ipsum</div>
<div>Molestiae sunt nisi nostrum sed, ipsa porro, minus ea.</div>
<div><img src="https://placekitten.com/500/300" alt="Catz!"></div>
</div>
<div class="About">
<div><img src="https://placekitten.com/310/290" alt="Catz!"></div>
<div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestiae sunt nisi nostrum sed, assumenda sequi doloribus excepturi quibusdam obcaecati tenetur</div>
<div>Lorem ipsum</div>
</div>
How can two divs be styled so that:
they are side by side
the right div expands to fit its contents
the left div expands to fit the space to the left of the right div,
but whose content wraps when it fills the div.
The effect can be achieved with tables like this:
<table class="container">
<tr>
<td class="max">
some text that we want to wrap within the div
</td>
<td class="min">
some other text
</td>
</table>
and
.container {
width: 100%;
}
td.min {
width: 1%;
white-space: nowrap;
border:1px solid blue;
}
td.max {
border:1px solid red;
}
In the following solution with divs, the left div doesn't fill the space if the content doesn't force it to; and the left div will appear above the right when it's content expands.
<div id="container">
<div id="left">some text that we want to wrap within the div</div>
<div id="right">some other text</div>
</div>
and
#container {
width: 100%;
}
#left {
float:left;
border: 1px solid red;
}
#right {
float:right;
border: 1px solid blue;
}
I think you want flexbox
#container {
width: 50%;
margin: 1em auto;
display: flex;
}
#left {
background: lightblue;
flex: 1;
}
#right {
background: lightgreen;
}
<div id="container">
<div id="left">some text that we want to wrap within the div</div>
<div id="right">some other text</div>
</div>
You can use flexbox:
.container {
display: flex;
width: 20%;
}
.right {
white-space: nowrap;
}
<div class="container">
<div class="left">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eum odit saepe eius quisquam! Adipisci obcaecati quia sit eaque quo provident, corrupti iure nisi consequuntur, vero nulla molestias, placeat esse ut!</div>
<div class="right">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Odit ullam facilis blanditiis, necessitatibus nihil dicta cupiditate cum vero quod ipsam, nulla minus maxime asperiores natus reprehenderit eaque error ab porro?</div>
</div>
I have product items to display like this
Now the challenge for me here is vertically middle aligning the text in first & third column, whereas the text in the middle column can increase or decrease reasonably, but i have to keep the content in the left & right column always vertically middle aligned.
e.g here is the code
<div class="product">
<div class="title">My Test Title</div>
<div class="price">
From<br/>
$2500
</div>
<div class="description">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Et blanditiis, quibusdam commodi beatae, dolorum reiciendis, ex veniam esse recusandae iusto sapiente labore quisquam illo deserunt odio non magni velit! Sed.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Et blanditiis, quibusdam commodi beatae, dolorum reiciendis, ex veniam esse recusandae iusto sapiente labore quisquam illo deserunt odio non magni velit! Sed.</p>
</div>
</div>
// AND THE CSS is
.product {
border: 1px solid #CCC;
}
.product > div {
padding : 10px;
border: 1px solid #CCC;
}
.title
{
width : 120px;
float:left;
}
.price {
width:120px;
float:right;
text-align:right;
}
.description {
overflow:hidden;
}
Here is the fiddle : https://jsfiddle.net/exleedo/rexwya5e/
Is there a way to do that without using as display:table-cell; or using javscript to find the height of the parent ?
The reason with using table-cell is because table-cell won't take width anymore from css, and using JS to calculate the height of the parent is not very great I think.
I think I am gonna go with display:table-cell; because that seems the most appropriate one for me. The reason I didn't go with this before is that I was trying to assign width to the table-column div, which is not correct. Now I have add a div inside table-column and assign width there which solves my concern.
e.g here is the code :
<div class="table product">
<div class="table-column">
<div class="title">
The Title
</div></div>
<div class="table-column">
<div class="description">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Porro fugit vero non dolorum, nisi modi veritatis ipsum magni, praesentium blanditiis vel error, odit dolores atque culpa, ratione tempore iusto neque. </p>
</div>
</div>
<div class="table-column">
<div class="price">
Price
</div>
</div>
</div>
// And the CSS
/* Start : CSS Table*/
.table .table-column {
display: table-cell;
vertical-align: middle;
padding-right: 10px;
}
.table .table-column.align-top {
vertical-align: top;
}
.table .table-column.align-bottom {
vertical-align: bottom;
}
.table .table-column.padding-left-10px {
padding-left: 10px;
}
.table .table-column.padding-right-15px {
padding-right: 15px;
}
.table .table-column.padding-right-20px {
padding-right: 20px;
}
.table .table-column.padding-right-40px {
padding-right: 40px;
}
.table .table-column.no-paddings {
padding: 0px;
}
.table .table-column.full {
width: 100%;
}
/* End : CSS Table*/
.product {
border: 1px solid #CCC;
}
.product > div {
padding : 10px;
border: 1px solid #CCC;
}
.title
{
width : 120px;
float:left;
}
.price {
width:120px;
float:right;
text-align:right;
}
.description {
overflow:hidden;
}
Check it in this fiddle : https://jsfiddle.net/exleedo/zL9arsj7/1/
In this fiddle: http://jsfiddle.net/tvqdrwp9/3/
I want the images (which could be any size), to stretch the height of all the adjacent table-cells to match. There will only be 2 rows, with 2 cells each. One cell contains an image, the other text. I want the text cells to match the height of the adjacent image. I have overflow:hidden on the cells, so image overflowing horizontally is not an issue.
The text in boxes 2 and 3 should be vertically aligned in the middle, and the rows should be dictated by the heights of the images in boxes (cells) 1 and 4.
I can't understand why I am still getting a red line at the bottom of each image.
.about-boxes {
display: table;
max-width: 600px;
}
.about-box-row {
display: table-row;
height: 100%;
}
.about-box {
display: table-cell;
vertical-align: middle;
width: 50%;
overflow: hidden;
}
.about-box img {
width: auto;
height: 100%;
}
.about-box-1,
.about-box-4 {
background: red;
}
.about-box-2,
.about-box-3 {
background: #CCC;
color: #000;
}
<div class="about-boxes">
<div class="about-box-row">
<div class="about-box about-box-1">
<img src="http://dummyimage.com/300x180/000/fff">
</div>
<div class="about-box about-box-2">
<h2>Text</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perspiciatis velit repellat voluptate eum est re- iciendis eius recusandae molestiae iusto, dolor quis- quam voluptas.</p>
</div>
</div>
<div class="about-box-row">
<div class="about-box about-box-3">
<h2>Text</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisic- ing elit. Perspiciatis velit repellat voluptate eum est reiciendis eius recusandae molestiae iusto, dolor quisquam voluptas.</p>
</div>
<div class="about-box about-box-4">
<img src="http://dummyimage.com/320x360/000/fff">
</div>
</div>
</div>
You can either use display: block or vertical-align: middle on the image to correct the alignment of it.
.about-box img {
width: auto;
height: 100%;
display: block;
}
Or..
.about-box img {
width: auto;
height: 100%;
vertical-align: middle;
}
Both work.