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>
Related
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>
i need a solution guys, i want the two boxes in pink and white to have same height as its parent container in blue box (that is to stop where that blue box stopped) without depending on the content of the div in pink and white boxes.
here is my fiddle code
https://jsfiddle.net/dcq4bufa/3/
my HTML CODE
<head>
<title>eco</title>
</head>
<body>
<header class="header">
<ul class="nav">
<li class="nav-items">HOME</li>
<li class="nav-items">APP</li>
<li class="nav-items">STORE</li>
</ul>
<div class="home">
<div class="home__right">
<p class="paragraph">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sapiente nemo
aliquid saepe tempora doloribus dicta quas aperiam, eius odio ipsa culpa ipsam rerum quam vero
itaque, recusandae sint perferendis ipsum.</p>
<h1 class="home__right--sub"> tenetur quidem ducimus quod odit totam, dolor</h1>
</div>
<home class="home__left">
<h1 class="paragraph">Lorem ipsum dolor sit amet, consectetur ad</h1>
</home>
</div>
</header>
</body>
First you must outline the height of the parent container(blue),then set the two children elements(pink and white)height of 100% since they are in a container that is 100 of your device height.Try this:
.home {
display: flex;
height:100vh;
&__right {
background-color: pink;
height:100%;
flex-basis: 50;
}
&__left {
background-color: white;
height:100%;
flex: 1;
}
}
You can adjust the height of .home if you are not comfortable with the overflow
If you can use flexbox, you may do the following:
.header {
height: 80vh;
background-color: blue;
/* Add flexbox */
display: flex;
flex-direction: column;
}
.home {
display: flex;
flex: 1; /* Set flex-grow: 1 */
...
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>
Imagine the following code...
<div style="border: 1px solid #000;"><img src="/images/me.png" style="float: right;"><!--100X250px--> How are you?</div>
Or this...
<li style="border: 1px solid #000;"><img src="/images/me.png" style="float: right;"><!--100X250px--> How are you?</li>
If these containers contained text only, you would see a very slender black box (perhaps 25-50 pixels tall) containing text. In fact, that's exactly what I see - except for the images, which extend above or below the container.
I know how to regulate image width, but how do I handle height? I guess there are two choices: 1) Make each image adapt to its container, or 2) make the container height adjust to the image. I think the second option sounds better. Also, I should point out that the amount of text each container contains is all over the map; some could contain a couple paragraphs.
EDIT:
Sorry, I left out an important piece of information. Most of these images are FLOATED to the right or left of the text. Therefore, I don't want the images to span the width of the container. I'd prefer to somehow make the container higher, so the image doesn't protrude.
Anyway, does anyone have any tips for dealing with this problem?
Would this be a start?
.container {
display: table;
width: 100%;
}
.row {
display: table-row;
}
.cell {
display: table-cell;
border: 1px solid #ccc;
vertical-align: top;
height: 140px;
}
.cell.image {
width: 140px;
height: auto;
background-position: center center;
background-repeat: no-repeat;
background-size: contain;
}
.cell.image img {
max-width: 100%;
width: auto;
height: 100%;
}
<div class="container">
<div class="row">
<div class="cell image" style="background-image: url('http://lorempixel.com/450/300/nature')">
</div>
<div class="cell text">
Some text
</div>
</div>
<div class="row">
<div class="cell text">
Some text
</div>
<div class="cell image" style="background-image: url('http://lorempixel.com/300/450/nature')">
</div>
</div>
</div>
You have to assign a width:100% rule to the image, in order to tell it to stretch 100% (not more) of the parent container.
If you infact try to remove that rule, you'll see the image exceeds the container size.
Here you can see an example:
div{
border:1px solid #000;
}
img{
width:100%;
}
<div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Labore ex, voluptate rerum optio omnis dolorum, deleniti nemo, similique totam voluptatibus quae? Provident assumenda accusamus aliquid laudantium voluptate aperiam dolore! Deserunt!<img src="http://lorempixel.com/550/350/animals" alt=""></div>
EDIT:
If images are floated I suggest you to use two divs, one for text , one for images.
Here is an example:
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
div {
border: 1px solid #000;
}
img{
width:100%;
}
.lfloat {
width: 800px;
margin: 0 auto;
}
.text,
.image {
position: relative;
float: left;
width: 50%;
}
<div class="lfloat">
<div class="text">
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Facere quam, cupiditate, soluta expedita error et adipisci placeat ullam! Eveniet mollitia excepturi eum nesciunt ipsam nihil illo modi voluptas non voluptate.
</p>
</div>
<div class="image"><img src="http://lorempixel.com/300/200/animals" alt=""></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/