Keeping image within the div container - html

I have a simple layout and trying to keep the image within the div, but its going out of the div in full width mode, is there a way to resize image to keep within the div, I can handle is using background image but I think there must be a way to keep within the div using image tag
Here is the JSFiddle
header {
float: left;
width: 100%;
height: 100px;
background: #f00;
box-sizing: border-box;
padding: 10px 0;
}
header .header_left {
float: left;
width: 20%;
}
header .header_left > a > img {
width: 100%;
height: auto;
}
header .header_right {
float: right;
width: 20%;
}
<header>
<div class="mcontainer">
<div class="header_left"> <img src="https://cdn.img42.com/63de8c9048f1b98a226f926f5d9d56c5.jpeg" alt="Welcome"> </div>
<div class="header_right">
<ul>
<li>Test/Test</li>
<li>Login</li>
</ul>
</div>
</div>
</header>

Your header has a fixed height of 100px and a top and bottom padding of 10px. So your content has a height of 80px, so simply add max-height:80px; to your image:
The Code (https://jsfiddle.net/y1hnyc3d/1/):
header {
float: left;
width: 100%;
height: 100px;
background: #f00;
box-sizing: border-box;
padding: 10px 0;
}
header .header_left {
float: left;
width: 20%;
}
header .header_left > a > img {
width: 100%;
height: auto;
max-height:80px;
}
header .header_right {
float: right;
width: 20%;
}
<header>
<div class="mcontainer">
<div class="header_left"> <img src="https://cdn.img42.com/63de8c9048f1b98a226f926f5d9d56c5.jpeg" alt="Welcome"> </div>
<div class="header_right">
<ul>
<li>Test/Test</li>
<li>Login</li>
</ul>
</div>
</div>
</header>

Related

child's padding is bigger than parent's size

I would like to make my tag is big as the parent div (#logo, and i'm having trouble making a 100% padding without making it bigger than the parent's tag.
HTML
<div id="header">
<div id="logo">
</div>
<div class="hide" id="navigation">
<ul>
<li>About</li>
<li>Examples</li>
<li>Form</li>
<li>Contact</li>
</ul>
</div>
</div>
CSS
div#logo {
background: url(https://i.imgur.com/sHTtXk4.png) no-repeat center center;
background-size: cover;
float: left;
line-height: 80px;
width: 10%;
height: 80px;
text-align: center;
}
div#logo a {
background: #fff;
width: 100%;
height: 100%;
padding: 100%;
https://jsfiddle.net/vwbo9exg/
Remove padding and add display for a tag
div#logo a {
background: #fff;
width: 100%;
height: 100%;
/* padding: 100%; */
display: inline-block; /*Add this*/
}

How to make a div fill remaining space?

I have a web page with a header, content and footer.
There is a background image in the content. I would like the image to fill the remaining space between the header and footer. There are divs that are children of the content div with the image that will sometimes have content and other times will not.
HTML:
<body>
<div id='main'>
<div id='header'>
<div id='logoCompany'>
<img class='headerGraphics' src='Graphics\logo smaller.jpg'><img class='headerGraphics' src='Graphics\Marvelous Header3 small.png'>
</div>
</div>
<div id='contentParent' class='floatClear'>
<div id='content' style = "text-align: center;">
<div id='leftPane'>
Left Col
</div>
<div id='rightPane'>
Right Col
</div>
</div>
</div>
<div id='footer'>
Footer
</div>
</div>
</body>
CSS:
* {
margin: 0px;
}
.floatClear {
clear: both;
}
.headerGraphics {
display: inline;
}
#header {
background: #023489;
text-align: center;
}
#logoCompany {
display: inline;
}
#contentParent {
height: 373px;
width: 100%;
background-image: url(../Graphics/background.jpg);
}
#leftPane {
background: yellow;
float: left;
margin: 100px 0 0 10%;
opacity: .5;
width:40%;
}
#rightPane {
background: green;
float: right;
margin: 100px 10% 0 0;
opacity: .5;
width:40%;
}
#footer {
width:100%;
}
I tried height: 100% but I suspect this fails without content. In fact I think that's why everything fails except when I hard code a height. But that is not a good solution for obvious reasons.
Here's an example
Anyone have any ideas how to make this work?
EDIT:
I tried changing this:
#contentParent {
height: 373px;
width: 100%;
background-image: url(../Graphics/background.jpg);
}
to this:
#contentParent {
flex: 1;
background-image: url(../Graphics/background.jpg);
}
But it shrunk the div to the size of the child div, making things worse..
Here is a solution which defines header, footer and #contentParent as position: fixed and gives #contentParent 100% height minus the height of header and footer (= 80px in this example - this depends on your own settings).
Any additional content has to be added inside #contentParent - this element will then scroll since it has overflow-y:auto;. The header and footer will always remain on the screen due to their absolute position and won't cover any part of the content since #contentParent has according margins at top and bottom which equal the height of the header and footer.
The background image will cover #contentParent completely and won't scroll diue to background-attachment: fixed (integrated in the shortcut background property)
html,
body,
#main {
height: 100%;
margin: 0px;
}
.floatClear {
clear: both;
}
.headerGraphics {
display: inline;
}
#header {
position: fixed;
top: 0;
width: 100%;
height: 40px;
background: #023489;
text-align: center;
}
#logoCompany {
display: inline;
}
#contentParent {
position: fixed;
height: calc(100% - 80px);
width: 100%;
overflow-Y: auto;
margin: 40px 0;
background: url(http://placehold.it/1500x800/fc7) center center no-repeat;
background-position: fixed;
background-size: cover;
}
#leftPane {
background: yellow;
float: left;
margin: 100px 0 0 10%;
opacity: .5;
width: 40%;
}
#rightPane {
background: green;
float: right;
margin: 100px 10% 0 0;
opacity: .5;
width: 40%;
}
#footer {
position: fixed;
bottom: 0;
width: 100%;
height: 40px;
width: 100%;
background: lightblue;
}
<body>
<div id='main'>
<div id='header'>
<div id='logoCompany'>
<img class='headerGraphics' src='Graphics\logo smaller.jpg'><img class='headerGraphics' src='Graphics\Marvelous Header3 small.png'>
</div>
</div>
<div id='contentParent' class='floatClear'>
<div id='content' style="text-align: center;">
<div id='leftPane'>
Left Col
</div>
<div id='rightPane'>
Right Col
</div>
</div>
</div>
<div id='footer'>
Footer
</div>
</div>
</body>
You can do that using flexbox,
here is a simplified version from your code.
body {
margin: 0
}
#main {
display: flex;
flex-direction: column;
height: 100vh
}
#header,
#footer {
background: green;
padding: 10px;
}
#contentParent {
flex: 1;
background: red;
}
#content {
display: flex;
justify-content:center;
align-items:center;
height:100%
}
<div id='main'>
<div id='header'>
<div id='logoCompany'>
Logo Name
</div>
</div>
<div id='contentParent'>
<div id='content'>
<div id='leftPane'>
Left Col
</div>
<div id='rightPane'>
Right Col
</div>
</div>
</div>
<div id='footer'>
Footer
</div>
</div>
Not sure if I understood your question correctly, but if you want to display stretch the image over the whole screen you can use the css tag:
background-size: cover;
You can read more here about the css tag

