Make div width scale to width of content - html

I was looking at this example: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_chat
and was interested in changing it so the maximum width of a message container is that of it's inner content (the length of the text). Currently the message container fills up the width of the body.
I have been trying many display methods etc however I've been unable to get the desired result.

body {
margin: 0 auto;
max-width: 800px;
padding: 0 20px;
}
.container {
border: 2px solid #dedede;
background-color: #f1f1f1;
border-radius: 5px;
padding: 10px;
margin: 10px 0;
}
.darker {
border-color: #ccc;
background-color: #ddd;
}
.container::after {
content: "";
clear: both;
display: table;
}
.container img {
float: left;
max-width: 60px;
width: 100%;
margin-right: 20px;
border-radius: 50%;
}
.container img.right {
float: right;
margin-left: 20px;
margin-right:0;
}
.time-right {
float: right;
color: #aaa;
}
.time-left {
float: left;
color: #999;
}
.section {
padding: 20px;
background-color: #fafafa;
display: flex;
flex-direction: column;
}
.container {
margin-left: 0;
margin-right: auto;
}
.container.darker {
margin-left: auto;
margin-right: 0;
}
<section class="section">
<div class="container">
<img src="https://www.w3schools.com/w3images/bandmember.jpg" alt="Avatar" style="width:100%;">
<p>Hello. How are you today?</p>
<span class="time-right">11:00</span>
</div>
<div class="container darker">
<img src="https://www.w3schools.com/w3images/avatar_g2.jpg" alt="Avatar" class="right" style="width:100%;">
<p>Hey! I'm fine. Thanks for asking!</p>
<span class="time-left">11:01</span>
</div>
<div class="container">
<img src="https://www.w3schools.com/w3images/bandmember.jpg" alt="Avatar" style="width:100%;">
<p>Sweet! So, what do you wanna do today?</p>
<span class="time-right">11:02</span>
</div>
<div class="container darker">
<img src="https://www.w3schools.com/w3images/avatar_g2.jpg" alt="Avatar" class="right" style="width:100%;">
<p>Nah, I dunno. Play soccer.. or learn more coding perhaps?</p>
<span class="time-left">11:05</span>
</div>
</section>

just adding display:inline-flex solves your problem but you may still need to add clearfix div at end of every container here is working example to help you out.Hope that helps
body {
margin: 0 auto;
max-width: 800px;
padding: 0 20px;
}
.container {
border: 2px solid #dedede;
background-color: #f1f1f1;
border-radius: 5px;
padding: 10px;
margin: 10px 0;
display:inline-flex;
}
.darker {
border-color: #ccc;
background-color: #ddd;
direction:rtl;
}
.container::after {
content: "";
clear: both;
display: table;
}
.container img {
float: left;
max-width: 60px;
width: 100%;
margin-right: 20px;
border-radius: 50%;
}
.container img.right {
float: right;
margin-left: 20px;
margin-right:0;
}
.time-right {
float: right;
color: #aaa;
}
.time-left {
float: left;
color: #999;
}
.clearfix::after {
content: "";
clear: both;
display: table;
}
<h2>Chat Messages</h2>
<div class="container clearfix">
<img src="https://www.w3schools.com/w3images/bandmember.jpg" alt="Avatar" style="width:100%;">
<p>Hello. How are you today?</p>
<span class="time-right">11:00</span>
</div>
<div id="clearfix"></div>
<div class="container darker clearfix">
<img src="https://www.w3schools.com/w3images/bandmember.jpg" alt="Avatar" class="right" style="width:100%;">
<p>Hey! I'm fine. Thanks for asking!</p>
<span class="time-left">11:01</span>
</div>
<div id="clearfix"></div>
<div class="container clearfix">
<img src="https://www.w3schools.com/w3images/bandmember.jpg" alt="Avatar" style="width:100%;">
<p>Sweet! So, what do you wanna do today?</p>
<span class="time-right">11:02</span>
</div>
<div id="clearfix"></div>
<div class="container darker clearfix">
<img src="https://www.w3schools.com/w3images/bandmember.jpg" alt="Avatar" class="right" style="width:100%;">
<p>Nah, I dunno. Play soccer.. or learn more coding perhaps?</p>
<span class="time-left">11:05</span>
</div>
<div id="clearfix"></div>

