Trouble adding div with 100% height, and keeping existing content vertically aligned - html

The below code works as intended (full width background image, with horizontally and vertically aligned text overlapping it), but I want to make a couple of changes. Instead of having a background image for #banner, I want to switch it to a color, and add a div before .teaser that is width: 1366px, height: 100% of parent, and includes a background image set to cover. I still need .teaser vertically aligned though. I've been struggling with this for awhile, so any help would be very much appreciated. http://jsfiddle.net/exczzyje/
HTML
<div id="banner">
<div id="valign">
<div class="teaser">
<h1>Title</h1>
<p>Tagline</p>
</div>
</div>
</div>
CSS
html, body {
width: 100%;
height: 100%;
}
#banner {
width: 100%;
height: 67%;
height: calc(67% - 123px);
background: #000 url('images/banner.jpg') 50% 0 no-repeat;
background-size: cover;
margin: 0 auto;
overflow: hidden;
padding: 0;
position: relative;
}
#banner[id] {
display: table;
position: static;
}
#banner #valign {
width: 100%;
position: absolute;
text-align: center;
top: 50%;
}
#banner #valign[id] {
display: table-cell;
position: static;
vertical-align: middle;
}
#banner .teaser {
width: 960px;
margin: -12px auto 0 auto;
padding: 21px 0;
position: relative;
top: -50%;
}
#banner .teaser h1 {
color: #fff;
font-size: 45px;
}
#banner .teaser h1:after {
content: "";
display: block;
width: 42px;
margin: 18px auto;
border-bottom: 3px solid #bfbfbf;
}
#banner .teaser p {
color: #808080;
font-size: 24px;
margin: 0;
text-align: center;
}

Are you looking for something like this:
See JSFiddle
CSS:
* {
margin: 0; padding: 0;
}
html, body {
width: 100%;
height: 100%;
}
#banner {
width: 100%;
height: 100%;
background-color: black;
padding: 0;
}
#banner #valign {
width: 100%;
height: 100%;
}
#newDiv {
height: 100%;
width: 1366px;
display: table;
margin: 0 auto;
background: #000 url('http://www.psdgraphics.com/file/colorful-triangles-background.jpg') 50% 0 no-repeat;
background-size: cover;
}
#banner .teaser {
display: table-cell;
width: 960px;
margin: 0 auto;
padding: 21px 0;
text-align: center;
vertical-align: middle;
}
#banner .teaser h1 {
color: #fff;
font-size: 45px;
}
#banner .teaser h1:after {
content: "";
display: block;
width: 42px;
margin: 18px auto;
border-bottom: 3px solid #bfbfbf;
}
#banner .teaser p {
color: #808080;
font-size: 24px;
margin: 0;
}

Just add a container around the outside instead.
Updated HTML
<div id="container">
<div id="banner">
<div id="valign">
<div class="teaser">
<h1>Title</h1>
<p>Tagline</p>
</div>
</div>
</div>
</div>
Updated CSS
#container {
width: 100%;
height: 67%;
height: calc(67% - 123px);
background: #000;
margin: 0 auto;
}
#banner {
width: 1366px;
height: 100%;
background: #000 url('images/banner.jpg') 50% 0 no-repeat;
background-size: cover;
margin: 0 auto;
overflow: hidden;
padding: 0;
position: relative;
}

Related

Can't set up image in the div