Different color background in same div

I feel like this should be much easier than I am making it...I'm looking for a solution to have 1 solid color extend from the left and end at a logo, then have a second solid color extend all the way to the right. I want the wrapper divs to extend 100%. So something like:
<div id="header-wrap"><!--100% page width-->
<div id="header"><!--1000px centered fixed width-->
<div id="logo"></div><!-- align left-->
<div id="nav"></div><!-- align right-->
</div>
<div id="sub-header-wrap">
...
</div>
</div>
Here's an image showing what I mean:
There is a lot that goes into this.
Let me start of with a link to a working fiddle: JSFiddle
How can I explain this?
Basically I have the two full-width divs that have the full background color. Inside those two divs I have a div classified as .inner that has 80% of the width (which can be whatever you want) that is aligned to the center with margins.
Inside .inner I have a left div and a right div of the proper sizes to contain the logo/navigation. Inside the left divs, I have another div, .shade that will darken the left side of the header.
The .left divs are relatively positioned and the .shade divs are absolutely positioned.
CSS:
body{
margin: 0;
}
header
{
display:block;
width: 100%;
}
header .top
{
background: #00a;
text-align: center;
color: white;
}
header .inner
{
width: 80%;
margin: 0 10%;
text-align: left;
}
header .inner .logo, header .inner .left
{
display: inline-block;
width: 20%;
margin: 0;
text-align: center;
height: 100%;
padding: 10px 0px;
position: relative;
z-index: 1;
}
header .inner .right
{
display: inline-block;
width: 78%;
margin: 0;
text-align: right;
}
header li
{
display: inline-block;
list-style: none;
}
header .bottom
{
background: #ca0;
}
header .shade
{
width: 1000%;
height: 100%;
position: absolute;
top: 0px;
right: 0px;
background: rgba(0, 0, 0, .3);
z-index: -1;
}
HTML:
<header>
<div class="top" align="center">
<div class="inner">
<div class="logo">
Logo
<div class="shade"></div>
</div>
<div class="right">
<li>Nav 1</li>
<li>Nav 2</li>
</div>
</div>
</div>
<div class="bottom">
<div class="inner">
<div class="left">
Subtext
<div class="shade"></div>
</div>
<div class="right">
<li>Link</li>
</div>
</div>
</div>
</header>
Full JSFiddle
If I understand you correctly, try something like this in a separate CSS file, or within a <style> block. It's not tested though, sorry.
#header {
margin: 0 auto; /* for centering */
width: 1000px;
}
#logo {
float: left;
width: 250px; /* for example */
background-color: red; /* for example */
}
#nav {
float: right;
width: 750px; /* for example */
background-color: blue; /* for example */
}
UPDATE:
If you can afford CSS3, this post can be intresting for you as well.

