I have a dilemma with some code in HTML & CSS, when I add a simple header with an image and I set properties to image {height:100%;
width: auto;} it works, but when I have the image in more complex code isn`t working anymore.
In the smaller code if i change height of header the images change as well,but on second code if i change height of header image stays on same size
Can someone please explain to me why that happens?
Down here i have two code snippets to see what I`m saying
header{
height:80px;
background:#eee;
}
.header-content{
display:flex;
}
.header-content img{
height:100%;
width:auto;
}
.links-list{
display:flex;
margin-top:50px;
}
.links-list li{
margin-right:4rem;
}
<!-- !Header -->
<header class="header ">
<div class="container">
<div class="header-content">
<img src="http://www.lazarangelov.com/wp-content/uploads/2015/12/logo1.jpg" alt="">
<div class="menu-links">
<ul class="links-list">
<li>Home</li>
<li>Bio</li>
<li>Training</li>
<li>Academy</li>
<li>Gallery</li>
</ul>
</div>
</div>
</div>
</header>
nav{
background:#eee;
height:80px;
}
nav img{
height:100%;
width:auto;
}
<nav>
<img src="http://www.lazarangelov.com/wp-content/uploads/2015/12/logo1.jpg" alt="">
</nav>
Here is the codepen link for reference: https://codepen.io/anon/pen/pmEgaM
you just need to keep the styling for header as height: 100%; and keep changing height of image in the header-content img styling this will give you expected result
header{
height: 100%; # this will make sure your header height is 100% irrespective of image geight
background:#eee;
}
.header-content img{
height:100%; #this height you can change
width:auto;
}
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'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 :))
I am trying to build a header similar to this one: http://themes.tf/preview/?momentum
I've set a height but the div still won't get displayed and the content slips to the top.
How can I fix this?
The css is:
.header {
position:relative;
display:block;
width:100%;
z-index:1;
height:688px;
}
.bg-img {
background-repeat: no-repeat;
background-size: cover;
background-position: center center;
background-attachment: fixed;
}
This is the html:
<div class="header bg-img" style="background-image: url('img/xlheader_s.jpg');">
<img src="img/schwager-baubetruung_logo.png" id="logo" alt="schwager baubetreuung logo">
<nav>
<ul>
<li class="current">Startseite</li>
<li>Über uns</li>
<li>Unsere Arbeit</li>
<li>Galerie</li>
<li>Kontakt</li>
<li>Impressum</li>
</ul>
</nav>
</div>
Here you can see the live version: http://suitsncats.de/index_copy.html
If you go to the index page you can see my previous attempt that worked but had a static header image.
I've looked into the source on your website and encountered two issues:
You've used an id selector (#header) instead of class selector (.header) in your style_copy.css and there's also a syntax error in it (no ; after the height declaration)
#header {
position:relative;
top:0px;
left:0;
height:668px
z-index:1;
}
/* should become: */
.header {
position:relative;
top:0px;
left:0;
height:668px; /* <--- note ; */
z-index:1;
}
In your live version, I've checked out your CSS file style_copy.css and noticed that you tell the CSS to look for #header even though in the HTML you're using .header.
You also forget the ; after the height declaration.
Solution:
Change this in the live version's CSS:
#header {
position:relative;
top:0px;
left:0;
height:668px;
z-index:1;
}
To this:
.header {
position:relative;
top:0px;
left:0;
height:668px;
z-index:1;
}
Hope this helps!
Hi I looked into your source at live website, try following solution:
<div class="header bg-img" style="background-image: url('img/xlheader_s.jpg');">
<img src="img/schwager-baubetruung_logo.png" id="logo" alt="schwager baubetreuung logo">
<nav>
<ul>
<li class="current">Startseite</li>
<li>Über uns</li>
<li>Unsere Arbeit</li>
<li>Galerie</li>
<li>Kontakt</li>
<li>Impressum</li>
</ul>
</nav>
</div>
and CSS:
.header {
position: static;
height: 100px;
width: 100%;
background-color: red;
}
nav {
width: 50%;
height: 30px;
margin: auto;
display: table;
z-index: 2;
text-transform: uppercase;
}
Not exactly how you wanted, but one step closer.
I have a row with columns on my main content section and i gave it a width of 1000px and centered it with margin:0 auto. Currently my title of the header is all the way to the left of course and i want it to line up right where the row will begin. How can i achieve this?
http://jsfiddle.net/zck9c8ud/
<header>
<nav>
<ul>
<h1>title of page</h1>
<li>Home</li>
<li>Portfolio</li>
<li>About</li>
</ul>
</nav>
</header>
<div class="row">
<div class="col">col1</div>
<div class="col">col1</div>
<div class="col">col1</div>
</div>
*{
margin:0px;
padding:0px;
}
header{
background:orange;
color:white;
padding:10px;
}
header h1{
display:inline;
position:relative;
left:200px;
}
nav ul{
display:inline;
}
nav ul li{
display:inline-block;
color:white;
position:relative;
left:70px;
}
.row{
width:1000px;
margin:0 auto;
}
.col{
background:#999;
width:33%;
float:left;
margin:10px;
padding:10px;
color:white;
height:400px;
}
I'm not sure if I understood your question properly, but here's how I tried to solve it:
TLDR: Just check out this fiddle: http://jsfiddle.net/arnellebalane/LLzmseoq/
First, I just wrapped the header and the row in a separate container called .wrapper, which I gave a width of 1000px.
<div class="wrapper">
<h1>title of page</h1>
<div class="row">
<div class="col">col1</div>
<div class="col">col1</div>
<div class="col">col1</div>
</div>
</div>
Then I changed the width of .row to 800px and the the width of h1 to 200px (adjust these values if you want`.
Then I floated them to separate directions so that they will sit beside each other.
Im trying to create a header of a website that has the logo left aligned, a navigation div that is 960px wide and centered and a log in area that is right aligned.
Here is a screen shot of my progress
The issue is that the login breaks to a new line and I don't know how to prevent it. Floating the elements doesn't work.
Here is a Fiddle
But it doesnt produce the same results I'm seeing when I run it locally.
HTML
<div id="header"><!-- Outside Container, Holds Logo and Log In -->
<div id="logoHolder">
<p>logo</p>
</div>
<div id="navigation">
<p>navigation</p>
</div>
<div id="loginHolder">
<p>login</p>
</div>
</div>
CSS
/*Header Options*/
#header{
width:100%;
background-color:green;
height:125px;
}
#logoHolder{
float:left;
}
#navigation{
width:960px;
background-color:blue;
margin-left:auto;
margin-right:auto;
}
#loginHolder{
float:right;
}
Just re-order your HTML to the following (move loginHolder above navigation) and it works fine:
<div id="header">
<!-- Outside Container, Holds Logo and Log In -->
<div id="logoHolder">
<p>logo</p>
</div>
<div id="loginHolder">
<p>login</p>
</div>
<div id="navigation">
<p>navigation</p>
</div>
</div>
jsFiddle example
You've got to be careful with widths. The navigation width is overwhelming the other divs. And, if I'm not mistaken, div has a built in line break. the answer above has one solution, I played with a couple edits also: you can copy in this css:
/Header Options/
header {
width:100%;
background-color:green;
height:125px;
}
logoHolder {
width: 100px;
background-color: red;
float:left;
}
navigation {
width:344px;
background-color:blue;
float:left;
}
loginHolder {
width:100px;
background-color:yellow;
float:left;
}