Related

Hidden the overflow part in 2 column

I have 3 images and I want to arrange them in 2 columns, one column for 1 image and the other for 2 images. The problem is 2 columns have different heights. And I want to hide the overflow part of the taller column. The picture below shows that.
Here is my HTML:
.row:before,
.row:after {
content: "";
display: table;
}
.row:after {
clear: both;
}
.col {
display: block;
float: left;
margin: 1% 0 1% 1.6%;
}
.section-features img {
width: 100%;
}
.section-features .span-1-of-3 {
width: 33.3%;
padding-left: 5px;
padding-top: 5px;
padding-right: 5px;
}
.section-features .span-2-of-3 {
width: 66.6%;
padding-top: 5px;
padding-left: 5px;
}
<section class="section-features">
<div class="row">
<div class="col span-2-of-3">
<img src="resources/img/modern-brownstone.jpeg" alt="Mordern Brownstone Picture">
</div>
<div class="col span-1-of-3">
<div class="row">
<img src="resources/img/intimate-setting.jpg" alt="An Intimate Setting">
</div>
<div class="row">
<img src="resources/img/edgy-classic.jpg" alt="Edge Classic">
</div>
</div>
</div>
</section>
So what you can do is that give a max-height to your section and set overflow:hidden that will do. Below is a demo for the same.
section {
max-height: 400px;
overflow: hidden;
}
.img1 {
height: 500px;
background-color: red;
display: inline-block;
width: 200px;
float: left;
}
.container {
width: 200px;
}
.img2 {
height: 200px;
background-color: blue;
display: inline-block;
width: 200px;
float: left;
}
div {
margin:5px;
}
.img3 {
height: 200px;
background-color: grey;
display: inline-block;
width: 200px;
float: left;
}
<section>
<div class="img1">
</div>
<div class="conatiner">
<div class="img2">
</div>
<div class="img3">
</div>
</div>
</section>
Hope this helps :)
Change the portion of the images in the .span-2-of-3 and .span-1-of-3 classes. Do not divide the images from 100% because you have added some padding, therefore reduce the padding from the total width. Just to be sure, add overflow: hidden; in the .section-features img.
.row:before,
.row:after {
content: "";
display: table;
}
.row:after {
clear: both;
}
.col {
display: block;
float: left;
margin: 1% 0 1% 1.6%;
}
.section-features img {
width: 100%;
overflow: hidden;
}
.section-features .span-1-of-3 {
width: 30%;
padding-left: 5px;
padding-top: 5px;
padding-right: 5px;
float: right;
}
.section-features .span-2-of-3 {
width: 60%;
padding-top: 5px;
padding-left: 5px;
height: 100%;
}
<section class="section-features">
<div class="row">
<div class="col span-2-of-3">
<img src="https://png.pngtree.com/element_origin_min_pic/16/07/22/2057921811589a1.jpg" alt="Mordern Brownstone Picture">
</div>
<div class="col span-1-of-3">
<div class="row">
<img src="https://png.pngtree.com/element_origin_min_pic/16/07/22/2057921811589a1.jpg" alt="An Intimate Setting">
</div>
<div class="row">
<img src="https://png.pngtree.com/element_origin_min_pic/16/07/22/2057921811589a1.jpg" alt="Edge Classic">
</div>
</div>
</div>
</section>
.section-features .row {
display: table;
width: 100%;
}
.col {
display: table-cell;
}
.col.span-2-of-3 {
width: 70%;
background-color: #f00;
}
.col.span-1-of-3 {
width: 30%;
}
.col.span-1-of-3 .row {
margin-bottom: 20px;
background-color: yellow;
height: 100px;
}
.col.span-1-of-3 .row:last-child {
margin-bottom: 0px;
}
<section class="section-features">
<div class="row">
<div class="col span-2-of-3">
</div>
<div class="col span-1-of-3">
<div class="row">
</div>
<div class="row">
</div>
</div>
</div>
</section>