Creating floating sidebars with a content area in the middle CSS

I am having trouble getting my basic layout to work. I am new at HTML and CSS. How can I attain a 3 column setup on my website to allow for proper placing of ads along the sides?
I currently have 2 floating sidebars left and right and one content area that is not floating but there seems to be an invisible margin between the content area and the side bars.
HTML
<div class="float-left left-ad-space">
<div class="filler"></div>
</div>
<div class="float-right right-ad-space">
<div class="filler"></div>
</div>
<div class="body-wrapper">
<div class="filler">
<div style="width: 100%; margin-left: auto; margin-right: auto;">
#RenderSection("Featured", false)
</div>
<div>
#RenderBody()
</div>
</div>
</div>
CSS
body {
width: 100%;
background-color: white;
height: 1200px;
}
.body-wrapper {
width: 900px;
height: 100%;
}
.left-ad-space {
height: 500px;
width: 160px;
}
.right-ad-space {
width: 160px;
height: 500px;
}
.float-left {
float: left;
}
.float-right {
float: right;
}
.filler {
width: 100%;
border-style: solid;
border-width: 3px;
border-color: blue;
height: 100%;
}
You need to center the middle column.
EXAMPLE HERE
.body-wrapper {
width:900px;
height:100%;
margin:0 auto;
}
Aside from that, your calculations are a bit off because of the borders.
You could use box-sizing:border-box in order to include the element's border within it's dimension calculations. Most people just apply this property to all elements:
* {
box-sizing: border-box;
-moz-box-sizing: border-box;
}
You would otherwise need to include this on the individual elements.
You can use this idea for your design. If you want more info look up html5 3 column layouts.
<div class="columns">
<div class="lcol"></div>
<div class="ccol"></div>
<div class="rcol"></div>
</div>
.colums { width:800px;} /*your wrappers width */
.lcol, .ccol, .rcol {
background: gray;
margin: 0 10px;
padding: 10px;
}
.lcol { /*left column*/
float: left;
width: 100px;
}
.ccol {/*center column*/
float: left;
width: 300px;
}
.rcol { /*right column*/
float: right;
width: 100px;
}

Multiple background colors and images in full-width div

I'm trying to make a header for a site that has multiple background colors and images. On the left is a tan-ish gradient and a logo image, on the right is a sunburst/cloud image that fades to teal, as shown below.
The left (logo) portion should be 230px wide, and the right (sunburst) portion should be 770px wide, for a total of 1000px, and this should be centered. The left side tan gradient should extend to the left edge of the browser, and the teal should extend to the right edge.
I attempted to do it with percentages:
CSS:
#header {
height: 105px;
min-width: 1000px;
}
#header .left {
width: 31%;
background: url(../images/header_left_gradient.png) bottom left repeat-x;
height: 105px;
float: left;
}
#header .left #logo {
float: right;
width: 230px;
}
#header .right {
width: 69%;
background: url(../images/header-right.png) bottom left no-repeat #009eb0;
height: 105px;
float: left;
}
HTML:
<div id="header">
<div class="left">
<div id="logo">
<img src="./media/images/logo.png" alt="">
</div>
</div>
<div class="right">
Text
</div>
</div>
This almost worked, but the header didn't stay centered with wide browsers.
Fiddle
set header margin & width:
#header {
height: 105px;
margin: 0 auto;
min-width: 1000px;
width: 100%;
}
#header .left {
background: none repeat scroll 0 0 #FF00FF;
float: right;
height: 105px;
width: 31%;
}
#header .right {
background: url("../images/header-right.png") no-repeat scroll 0 0 #009EB0;
float: left;
height: 105px;
width: 69%;
}
this is worked for me.
A bit hacky, but this should do the trick:
body {
text-align: center;
}
#header {
display: inline-block;
height: 105px;
margin-left: auto;
margin-right: auto;
min-width: 1000px;
text-align: left;
}
you could use a wrapper div to apply the text-align to instead of the body, but I posted this in case you don't want to change the structure.
I ended up solving this in a bit of a hacky way. First, I added a container around the header like #Raad suggested, then added two more divs inside to hold my colors:
<div id="header-wrap">
<div id="filler-left">
</div>
<div id="filler-right">
</div>
<div id="header">
<div class="left">
<div id="logo">
<img src="./media/images/logo.png" alt="">
</div>
</div>
<div class="right">
Text
</div>
</div>
</div>
Then the CSS:
#header-wrap {
width: 100%;
position: relative;
}
#header-wrap #filler-left {
left: 0;
width: 50%;
position: absolute;
height: 105px;
background: url(../images/header_left_gradient.png) bottom left repeat-x;
}
#header-wrap #filler-right {
left: 50%;
width: 50%;
position: absolute;
height: 105px;
background: #009eb0;
}
#header {
height: 105px;
width: 1000px;
margin: 0 auto;
position: relative;
}
and set my .left and .right divs to fixed width. Worked out pretty well. Not my ideal solution, but it worked.