I'd like to have a div, which contains:
Text on the top
Some image below it
How I may make it? Here's what I did so far
.container {
text-align: center;
display: inline-block;
width: 100%;
height: 180pt;
border: 1px solid red;
}
.text {
width: 12%;
height: 180px;
text-align: left;
background-color: yellow;
margin: 20px;
}
.text p {
font-size: 20px;
margin-left: 0 auto;
margin-right: 0 auto;
margin-top: 15px;
text-align: center;
}
.image {
width: 100%;
height: 100%;
display: inline-block;
background-image: url("url_to_img");
background-size: cover;
background-repeat: no-repeat;
}
<div class="container">
<div class="text">
<p>Lorem</p>
<div class="image"></div>
</div>
</div>
For now, the <p> is working just fine, but I have struggle with the image it's sticking out the borders of container div.
It's because you gave the test class too much margin. try not to mix up size units if possible.
add overflow:hidden; on .text class
.container {
text-align: center;
display: inline-block;
width: 100%;
height: 180pt;
border: 1px solid red;
}
.text {
width: 19%;
height: 180px;
text-align: left;
background-color: yellow;
margin: 10px;
overflow:hidden;
}
.text p {
font-size: 20px;
margin-left: 0 auto;
margin-right: 0 auto;
margin-top: 1px;
text-align: center;
}
.image {
width: 100%;
height: 100%;
display: inline-block;
background-image: url("https://avatars.mds.yandex.net/get-pdb/1380368/3e559dd1-32ad-4226-aac5-77ed3782a962/s1200");
background-size: cover;
background-position:center;
background-repeat: no-repeat;
}
<div class="container">
<div class="text">
<p>Lorem</p>
<div class="image"></div>
</div>
</div>
Here is a fiddle to get you started with, I didn't change much code because I didn't new your requirements Just did workaround around same code
https://jsfiddle.net/junaidmasoodi/vctwu2g5/11/
.text {
width: 100%;
height: 180px;
text-align: left;
background-color: yellow;
margin: 20px;
}
.text p {
font-size: 20px;
margin-left: 0 auto;
margin-right: 0 auto;
margin-top: 15px;
text-align: center;
}
.image {
width: 100%;
height: 100%;
display: block;
background-image: url("https://309w5s255371fs4df2vftgbl-wpengine.netdna-ssl.com/wp-content/uploads/2017/09/placeholder.jpg");
background-size: cover;
background-repeat: no-repeat;
}
for your .image block you could try background-size: contain; instead of cover

Responsive div to stay in nested div doesnt work

The button will not stay with the image when I adjust the size of the browser. I tried the position:absolutein the img div and the responsive didn't work well with the position property. Obviously the float:left doesn't work either as written in CSS.
.section6 {
margin: 0 auto;
text-align: center;
margin-top: 0;
}
.img-group img {
z-index: 2;
text-align: center;
margin: 0 auto;
border: 1px solid red;
}
div.bg-bar {
margin-top: -150px;
max-height: auto;
height: 150px;
background-color: #7290ab;
z-index: 3;
}
.section6 button {
float: left;
position: relative;
z-index: 1;
margin-top: 200px;
margin-left: 330px;
top: 40px;
}
<section class="section6">
<button>REQUEST AN INTERPRETER</button>
<div class="img-group"><img src="http://dignityworks.com/wp-content/uploads/2016/04/group-people-standing-copyspace-7235283.jpg" alt="World-class SVRS interpreters"></div>
<div class="bg-bar"></div>
</section>
See on JSFIDDLE of what I did.
You're using fixed sizing units and this is not how you make responsive pages.
If you want the button to stay in the middle, you have to position it absolutely inside the relative div.
Something like this:
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
.relative {
position: relative;
padding: 10px;
background: #0fc0fc;
animation: reduce 2s ease-in-out infinite;
height: 50px;
}
button.centered {
position: absolute;
left: 50%;
/* Kind of makes the anchor point of the element to be in the horizontal center */
transform: translateX(-50%);
}
#keyframes reduce {
0%,
100% {
width: 100%;
}
50% {
width: 50%;
}
}
<div class="relative">
<button class="centered">I'm in the middle</button>
</div>
You are better off changing the image to be a background image on that div and moving the button to be inside of it.
HTML:
<section class="section6">
<div class="img-group"><button>REQUEST AN INTERPRETER</button></div>
<div class="bg-bar"></div>
</section>
CSS:
.section6 {
margin: 0 auto;
text-align: center;
margin-top: 0;
}
.img-group {
z-index: 2;
text-align: right;
margin: 0 auto;
border: 1px solid red;
position: relative;
background: url('http://dignityworks.com/wp-content/uploads/2016/04/group-people-standing-copyspace-7235283.jpg') no-repeat;
background-size: cover;
width: 400px;
height: 370px;
}
div.bg-bar {
margin-top: -150px;
max-height: auto;
height: 150px;
background-color: #7290ab;
z-index: 3;
}
.section6 button {
position: relative;
z-index: 5;
top: 100px;
margin-right: 20px;
}
Try this:
HTML:
<section class="section6">
<div class="img-group">
<img src="http://dignityworks.com/wp-content/uploads/2016/04/group-people-standing-copyspace-7235283.jpg" alt="World-class SVRS interpreters">
<button>REQUEST AN INTERPRETER</button>
</div>
<div class="bg-bar"></div>
</section>
CSS:
.section6 {
margin: 0 auto;
text-align: center;
margin-top: 0;
}
.img-group {
position: relative;
}
.img-group img {
text-align: center;
max-width: 100%;
margin: 0 auto;
border: 1px solid red;
}
.img-group button {
display: block;
position: absolute;
z-index: 10;
margin-left: -75px;
top: 50%;
left: 50%;
max-width: 100%;
}
div.bg-bar {
margin-top: -150px;
max-height: auto;
height: 150px;
background-color: #7290ab;
}