Position div behind overlapping div

I've got the following setup http://jsfiddle.net/47x60k4w/529/.
HTML
<div class="header">
header
</div>
<div class="inner_block">
<div class="column">
<img src="xxx" />
</div>
<div class="column">
<img src="xxx" />
</div>
<div class="column">
<img src="xxx" />
</div>
</div>
<div class="footer">
footer
</div>
The inner_block should overlap the header class and the footer should be placed right behind the inner_block.
In my solution I just don't get the footer behind the inner_block without doing not responsible stuff like calling a margin-top with x.xem on it. I just found some links with z-index stuff which didn't worked for me because the inner_block lost his passed height and width from the nested block.
The result should look like this beautiful mockup.
Do you have any ideas?
Thanks in advance.
So I made the following changes to your code:
Remove the position: absolute for the inner-block.
As you are floating the contents of the inner-block you have clear the floats so that the parent container will not lose height.
.inner_block:after {
content: '';
display: block;
clear: both;
}
Whenever using floats, remember to clear it.
Added position: relative to the inner_block to position it over the header and footer.
Added display: block to the img so that you can remove the small space below it characteristic on inline elements (the default display).
Also tinkered a bit with the margins and widths to achieve the layout.
.header {
position: relative;
background-color: black;
width: 100%;
height: 50px;
}
.footer {
clear: both;
background-color: red;
width: 100%;
height: 50px;
}
.inner_block {
position: relative;
/*width: 100%;*/
border: solid 1px black;
padding: 5px;
margin-left: 2.5%;
margin-top: -2.5%;
margin-right: 2.5%;
margin-bottom: 2.5%;
background-color: white;
}
.inner_block:after {
content: '';
display: block;
clear: both;
}
.column {
max-width: 30%;
float: left;
margin-right: 2.5%;
}
.column:first-child{
margin-left: 2.5%;
}
.column:last-child{
margin-left: 0;
}
.column img {
max-width: 100%;
height: auto;
display: block;
}
<div class="header">
</div>
<div class="inner_block">
<div class="column">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088605.jpg" />
</div>
<div class="column">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088607.jpg" />
</div>
<div class="column">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088606.jpg" />
</div>
</div>
<div class="footer">
test
</div>
Hope this gives you a head-start. Check it out and let me know your feedback on this. Thanks!
Alternate Solution:
So here is a solution using a flexbox which is easier to set up:
First remove the floating container and the clearfix.
Now Wrap the inner_block with another div
.inner_block_wrapper {
margin: -2.5% 2.5% 2.5% 2.5%;
background-color: white;
position: relative;
}
.inner_block {
border: solid 1px black;
background-color: white;
padding: 5px;
display: flex;
justify-content: center;
}
.column {
margin: 5px;
}
Using display: flex allows the images to take the available space along the row and justify-content: center aligns it along the center. Check this out!
.header {
position: relative;
background-color: black;
width: 100%;
height: 50px;
}
.footer {
clear: both;
background-color: red;
width: 100%;
height: 50px;
}
.inner_block_wrapper {
margin: -2.5% 2.5% 2.5% 2.5%;
background-color: white;
position: relative;
}
.inner_block {
border: solid 1px black;
background-color: white;
padding: 5px;
display: flex;
justify-content: center;
}
.column {
margin: 5px;
}
.column img {
max-width: 100%;
height: auto;
display: block;
}
<div class="header">
</div>
<div class="inner_block_wrapper">
<div class=" inner_block ">
<div class="column ">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088605.jpg " />
</div>
<div class="column ">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088607.jpg " />
</div>
<div class="column ">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088606.jpg " />
</div>
</div>
</div>
<div class="footer ">
test
</div>
You can even try something as below, your codes were fine just set your .footer margin-top equal to the height of .header and .inner_block using css calc() function.
.header{
position:relative;
background-color:black;
width:100%;
height:50px;
}
.footer{
background-color:red;
width:100%;
height:50px;
margin-top:calc(100% - 82%);
}
.inner_block{
position: absolute;
width:90%;
border:solid 1px black;
padding: 5px;
background-color:white;
margin:-2.5% calc(100% - 97%);
}
.column {
width:30%;
float:left;
margin:0 1.6%;
}
.column img {
max-width:100%;
height:auto;
}
<div class="header">
</div>
<div class="inner_block">
<div class="column">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088605.jpg" />
</div>
<div class="column">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088607.jpg" />
</div>
<div class="column">
<img src="http://www.healthytravellovers.com/wp-content/uploads/2016/09/photo233227749810088606.jpg" />
</div>
</div>
<div class="footer">
test
</div>
is this what you were looking for ?
.header{
position:relative;
background-color:black;
width:100%;
height:50px;
}
.footer{
clear:both;
background-color:red;
width:100%;
height:50px;
}
.inner_block{
position: absolute;
width:100%;
border:solid 1px black;
padding: 5px;
margin-left: 2.5%;
margin-top:-2.5%;
background-color:white;
}
http://jsfiddle.net/8y4e8L08/
.header {
height: 200px;
width:800px;
background-color:#000;
margin:20px;
}
.header {
margin-bottom: -25px;
}
.inner_block {
width: 35%;
height: 150px;
margin: auto 200px;
background-color:#FFF;
border:1px solid #000;
margin-top: -45px;
}
.column{
max-width:20%;
float:left;
border: 2px soid #999;
margin:25px;
}
.column img{
max-width:100%;
height:auto;
}
.footer {
height: 100px;
margin-top: -25px;
margin:20px;
background-color:#F00;
width:800px;
}
.content {
position: relative;
z-index: 1;
}
<div class="header"></div>
<div class="inner_block">
<div class="column">
<img src="download.jpg"/>
</div>
<div class="column">
<img src="download.jpg"/>
</div>
<div class="column">
<img src="download.jpg"/>
</div>
</div>
<div class="footer">
</div>
Well just using the z-index won't always work. You also need to specify the 'position' property as well so as to define the z-index wrt some position of the frame.
Z-index is a property which defines the 'depth' or 'height' of an element. If your <header> has z-index of '100' and; <div> element defined inside the header, usually it would be shown above it but once you define the z-index:50; since 50<100, <div> element would be hidden behind it.
Example of z-index
1) http://www.w3schools.com/cssref/tryit.asp?filename=trycss_zindex
2) https://css-tricks.com/almanac/properties/z/z-index/
Hope it helps.

