How to fit an image inside a container with non-fixed height? - html

#myDiv {
display: inline-block;
border:1px solid red;
width:200px;
}
#myImg {
max-height:100%;
max-width:100%;
}
#anotherDiv {
display:inline-block;
border:1px solid blue;
height:100px;
width:50px;
}
<div id="myDiv">
<img src='https://i.imgur.com/rw9ypWD.png' id="myImg">
<div id="anotherDiv">
</div>
</div>
I want to fit the image height to its container div.
Using height:100%; works only when the container have a fixed height.

Simply add display:flex to container to get the stretch alignment by default:
#myDiv {
display: inline-flex;
border:1px solid red;
width:200px;
}
#anotherDiv {
display:inline-block;
border:1px solid blue;
height:100px;
width:50px;
}
<div id="myDiv">
<img src='https://i.imgur.com/rw9ypWD.png' id="myImg">
<div id="anotherDiv">
</div>
</div>
A different result with display:grid:
#myDiv {
display: inline-grid;
grid-auto-flow:column; /* column flow */
justify-content: flex-start; /* align everything to left */
border:1px solid red;
width:200px;
}
#myImg {
height:100%; /* image 100% height of the div */
}
#anotherDiv {
display:inline-block;
border:1px solid blue;
height:100px;
width:50px;
}
<div id="myDiv">
<img src='https://i.imgur.com/rw9ypWD.png' id="myImg">
<div id="anotherDiv">
</div>
</div>
<div id="myDiv">
<img src='https://i.imgur.com/rw9ypWD.png' id="myImg">
<div id="anotherDiv" style="height:50px">
</div>
</div>

Here the another way without using flex: I will recommend that display:flex; will be the good and easy one.
#myDiv {
display: inline-block;
border: 1px solid red;
width: 200px;
position: relative;
padding-left: 22px;
}
#myImg {
position: absolute;
left: 0;
height: 100%;
}
#anotherDiv {
display: inline-block;
border: 1px solid blue;
height: 100px;
width: 50px;
}
<div id="myDiv">
<img src="https://i.imgur.com/rw9ypWD.png" id="myImg" />
<div id="anotherDiv"></div>
</div>

Related

HTML parent with 2 child using flexbox but one of them overlap the max-width for parent [ IMG Included ]

How do I make this layout with CSS? The parent div is set to max-width 1500px and it is a display flex and a gap 18px with two children #text, #news... news is overlapping to touch the right edge of whatever screen the user has.
html{
border:1px solid red;
}
html:after{
content:"HTML";
color:gray;
}
.main{
display:flex;
flex-flow:row;
max-width:350px;
color:black;
}
.main .child-1 {
flex: 1 1 40%;
border:1px solid #000
}
.main .child-2 {
flex: 1 1 100vw;
border:1px solid #000;
}
.main ul p {
display:block;
width 250px; // Whatever
}
ul{
list-style:none;
display:flex;
}
<div class="main">
<p class="child-1">Some lorem Text</p>
<div class="child-2">
<ul>
<li><p>1</p></li>
<li><p>2</p></li>
<li><p>3</p></li>
<li><p>4</p></li>
</ul>
</div>
</div>
you can use negative margin to achieve the goal.
div{
border: 1px solid;
min-height: 100px;
}
.parent{
display: flex;
max-width: 250px;
padding: 20px;
column-gap: 20px;
}
.text{
width: 25%;
}
.news{
width:75%;
margin-right: -20px;
}
<div class="parent">
<div class="text"></div>
<div class="news"></div>
</div>

two divs side by side third on right side above content

