HTML/CSS div alignment - html

When resized, I need the div order changed to move two logos next to each other at 50% width each and the third div (title) 100% width below. Can anyone please direct me with where I am going wrong.
.logo1 {
width: 50%;
float: left;
top: 0px;
}
.logo2 {
width: 50%;
float: right;
top: 0px;
}
.title {
width: 100%;
clear: both;
}
<header>
<div class="logo1">
<img src="Pictures/logo.png" width="189" height="61" border="0">
</div>
<div class="title">
<h1 class="map_title">Infrastructure Web Map</h1>
</div>
<div class="logo2">
<img src="Pictures/Picture1.png" width="310" height="70" border="0">
</div>
</header>

You can do it with the Flexbox:
body {margin: 0}
h1 {
text-align: center;
}
header {
display: flex; /* displays flex-items (children) inline */
flex-wrap: wrap; /* enables their wrapping */
}
header > div {
flex: 1; /* each takes 33.33% of the parent's width */
display: flex; /* addition */
justify-content: center; /* addition */
}
img {
display: block; /* removes bottom margin/whitespace */
width: 100%; /* responsiveness */
}
#media (max-width: 568px) {
header > div {
flex: 0 1 50%; /* flex-grow = default, flex-shrink = default, flex-basis = 50% (initial width) */
}
.title {
flex: 1; /* stretches to fill the parent's width (100%) */
order: 1; /* changes the order, i.e. puts it below/after the #logo2 (by default its "order: 0") */
}
}
<header>
<div class="logo1">
<img src="http://placehold.it/300x200" alt="logo1">
</div>
<div class="title">
<h1 class="map_title">Infrastructure Web Map</h1>
</div>
<div class="logo2">
<img src="http://placehold.it/300x200" alt="logo2">
</div>
</header>

Thanks to all for the help, after some further research and a combination of all the suggestions, this is what I used to get it working:
header {
background-color: #cccccc;
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
img {
margin: auto;
display: block;
}
.logo1, .logo2 {
width: 50%;
flex: 1 1 0;
}
.title {
color: #336699;
flex: 100%;
order: 3;
text-align: center;
}
}

Related

Possitioning an image next to two different text elements

I am trying to make a header bar on my HTML page that has a h1 and h2 element but then I want an image to appear to the right of both of them.
What ever i try the image appears below the other two, i have tried display:inline-block; but that didn't seem to work.
Any ideas how i do it?
.headerBar {}
.headerBar h1 {
float: left;
width: 400px;
line-height: 0;
}
.headerBar h2 {
width: 400px;
line-height: 1;
}
.headerBar img {
width: 40px;
border-radius: 50px;
}
<div class="headerBar">
<h1>My Name</h1>
<h2>Welcome</h2>
<img src="https://dummyimage.com/300x200/000000/fff" alt="Photo" />
<br>
</div>
You can wrap your headings in wrapper and make .headerBar flex, for more details see CSS part of Code Snippet.
.headerBar {
display: flex; /* make main container flex */
flex-direction: row; /* this will make container in one row */
align-items: flex-start; /* vertical align content to top */
justify-content: space-between; /* add gap between content */
}
.headerBar h1 {
line-height: 0;
}
.headerBar h2 {
line-height: 1;
}
.headerBar img {
width: 40px;
border-radius: 50px;
}
<div class="headerBar">
<div class="headerWrapper">
<h1>My Name</h1>
<h2>Welcome</h2>
</div>
<img src="https://dummyimage.com/300x200/000000/fff" alt="Photo" />
</div>
Flex is a good solution. You also can grid for that. Is it what you want?
.headerBar {
display: grid;
grid-template-columns: auto auto;
width: 100%;
}
.headerBar h1 {
line-height: 0;
}
.headerBar h2 {
line-height: 1;
}
.headerBar .image {
border-radius: 50px;
grid-row: 1 / 3;
grid-column: 2 / 3;
}
<div class="headerBar">
<h1>My Name</h1>
<div class="image"><img src="https://dummyimage.com/300x200/000000/fff" alt="Photo" /></div>
<h2>Welcome</h2>
</div>

How can I make multiple images fit in a container?