CSS: main content should have background-color [duplicate]

This question already has answers here:
How wide is the default `<body>` margin?
(4 answers)
Closed 5 years ago.
I am making a common header and footer which will be used throughout in all the HTML pages. My page has a white header and white footer and the body is grey colored. Now, my work demands to achieve as below:
What I achieved so far is as below:
I don't know why I am getting these white strips in the sides of the body tag. Please suggest, my code is as below.
main {
background-color: lightgrey;
padding: 50px;
}
header {
background-color: white;
width: 100%;
height: 50px;
}
.content-section {
background-color: lightgrey;
width: 100%;
}
.logo {
height: 20px;
margin: 15px 5px;
width: 116px;
}
.open-card-BG {
font-weight: 300;
margin: 0 auto;
width: 65%;
padding: 20px 40px;
object-fit: contain;
max-width: 325px;
min-height: 200px;
border-radius: 5px;
display: table;
background-color: white;
position: relative;
box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.2);
}
.open-card-BG::after {
content: '';
position: absolute;
top: 30px;
left: 100%;
width: 30px;
height: 30px;
background: url(../secure.svg) center no-repeat;
background-size: contain;
}
<header>
<img class="logo" src="logo.gif" />
</header>
<main>
<div class="open-card-BG">main content</div>
</main>
<footer>
I am footer
</footer>
That is because of the default (user agent stylesheet) margin applied by the browser on the body tag - see how the white stripes vanish when I set margin: 0 for body.
Demo below:
body {
margin: 0;
}
main {
background-color: lightgrey;
padding: 50px;
}
header {
background-color: white;
width: 100%;
height: 50px;
}
.content-section {
background-color: lightgrey;
width: 100%;
}
.logo {
height: 20px;
margin: 15px 5px;
width: 116px;
}
.open-card-BG {
font-weight: 300;
margin: 0 auto;
width: 65%;
padding: 20px 40px;
object-fit: contain;
max-width: 325px;
min-height: 200px;
border-radius: 5px;
display: table;
background-color: white;
position: relative;
box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.2);
}
.open-card-BG::after {
content: '';
position: absolute;
top: 30px;
left: 100%;
width: 30px;
height: 30px;
background: url(../secure.svg) center no-repeat;
background-size: contain;
}
<header>
<img class="logo" src="logo.gif"/>
</header>
<main>
<div class="open-card-BG">main content</div>
</main>
<footer>
I am footer
</footer>
add body{margin:0} in your stylesheet.
By default body tag have margin of 8px in most major browsers.
body{
margin:0
}
main {
background-color: lightgrey;
padding: 50px;
}
header {
background-color: white;
width: 100%;
height: 50px;
}
.content-section {
background-color: lightgrey;
width: 100%;
}
.logo {
height: 20px;
margin: 15px 5px;
width: 116px;
}
.open-card-BG {
font-weight: 300;
margin: 0 auto;
width: 65%;
padding: 20px 40px;
object-fit: contain;
max-width: 325px;
min-height: 200px;
border-radius: 5px;
display: table;
background-color: white;
position: relative;
box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.2);
}
.open-card-BG::after {
content: '';
position: absolute;
top: 30px;
left: 100%;
width: 30px;
height: 30px;
background: url(../secure.svg) center no-repeat;
background-size: contain;
}
<header>
<img class="logo" src="logo.gif"/>
</header>
<main>
<div class="open-card-BG">main content</div>
</main>
<footer>
I am footer
</footer>
you can try giving the main tag a margin: 0 -10px, may solve your problem.
See the fiddle
you can add this code in your css:
body {
display: initial;
}
Try the following:
html, body {margin: 0;}

How to fix code to make cover completely centered w/ CSS3

