I have a child div inside a parent div in order to make the child div responsive. I would like to make the parent div responsive as well, but after days of searching I haven't found any solutions.
My Code:
CSS
.parent {
width: 100%;
height: auto;
margin: 0 auto 0 auto;
background-color: #b5b5b5;
overflow: hidden;
position: relative;
}
.css-slideshow {
position: relative;
max-width: 1050px;
height: 345px;
margin: 3.5em auto auto auto;
z-index: 1;
}
.css-slideshow div {
margin: 0;
position: absolute;
max-width:100% !important;
height:auto;
}
.css-slideshow img {
box-shadow: 0 0 2px #666;
max-width:100% !important;
height:auto;
}
HTML
<div class="parent">
<div class="css-slideshow">
<div>
<img src="http://unilaboralgirona.com/wp-content/uploads/2015/03/ZContact.jpg" />
</div>
</div>
</div>
Here's a jsfiddle.
The grey portion is the parent div that I would like to be responsive so that it's bottom aligns with the bottom of child div (css-slideshow) as resolution decreases. I'm not sure if it makes a difference, but I would like the parent div's height to be responsive, not it's width, which is set to 100% in order to make it stretch across the entire page.
NOTE: The space above the image (child div: css-slideshow) is intentional; it makes room for a navigation bar. The z-index in css-slideshow is to keep it behind a responsive dropdown menu.Thanks for reading!
If you get rid of the position: absolute on the child and the height on the parent it appears to work. Is there a reason you made it position absolute?
http://jsfiddle.net/nupebzh4/4/
When you made the child position: absolute, it took it out of the dom flow. This made it so the parent did not know how tall the content was, which is why you had to hardcode a height. If you put the child back in the flow the parent container can auto size itself to contain the content as it changes size since you don't need to hardcode a height.
.parent {
width: 100%;
height: auto;
margin: 0 auto 0 auto;
background-color: #b5b5b5;
overflow: hidden;
position: relative;
}
.css-slideshow {
position: relative;
max-width: 1050px;
margin: 3.5em auto auto auto;
z-index: 1;
}
.css-slideshow div {
margin: 0;
max-width:100% !important;
/* responsive */
height:auto;
}
.css-slideshow img {
box-shadow: 0 0 2px #666;
max-width:100% !important;
/* responsive */
height:auto;
}
I changed a bit your css :
remove overflow:hidden
commented the height for .css-slideshow
remove the height:auto
http://jsfiddle.net/nupebzh4/5/
.parent {
width: 100%;
margin: 0 auto 0 auto;
background-color: #b5b5b5;
position: relative;
display:inline-block;
}
.css-slideshow {
position: relative;
max-width: 1050px;
/*height: 345px;*/
margin: 3.5em auto auto auto;
z-index: 1;
}
.css-slideshow div {
margin: 0;
position: absolute;
width:100%;
}
.css-slideshow img {
box-shadow: 0 0 2px #666;
width:100%;
}
Related
I have this css because I have two static navbars with fixed position.
But the #page takes height of its content and not of screen size, so it doesn't scroll.
I already tried height:100vh;
Any solution to make this #page take all the available space in the view so it makes the scroll useful?
#page {
position: relative;
margin-top: 110px;
margin-left: 220px;
padding: 15px 15px 15px 15px;
height: 100%;
overflow: scroll;
}
NAVBARS
.navbar-right{
width: 220px;
left: 0;
top: 0;
margin-top:110px;
position: fixed;
}
.navbar-top{
border-bottom-color: #fe6803 !important;
border-bottom: 5px solid;
position: fixed;
height: 110px;
left: 0;
width: 100%;
}
This will make the page fill out the screen, and subtract the margin above:
#page {
position: relative;
margin-top: 110px;
margin-left: 220px;
padding: 15px 15px 15px 15px;
overflow: scroll;
height: calc(100vh - 110px);
}
In order to make something of 100% height, parent of this element needs to also have a defined height. That includes body and html elements if it is the parent.
So if #page is a div inside another div (let's call it divA) inside body. Then html, body, divA and #page needs to be defined with 100% height.
html, body {
height:100%;
}
#page {
position: relative;
margin-top: 110px;
margin-left: 220px;
padding: 15px 15px 15px 15px;
height: 100%;
overflow: scroll;
}
In JSFiddle
This differs in behavior from width as width is often pre-defined to 100% width whereas the heights are automatic.
Take a look at this fiddle: https://jsfiddle.net/hkbynkmf/1/
How do I let the green border flow around all the divs, with no div "overflowing" the border? The upper div is OK, but the lower one is not.
Also, I need some distance between the divs;
I know that padding and margin is transparent, so I chose (a green) border to illustrate my point. In the end, the clearance should be transparent.
HTML:
body {
position: relative;
background-color: #ff0000;
background-repeat: no-repeat;
background-attachment: fixed;
padding: 0px;
border: 10px solid #190;
margin: 0px;
}
#header {
position: relative;
margin:0 auto; /* div will be H-centered */
top: 10px;
left: 0px;
bottom: 0px;
right: 0px;
width: 960px;
height: 250px;
background-color: #DDDDDD;
overflow: hidden; /* Keep all sub-elements inside this div */
}
#intro {
position: relative;
margin:0 auto; /* div will be H-centered */
top: 15px;
left: 0;
bottom: 0px;
right: 0px;
width: 960px;
height: 150px;
background-color: blue;
overflow: hidden; /* Keep all sub-elements inside this div */
}
<body> <!--position: relative;-->
<div id="header"> <!--position: relative;-->
</div>
<div id="intro"> <!--position: relative;-->
</div>
</body>
You're using the top attribute to move your intro div 15px down, below the header. This is causing the 15px overlap with the container. When positioning items this way you should consider using margin to apply the change, rather than the positioning attributes of top, right, bottom or left.
You have a lot going on with your CSS which is making the stylesheet much more complicated than it needs to be. I have simplified your CSS as follows to achieve the same effect:
body {
background-color: #ff0000;
border: 10px solid #190;
margin: 0px;
padding: 0px;
}
a img {
border:none;
}
#header {
background-color: #DDDDDD;
height: 250px;
margin:0 auto;
overflow: hidden;
width: 300px;
}
#intro {
background-color: blue;
height: 150px;
margin:15px auto 0;
overflow: hidden;
width: 300px;
}
See updated fiddle
In your code, the #intro is positioned 15px below the #header. Doing so leaves no place for the div in body.
Not sure what you are trying to achieve here with position: relative; but the #intro can be written like
#intro
{
margin:10px auto;/* div will be H-centered */
width: 300px;
height: 150px;
background-color: blue;
overflow: hidden;/* Keep all sub-elements inside this div */
}
Using the margin top property on the #intro div will allow the green border to flow, while also having the space in between the divs. Here is the fiddle:
https://jsfiddle.net/hkbynkmf/17/
#intro
{
position: relative;
margin:15px auto 0px auto /* div will be H-centered */
left: 0;
bottom: 0px;
right: 0px;
width: 300px;
height: 150px;
background-color: blue;
overflow: hidden; /* Keep all sub-elements inside this div */
}
I have a parent element with a relative width. With the browser fully expanded, its child element has a smaller width. By shrinking the browser, I can make the child element retain its width until it eventually becomes the SAME width as its parent element and subsequent shrinking causes both elements to retain equal width, but I have to use a FIXED max-width and can't make it work for a relative max-width. For example, I want to be able to achieve this by giving the child element max-width: 50% rather than max-width: 900px. Here is the CodePen.
HTML:
<div id="container">
<div id="firstDiv"</div>
CSS:
#container {
width:90%;
margin: 0 auto;
height: 300px;
background-color: #2b6dad;
border: 8px solid #2b6dad;
}
#firstDiv {
max-width: 800px; /*works for FIXED width*/
/*max-width: 50%;/* /*Doesn't work for RELATIVE width*/
height: 100%;
background-color: #da2225;
margin: auto;
}
#container {
width:90%;
margin: 0 auto;
height: 300px;
background-color: #2b6dad;
border: 8px solid #2b6dad;
overflow:hidden;
}
#firstDiv {
min-width: 50%;
width: 300px;
max-width:100%;
height: 100%;
background-color: #da2225;
margin: auto;
border-left: solid black 5px;
border-right: solid black 5px;
box-sizing: border-box;
}
<div id="container">
<div id="firstDiv"</div>
</div>
codepen
Your problem is here
#firstDiv {
width: 50%; /*Doesn't work for RELATIVE width*/
height: 100%;
background-color: #da2225;
margin: auto;
}
it may help you
I am trying to get an image horizontally and vertically centered within a div of variable height and width.
So far, so good.(See jsfiddle links below)
Now the catch is the image should also be responsive and adjust if either the height and/or width of the container is smaller than the images height or width.
After researching, going through all the different "solutions" out there and fiddling for a solution, I was unable to find the perfect one, however two came close:
1.) This first one does everything I need except when the window width is smaller than the image width, the image is no longer horizontally aligned:
http://jsfiddle.net/me2loveit2/ejbLp/8/
HTML
<div class="overlay">
<div class="helper"></div>
<img src="http://dummyimage.com/550x480/000/fff?text=H/V Centered and height/width variable">
</div>
CSS
.helper {
height:50%;
width:100%;
margin-bottom:-240px;
min-height: 240px;
}
.overlay {
width: 100%;
height: 100%;
position: fixed;
top: 0px;
left: 0px;
cursor: pointer;
background-color: rgba(0, 0, 0, 0.5);
}
img {
margin: 0 auto;
max-width: 100%;
max-height: 100%;
display: block;
}
2.) This second example keeps everything aligned, but the image does not scale down when the height is less than the image height:
http://jsfiddle.net/me2loveit2/ejbLp/9/
HTML
<div id="outer">
<div id="container">
<img src="http://dummyimage.com/550x480/000/fff?text=H/V Centered and height/width variable">
</div>
</div>
CSS
#outer {
height:100%;
width:100%;
display:table;
position:fixed;
top:0px;
left:0px;
background-color: rgba(0, 0, 0, 0.5);
}
#container {
vertical-align:middle;
display:table-cell;
max-width: 100%;
max-height: 100%;
}
img {
margin: 0 auto;
max-width: 100%;
max-height: 100%;
display:block;
}
I have attempted this in the past and the only non-JS solution I've come up with (which is frowned upon, by the way) is this:
html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
body {
display: table;
}
.container {
display: table-cell;
vertical-align: middle;
border: 1px solid #f00;
}
.container > div {
width: 100%;
height: 100%;
margin: 0 auto;
border: 1px solid #00f;
max-width: 550px;
max-height: 480px;
background: url("http://dummyimage.com/550x480/000/fff?text=H/V Centered and height/width variable") 50% 50% no-repeat;
background-size: contain;
}
<div class="container"><div></div></div>
http://jsfiddle.net/pYzBf/1/
Set the background color of the div to black to see the scaling without the image edges. I used those dimensions so you can test it by resizing the frame.
I'm trying to get a div to fill the remaining height of a div. Here's my HTML and CSS:
CSS:
* {
padding: 0px;
margin: 0px;
}
#container {
margin: 85px auto 0px auto;
background: #444444;
min-height: 500px;
width: 900px;
}
#topbar {
width: 900px;
height: 85px;
background: #555555;
}
#leftbar {
width: 250px;
height: 100%;
background: #666666;
}
HTML:
<div id="container">
<div id="topbar">
</div>
<div id="leftbar">
</div>
</div>
I expected leftbar to fill the height between the bottom of topbar and the bottom of container, but it's scretching container so that leftbar is 100% of the page height.
You can stretch the leftbar with absolute positioning and setting the top/bottom values:
* {
padding: 0px;
margin: 0px;
}
#container {
position: relative;
margin: 85px auto 0px auto;
background: #444444;
min-height: 500px;
width: 900px;
}
#topbar {
width: 900px;
height: 85px;
background: #555555;
}
#leftbar {
position: absolute;
top: 85px;
bottom: 0;
width: 250px;
background: red;
}
Fiddle:
http://jsfiddle.net/robertp/CQ7pf/
Try adding this to container:
position: relative;
and then add this to leftbar:
position: absolute;
top: 0;
bottom: 0;
Set your left bar to position: relative;
So leftbar should be container's height minus topbar's height. Since container and topbar have hard-coded height values, it follows that leftbar will have to be hard-coded also. It's not the prettiest thing in the world but it's simpler than the alternative, JavaScript.
If container is 500px in height, subtract the height of topbar (85) and container's margin (85) to arrive at a height of 330px. Since container uses min-height, use min-height for leftbar also to allow it to stretch the container if need be. You should also change leftbar's position to relative to render the height of container correctly.
Bottom line:
#leftbar {
position: relative;
min-height: 330px;
}