I have multiple images in a container and would like them to fit into one container but the problmem is that I can't change the width and the height as I'm currently making an app that makes it dynamic.
As you can see I put a red border for the container, the right border is missing.
This is what I got:
.container {
display: flex;
width: 450px;
height: 300px;
border: 2px solid red;
}
.container img {
max-width: 100%
}
<div class="container">
<img src="https://cdn.hasselblad.com/ec67f4db463750c394c4e720acedf6b506b55b48_x1d-ii-sample-01-web.jpg">
<img src="https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg">
<img src="https://www.fujifilmusa.com/products/digital_cameras/x/fujifilm_x20/sample_images/img/index/ff_x20_008.JPG">
</div>
How can I make these images fit inside the red border?
You can try this with flex: 1 CSS property.
.container {
display: flex;
width: 450px;
height: 300px;
border: 2px solid red;
}
.container div {
flex: 1;
}
.container img {
max-width:100%;
}
<div class="container">
<div>
<img src="https://cdn.hasselblad.com/ec67f4db463750c394c4e720acedf6b506b55b48_x1d-ii-sample-01-web.jpg">
</div>
<div>
<img src="https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg"> </div>
<div>
<img src="https://www.fujifilmusa.com/products/digital_cameras/x/fujifilm_x20/sample_images/img/index/ff_x20_008.JPG">
</div>
</div>
Here is how I would recommend doing this, you can keep all the images in one line, it's responsive, and calculated:
.section {
width: 100%;
margin: 0 auto;
}
.section .section-inner {
width: 100%;
max-width: 1248px;
margin: 0 auto; /* centers container */
padding: 10px; /* this combined with the margin in the divs will make the margins appear consistent */
box-sizing: border-box;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-wrap: wrap;
flex-wrap: wrap;
flex-direction: row; /* sort into rows */
align-items: center;
justify-content: space-between;
text-align: center;
background-color: rgba(255,100,100,0.25);
}
.section .section-inner div {
width: calc((100% / 4) - 20px); /* calculate width for maximum accuracy | minus margin */
padding-top: calc((100% / 4) - 20px); /* calculate width for maximum accuracy | minus margin */
margin: 10px; /* space between boxes */
background-size: cover;
background-position: center;
}
#media screen and (min-width: 640px) and (max-width: 1023px) {
.section .section-inner div {
width: calc((100% / 2) - 20px); /* calculate width for maximum accuracy | minus margin */
padding-top: calc((100% / 2) - 20px); /* calculate width for maximum accuracy | minus margin */
}
}
#media screen and (max-width: 639px) {
.section .section-inner div {
width: 100%; /* calculate width for maximum accuracy | minus margin */
padding-top: 100%; /* calculate width for maximum accuracy | minus margin */
}
}
.image-one {
background-image: url(https://www.fairtrade.org.uk/~/media/FairtradeUK/Media%20Centre/Flowers.jpg?h=397&la=en&mw=760&w=760);
}
.image-two {
background-image: url(https://www.floraqueen.com/blog/wp-content/uploads/2017/11/Untitled-design-3-1.jpg);
}
.image-three {
background-image: url(https://www.elimaysflowers.co.uk/assets/uploads/elimays1.jpg);
}
.image-four {
background-image: url(https://cdn.mos.cms.futurecdn.net/AsaMYX4F9WRNVTLsARRAsR-1920-80.jpg);
}
<section class="section">
<div class="section-inner">
<div class="image-one"></div>
<div class="image-two"></div>
<div class="image-three"></div>
<div class="image-four"></div>
</div>
</section>
If you want them to keep scaling and not go responsive, remove the responsive CSS.
You can use this code
body {
margin: 0;
}
.container {
display: flex;
width: 450px;
height: 300px;
border: 2px solid red;
padding: 0px;
}
.container img {
max-width: 100%;
float: left;
width: 33.3%;
}
<div class="container">
<img src="https://cdn.hasselblad.com/ec67f4db463750c394c4e720acedf6b506b55b48_x1d-ii-sample-01-web.jpg">
<img src="https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg">
<img src="https://www.fujifilmusa.com/products/digital_cameras/x/fujifilm_x20/sample_images/img/index/ff_x20_008.JPG">
</div>
You can use this way.
.container {
padding-right: 15px;
padding-left: 15px;
margin-right: auto;
margin-left: auto;
width: 1170px;
border: 1px solid red;
}
<div class="container">
<img src="https://cdn.hasselblad.com/ec67f4db463750c394c4e720acedf6b506b55b48_x1d-ii-sample-01-web.jpg" width="304" height="236">
<img src="https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg" width="304" height="236">
<img src="https://www.fujifilmusa.com/products/digital_cameras/x/fujifilm_x20/sample_images/img/index/ff_x20_008.JPG" width="304" height="236">
</div>
You should add like this
.container {
display: flex;
width: 450px;
height: 300px;
border: 2px solid red;
}
.container div{
width:33.33%;
}
.container img {
max-width: 100%
}
<div class="container">
<div>
<img src="https://cdn.hasselblad.com/ec67f4db463750c394c4e720acedf6b506b55b48_x1d-ii-sample-01-web.jpg">
</div>
<div>
<img src="https://4.img-dpreview.com/files/p/E~TS590x0~articles/3925134721/0266554465.jpeg">
</div>
<div>
<img src="https://www.fujifilmusa.com/products/digital_cameras/x/fujifilm_x20/sample_images/img/index/ff_x20_008.JPG">
</div>
</div>

Absolute Position Vertical Nav with Flexbox

I want to make a vertical nav that say will be 50px and then I want to have a flex area that has my header, main content area and footer.
Right now when I use absolute the flexbox container gets covered over as absolute it doing it's own thing. I am wondering if I can tell my flex container to start 50px from the left so I don't have to worry about icons and such getting swallowed up by it.
Do I have to make the flex container absolute as well?
You don't need any positioning or margins, just make it natural with the additional flex wrapper:
body {margin: 0}
.outerFlex {
display: flex; /* displays flex-items (children) inline */
height: 100vh; /* 100% of the viewport height */
}
nav {
flex-basis: 50px; /* initial width */
background: lightblue;
}
.innerFlex {
flex: 1; /* takes the remaining width */
display: flex;
flex-direction: column; /* stacks flex-items vertically */
background: lightgreen;
}
main {
flex: 1; /* takes the remaining height */
}
<div class="outerFlex">
<nav>Nav</nav>
<div class="innerFlex">
<header>Header</header>
<main>Main</main>
<footer>Footer</footer>
</div>
</div>
You can use margin-left:50px on the flex area to make it start at 50px;
see code sample
body {
margin: 0;
height: 100vh;
}
.container {
height: 100%;
}
.nav {
position: absolute;
width: 50px;
background: green;
height: 100%;
}
.flex-container {
display: flex;
background: yellow;
height: 100%;
margin-left: 50px;
}
<div class="container">
<div class="nav"></div>
<div class="flex-container">text sample</div>
</div>
You just need to position your nav as absolute or fixed, then give padding/margin that equals the width of your nav to the main content.
Here's an example.
.container {
width: 100%;
}
.left-nav {
position: fixed;
width: 50px;
height: 100%;
background: black;
}
.main {
display: flex;
flex-direction: column;
justify-content: space-between;
padding-left: 50px;
background: rgba(255,0,0,0.1);
}
.main-header {
background: red;
}
.main-body {
background: green;
}
.main-footer {
background: blue;
}
<div class="container">
<div class="left-nav">Nav</div>
<div class="main">
<div class="main-header">Header</div>
<div class="main-body">Body</div>
<div class="main-footer">Footer</div>
</div>
</div>

flex dynamic height fit image

Need to fit img into dynamically calculated height of li due to flex. Currently it takes all height, and pull height content. Image should just fit into dynamically calculated height of LI, e.g. 50px and fit into it.
No hardcoded height is needed. Page should be 100% in any device. header/footer - flex: 1, main - flex: 3.
body,
ul {
margin: 0;
padding: 0;
}
html,
body,
.root {
height: 100%;
}
.root {
display: flex;
flex-direction: column;
}
main {
flex: 3;
background: lightyellow;
}
ul {
display: flex;
flex-direction: column;
height: 100%;
}
ul li {
flex: 1;
background: aliceblue;
outline: 1px solid #000;
display: flex;
/* align-items: center; */
justify-content: space-between;
}
img {
/* object-fit: contain; */
}
header,
footer {
flex: 1;
}
header {
background: lightgray;
}
footer {
background: lightblue;
}
<div class="root">
<header>
HEAD
</header>
<main>
<ul>
<li>
<img src="http://via.placeholder.com/200x200" />
<span>text</span>
</li>
<li>
<img src="http://via.placeholder.com/200x200" />
<span>text</span>
</li>
<li>
<img src="http://via.placeholder.com/200x200" />
<span>text</span>
</li>
</ul>
</main>
<footer>
FOOTER
</footer>
</div>
https://jsfiddle.net/sefb3o95/18/
The main thing to make this work is to use Flexbox all the way.
Simply put, one need to nest flex parent/child instead of using height: 100%, to make use of Flexbox's own properties making its flex children stretch/fill their parents, and the reason is that height: 100% is not gonna work properly.
Additionally, having the img as a flex item will cause you some cross browser issues, so wrap it and give it a max-height: 100%, and it will size properly.
See my notes in the CSS. The min-height: 0 fix for Firefox is well explained here:
Why don't flex items shrink past content size?
Updated fiddle
Stack snippet
body,
ul {
margin: 0;
padding: 0;
}
html,
body,
.root {
height: 100%;
}
.root {
display: flex;
flex-direction: column;
}
main {
flex: 3;
background: lightyellow;
display: flex; /* added */
flex-direction: column; /* added */
min-height: 0; /* added, Firefox need this */
}
ul {
display: flex;
flex-direction: column;
min-height: 0; /* added, Firefox need this */
}
ul li {
flex: 1;
background: aliceblue;
outline: 1px solid #000;
display: flex;
/* align-items: center; */
justify-content: space-between;
min-height: 0; /* added, Firefox need this */
}
img {
max-height: 100%; /* added */
}
header,
footer {
flex: 1;
}
header {
background: lightgray;
}
footer {
background: lightblue;
}
<div class="root">
<header>
HEAD
</header>
<main>
<ul>
<li>
<span>
<img src="http://via.placeholder.com/200x200" />
</span>
<span>text</span>
</li>
<li>
<span>
<img src="http://via.placeholder.com/200x200" />
</span>
<span>text</span>
</li>
<li>
<span>
<img src="http://via.placeholder.com/200x200" />
</span>
<span>text</span>
</li>
</ul>
</main>
<footer>
FOOTER
</footer>
</div>

How to use flexbox to fill remaining height?

html,
body {
height: 100%;
}
.site-main {
height: calc(100% - 72px);
display: flex;
}
.site-main > div {
min-width: 85%;
height: 100%;
}
.site-main > aside {
min-width: 15%;
height: 100%;
background-color: $darkest-blue-grey;
}
<header>
<h1>Title</h1>
</header>
<div class="site-main">
<div></div>
<aside></aside>
</div>
I have the header at a fixed height of 72px.
I have given .site-main div a width of 85%.
I have given .site-main aside a width of 15%.
What I want is for .site-main div and .site-main aside to be side by side, and have .site-main fill the remaining white space after the header.
And have .site-main div and .site-main aside fill .site-main's height.
Any help would be greatly appreciated!
You can use flex-direction: column on body and flex: 1 on site-main.
body {
display: flex;
flex-direction: column;
min-height: 100vh;
margin: 0;
padding: 0;
}
header {
height: 75px;
}
.site-main {
display: flex;
flex: 1;
}
.site-main div {
flex: 0 0 85%;
background: lightblue;
}
aside {
flex: 0 0 15%;
background: lightgreen;
}
<header>
<h1>Title</h1>
</header>
<div class="site-main">
<div></div>
<aside></aside>
</div>
For you who need to support IE11 (and 10), this one solves the IE min-height bug
Note, for this to work on IE10, prefixed flexbox properties needs to be added
html, body {
margin: 0;
}
body {
display: -ms-flexbox; /* IE 10 */
display: flex; /* IE11/10 bug fix */
}
.wrapper {
-ms-flex: 1; /* IE 10 */
flex: 1; /* fill 100% width */
display: -ms-flexbox; /* IE 10 */
display: flex;
-ms-flex-direction: column; /* IE 10 */
flex-direction: column;
min-height: 100vh;
}
header {
height: 72px;
}
.site-main {
-ms-flex: 1; /* IE 10 */
flex: 1; /* fill 100% height */
display: -ms-flexbox; /* IE 10 */
display: flex;
}
.site-main > div {
width: 85%;
background-color: lightgreen;
}
.site-main > aside {
width: 15%;
background-color: lightblue;
}
<div class="wrapper">
<header>
<h1>Title</h1>
</header>
<div class="site-main">
<div></div>
<aside></aside>
</div>
</div>