Good Morning, I am currently working on portfolio and I am trying to make my cover elements completely center vertically and horizontal. I have managed to do this with margin: 0 auto and display: table-cell, vertical-align and for some reason when I am added my CSS3 triangles it began to push my logo to the left. Is there any tricks to fix this?
.cover {
background-size: cover;
background-color: white;
display: table;
width: 100%;
padding: 0 20%;
height: 1900px;
}
.logo-container {
margin: 0 auto;
background-size: cover;
height: auto;
display: table-cell;
text-align: center;
vertical-align: middle;
padding-top: 40px;
padding-bottom: 40px;
}
#triangle-bottomright {
width: 0;
height: 0;
border-bottom: 100px solid #84cfc5;
border-left: 100px solid transparent;
padding-top: 1500px;
margin-left: 850px;
margin: 0 auto;
}
#triangle-topright {
width: 0;
height: 0;
border-top: 100px solid #84cfc5;
border-right: 100px solid transparent;
margin-right: 1000px;
margin-left: -200px;
position: relative;
left: -1000px;
right: -1000px;
top: 150px;
}
<div class="cover">
<div class="logo-container">
<img class="logo" src="images/personal-logo.png" alt="logo-brand" />
</div>
<div id="triangle-topright"></div>
<div id="triangle-bottomright"></div>
</div>
The triangles are causing a shift because they are taking up space in the page flow. You should position them using position:absolute to get them out of the page flow, with position:relative set on the parent .cover div. Example below.
html, body {
height: 100%;
margin: 0;
}
.cover {
background-color: white;
display: table;
width: 100%;
height: 100%;
position: relative;
}
.logo-container {
display: table-cell;
text-align: center;
vertical-align: middle;
}
#triangle-bottomright {
width: 0;
height: 0;
border-bottom: 100px solid #84cfc5;
border-left: 100px solid transparent;
position: absolute;
bottom: 0;
right: 0;
}
#triangle-topright {
width: 0;
height: 0;
border-top: 100px solid #84cfc5;
border-right: 100px solid transparent;
position: absolute;
left: 0;
top: 0;
}
<div class="cover">
<div class="logo-container">
<img class="logo" src="http://placehold.it/240x80" alt="logo-brand" />
</div>
<div id="triangle-topright"></div>
<div id="triangle-bottomright"></div>
</div>

Position two div next to each other inside a parent DIV

I have two DIVs inside another two parent DIVs. What I am looking to do is align the "clocks" DIV to the left of the "panel" DIV and the "sd" DIV toward the center of the "panel" DIV.
What is showing up as now:
What I am looking to do:
My HTML looks like:
<div id="weather">
<div id="panel">
<div class="clocks">
</div>
<div id="sd">
<script type="text/javascript" src="http://www.weathers.com"></script>
</div>
</div>
<p class="slide">Weather</p>
</div>
My CSS looks like:
#weather {
margin: 0 auto;
padding: 0;
width: 990px;
font: 75%/120% Arial, Helvetica, sans-serif;
top: 0;
position: absolute;
z-index: 999999999;
left: 0%;
margin-left: 0px;
}
#panel {
background: url('weatherBack.png') no-repeat;
height: 195px;
width: 990px;
display: none;
text-align: center;
margin: 0 auto;
line-height: 195px;
}
#sd {
margin: 0 auto;
padding: 0;
width: 175px;
}
.slide {
margin: 0;
padding: 0;
border-top: solid 4px #422410;
background: url(btn.png) no-repeat center top;
}
.btn-slide {
background: url(white-arrow.gif) no-repeat right -50px;
text-align: center;
width: 144px;
height: 31px;
padding: 10px 10px 0 0;
margin: 0 auto;
display: block;
font: bold 120%/100% Arial, Helvetica, sans-serif;
color: #fff;
text-decoration: none;
}
.active {
background-position: right 12px;
}
.learn {
z-index: 9999999999999;
position: absolute;
top: 85px;
right: 3px;
}
.box-cnt {
padding: 10px;
padding-bottom: 2px;
background-color: #1FAAEB;
zoom: 1;
filter: alpha(opacity=80);
opacity: 0.8;
}
.clocks {
width: 209px;
height: 131px;
position: relative;
top: 0;
left: 151;
background: url('css/images/clockbg.png') no-repeat 0 0;
}
Try the float property with a value of left on your clock and weather panels:
.clocks {
width: 209px;
height: 131px;
position: relative;
float: left;
background: url('css/images/clockbg.png') no-repeat 0 0;
}
#sd {
margin: 0 auto;
padding: 0;
width: 175px;
position: relative;
float: left;
}
Here is a working JSFiddle link: http://jsfiddle.net/cYFfT/
If you want to have more space between them, simply set a left margin on the SD panel,
as in this update: http://jsfiddle.net/cYFfT/1/