I need to position 3 divs inside a container and I need first div with specific width second next to it to fill remaining space and third which would be on the right side and above second div.
This is what I got so far https://jsfiddle.net/96v5zdra/
<div class="wrapper">
<div class="first">1</div>
<div class="second">Some lorem ipsum long text</div>
<div class="third">3</div>
</div>
.wrapper {
width: 200px;
border: solid 1px black;
}
.first {
width: 50px;
background-color: red;
float: left;
}
.second {
background-color: green;
float: left;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.third {
background-color: rgba(255, 0, 0, 0.4);
width: 50px;
float: right;
}
How would such css style look?
body {
padding:0;
margin:0;
}
.container {
width:600px;
height:200px;
background:#fff;
border:10px solid #000;
margin:100px auto 0;
position:relative;
display:flex;
}
.red {
height:100%;
width:30%;
background:red;
border-right:10px solid #000;
box-sizing:border-box;
/* the following codes for numbers. deletable.*/
display:flex;
justify-content:center;
align-items:center;
font-size:90px;
font-family:monospace;
color:#fff;
}
.green {
heigth:100%;
width:70%;
background:green;
box-sizing:border-box;
/* the following codes for numbers. deletable.*/
display:flex;
justify-content:center;
align-items:center;
font-size:90px;
font-family:monospace;
color:#fff;
}
.green_right{
position:absolute;
right:0;
top:0;
background:transparent;
border:10px dashed #000;
height:100%;
width:30%;
box-sizing:border-box;
/* the following codes for numbers. deletable.*/
display:flex;
justify-content:center;
align-items:center;
font-size:90px;
font-family:monospace;
color:#fff;
}
<div class="container">
<div class="red">
1
</div>
<div class="green">
2
<div class="green_right">
3
</div>
</div>
</div>
This could be a way with some bootstrap:
HTML:
<div class="container">
<div class="wrapper row">
<div class="first">Some<br> lorem <br>ipsum <br>long <br>text</div>
<div class="second col">Some<br> lorem <br>ipsum <br>long <br>text</div>
<div class="third">Some<br> lorem <br>ipsum <br>long <br>text</div>
</div>
</div>
CSS:
.wrapper {
border: solid 1px black;
position:relative;
}
.first {
background-color: red;
width: 50px;
}
.second {
background-color: green;
}
.third {
background-color: rgba(255, 0, 0, 0.4);
position: absolute;
right: 0;
}
Live example: https://codepen.io/PMertgen/pen/zMyjQd

column-count and position fixed on Microsoft Edge

On a current project I have a similar structure (here I have very simplified the structure):
http://jsfiddle.net/6j5ouhz4/3/
HTML
<div class="container">
<div class="columns">
<div class="column1">
<div class="openFlexbox"> OPEN </div>
<div class="flexbox">TEST
<span class="close">X</span>
</div>
</div>
<div class="column1">
<div class="openFlexbox"> OPEN </div>
<div class="flexbox">TEST
<span class="close">X</span>
</div>
</div>
<div class="column1">
<div class="openFlexbox"> OPEN </div>
<div class="flexbox">TEST
<span class="close">X</span>
</div>
</div>
<div class="column1">
<div class="openFlexbox"> OPEN </div>
<div class="flexbox">TEST
<span class="close">X</span>
</div>
</div>
</div>
</div>
CSS
.container {
border:1px solid black;
width:600px;
min-height:200px;
margin: 0 auto;
background: #ddd;
display:flex;
display: -ms-flexbox;
}
.columns {
column-gap: 8em;
column-count: 2;
}
.column1 {
display: block;
border:1px solid red;
width:200px;
height: 200px;
margin:10px;
position:relative;
}
.flexbox {
display:none;
position: fixed;
top:0;
left:0;
width:100%;
height:100%;
background: #aaa;
font-size:30px;
text-align:center;
z-index: 9999
}
.flexbox.open {
display:block;
}
.close {
border:1px solid #fff;
padding: 5px;
}
.openFlexbox {
background: #a6dbea;
padding: 10px 0;
text-align:center;
display:inline-block;
position:absolute;
width: 100px;
left: 50%;
margin-left:-50px;
top: 40%;
}
JS
jQuery('.openFlexbox').on('click',function(e) {
jQuery(this).next('.flexbox').addClass('open');
});
jQuery('.close').on('click',function() {
jQuery('.flexbox').removeClass('open');
});
Firefox and Chrome don't have any problem, on Microsoft edge the modalbox appear "halfsize" occupying the half area of column where this block is located (in this example instead, it does not appear at all).
Actually, by removing the relative position, the problem disappears, but the "position: relative" I use to center the button..
but the way, the relative position shouldn't effect the fixed positions.
There is a fix for this problem?
the 'openflexbox' does not cover the whole area but maybe this would work for you?
css:
.flexbox {
display:none;
/*position: fixed;*/
top:0;
left:0;
width:100%;
height:100%;
background: #aaa;
font-size:30px;
text-align:center;
z-index: 9999;
}
js:
jQuery('.openFlexbox').on('click',function(e) {
jQuery(this).next('.flexbox').addClass('open');
$('.openFlexbox').css('display', 'none');
});
jQuery('.close').on('click',function() {
jQuery('.flexbox').removeClass('open');
$('.openFlexbox').css('display', 'inline-block');
});

CSS position a div based on dynamic height of another div

In my site layout I'm using a negative margin to move my left column up next to my banner so it overlaps. The problem is I don't know what the banner's height will be in the final version. At first I used position:absolute on the left column, but that won't work because it needs to be part of the layout and push down the footer if necessary. I'd like to know how to position the left column to the top of the page, because then I could set a top margin the same height as the header since that won't change height. I could figure this out with javascript but I'd like to avoid that and use pure css.
https://jsfiddle.net/z77fwaj7/1/
#Header
{
background-color: gray;
height: 50px;
}
#Banner
{
background-color: orange;
height: 50px;
}
#Content
{
background-color:white;
border:1px solid red;
max-width:500px;
margin:0px auto;
}
#LeftColumn
{
float:left;
height:200px;
width:25%;
background-color: blue;
margin-top:-51px;/*this needs to be dynamic*/
}
#MiddleColumn
{
float:left;
height:200px;
width:45%;
background-color: yellow;
}
#RightColumn
{
float:left;
height:250px;
width:30%;
background-color: green;
}
#Footer
{
background-color: gray;
height: 50px;
}
<div id="Header">header</div>
<div id="Banner">banner</div>
<div id="Content">
<div id="LeftColumn">left</div>
<div id="MiddleColumn">middle</div>
<div id="RightColumn">right</div>
<div style="clear:both;"></div>
</div>
<div id="Footer">footer</div>
Is this Ok.
<style type="text/css">
#Header
{
background-color: gray;
height: 50px;
}
#Banner
{
background-color: orange;
height: 50px;
}
#Content
{
background-color:white;
border:1px solid red;
max-width:500px;
margin:0px auto;
}
#LeftColumn
{
float:left;
height:200px;
width:25%;
background-color: blue;
margin-top:0px;
}
#MiddleColumn
{
float:left;
height:200px;
width:45%;
background-color: yellow;
}
#RightColumn
{
float:left;
height:250px;
width:30%;
background-color: green;
}
#Footer
{
background-color: gray;
height: 50px;
}
</style>
<div>
<div id="Header">header</div>
<div id="Banner">banner</div>
<div id="Content">
<div id="LeftColumn">left</div>
<div id="MiddleColumn">middle</div>
<div id="RightColumn">right</div>
<div ></div>
</div>
<div id="Footer" style="clear:both;">footer</div>
</div>
If anyone is curious I had to change my layout in order to get it working without javascript. BackgroundBanner won't change height when Banner shrinks, but in my case that doesn't matter since it will be out of view anyway.
https://jsfiddle.net/z77fwaj7/4/
css:
#Header
{
background-color: gray;
height: 50px;
}
#Background
{
position:absolute;
width:100%;
z-index:-1;
}
#BackgroundBanner
{
height: 50px;
background-color:orange;
}
#Banner
{
float:left;
background-color: orange;
height: 50px;
width:75%;
}
#Content
{
background-color:white;
border:1px solid red;
max-width:500px;
margin:0px auto;
}
#LeftColumn
{
float:left;
height:200px;
width:25%;
background-color: blue;
}
#MiddleColumn
{
float:left;
height:200px;
width:45%;
background-color: yellow;
}
#RightColumn
{
float:left;
height:250px;
width:30%;
background-color: green;
}
#Footer
{
background-color: gray;
height: 50px;
}
html:
<div id="Header">header</div>
<div id="Background">
<div id="BackgroundBanner"></div>
</div>
<div id="Content">
<div id="LeftColumn">left</div>
<div id="Banner">banner</div>
<div id="MiddleColumn">middle</div>
<div id="RightColumn">right</div>
<div style="clear:both;"></div>
</div>
<div id="Footer">footer</div>
As a conclusion:
Using a value of an element on another element in css is not possible. (as far as i know)
So there are two solutions:
Change the layout.
Use javascript.
I would prefer the second. Don't know why it's such a shame to do so.
A short simple javascript is better then mess up the layout. (In my opinion)

how align div one beside other, and on other div

i tried some codes but, no works anything.
would like make this with css, thanks =)
this code i tried, but doesn't work.
#left{
float:left;
width:65%;
overflow:hidden;
}
#right{
overflow:hidden;
}
<div id="wrapper">
<div id="left">Left side div</div>
<div id="right">Right side div</div>
</div>
i don{t know why this doesnt work.
A simple solution with no floats:
#main {
width: 200px; /* adjust as needed */
font-size: 0;
}
div div {
display: inline-block;
height: 60px; /* adjust as needed */
width: 100%;
box-shadow: inset 0 0 4px #000; /* cosmetics only */
background: #eee; /* cosmetics only */
}
div.h {
width: 50%;
}
<div id="main">
<div class="h"></div>
<div class="h"></div>
<div></div>
</div>
Note: using font-size: 0 for the container div to avoid the actual whitespace in the markup - can be avoided by removing spaces between elements, of course: <div>content here...</div><div>other one...</div>
Add float:left; to #right, then it should work. Note that you could also use float:right; to #right, then #right would be on the right side. Using float: left; for both displays both divs next to each other without any gap.
For reference: https://developer.mozilla.org/en-US/docs/Web/CSS/float
Try this script, I wrote it on JSfiddle:
http://jsfiddle.net/xb5vvpzn/1/
HTML
<div class="main">
<div class="top"> </div>
<div class="bottom1"> </div>
<div class="bottom2"> </div>
</div>
CSS
html, body {
padding:0;
margin:0;
}
.main {
width:400px;
border:1px solid #000;
height:400px;
padding:10px;
}
.main div {
display:inline-block;
}
.top {
width:396px;
border: 1px solid #cc0000;
height:100px;
}
.bottom1, .bottom2 {
margin-top:10px;
border: 1px solid #cc0000;
width:195px;
height:100px;
}
Here's a jsFiddle that I've quickly created for you. The layout is same as what you had requested and it's responsive as well.
HTML:
<div id="container">
<div id="onetwo">
<div id="one"></div>
<div id="two"></div>
</div>
<div id="three"></div>
</div>
CSS:
#container {
width: 100%;
border: 3px solid blue;
padding: 1% 1%;
text-align: center;
}
#onetwo {
display: block;
width: 100%;
}
#one, #two {
width: 49%;
border: 3px solid red;
height: 50px;
display: inline-block;
}
#three {
width: 100%;
border: 3px solid red;
height: 50px;
}
#media (max-width: 820px) {
#one, #two {
width: 46%;
}
}
#media (max-width: 240px) {
#one, #two {
width: 40%;
}
}