I've been trying to make a page in which there will be one image that needs to be the same height as the viewport while the width would stretch accordingly. The img needs to be centered.
Here's the tricky part (for me anyway): I would need to place a div at the bottom of that image which would have some buttons. I thought that it would be best if I can set the width of that div to be the same as the width of the img so that whenever the screen size changes, everything would stay at the same position.
Here is what I have so far in the HTML:
<div id="main_container">
<div class="exercises">
<img class="bg" src="image.png"/>
<div class="footer">
<ul class="buttons">
<li>Reset</li>
<li>Next</li>
<li>Sortie</li>
</ul>
</div>
</div>
</div>
So: height is always == viewport, width is variable.
I thought about placing a div as parent to the img, but I would still need to make that div somehow fit the size of its child (the image).
I've read some posts here, but none were too related to my issue.
edit:
I'm sorry if I wasn't clear enough. I've made an illustration of what the issue is. I hope this helps:
You can try this:
#main_container {
width:600px;
}
.exercises {
width: 100%;
}
.exercises img.bg {
max-width: 100%;
max-height: 100%;
object-fit:contain;
}
.footer .buttons {
width: 100%;
}
.buttons {
padding:0;
margin:0;
}
.buttons li {
width:100%;
height:50px;
border:1px solid #000;
list-style:none;
}
<div id="main_container">
<div class="exercises">
<img class="bg" src="http://blog.caranddriver.com/wp-content/uploads/2015/11/BMW-2-series.jpg"/>
<div class="footer">
<ul class="buttons">
<li>Reset</li>
<li>Next</li>
<li>Sortie</li>
</ul>
</div>
</div>
</div>
Obviously, edit the image URL to your own :)
Consider it a start. Change size of image in inspector and see what happens.
.exercises{background: silver; display: inline-block;}
img{ border: 1px solid black;}
ul{
margin: 0;
padding: 0;}
li{
float: left;
width: 33%;
background: #ccff00;
}
<div id="main_container">
<div class="exercises">
<img class="bg" src="https://www.techwibe.com/wp-content/uploads/2015/04/chrome_medium.jpg"/>
<div class="footer">
<ul class="buttons">
<li>Reset</li>
<li>Next</li>
<li>Sortie</li>
</ul>
</div>
</div>
</div>
Try this one may help
<div id="main_container">
<div class="exercises">
<img class="bg" src="https://static.pexels.com/photos/27714/pexels-photo-27714.jpg"/>
<div class="footer">
<ul class="buttons">
<li>Reset</li>
<li>Next</li>
<li>Sortie</li>
</ul>
</div>
</div>
</div>
<style>
#main_container{
/*Make the container follow the width of body*/
position:relative;
width:100%;
}
#exercises{
/*Make the this div follow the width of #main_container*/
width:100%;
}
.bg{
/*Make the the image follow the width of #exercises*/
width:100%;
height:auto;
}
.buttons{
/*modifying ul to the bottom position*/
position:absolute;
width:100%;
bottom:0;
}
.buttons li{
/*Style the button whatever you want*/
list-style:none;
margin:10px 2%;
padding:8px 12px;
background:orange;
width:20%;
color:white;
float:left;
}
</style>
Run code snippet to see the result
I apologize again if I've set the question in a vague manner. It was a bit complicated (for me) to formulate it. And thanks to the people who tried to help me with their solutions.
However, I decided to got with JQuery on this one and set the sizes dynamically.
This is the code with which I managed to set the width of the parent div the same size as the width of it's child img (after the img's height has been rescaled relative to the window height):
var imageResize = function () {
var screenHeight = $(window).height();
$('.bg').height(screenHeight);
var resizedImgWidth = $('.bg').width();
$('.exercises').width(resizedImgWidth);
}
$(window).resize(imageResize);
$(window).load(imageResize);
As always, anyone is welcome to give their two cents regarding my solution. (perhaps its very heavy and could be better optimized. I'm new at JS, so feel free :))
Related
I have a web page layout. consists of three parts. the layout is pretty simple and works fine. The problem for small screen the header expands and covers the container beneath it. My question is, how to make the header container expands and in the same time do not cover the container beneath it? Please exclude the fixed height. for example (Height: 150px;). the height must be set to auto or any mode that makes it responsive.
here is the HTML code:
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="repeat.css">
</head>
<body>
<div class="header-container">
<div>
<ul class="left">
<li>home</li>
<li>menu</li>
<li>links</li>
<li>contacts</li>
</ul>
<ul class="right">
<li>home</li>
<li>menu</li>
<li>links</li>
<li>contacts</li>
</ul>
</div>
</div>
<div class="main-container">
<div class="left-column">
<div class="wrap">
</div>
</div>
<div class="main-column">
<div>
<p> this part should not be covered when the header
container change its height </p>
<p> this part should not be covered when the header
container change its height </p>
<p> this part should not be covered when the header
container change its height </p>
<p> this part should not be covered when the header
container change its height </p>
<p> this part should not be covered when the header
container change its height </p>
<p> this part should not be covered when the header
container change its height </p>
</div>
</div>
</div>
</body>
</html>
The css code :
* {
margin: 0px;
padding: 0px;
}
.header-container{
position:fixed;
top:0;
left:0;
width:100%;
height:80px;
background: #ccc;
box-sizing: border-box;
z-index: 10;
}
.left{
padding-left: 50px;
float:left;
}
.right{
padding-left: 300px;
float: left;
}
.main-container{
position:absolute;
margin-top: 80px;
left:0px;
width:100%;
height: calc(100% - 80px);
box-sizing: border-box;
background-color: pink;
z-index:1;
}
.left-column{
position:relative;
padding:0;
margin:0;
float:left;
width:20%;
height:100%;
background-color: #4db6ac;
}
.main-column{
position:relative;
padding:0;
margin:0;
float:left;
width:80%;
height:100%;
background-color: #00e5ff;
}
#media screen and (max-width:720px){
.header-container{
height: auto;
}
.left-column{
display:none;
}
.main-column{
width:100%;
}
}
I got the solution using the jQuery. The solution is posted by Marc Audet. You can find the solution following this link:
CSS, image with height:auto, covers div beneath it upon screen resize (phone held horizontally)
Please if any one has another solution using only css will be highly appreciated.
I have a div container in my html page and i want set its height to expand all remaining page in the screen..
How can i do that ??
That's my code :
HTML
<div class="row">
<div id="builder-container" class="col-xs-12 col-lg-9">
<div id="builder-content" > </div>
</div>
</div>
CSS
#builder-container {
background-color: #ccc;
height: 100%;
}
You have to give all of the parent elements, including the div you want to extend, a height of 100%.
Actually it would not get cover your whole page without enough content, but the best way is to give it 'position:absolute/fixed/relative' and give the same div top:whateveryouwant px; and bottom: 0px/0%; width and height :100%
JSFiddle - Edited: Check it now
CSS
body
{
margin:0;
width:100%;
height:100%;
}
#builder-container {
display:block;
position:absolute;
margin-top:5%;
left:0%;
bottom:0%;
background-color: #ccc;
height: 100%;
width:100%;
}
html
<div class="row full_height">
<h1>Test elem</h1>
</div>
css
.full_height {
height: 100vh
}
I am trying to make a phpBB theme, but am having trouble setting up the DIV's the way I envisioned the page to be.
This is what I am looking to do.
http://imgur.com/n6eZbsT (image to help with understanding)
This is what I had so far.
index.html
<body>
<div id='container'>
<div id='header'>
</div>
<div id='sidemenu'>
</div>
<div id='content'>
</div>
</div>
</body>
css.css
*{
padding: 0px;
margin: 0px
}
#header {
width:100%;
height:50px;
background-color:#6FF;
}
#sidemenu {
display: block;
position:absolute;
width:100px;
height:100%;
background-color:#6F6;
}
The problem with this is that the sidemenu DIV overflows to the bottom of the page and creates a scrollbar.
I have tried looking for a solution, and only managed to get the overflow to go away by using bottom:0px; inside the sidemenu css
But this pushes the header to the right, and sidemenu to the top left corner like this..
http://imgur.com/LKk9VtF
I am stuck here, css gives me a huge headache ahaha. Can anyone please help me with a solution?
Thank you so much!
You should use height:calc(100% - 50px); top:50px; for your sidemenu. Run the snippet bellow.
*{
padding: 0px;
margin: 0px
}
#header {
width:100%;
height:50px;
background-color:#6FF;
}
html,body,#container{height:100%;}
#sidemenu {
display: block;
position:absolute;
width:100px;
height:calc(100% - 50px);
top:50px;
background-color:#6F6;
}
<div id='container'>
<div id='header'>
</div>
<div id='sidemenu'>
</div>
<div id='content'>
</div>
</div>
How do I make the <hr> width 100% of the screen, not its parent?
CSS
#abc {
width: 700px;
}
hr {
width: 100%;
}
HTML
<div id="abc">adsasd<hr></div>
You cannot do it the way that you have it structured. You need the <hr> outside of that container that has a fixed width. Otherwise 100% width of the <hr> will be relative of its parent, not the page. Try
<div id="abc">
adsasd
</div>
<!-- put the hr on its own -->
<hr>
<div id="def">
asdfghjkl
</div>
Another thing that you can do is to enclose the content in a container with that width, but not the div containing the content and the hr.
<div id="row"><!-- use a class that takes up all the width of the page -->
<div class="has-width">adsasd</div> <!-- create a class with a width-->
<hr>
</div>
If it was me, I'd refactor the markup to something like:
<div id="menu">
<div class="buttons">Buttons</div>
</div>
<div id="main">
Main
</div>
and use CSS for the horizontal lines:
#menu
{
width:100%;
border-top:solid 1px black;
border-bottom:solid 1px black;
}
#menu .buttons, #main
{
width:400px;
margin:0 auto;
}
#menu .buttons
{
background-color:#F00;
}
Demo
I can't set a height (in %) to a div (class="item") whose parent (class="site-section") has a min-height: 100%.
This is my HTML:
<div class="spacer"></div>
<header class="site-header"></header>
<div class="spacer"></div>
<div class="spacer"></div>
<section class="site-section">
<div class="column">
<div>I would like to be able to set an later
change the height of this green item.
<div class="item">ITEM</div>
</div>
</section>
<div class="spacer"></div>
<div class="spacer"></div>
<footer class="site-footer"></footer>
<div class="spacer"></div>
This is my CSS:
html, body {
height:100%;
margin 0;
color:blue;
}
.spacer {
height:1%;
}
.site-header {
height:8%;
background-color:yellow;
}
.site-section {
min-height:78%;
background-color:#ffcccc;
color:#aaa;
}
.site-footer {
height:8%;
background-color:yellow;
}
.column {
width: 50%;
}
.item {
height: 40%;
background-color: #33cc33;}
Here is the DEMO.
Everything was working fine until I added DOCTYPE to my HTML. There was no need to set height (in %) for html, body and .site-section, so .item was having his height: 20%. Now, because of DOCTYPE I need to set height for html, body, and .site-section. The consequence is that .item does not react to height: 20% anymore.
Any idea how to solve this?
P.S. I've based my demo on Bart's demo in this question.
#CBroe is correct in that you can't really get a height percent unless the parent itself has a height (ex. height: 35px). I would recommend setting the height of the div, then your inside divs can be set to percentages.
But I played a tiny bit with your fiddle and didn't know if adding position: absolute to the your class item CSS is sort of what you're looking for? So your CSS would look something like this:
.item {
position: absolute;
height: 40%;
background-color: #33cc33;
}
Here is the demo modified to show the example.
NOTE: Even though the height is flexible, if you set the height to 100% it will go above the rest of the divs.
.item{
position:absolute;
height:40%;
background:#33cc33;
}
.items {
position:relative;
height:100%;
background:inherit;}
HTML
<div class="item">
<div class='items'>
ITEM Try
</div>
</div>
Try :)