separator by not using width hack

I often do the width hack 49% and border 1px to do seperator for 2 column. It worked, just like the below demo. But is there any better way of doing it? I want to avoid this 49% hack, because when the viewport shrink to a larger or smaller size, it's obvious and the design will break.
body{
margin:0;
}
.left {
background: #eee;
float: left;
width: 49%;
border-right:1px solid #333;
}
.right {
background: #eee;
float: right;
width: 50%;
}
img {
margin: 0 auto;
display: block;
width: 44px;
padding: 5px 0;
}
<div class="navigate" style="width: 170px;">
<div class="left">
<img src="https://cdn0.iconfinder.com/data/icons/arrow-set/512/previous_arrow_point_flat-128.png">
</div>
<div class="right">
<img src="https://cdn0.iconfinder.com/data/icons/arrow-set/512/next_arrow_point_flat-128.png">
</div>
</div>
You can use box-sizing
CSS
body {
margin:0;
}
.left {
background: #eee;
float: left;
width: 50%;
border-right:1px solid #333;
box-sizing:border-box;
}
.right {
background: #eee;
float: right;
width: 50%;
}
img {
margin: 0 auto;
display: block;
width: 44px;
padding: 5px 0;
}
HTML
<div class="navigate" style="width: 170px;">
<div class="left">
<img src="https://cdn0.iconfinder.com/data/icons/arrow-set/512/previous_arrow_point_flat-128.png">
</div>
<div class="right">
<img src="https://cdn0.iconfinder.com/data/icons/arrow-set/512/next_arrow_point_flat-128.png">
</div>
</div>
DEMO HERE

