I want two images each taking up half the page in width and the full page (without scrolling) in height. As illustrated in the image below. The images should be scaled (without stretching) to fill the space, even if that requires cropping.
I can't find anything online that works. The image always extends outside the bounds of a container or doesn't scale properly to fit it.
This would be easier to accomplish without Bootstrap since Bootstrap will add gutters between the columns. One way would be to set the images as background images and then use background-size:contain;
html,body {
height:100%;
margin:0;
}
nav {
height:100px;
line-height:100px;
background:#0080ff;
color:#fff;
font-size:40px;
text-align:center;
}
.column {
width:50%;
height:100%;
background-size:contain;
}
.column.left {
background-image:url(http://placehold.it/1000x800/ca6602/fff?text=Image+1);
float:left;
}
.column.right {
background-image:url(http://placehold.it/1000x800/e10227/fff?text=Image+2);
float:right;
}
<nav>Nav Bar</nav>
<div class="column left"></div>
<div class="column right"></div>
Something like this?
html,
body{
padding: 0;
margin: 0;
}
img{
max-width: 100%;
height: auto;
}
.header{
width: 100%;
height: 100px;
background: #eee;
display: flex;
justify-content: center;
align-items: center;
}
.content{
height: calc(100vh - 100px);
width: 100%;
display: flex;
align-items: center;
text-align: center;
background: #ddd;
overflow: hidden;
}
.content > div{
flex: 1;
width: 100%;
}
<div class="header">
<p>Header</p>
</div>
<div class="content">
<div class="content-one">
<img src="http://via.placeholder.com/1500x1500">
</div>
<div class="content-two">
<img src="http://via.placeholder.com/1500x1500">
</div>
</div>
Related
My goal: A responsive navbar where the logo is always in the middle and an element
is always on the left. Depending on the context (page dependent), buttons can be
displayed in the right area or not.
My approach: I use a flexbox for the navbar. I have three divs in the flexbox. I have given all divs a fixed width. The middle box is also a flexbox. The div with a logo is located there. I position the logo on the right edge of the middle flexbox. The div with the logo has a fixed width (80px).
The problem: The approach works but I don't find this way very nice. Because the widths are dependent on each other. If you would change the logo and it would be wider or narrower then you would have to adjust the relative width of the middle and right box. The second problem is if the device smaller as 900px then this solution dont work.
Question: What other possibilities are there and what possibilities would resolve this "width" dependency?
#app {
margin: 0 auto;
max-width: 900px;
width:100%;
}
header {
height: 80px;
display: flex;
justify-content:space-between;
}
.header-left {
width:20%;
background: green;
}
.header-middle {
width:34%;
background: gray;
display: flex;
justify-content:flex-end;
}
.header-right {
width:46%;
background: green;
}
.logo {
background-color: red;
width:80px;
height: 80px;
text-align:center;font-size:70px;
}
<div id="app">
<small>width: 900px</small>
<header>
<div class="header-left">Burger Menu</div>
<div class="header-middle">
<div class="logo">
I
</div>
</div>
<div class="header-right">Context Buttons</div>
</header>
<div>
<div style="width:50%; background: black;color:white; text-align:center;">Controller Div 50%</div>
</div>
</div>
You can use flex-grow: 1 on the left and right elements, the middle element will be in center naturally. In this case, you don't need to set widths on elements.
#app {
margin: 0 auto;
max-width: 900px;
width:100%;
}
header {
height: 80px;
display: flex;
justify-content:space-between;
}
.header-left {
flex-grow: 1;
background: green;
}
.header-middle {
background: gray;
display: flex;
justify-content:flex-end;
}
.header-right {
flex-grow: 1;
background: green;
}
.logo {
background-color: red;
width:80px;
height: 80px;
text-align:center;font-size:70px;
}
<div id="app">
<small>width: 900px</small>
<header>
<div class="header-left">Burger Menu</div>
<div class="header-middle">
<div class="logo">
I
</div>
</div>
<div class="header-right">Context Buttons</div>
</header>
<div>
<div style="width:50%; background: black;color:white; text-align:center;">Controller Div 50%</div>
</div>
</div>
Since you're looking for different possibilities i'll suggest you to take the approch used by Tepken Vannkorn :
Centering brand logo in Bootstrap Navbar
Based on your comments, I would suggest the following code as a simple solution.
I have added a max-width value to your .logo CSS class and I have also moved your inline CSS from the front-end code, and created a .controller CSS class for it.
#app {
margin: 0 auto;
max-width: 900px;
width: 100%;
}
header {
height: 80px;
display: flex;
justify-content: space-between;
}
.header-left {
width: 20%;
background: green;
}
.header-middle {
width: 34%;
background: gray;
display: flex;
justify-content: flex-end;
}
.header-right {
width: 46%;
background: green;
}
.logo {
background-color: red;
width: 80px;
height: 80px;
text-align: center;
font-size: 70px;
max-width: 80px;
}
.controller {
width: 50%;
background: black;
color: white;
text-align: center;
}
<div id="app">
<small>width: 900px</small>
<header>
<div class="header-left">Burger Menu</div>
<div class="header-middle">
<div class="logo">
I
</div>
</div>
<div class="header-right">Context Buttons</div>
</header>
<div>
<div class="controller">Controller Div 50%</div>
</div>
</div>
A solution would be to use a mix of flex and position: absolute. Then you need only the left and the right container. the logo you can center with position left: left: calc(50% - calc(80px / 2));. The 80px is the width from your logo.
* {
margin: 0;
padding: 0;
}
#app {
margin: 0 auto;
max-width: 900px;
width:100%;
}
.header {
display: flex;
justify-content: space-between;
height: 80px;
background: yellow;
position: relative;
}
.header-left {
background-color: green;
width: 20%
}
.header-right {
background-color: green;
width: 44%;
}
.logo {
background-color: red;
width:80px;
height: 80px;
text-align:center;
font-size:70px;
position: absolute;
left: calc(50% - calc(80px / 2));
}
<div id="app">
<div class="header">
<div class="header-left">left</div>
<div class="logo">X</div>
<div class="header-right">right</div>
</div>
<div style="width:50%; background: black;">Controller Div 50%</div>
</div>
I have 2 divs within another container div. I want these 2 divs to be centered vertically in their parent container
<div class="wrapper">
<div class="div1"> some contents here</div>
<div class="div2"> some contents here</div>
</div>
Div1 and Div2 both have different dimensions and height specifically)
This is my css:
.wrapper{width:100%;
display:inline-block;
margin:0 auto;
}
.div1{max-width:760px;
display:inline-block;
margin:0 auto;}
.div2{max-width:540px;
height:auto;
display:inline-block;
margin:0 auto;
vertical-align:top
}
Contents of div1 and div2 are responsive elements(either images or a slideshow)
All I want is for Div 2 to be centered vertically in the main wrapper because it is a bit smaller than Div1.
Any leads where to start?
I am trying to avoid using top margins on div2 because it gets very messy on mobile.
I would recommend using flexbox. Here's a fiddle showing how it works
https://jsfiddle.net/m0nk3y/xchy52u7/
CSS:
.wrapper {
display: flex;
justify-content: center;
align-items: center;
height: 300px;
width: 100%;
background: green;
}
.div1 {
max-width: 760px;
height: 100px;
background: gold;
}
.div2 {
max-width: 540px;
height: 50px;
vertical-align: top;
background: silver;
}
.wrapper {width:100%;
display:flex;
margin:0 auto;
align-items: center;
}
optionally, you may want to add flex-wrap:wrap; and justify-content: with a value of space-around or space-between
give the divs width:100%; and add clear: both; and make the container text-align: center; see code snippet
.wrapper{width:100%;
display:inline-block;
margin:0 auto;
text-align: center;
}
.div1 {
max-width: 760px;
width: 100%;
display: inline-block;
margin: 0 auto;
clear: both;
text-align: center;
}
.div2 {
max-width: 540px;
width: 100%;
height: auto;
display: inline-block;
margin: 0 auto;
vertical-align: top;
clear: both;
text-align: center;
}
<div class="wrapper">
<div class="div1"> some contents here</div>
<div class="div2"> some contents here</div>
</div>
I have a container that is 100vh (minus the fixed nav height).
<section class="container">
Inside this container I have some text:
<div class="text">
<p>title</p>
</div>
Which can be of any length as the content is dynamic.
Below this text I have an image:
<div class="image">
<img src="https://s-media-cache-ak0.pinimg.com/736x/d1/a6/64/d1a664bca214bf785a293cbc87950fc4.jpg">
</div>
The image needs to fill the rest of the 100vh (- nav height) container.
I use:
.container{
display:flex;
flex-flow: column nowrap;
....
Fiddle
The issue I am having is that I need the image to be the height of the rest of the space.
How can I do this? In my fiddle, if your screen is small it is being cut off and if your screen is large it does not fill the space. Height: 100% fails to work, making it too large.
Flex solutions only please, no table tricks - thanks!
Make the image container (.image) a flex container with height: 100%.
You can then fine-tune the image's aspect ratio and alignment with object-fit / object-position.
nav {
position:fixed;
background:grey;
width:100%;
height: 100px;
}
main {
padding-top: 100px;
}
.container {
display:flex;
flex-flow: column nowrap;
height: calc(100vh - 100px);
background: green;
border: 3px solid brown;
}
.text { background: yellow; }
/* ***** NEW ***** */
.image {
display: flex;
justify-content: center;
height: 100%;
}
img {
width: 100%;
object-fit: contain;
}
<nav>Nav</nav>
<main>
<section class="container">
<div class="text"><p>title</p></div>
<div class="image">
<img src="https://s-media-cache-ak0.pinimg.com/736x/d1/a6/64/d1a664bca214bf785a293cbc87950fc4.jpg">
</div>
</section>
<section class="container">
<div class="text"><p>hello</p></div>
<div class="image">
<img src="https://s-media-cache-ak0.pinimg.com/736x/d1/a6/64/d1a664bca214bf785a293cbc87950fc4.jpg">
</div>
</section>
</main>
Revised Fiddle
Note that the object-fit property is not supported by IE. For more details and a workaround see: https://stackoverflow.com/a/37127590/3597276
Maybe not exactly what you wanted, but if you move the image to a div and use it as a background, you can get the desired effect.
Fiddle
HTML:
Nav
title
<section class="container">
<div class="text">
<p>hello</p>
</div>
<div class="imageWrap">
<div class="image"></div>
</div>
</section>
</main>
CSS:
nav {
background: grey;
width: 100%;
height: 100px;
position: fixed;
}
main{
padding-top: 100px;
}
.container{
display: flex;
flex-flow: column nowrap;
height: calc(100vh - 100px);
background: green;
border: 3px solid brown;
}
.imageWrap {
flex: 1;
display: flex;
}
.image {
flex: 1;
background-size: contain;
background-repeat: no-repeat;
background-image: url(https://s-media-cache-ak0.pinimg.com/736x/d1/a6/64/d1a664bca214bf785a293cbc87950fc4.jpg)
}
.text{
background: yellow;
}
First of all I'd like to say that I am new to all this. So, here is my situation.
I have set a background image for my entire body.
I have a div .wrapper that I include in all my pages. It's main purpose is to keep my content centered and organized. It's background color is set to grey.
Inside the div .wrapper there is another div .example.
I want this particular div .example to take the background-image of the body and not the color of the .wrapper, but still keep the rest of the .wrapper's properties. Is it possible? I tried no to set the background-image of the .example the same as the body's because I think it shows like a "foreign" object...
<html>
...
<body>
<div class="wrapper">
<div class="example">
<img src="example.png">
</div>
</div> <!-- Wrapper -->
</body>
</html>
body {
background: #ffffff url("bg.jpg") repeat;
background-size: 4%;
background-position: center;
}
.wrapper {
width: 80vw;
margin: auto;
padding: 0px 0px;
background: #e9eaed;
display: block;
}
.example {
width: 100%;
/* background-color: #f6c5b6;*/
}
.example img {
width:40%;
margin: 40px auto;
display: block;
}
The Only way to achieve this is to build your div around it.
.body {
background-image: url('http://placekitten.com/g/40/40');
width: 500px; height:700px; background-repeat:repeat;
}
.wrapper {
width:300px; height:50px; background:grey; margin:0 auto;
}
.mask {
width: 300px; height:100px; margin:0 auto;
}
.left {
width: 50px; height:100px; float:left; background:red;
}
.right {
width: 50px; height:100px; float:right; background:red;
}
<div class='body'>
<div class="wrapper">
box 1
</div>
<div class="mask">
<div class="left">
box 2
</div>
<div class="right">
box 3
</div>
</div>
<div class="wrapper">
box 4
</div>
</div>
When there is no content in <div #id="profile-body"></div> the divs are parallel thanks to display: inline-block;, but when I fill this div with any kind of content, the height of the content acts as though it's changing the other divs margin-top that are parallel.
stylesheet.css
.profile-header{
margin: 0%;
padding: 0%;
font-size: 1em;
display: block;
}
.profile-body{
margin-left: 2%;
}
#profile-left{
display: inline-block;
width: 30%;
height:100%;
min-height:300px;
}
#profile-middle{
display: inline-block;
width: 30%;
height:100%;
min-height:300px;
}
#profile-middle-body div{
display: inline-block;
width: 30%;
}
#profile-middle-body p{
display: inline-block;
width: 65%;
}
#profile-right{
display: inline-block;
width: 30%;
height:100%;
min-height:300px;
}
index.html
<div id="profile-left">
<div class="profile-header">
<hr><p>Too Excessive</p><hr>
</div>
</div>
<div id="profile-middle">
<div class="profile-header">
<hr><p>Bio</p><hr>
</div>
<div id="profile-middle-body">
<!-- Comment to fix dix placement -->
<div>name</div><p>Brandon Nadeau</p>
<div>age</div><p>17</p>
<div>location</div><p>Alaska</p>
<div>member for</div><p>1 year</p>
<div>profile views</div><p>62</p>
</div>
</div>
<div id="profile-right">
<div class="profile-header">
<hr><p>About Me</p><hr>
</div>
</div>
Use float to those 3 divs. It should go up and remain there.
Fiddle
#profile-right{
width: 30%;
height:100%;
min-height:300px;
float:left;
}
#profile-left{
width: 30%;
height:100%;
min-height:300px;
float:left;
}
#profile-middle{
width: 30%;
height:100%;
min-height:300px;
float:left;
}
Since you're using inline-block with % widths for the layout - if you want to avoid floats, you could also try vertical-align:top; on all 3 divs.