Please check link for more details on my code. I need to have the placeholder image aligned to left of the screen i.e. to the bank white space as illustrated by the shared code.
* {
margin: 0;
padding: 0;
}
main{
width: 100%;
display: flex;
flex-direction: row;
justify-content: flex-end;
}
aside {
width: 300px;
height: 100vh;
background-color: red;
}
article {
width: 100%;
max-width: 500px;
height: 100vh;
background-color: green;
}
figure {
padding-top : 50%;
position:relative;
img {
top:0;
left:0;
right:0;
bottom: 0;
position: absolute;
max-width: 100%;
width: 100%;
}
}
<main>
<article>
<figure>
<img src="http://via.placeholder.com/350x150" alt=""> </figure>
</article>
<aside>
</aside>
</main>
Remove:
justify-content: flex-end;
from .main in your CSS.
In .main css you can remove justify-content or set it with flex-start, If you want to take red section in right and the image in left you can set justify-content space-between.
Do you mean this?
* {
margin: 0;
padding: 0;
}
main{
width: 100%;
display: flex;
flex-direction: row;
justify-content: flex-end;
}
aside {
width: 300px;
height: 100vh;
background-color: red;
}
article {
width: 100%;
max-width: 500px;
height: 100vh;
background-color: green;
}
figure {
padding-top : 50%;
position:relative;
img {
top:0;
left:0;
right:0;
bottom: 0;
position: absolute;
max-width: 100%;
width: 100%;
}
}
img{
width:100%
}
<main>
<img src="http://via.placeholder.com/350x150" alt="">
<article>
<figure>
</figure>
</article>
<aside>
</aside>
</main>
Issue is here. When you set a max-width, when the size is more than 500px + 300px of the sidebar, you get the white space.
article {
width: 100%;
/*Issue is here.*/
max-width: 500px;
height: 100vh;
background-color: green;
}
To use the same code, you need to adjust the code to this. I will comment out the unnecessary part so you can delete them as you follow the code.
* {
margin: 0;
padding: 0;
}
main{
/*width: 100%;*/
display: flex;
flex-direction: row;
justify-content: flex-end;
}
aside {
width: 300px;
height: 100vh;
background-color: red;
}
article {
/*
This is fine but use the flexbox syntax flex: 1 or flex-basis: 100% to get the remaining space from aside.
width: 100%;*
/
/*Issue is here.
max-width: 500px;
*/
flex: 1;
height: 100vh;
background-color: green;
}
figure {
/*
Not needed.
padding-top : 50%;
position:relative;
*/
img {
/*
top:0;
left:0;
right:0;
bottom: 0;
position: absolute;
max-width: 100%;
width: 100%;
*/
/*To make the image responsive and fit figure tag*/
max-width: 100%;
height: auto;
}
}
<main>
<article>
<figure>
<img src="http://via.placeholder.com/350x150" alt=""> </figure>
</article>
<aside>
</aside>
</main>
Or else restructure your markup to have the figure + image out of the article into main
Related
I have following html:
<div class='fullHeight'>
<div class='flexbox'>
<div class='first'>
<p>foo</p>
</div>
<div class='second'>
<p>bar</p>
<img src='http://www.mandysam.com/img/random.jpg'>
</div>
</div>
</div>
and css:
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.fullHeight {
height: 100vh;
background-color: red;
}
.flexbox {
display: flex;
flex-direction: column;
height: 100%;
maxHeight: 100%;
width: 100%;
background-color: green;
}
.first {
background-color: magenta;
}
.second {
background-color: yellow;
flex: 1 1 auto;
max-height: 100%;
height: 100%;
widht: auto;
}
As long there is no image, everything works fine:
But as soon as a picture comes in, it overflows the container, instead of being shrinked to fit available height:
Codepen
You missed
display: flex;
flex-direction: column;
in your .second class, that's why the flex property isn't doing anything. Also it should be max-height instead of maxHeight and width instead of widht.
You can use background-image for your purpose.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.fullHeight {
height: 100vh;
background-color: red;
}
.flexbox {
display: flex;
flex-direction: column;
height: 100%;
width: 100%;
background-color: green;
}
.first {
background-color: magenta;
}
.second {
background-color: yellow;
display: flex;
flex-grow: 1;
flex-direction: column;
}
.container {
background-color: green;
flex-grow: 1;
background-image: url('http://www.mandysam.com/img/random.jpg');
background-size: contain;
background-repeat: no-repeat;
}
<div class='fullHeight'>
<div class='flexbox'>
<div class='first'>
<p>foo</p>
</div>
<div class='second'>
<p>bar</p>
<div class="container">
</div>
</div>
</div>
</div>
Add height and width properties to your image. Or just height. Maybe also object-fit: cover;
.second img {
height: 100%;
width: 100%;
object-fit: cover;
}
Using positions is the other solution but it's very risky and it depends on your plan for future code in this project!!
.second {
background-color: yellow;
flex: 1 1 auto;
max-height: 100%;
height: 100%;
widht: auto;
position:relative
}
.second img{
height: 95%;
width: 100%;
position:absolute;
bottom:0;
right:0;
}
I have structure like this:
img {
width: auto;
height: 200px;
}
.cards {
display: flex;
justify-content: center;
margin-top: 2em;
width: 80%;
flex-wrap: wrap;
}
.card {
margin: 1em;
box-shadow: 0 0 6px #000;
object-fit: cover;
}
.info {
padding: 1em;
border-top: none;
}
<div class='cards'>
<div class="card">
<img src="https://picsum.photos/id/1004/5616/3744" alt="1004" />
<div class="info">
<h3>Greg Rakozy</h3>
<div><small>https://unsplash.com/photos/SSxIGsySh8o</small></div>
</div>
</div>
</div>
on computers with long width image is rendered a little wrong.
how can I fix this so that it displays correctly, i.e. sticks to the '.card' block?
First you need to limit the width of you main container:
.cards {
display: flex;
justify-content: center;
margin-top: 2em;
width: 80%;
flex-wrap: wrap;
max-width: 1440px; /* whatever you desire */
margin-left: auto; /* center the container */
margin-right: auto; /* center the container */
}
Then each image should take 100% for it's container:
.card {
margin: 1em;
box-shadow: 0 0 6px #000;
object-fit: cover;
flex: 0 0 25%; /* each card will be 25% width */
}
img {
width: 100%;
height: 200px;
object-fit: cover;
}
Adding those to .card class
width: 100%;
height: auto;
Google how to make image responsive with css, it's not related to React.
.card {
width: 100%;
height: auto;
}
You can either put those images in a div.img-container and set the div width & height like this.
.img-container {
width: 100%;
height: // as you want;
}
and then put that image inside .img-container and set the image width to 100%.
.container {
width: 350px;
height 350px;
border: 2px solid black;
border-radius: 5px;
}
.container .img-container {
width: 100%;
height: 200px;
}
.container .img-container img {
width: 100%;
height: 100%;
}
.container .card-info {
width: 100%;
height: auto;
}
<div class="container">
<div class="img-container">
<img src="https://picsum.photos/200">
</div>
<div class="card-info">
<h5>Title</h5>
<small>Your link here</small>
</div>
</div>
and either set image width 100% and height auto.
This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
How can I vertically center a div element for all browsers using CSS?
(48 answers)
How can I vertically align elements in a div?
(28 answers)
Closed 2 years ago.
Lets say I have this simple html page:
<div class="main">
<div class="header">
<h1>HEADER</h1>
</div>
<div class="content">
<div class="box">
</div>
</div>
</div>
My header is fixed and the content should be beneath it and with height 100% of what ever left of the body.
I've already done that with this style:
*{
margin-left: 0;
margin-top: 0;
}
body,
html {
height: 100%;
width: 100%;
margin: 0;
}
.header {
background-color: red;
text-align: center;
position: fixed;
width: 100%;
}
.content {
background-color: antiquewhite;
padding-top: 38px;
}
h1 {
margin-bottom: 0;
}
.main {
display: flex;
flex-direction: column;
height: 100vh;
}
.content {
flex: 1;
}
.box {
width: 150px;
height: 150px;
background-color: yellow;
}
Here's how the page looks for now: https://elbargho.github.io/sudoku/centerdiv.html
now I'm trying to center the box div horizontally and vertically in relative to the full body - the header size
what I've tried to do:
margin-top: 50% - for some reason the box went all the way down to the bottom
setting the position of content div to relative, and of box div to absolute - the content div overlapped the fixed header
You can set content class as
.content {
/* flex: 1; */
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
}
*{
margin-left: 0;
margin-top: 0;
}
body,
html {
height: 100%;
width: 100%;
margin: 0;
}
.header {
background-color: red;
text-align: center;
position: fixed;
width: 100%;
}
.content {
background-color: antiquewhite;
padding-top: 38px;
}
h1 {
margin-bottom: 0;
}
.main {
display: flex;
flex-direction: column;
height: 100vh;
}
.content {
/*flex: 1; */
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
}
.box {
width: 150px;
height: 150px;
background-color: yellow;
}
<div class="main">
<div class="header">
<h1>HEADER</h1>
</div>
<div class="content">
<div class="box">
</div>
</div>
</div>
This is probably what you need. Documented in the code.
* {
margin-left: 0;
margin-top: 0;
}
body,
html {
height: 100%;
width: 100%;
margin: 0;
}
/* Modified */
.header {
background-color: red;
text-align: center;
/* position: fixed; */
position: sticky;
width: 100%;
}
.content {
background-color: antiquewhite;
padding-top: 38px;
}
h1 {
margin-bottom: 0;
}
/* Modified */
.main {
display: flex;
flex-direction: column;
height: 100vh;
align-items: center;
}
/* Modified */
.content {
/*flex: 1;*/
display: flex;
align-items: center;
height: inherit;
}
.box {
width: 150px;
height: 150px;
background-color: yellow;
}
<div class="main">
<div class="header">
<h1>HEADER</h1>
</div>
<div class="content">
<div class="box">
</div>
</div>
</div>
Here solution:
.content {
display: flex;
flex: 1;
justify-content: center;
align-items: center;
}
One way is to use CSS Transform.
.box {
position: relative;
top: 50%;
transform: translateY(-50%);
/* horizontal center */
margin: 0 auto;
}
Check out this website for all CSS centering help:
http://howtocenterincss.com/
So I have HTML that CANNOT be changed whatsoever and I have a certain design that I'm aiming for. And I cannot use JS for this yet.
Just a note too, the container is actually the parent of all the other elements on the screen. However, the reason I drew like that is because that's the look i want, and if possible, as I scroll down the only thing that scrolls is element 3 and 4, and 3 and 1 stay fixed. At the moment the container is a flex box. The CSS that I have doesn't really do what I need it to do, but here it is:
html,
body {
min-height: 100%;
height: 100%;
}
#container {
display: flex;
flex-wrap: wrap;
height: 100%;
}
#intro {
background-color: yellow;
width: 20%;
order: 2;
}
#nav {
background-color: red;
width: 15%;
order: 1;
}
#content {
background-color: blue;
width: 65%;
order: 3
}
article {
order: 4;
}
footer {
background-color: magenta;
}
<div id="container">
<section id="intro">...</section>
<div id="content">...</div>
<aside id="nav">...</aside>
<article>
<li><img src="http://via.placeholder.com/100x100"></li>
<li><img src="http://via.placeholder.com/100x100"></li>
</article>
</div>
<footer>...</footer>
as I scroll down the only thing that scrolls is element 3 and 4
Using position: fixed we can make nav/intro/footer stay while content/article scroll.
The align-items: flex-end; on content/article will keep them right aligned, and flex-grow: 1 on container/content will make them fill the remaining space.
Stack snippet
html {
display: flex; /* IE min-height bug fix */
}
body {
width: 100%; /* using IE bug fix it need a width */
display: flex;
flex-direction: column;
min-height: 100vh; /* instead using precent all over */
margin: 0;
}
#container {
flex-grow: 1;
display: flex;
flex-direction: column;
align-items: flex-end;
}
#nav {
position: fixed;
left: 0;
top: 0;
width: 15%;
height: calc(100% - 30px); /* make up for footer */
background-color: red;
}
#intro {
position: fixed;
left: 15%;
top: 0;
width: 20%;
height: calc(100% - 30px); /* make up for footer */
background-color: yellow;
}
#content {
background-color: lightblue;
width: 65%;
flex-grow: 1;
}
article {
margin-bottom: 30px; /* make up for footer */
}
article li {
list-style: none;
}
footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
height: 30px;
background-color: magenta;
}
<div id="container">
<section id="intro">Intro</section>
<div id="content">
Content along with below article that will scroll and leave "Nav"/"Intro" fixed. <br>
Content along with below article that will scroll and leave "Nav"/"Intro" fixed. <br>
</div>
<aside id="nav">Nav</aside>
<article>
<li><img src="http://via.placeholder.com/100x100"></li>
<li><img src="http://via.placeholder.com/100x100"></li>
</article>
</div>
<footer>Footer</footer>
You are almost there.
Just set the article to have the same width as the #content and set the
justify-content:flex-end on the #container.
#container {
display: flex;
flex-wrap: wrap;
height: 100%;
justify-content: flex-end;
}
article {
order: 4;
width:65%;
}
html,
body {
min-height: 100%;
height: 100%;
}
#container {
display: flex;
flex-wrap: wrap;
height: 100%;
justify-content: flex-end;
}
#intro {
background-color: yellow;
width: 20%;
order: 2;
}
#nav {
background-color: red;
width: 15%;
order: 1;
}
#content {
background-color: blue;
width: 65%;
order: 3
}
article {
order: 4;
width: 65%;
background: cyan;
}
footer {
background-color: magenta;
}
<div id="container">
<section id="intro">...</section>
<div id="content">...</div>
<aside id="nav">...</aside>
<article>
<li><img src="http://via.placeholder.com/100x100"></li>
<li><img src="http://via.placeholder.com/100x100"></li>
</article>
</div>
<footer>...</footer>
I build a responsive lightbox with flexbox-styling and the image in this lightbox doesnt fit in the container. (Its working fine if there is enough vertical space).
Here is a image to visualize the problem:
Thats the HTML code:
<div id="dialog-container">
<div id="dialog">
<div id="dialog-content">
<div class="image-wrapper">
<img src="https://spotwild.org/css/images/dist/header/header-07-1600.jpg">
</div>
<div class="thumbs">
Here are the thumbs
</div>
</div>
</div>
</div>
CSS:
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
width: 100%;
}
#dialog-container {
position: absolute;
width: 100%;
height: 100%;
background: black;
display: flex;
justify-content: center;
align-items: center;
}
#dialog {
max-width: 80%;
max-height: 80%;
background: white;
padding: 50px;
box-sizing: border-box;
position: relative;
}
#dialog-content {
display: flex;
flex: 1 1 100%;
flex-direction: column;
box-sizing: border-box;
height: 100%;
max-height: 100%;
}
.image-wrapper {
display: flex;
flex: 1 1 auto;
justify-content: center;
align-items: center;
min-width: 0;
min-height: 0;
}
img {
min-width: 0;
min-height: 0;
max-height: 100%;
max-width: 100%;
display: flex;
flex: 0 0 auto;
}
.thumbs {
background: #eee;
padding: 20px;
line-height: 25px;
box-sizing: border-box;
}
And here is the corresponding jsfiddle:
https://jsfiddle.net/ppkzt7m6/1/
Does somebody has a solution and an explanation why this happens?
The important part is to define the height on all the parent containers for the <img> element to make the percentage heights to work properly.
And use object-fit: cover; on the <img> element. Note, the current IE11 and Edge don't support it, works fine on all other modern browsers though, see the support tables.
jsFiddle
You can also do it without flexbox, the key is to set the image caption to absolute position
jsFiddle
I suggest to use background image if you do need to support IE, example:
jsFiddle
<div class="image-wrapper" style="background: url('https://spotwild.org/css/images/dist/header/header-07-1600.jpg') center / cover;">
add height in #dialog
#dialog {
background: white none repeat scroll 0 0;
box-sizing: border-box;
height: 100%;
max-height: 80%;
max-width: 80%;
padding: 50px;
position: relative;
}
USE THIS.
img {
display: block;
height: 100%;
max-height: 100%;
max-width: 100%;
min-height: 0;
min-width: 0;
width: 100%;
}
#dialog {
background: #ffffff none repeat scroll 0 0;
box-sizing: border-box;
height: 100%;
max-height: 80%;
max-width: 80%;
padding: 50px;
position: relative;
}