How to create this hover effect on portfolio page?

I'm trying to write a homepage with this little effect on a hover.
It should look something like this:
How would you do this? My code does not work like it should.
.stage_wrapper {
margin: 0 auto;
width: 1100px;
height: auto;
}
.work {
margin-left: 8px;
margin-right: 5px;
margin-bottom: 15px;
display: inline-block;
height: 350px;
width: 350px;
}
.work img {
width: 350px;
height: auto;
}
<div class="stage_wrapper">
<div class="stage">
<div class="work">
<a href="#" target="_blank">
<img src="//dummyimage.com/350" class="media" alt="#" />
<div class="caption">
<div class="work_title">
<h1>Something in 3 rows</h1>
</div>
</div>
</a>
</div>
</div>
</div>
Something like this?
.work {
width: 300px;
height: 300px;
line-height:300px;
text-align: center;
background-color: #2d3e50;
border: 10px solid #2a81b9;
color: white;
display: inline-block;
}
.work span {
display: none;
vertical-align: middle;
}
.work:hover span {
display: inline-block;
}
.work:hover img {
display: none;
}
<div class="work">
<span>Some text</span>
<img class="portfolio-image" src="http://dummyimage.com/300.png/09f/fff" width="300" height="300" />
</div>

Float left div inside center container

All...
I want to ask very basic CSS. Please see here http://jsfiddle.net/fzJ8X/5/
HTML:
<div class="container">
<div class="item">
<div class="overlay">
<img src="http://placehold.it/200x200" />
</div>
</div>
<div class="item">
<div class="overlay">
<img src="http://placehold.it/200x200" />
</div>
</div>
<div class="item">
<div class="overlay">
<img src="http://placehold.it/200x200" />
</div>
</div>
CSS:
.container {
width: 500px;
margin: 0 auto;
text-align: center;
border: #000 1px solid;
}
.item {
display: inline-block;
border: #F00 5px solid;
width: 200px;
height: 200px;
text-align: left;
}
.overlay {
border: #00F 5px solid;
width: auto;
height: auto;
}
.item img{
max-width: 100%;
height: auto;
display: block;
}
I have three boxes inside container with style text-align:center. Now all boxes centered like I want, but how to all boxes in float left? like screenshot below :
Screenshot here
Thank you very much :)
Try this:
JS iddle: http://jsfiddle.net/xZst7/3/
CSS:
.container {
width: 500px;
margin: 0 auto;
text-align: center;
border: #000 1px solid;
display: inline-block;
}
.item {
display: inline-block;
border: #F00 5px solid;
width: 200px;
height: 200px;
text-align: left;
float: left;
margin-right: 5px;
margin-bottom: 5px;
}
.overlay {
border: #00F 5px solid;
width: auto;
height: auto;
}
.item img{
max-width: 100%;
height: auto;
display: block;
}
HTML:
<div style="text-align: center;">
<div class="container">
<div style="display: inline-block; width: 430px;">
<div class="item">
<div class="overlay">
<img src="http://placehold.it/200x200" />
</div>
</div>
<div class="item">
<div class="overlay">
<img src="http://placehold.it/200x200" />
</div>
</div>
<div class="item">
<div class="overlay">
<img src="http://placehold.it/200x200" />
</div>
</div>
</div>
</div>
</div>
add float:left; to the code
.item {
display: inline-block;
border: #F00 5px solid;
width: 200px;
height: 200px;
text-align: left;
float:left;
}
jsfiddle:http://jsfiddle.net/fzJ8X/12/
Nothing much to do.
Add float:left; to .item{} as below.
.item {
display: inline-block;
border: #F00 5px solid;
width: 200px;
height: 200px;
text-align: left;
float:left;
}
Check the fiddle
Fiddle