defining height of a div to scale - html

Is there any way to define the height of a div without it being fixed to that defined size? For example setting a div to 200px it will be locked to that, and so when it scales it wont change.
I know that you can use % but when I try to do this my image disappears so im forced to using units like px or vw, and im not sure how to create a fluid design without being locked into a specific set size.
Here is my code. I'm wanting .face background image to appear but by using a % it no matter how big I make it, it wont show the image. If I add a px or vw number to its container .topwrap it then creates a large gap to the div container .lowerwrap. So basically I'm trying to find a way to get the image to display itself in a correctly sized div that is able to scale to browser size.
/*----------------------------Top ----------------------------*/
.topwrap{
position:relative;
top:50px;
height:auto;
}
.face{
position:relative;
float:right;
background:url(../images/face2.png) no-repeat;
background-size:100%;
top:10%;
width:50%;
height:200%;
display: flex;
right:16%;
display: block;
min-width:160px;
}
.txtwrap{
position:absolute;
margin-top:1.3%;
font-size:1.3vw;
float:right;
right:39%;
text-align: center;
}
.sptxt{
color:#171717;
font-weight:bold;
}
.sptxt2{
color:#171717;
font-weight:400;
}
/*----------------------------Lower----------------------------*/
.lowerwrap{
position:relative;
float:left;
width:100%;
height:300px;
top:50px;
padding-top:10px;
display: flex;
box-shadow: 0px 10px #333 inset;
}
.contentwrap{
position:relative;
width:100%;
background:url(../images/content.png) no-repeat;
background-size:100%;
display: block;
z-index:1;
}
/*----------------------------Footer----------------------------*/
.footerwrap{
position:relative;
float:left;
width:100%;
background:#09C;
top:50px;
text-align:center;
}
.foottxt{
text-align: center;
font-family: 'Roboto', sans-serif;
font-size:0.5vw;
color:#FFFFFF;
font-weight:200;
z-index:300;
}

Since we cannot see the HTML that you are applying your styles to, it is difficult to provide a solution. Have you tried using the following on the .face class?
min-height: 200px;

If you want to allow a <div class="face"> element to resize fluidly based on that percentage width, but maintain a given aspect ratio, try replacing your .face rule set with this:
.face {
position:relative;
float:right;
background:url(../images/face2.png) no-repeat;
background-size:100%;
top:10%;
width:50%;
right:16%;
display: block;
min-width:160px;
}
.face:before {
display: block;
content: '';
padding-top: 100%;
}
The above code assumes face2.png's height and width are the same (a square image, or 1:1 aspect ratio). To make div.face fit your actual image, divide your image's height by its width in pixels, multiply that by 100%, and use that for .face:before's padding-top value. For example, if face2.png is 180px tall by 120px wide, use padding-top: 150%;.
See: http://www.goldenapplewebdesign.com/responsive-aspect-ratios-with-pure-css/

Related

How to center an image horizontally while pushing the bottom (unknown resolution) and keep centered if window is too narrow

Let's say I have an image of unknown resolution. I want to center it horizontally, even if the window is narrower than the picture, and push the bottom of the window to fit the height of this picture.
How can I achieve that with css only? (no javascript)
Obviously the picture will be in an < img > tag since this is the only way to push the bottom. Align it center is easy, the hard part is to keep it centered just like a background-position:center top because when simply centering this < img > tag it will hit the left border of the window instead of overflowing hidden and staying centered.
Thanks!
If you want to do this with the img tag you can use the following two codes:
object-fit:cover;
object-position:50% 50%;
These are pretty much the same as background-size and background-position.
For example how this would be done in code:
html,body {
width: 100%;
height: 100%
}
img {
width: 100%;
max-width: 100%;
max-height: 100%;
object-fit: cover;
object-position: 50% 50%;
float:left;
}
div {
width: 100%;
height: 100%;
background: #222;
display:flex;
color:#fff;
align-items:center;
justify-content:center;
}
<img src="https://www.rafaeldejongh.com/wp-content/uploads/2016/12/GlitchSkull1.jpg" alt="Rafael De Jongh - Glitch Skull"/>
<div>
<p>Other Content</p>
</div>
You can view this as well on JSFiddle
And in case you do want to have a full fluid background image that is set to 100% of the viewports width and height, you could also do this via the following code:
html,body {
width:100%;
height:100%
}
#img {
width:100%;
height:100%;
max-width:100%;
max-height:100%;
background-image:url(https://www.rafaeldejongh.com/wp-content/uploads/2016/12/GlitchSkull1.jpg);
background-size:cover;
background-position:center center;
background-repeat:no-repeat;
float:left;
display:inline-block;
}
#content {
width:100%;
height:100%;
background: #222;
display:flex;
color:#fff;
align-items:center;
justify-content:center;
}
<div id="img"></div>
<div id="content"><p>Other Content</p></div>
Also on JSFiddle
Feel free to ask if you have any further questions!
Voila:
https://jsfiddle.net/gwja6f6z/
HTML:
<div id=main>
<div id=imgwrp>
<img src="https://upload.wikimedia.org/wikipedia/commons/9/94/Ingres%2C_Self-portrait.jpg" border=0>
</div>
</div>
<p>
The picture stays center and does not hit the borders of the window when window is too narrow, and this text is pushed without knowning the height of the picture.
</p>
CSS:
html, body {
width:100%;
height:100%;
text-align:center;
overflow-x:hidden;
}
#main {
position:relative;
left:50%; /* This div is just a 0px width line in middle of window */
width:0;
overflow-x:visible;
text-align:center;
}
#imgwrp {
display:inline-block; /* Image container width is same as image itself */
position:relative;
margin:0 auto 0 auto;
}
#imgwrp img {
display:block;
position:relative;
margin-left:-50%; /* The image is pushed left by 50% its container width */
}

