I am new to Bootstrap, responsive web design, and am trying to create a page for my band but I am running into a few issues. I will try to explain as best as I can my situation and I greatly appreciate any and all help...
I have a div that has a background image of the band and I need it centered in the screen (regardless of the size of the screen) inside of one of Bootstrap's container DIV's. On top of that image, I have another image that I want to include. I am able to get that result so far. The problem I am running into is when I try to position the child DIV inside of the parent DIV. I have tried to set the parent DIV with a position: relative and the child DIV with a position: absolute, but as I begin to resize the screen, the inside image suddenly enlargens and stretches outside of the parent DIV. Also, when using this method and trying to position the child DIV 75% from the top of the parent DIV by using top: 75%;, it makes the image jump out of the parent again and puts it down at the bottom of the screen.
Here is the code that I have used so far but cannot get to work. Again, any help at all would be greatly appreciated as I am new to Bootstrap and responsive design but I would love to figure this stuff out. Thanks in advance!
MY HTML:
<div class="container">
<div class="row">
<div class="col-md-12 bandphoto">
<div class="row">
<div class="col-md-6 col-md-offset-3 theband-title">
<img src="images/theband.png" class="img-responsive ">
</div>
</div>
</div>
</div>
MY CSS:
.bandphoto {
position: relative;
max-width: 2000px;
min-height: 999px;
background: url(images/bandphoto.png);
background-size: 100%;
background-repeat: no-repeat;
}
.theband-title {
position: absolute;
top: 75%;
}
http://jsfiddle.net/isherwood/w3mgQ
Am I doing something wrong here? If so, please advise with and feel free to educate me as well so that I can better understand this stuff.
I think you want to avoid using absolute positioning (because it's very difficult to keep centered), and don't try to force Bootstrap elements when they're not needed. It's not clear to me how you want to position the inner image because I have no size reference, but maybe this gets you closer:
http://jsfiddle.net/isherwood/w3mgQ/3/
.bandphoto {
position: relative;
max-width: 2000px;
min-height: 999px;
background: url(http://placehold.it/600x600/333333);
background-size: 100%;
background-repeat: no-repeat;
}
.theband-title {
margin: 0 auto 0;
}
<div class="container">
<div class="row">
<div class="col-md-12 bandphoto">
<img src="http://placehold.it/500x500" class="theband-title img-responsive" />
</div>
</div>
</div>
I believe what you're looking for is display:table and his friends. Here's a JSFiddle demonstrating:
http://jsfiddle.net/VcFf9/4/
I've set the container to be 100% height, and then vertically aligned the background and the content of the cell. Bootstrap's grid lends itself quite well to display:table, so try it out! :D
Related
I've a sidebar which is positioned sticky but in some cases larger than the height of the screen.
If the sidebar is in fact larger as the screen height, I don't want it to stick on the top. It should scroll down with the content of the page.
I'm using Bootstraps sticky-top class for that.
It has the following attributes:
.sticky-top {
position: sticky;
top: 0;
z-index: 1020;
}
I changed the top: 0 to top: 50px in my case because I need the space above.
Here's some example code: https://codepen.io/cray_code/pen/ZEaOXwo
<div class="container">
<div class="row">
<div class="col-lg-3">
<div class="toc sticky-top">
<nav class="list-group">
Links (see example)
</nav>
</div>
</div>
<div class="col-lg-9">
Content (see example)
</div>
</div>
</div>
I tried the solution from here and added the following code to my class:
.toc {
overflow-y: auto;
max-height: 100vh;
}
But that doesn't help.
Is there a pure CSS solution for that or do I need to use JavaScript?
Not sure if this is what you want, but maybe using the calc() in your css could help you.
.toc{
overflow-y: auto;
max-height: calc(100vh - 50px);
}
Hope this pen helps
Some explanations:
in js we use .offsetHeight&.clientHeight to get height we check weather this height(493px) + 50px offset is more than screen height or not.
When screen size is small we set position to static
Also we set margin-top: 50px instead of top: 50px
because top works only for sticky and margin-top works for static
For aside blocks that higher then window height you can use smartSticky script
Just add "data-smartsticky" atribute to your aside block
https://www.npmjs.com/package/smartsticker
Parent block of sticky block must be height 100%, or for flex - flex-grow:1
I want to fit an image inside some divs and make it as big as possible without changing the aspect ratio. Setting max-height and max-width to 100% on both the image and its parent does not work and the image overflows (I guess because the parent does not really have a width or height so the image can not resize to it?).
JSFiddle
<div class="block">
<div class="row">
<div class="col-xs-9">
<div class="thumbnail">
<img class="placeholder" src="https://unsplash.it/900/600" alt="">
</div>
</div>
<div class="col-xs-3">
</div>
</div>
</div>
Edit:
I will clarify what I really want to achieve later this evening when I have time to write.
Set the the image as background image to .thumbnail, and constrict it's size using background-size (demo):
.thumbnail {
display: inline-block;
margin: 0;
height: 100%;
width: 100%;
background: url("https://unsplash.it/900/600") no-repeat;
background-size: contain;
}
If you want the parent div to match its content, add display:table; in your "outline-block" css class.
Fiddle
Similar post on stack overflow: how-do-i-auto-resize-an-image-to-fit-a-div-container
So,
i'm guessing this has been asked before but im realy missing something here which i've been starring myself dead at for the last hour.
I've got a container with two divs, position relative and absolute since i want them to stack. The stacking part works. However, this container, the #mockupcontainer, needs to be inside the #what-container-inner which i cant seem to get to work. At the moment it just stops the #what-container-inner and placed the #mockupcontainer after it. My jsfiddle illustrates perfect whats going wrong. I hope someone can help me on this one.
https://jsfiddle.net/rvq41vaf/
CSS:
#what-container-outer {
background-color: #ececec;
margin-left: auto;
margin-right: auto;
}
#what-container-inner {
background-color: #ececec;
width: 75%;
margin-left: auto;
margin-right: auto;
position: relative;
}
.mockupcontainer{
position: relative;
}
#pin-mockup{
position: absolute;
z-index: 10;
}
#pin-mockup-site1{
position: absolute;
z-index:11;
}
HTML Code:
<div id="what-container-outer">
<div id="what-container-inner">
sdfsdfsdfsdfsdfsdfsdfsdf<br>sdfsdfsdfsdfsdf
<div id="trigger-mockup"></div>
<div class="mockupcontainer">
<div id="pin-mockup">
<img src="image2" class="img-responsive" alt="Realiseren">
</div>
<div id="pin-mockup-site1">
<img src="image1" class="img-responsive" alt="Realiseren">
</div>
</div>
</div>
</div>
Kind Regards,
Donny.
Since you have your two image-containing divs (pin-mockup and pin-mockup-site1) positioned absolutely, they are pulled out of the flow and the enclosing divs no longer leave room for them. The solution is to set the larger one to position: relative so that the enclosing div does make room for it. Setting top:0 on the second (absolutely positioned) div brings it to the top of the enclosing div.
Here's the fiddle.
See also the MDN docs on the position property.
I'm just starting to learn html and css and I've been looking at various websites to practice.
This particular website (http://jsfiddle.net/Hexapod/CWB39/260/show/) had caught my attention but I'm having trouble figuring out how the elements here are working.
If you go to the website, there are "facts boxes" that were made using div elements. These div elements however, are grouped together by a another div element. This div element has an absolute position and an offset of 0px in all directions. Can anyone explain to me what the purpose of this is?
Here's what it looks like:
#container {
position: absolute;
top: 0px;
bottom: 0px;
left: 0px;
right: 0px;
}
<div id="container">
<div id="factbox1" class="info">
<!-- some code -->
</div>
<div id="factbox2" class="info">
<!-- some code -->
</div>
</div>
Thanks in advance!
PS. If I'm doing something wrong with the formatting or anything, please inform me! This is my first time posting here.
This is in place to stretch the element to the full extremes of the closest parent with position set. In this case, to extend the full height and width of the browser viewport.
Its basically telling the element that its top should meet the top side of its parent, its bottom should stretch to the bottom of its parent and the same for its left and right sides.
An alternative would be to use the below CSS:
html, body, #container{
height:100%:
width:100%;
}
The difference being that by using position:absolute the option for layering content is provided.
You can use the inset shorthand these days (not supported by IE of course)
#container {
position: absolute;
background: #002D62;
inset: 0px;
color: #fff;
}
<div id="container">
<div id="factbox1" class="info">
Full with and height 😄
<!-- some code -->
</div>
<div id="factbox2" class="info">
<!-- some code -->
</div>
</div>
I got a centered website with a fixed width. Now, I want to add an ad banner of fixed width/height to the right side of the page.
There is a wrapper div of width 700px. But I can't get my banner on the side. I dont know why. Can someone help me please with my CSS id?
<div class="wrapper">
<div class="head"></div>
<div class="content"></div>
</div> //wrapper
EDIT:
Image - (link removed as broken)
This is what I want. I got the orange one. I don't want to set the banner position to the right browser border. It should be near the wrapper class.
If you want the banner to be inside the wrapper:
HTML
<div class="wrapper">
<div class="banner"></div>
<div class="head"></div>
<div class="content"></div>
</div>
CSS
div.banner { float: right; width: 100px; height: 400px; }
If you want it outside the wrapper:
HTML
<div class="banner"></div>
<div class="wrapper">
<div class="head"></div>
<div class="content"></div>
</div>
CSS
div.banner { float: right; width: 100px; height: 400px; }
You can also check out this site for a bunch of different CSS/HTML layouts:
http://layouts.ironmyers.com/
Following Edit...
I think this question at Stack Overflow has what you want.
Go to this site and you will find the answer to your question and all the your future questions (judging by your question)
http://www.w3schools.com/css/css_float.asp
put the give the ad-div in the wrapper-div, give it an absolute position and move it to the right. Make sure to give the wrapper-div a relative position.
html
<div id="wrapper">
<div id="ad">ad</div>
</div>
css
#wrapper{
position: relative;
}
#ad {
position: absolute;
right: -140px;
}
The wrapper div will still be centered, the ad-div will 'hang' next to it.
Did you try using float: right property ?