Can't move bg to the middle somehow - css

So basically I want to move the white background to the middle/center
with a width 65% and height 100%
.middlebg {
background-color: #FFFFFF;
background-position: center top;
background-repeat: no-repeat;
background-attachment: fixed;``
width: 65%;
height: 100%;
clear: both;

The key to this is setting the margin in the middlebg class to margin: 0 auto the first value being the vertical margin and the second being the horizontal margin. Setting the horizontal margin to auto will centre this element for you.
http://codepen.io/lostdino/pen/rOEEPr
body {
background: black;
position:relative;
width: 100%;
}
.middlebg {
margin: 0 auto;
background-attachment: fixed;
width: 65%;
height: 80px;
background:white;
}

Related

Unable to add image in the bg with the background-image property-CSS

I'm using the background-image prop to get an image in the bg and a text on the foreground:
fiddle:
https://jsfiddle.net/zvy0j3r1/5/
however I dont see any image getting displayed. i'm not sure what I'm I missing here
CSS:
.main {
padding: 40px 70px;
display: inline-block;
width: 100%; //customizable user controlled width (not necessarily be 100% all time)
color: #AFBEC6;
text-align: center;
border: 3px solid #E7ECEE;
background-color: #F7F8F9;
}
.icon {
background-image: url(https://mdn.mozillademos.org/files/7693/catfront.png);
width: 100%;
height: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.text {
font-size: 24px;
position: relative;
top: -18px;
}
Just set the .main as relative and .icons as absolute.
.main {
padding: 40px 70px;
display: inline-block;
width: 100%;
color: #AFBEC6;
text-align: center;
border: 3px solid #E7ECEE;
background-color: #F7F8F9;
position: relative;
}
.icon {
background-image: url(https://mdn.mozillademos.org/files/7693/catfront.png);
width: 100%;
height: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
position: absolute;
left: 0;
top: 0;
}
.text {
font-size: 24px;
position: relative;
top: -18px;
}
<div class="main">
<div class="icon"></div>
<div class="text">No Data available</div>
</div>
The background image is not showing because the element doesn't have any height. You might think that using height: 100% to the element, would make it take up the same height of it's parent, but it doesn't work like that.
When a child element has height: 100%, it will only take up 100% of it's parent if the parent has an explicit height set, like with pixels, ems, vm, etc.

background-image stretch to fit beyond viewport

I have a page where I want a full-screen background image. This works fine when the the body fits within the viewport, I have html { height: 100%; } and body { min-height: 100%; padding-top: 70px; ... background-size: cover; } (the top padding is for a page header). The issue arises when the page becomes larger than the viewport. The body stretches to the correct height, however the background attachment never grows larger than the size of the viewport. Here is a sample fiddle [https://jsfiddle.net/xdsgek6t/]. In the live version there is also an image overlay, but in the fiddle you can easily see the line where the radial gradient ends, even though I've told it to cover the body, which in this fiddle is 3000px tall due to a child element.
html { height: 100%; box-sizing: border-box; }
body {
overflow-y: scroll;
padding-bottom: 30px;
padding-top: 70px;
background-color: #363636;
min-height: 100%;
background-color: #1976D2;
background-image: radial-gradient( circle at top right, #64B5F6 0%, #1976D2 90% );
background-repeat: no-repeat;
background-attachment: scroll;
background-position: right 70px;
background-size: cover;
margin: 0;
box-sizing: border-box;
}
div.something { height: 3000px; width: 10px; }
header { position: absolute; width: 100%; top: 0; left: 0; height: 70px; z-index: 500; background-color: #ddd; }
<body>
<header></header>
<div class="something"></div>
</body>
This ends up looking really strange when the page grows a tiny bit larger, and is really evident on mobile.
Remove height: 100%; from html and it will extend. And if you need min-height: 100% on body, you can use min-height: 100vh instead, and that will not rely on height: 100% on html
html { box-sizing: border-box; }
body {
overflow-y: scroll;
padding-bottom: 30px;
padding-top: 70px;
background-color: #363636;
min-height: 100vh;
background-color: #1976D2;
background-image: radial-gradient( circle at top right, #64B5F6 0%, #1976D2 90% );
background-repeat: no-repeat;
background-attachment: scroll;
background-position: right 70px;
background-size: cover;
margin: 0;
box-sizing: border-box;
}
div.something { height: 3000px; width: 10px; }
header { position: absolute; width: 100%; top: 0; left: 0; height: 70px; z-index: 500; background-color: #ddd; }
<header></header>
<div class="something"></div>

Background image aligned at bottom even when resized

I'm trying to align an image at the bottom of my DIV, which happens to be the background. Just to make the case even more real, I also added some overlay (as that is how I am doing it in my project).
https://jsfiddle.net/jy0w2jmr/
background-size: 100% 100%;
That will make the background size 100% of the DIV, but not stretched out, so it will be 300px tall (if my DIV is 300px tall).
How do I make it, so when I resize the DIV, the background image also resizes, but sticks to the bottom of the DIV and NEVER overflows the DIV? background-position: bottom; does not seem to stick it to the bottom of my DIV.
Does this work for you?
#container {
width: 100%;
height: 300px;
background: url("https://upload.wikimedia.org/wikipedia/commons/2/2f/Google_2015_logo.svg");
position: relative;
background-size: contain;
background-position: bottom;
background-repeat: no-repeat;
}
.overlay {
position: absolute;
width: 100%;
height: 100%;
background: #333;
opacity: 0.5;
}
#container {
width: 100%;
height: 300px;
background: url("https://upload.wikimedia.org/wikipedia/commons/2/2f/Google_2015_logo.svg");
position: relative;
background-size: contain;
background-position: bottom;
background-repeat: no-repeat;
}
.overlay {
position: absolute;
width: 100%;
height: 100%;
background: #333;
opacity: 0.5;
}
<div id="container">
<div class="overlay"></div>
</div>

100 percent width navbar has gap at top

This css below works with a workaround of taking the margin at -22px in the body. I was wondering if there might be a better way to do this. I have a full width background image and I want the nav bar to be the same.
html {
background: repeating-linear-gradient(transparent 0%,transparent 90%,#000000 100% ),
url("../../Images/bg2.jpg") center center no-repeat;
background-size: 100%;
height: 135%;
}
body{
margin: -22px auto;
padding: 0 auto;
}
nav{
width: 100%;
float: left;
margin: 0 0 1em 0;
padding: 0;
background-color: white;
}
CSS
jsFiddle
html {
background-image: url(http://placehold.it/1800x900);
background-size: cover;
}
body{
margin: 0; /* override the default 10px margin */
}
nav{
width: 100%;
float: left;
background-color: white;
}

%100 background image in container and how can do scale width to page

repeat-x background image in 500px container div and how can I scale background image to page width. I am sorry for my bad English. Have a look at the following image to understand my case.
.container {
width: 500px;
height: 60px;
background-color:#f2d88c;
}
.menubg {
width: 100%;
height: 30px;
background-image: url(bg.jpg) center repeat-x;
}
http://jsfiddle.net/PjGqv/9/
Your div.menubg is a child of div.container The child element cannot be wider then its 500px width parent.
You can use absolute positioning if the parent elements position it is relative to has a 100% width. I've included a jsfiddle
However by using position: absolute; you are taking the child out of the parents container. Depending on your situation, you would have to adjust its position values.
.menubg {
position: absolute;
width: 100%;
height: 30px;
background-image: url(bg.jpg) center repeat-x;
}
You can use 3 elements and use this css
.container {
position: relative;
display: block;
height: 60px;
width: 100%;
text-align: center;
padding: 0;
}
.bg {
background-color: #f2d88c;
position: relative;
height: 60px;
width: 500px;
margin: 0 auto;
}
.menubg {
position: absolute;
width: 100%;
top: 15px;
height: 30px;
display: block;
text-align: center;
background-image: url(bg.jpg) center repeat-x;
}
<div class="container">
<div class="bg"></div>
<div class="menubg"></div>
</div>
Try adding:
background-size: cover;
to the .menubg

Resources