image disappears when changing height in % vs vw in CSS

Im busy designing my portfolio website and I have some images that I want to be able to scale correctly to the size of the browser or device. I have been messing around with vw and it seems to solve the problem quite nicely. However due to being a student im looking at making sure the code is solid in that it wont be affected on different browsers or devices.
So the main problem here is when I use % on one image to adjust the height, the image dissapears completely. I can only make it appear in the correct position using either px or vw and was trying to find out why? (Take note I am only needing to adjust the height, id like to be able to use % otherwise I end up with a huge gap when scaling it down to smaller devices and browsers)
Here is my CSS:
/*----------------------------Top ----------------------------*/
.topwrap{
position:relative;
top:50px;
}
.face{
position:relative;
float:right;
background:url(../images/face2.png) no-repeat;
background-size:100%;
top:10%;
width:50%;
height:15vw;
right:16%;
min-width:160px;
}
.txtwrap{
position:absolute;
margin-top:1.3%;
font-size:1.3vw;
float:right;
right:39%;
text-align: center;
}
.sptxt{
color:#171717;
font-weight:bold;
}
.sptxt2{
color:#171717;
font-weight:400;
}
/*----------------------------Lower----------------------------*/
.lowerwrap{
position:relative;
float:left;
height:100%;
width:100%;
top:50px;
padding-top:10px;
box-shadow: 0px 10px #333 inset;
}
.contentwrap{
position:relative;
width:100%;
height:15vw;
background:url(../images/content.png) no-repeat;
background-size:100%;
display: block;
z-index:1;
}
body{ font-size: 16px }
#media screen and (max-width: XXXpx) {body { font-size: 12px } }
#media screen and (max-width: XXXpx) {body { font-size: 8px } }
for the rest of your code, such as padding, margin you can use em/rem,
if will scale well, according to the font-size of body; that way your "GAP" between image will resize as well.

Mobile Layout Not Rendering Width: 100%

My first attempt using bootstrap on a webapp - this is how it renders on my iPhone.
Why is everything showing up under 100% width?
Example: The top of the page is called dark container, a wrapper with an image embedded, this is the css
#darkcontainer {
height:200px;
width:100%;
border: none;
background-color:whitesmoke;
text-align:center;
}
My guess is that the image beneath(part of a slider) is pushing everything under 100% because it is much larger? That slider has the following css
#slides {
height:500px;
width:1065px;
margin: 0 auto;
ul.pagination {
width:100%;
text-align: center;
li {
display:inline-block;
}
}
}
.slides_container {
width:1065px;
height:500px;
}
.slides_container div {
width:1065px;
height:500px;
display:block;
}
The mailing list looks like it would be different, because there is more of the background color there before it turns white, but that's just... a 100% width wrapper centering the child
#autoheightwrap {
height:auto;
width:100%;
background-color:whitesmoke;
}
#mailform {
height:100%;
width:600px;
margin: 0 auto;
background-color: whitesmoke;
padding: 6px 12px;
...
Feel free to look at the website in your browser here.
Any ideas why it is being rendered this way?
#slides is set at a width of 1065px. This is the culprit that is breaking your site. You'll have to restyle it with a media query targeting mobile.
For a more responsive friendly slideshow, might I suggest cycle2?

How to change height and width according to the screen, (with minimum)

There is a box in the center of the page which will be presented as slides. I would like to increase its size according to the users' screens. and also set a minimum size, so when a screen is so small but my box wont be smaller.
Here is my code:
.container{
width:1000px;
height:500px;
position:absolute;
left:50%;
top:50%;
margin:-275px 0 0 -500px;
}
.main-content{
float:left;
width:1000px;
height:500px;
position:absolute;
background:url(../img/map.png) no-repeat center top;
border: rgba(255,255,255,0.2) solid 1px;
}
HTML
<div class="container">
<div class="main-content">hi</div>
</div>
I think I have to use min-height/width but should my actual width and height be in percentage? it didnt work in px.
So generally I am looking for a standard solution for this issue.
Use percentages instead of fixed values:
.main-content{
float:left;
width: 80%; /* Use the percentage that fits to your needs */
height: 80%;
min-height: 300px; /* You can also define minimum width and height */
min-width: 300px;
position:absolute;
background:url(../img/map.png) no-repeat center top;
border: rgba(255,255,255,0.2) solid 1px;
}

CSS Background Color Position Off

I set a background color for a div, but for some reason the color box is not underneath the text, but like 100px toward the top. This is my HTML code;
<div class="youtube_gallery_caption">The Fast Show - Unlucky Alf</div>
and This is CSS
.youtube_gallery_caption {
background:red;
color:black !important;
width:140px;
height:10px;
padding:10px;
position:relative;
z-index:99;
display:none;
}
Thank you.
Remove height and add line-height:30px...change the value to whatever height you want. Also, add overflow: hidden;. You might also want to add padding to just the left and right, versus all around, so you're not adding to the line-height. Instead this will remove any padding on the top and bottom padding:0 10px;
Try replacing your CSS with this:
.youtube_gallery_caption {
background:red;
color:black !important;
width:140px;
line-height:30px;
padding:0 10px;
position:relative;
z-index:99;
display:none;
overflow:hidden;
}
Your box has a height of 10px, and you font-size isn't specified so is likely larger.
Match up the font-size and line-height with the actual height of the div.
Eg: (using the 10px value):
http://jsfiddle.net/D2g9H/
.youtube_gallery_caption {
background:red;
color: black !important;
width:140px;
height: 10px;
font-size: 10px;
line-height: 10px;
padding:10px;
position:relative;
z-index:99;
}