Stretch div according to another one - html

I have the following code:
#left {
width:383px;
margin:0px;
float: left;
position:relative;
}
.bg_top{
background: transparent url(../bg_top.png) no-repeat;
width: 383px;
height: 140px;
}
.bg_middle{
background: transparent url(../bg_middle.png) no-repeat;
width: 383px;
height: 100%;
}
.bg_bottom{
background: transparent url(../bg_bottom.png) no-repeat;
width: 383px;
height: 131px;
}
#left_inner{
width:375px;
border:2px solid #98b73f;
position: absolute;
top: 5px;
}
<div id='wrap'>
<div id='left'>
<div class='bg_top'>
bg_top
</div>
<div class='bg_middle'>
bg_middle
</div>
<div class='bg_bottom'>
bg_bottom
</div>
<div id='left_inner'>
<p>long text</p></div</div</div>
I want the bg_middle class to stretch to the height of the left_inner div. I have tried with height 100% and searched the net for answers. I'm new in the world of css. I think I have to connect the 3 divs that are the background to the inner div somehow...

With the HTML as is it you can't do it without Javascript. Block elements adjust in height to their container. Si if the goal is to use a background-image just apply it to the left-inner. Don't use position absolute 'cause that block would postion itself indepent from the content flow.
Given that you have fixed heights you can use min-height in case there's not enough content.
I'm not sure this is what you're looking for.

I would strongly recommend to do something different here..
However this is the solution I suggest with the original approach:
#left {
width:383px;
margin:0px;
position: relative;
}
.bg_bottom{
background: transparent url(../bg_bottom.png) no-repeat;
height: 131px;
position: absolute;
bottom: 0;
z-index: -1
}
.bg_top{
background: transparent url(../bg_top.png) no-repeat;
height: 140px;
position: absolute;
top: 0;
z-index: -1
}
.bg_middle{
height: 100%; /* it does work*/
background: transparent url(../bg_middle.png) repeat top left;
position: absolute;
z-index: -2;
}
height= 100% works if the parents (or at least one) have a height (100% of 0 is 0).
You can cascade starting with the html
html, body{height: 100%;}
Or you can just give the #left a height or min-height
This should work

Thanks to all,
Last night I found a way around this. This is my final approach, it still needs some small adjustments, but it works (for now).
<div id="wrap">
<div id="header"></div>
<div id="main">
<div class='bg_top'></div>
<div class='bg_middle'>
<div>LONG LONG TEXT</div>
</div>
<div class='bg_bottom'></div>
</div>
#wrap{
width: 800px;
margin: 0px auto;
border: 1px solid black;
}
.bg_top{
background: transparent url(../bg_top.png) no-repeat;
height: 140px;
}
.bg_middle{
background: transparent url(../bg_middle.png) repeat;
}
.bg_middle div{
margin:-130px 10px -100px 10px;
border:1.5px solid #98b73f;
}
.bg_bottom{
background: transparent url(../bg_bottom.png) no-repeat;
height: 131px;
}
#main {
float:left;
width:500px;
margin-left:0px;
padding:10px;
}
Thanks #Maroshii for the reply. I will also try your approach, but for now I'll stick to this. Hope I will not have to change a lot for browser compatibility :)

Related

Put image in the center of bottom border with CSS

Everything is explained in the title of this post. I'm trying to put an PNG image in the center bottom border of a div element.
.content_block {
border: ridge;
border-width: 1px;
border-color: #969696;
height: 200px;
width: 200px;
}
.content_block.orange {
background-image: linear-gradient(#FBB03B, #FF9933);
}
<div class="content_block orange"></div>
Here's an image of what I'm trying to do:
I searched the net for a way to that with CSS, and border-image and stuff, but nothing worked.
To achieve the effect of it being exactly in the middle of the border, you will have to include the border with the image by inheriting it, and making it invisible. Like this, you can 'calculate' with it.
See this Fiddle for the effect. In this Fiddle, I've created a pseudo element that has a background-image of a play button.
The CSS that does the trick is this:
div::after{
content: '';
position: absolute;
top: 100%;
left: 50%;
width: 30px;
height: 30px;
background-image: url('http://www.iconsdb.com/icons/preview/gray/video-play-3-xxl.png');
background-size: cover;
background-repeat: no-repeat;
transform: translate(-50%, -50%);
border-top: inherit;
border-top-color: transparent;
}
I've placed it to the absolute bottom and 50% from the left. Then with the transform property, I shifted it to be centered around these points (50% from the left, and 100% from the top);
Then to make it move along with the border, I inherited only at the top, and made it invisible.
.content-block {
position: relative;
width: 200px; height: 100px;
border: 1px solid #f0f;
}
.content-block img{
position: absolute;
left: 50%; bottom: 0;
width: 50px; height: 50px; margin: -25px;
}
<div class="content-block">
<img src="http://placehold.it/50x50" alt="">
</div>
If you have a relative positioned parent, you can manipulate the position of an inner child using position:absolute;
Add an img in html
<div class="content_block orange">
<img class='element' src='https://cdn0.iconfinder.com/data/icons/form-elements-kit/100/checked-green-rounded-01-128.png'/>
</div>
Add this to your css.
.element { width:32px;
height:33px;
display:block;
background-color:grey;
margin-left:auto;
margin-right:auto;
margin-top:185px;
border-radius:100%;
}
Hope that helps!
Put an image inside the orange div and add text-align:center to the div
<div class="content_block orange">
<img src="" height="30" width="30">
</div>
and then set margin-top to the img. Check this Fiddle

Make opacity of div on sides

I recently have created this banner for my website, but I realized that I only want the main part of my site to be 900px long. However, I want the banner to run off the page, but have the part where it runs off be darkened (through opacity). So, this means, I need to make the image of my site positioned in the middle. Here is what I developed so far:
https://jsfiddle.net/h3w89t9y/4/
As you can see, this doesn't really get what I need. Here's the issue:
.banner {
background:url(https://i.gyazo.com/74f0fa6b9d9ed6652f3e220ceae113cf.png) no-repeat;
background-position: center;
margin: 0 auto;
height:185px;
}
The banner isn't 800px. If I add in a width of 800px, it will go to the middle just like I wanted. However, the image will be limited to only be 800px long rather than overflowing off of 800px.
This is what I'm trying to get it to look like:
https://i.gyazo.com/c38cae7bd34379477a6fcc8eeb160c22.png
How do I make it to where my banner is centered to the middle, but has the sides overlapped with opacities?
You can achieve what you want using pseudo like this:
body {
margin: 0;
}
.wrapper {
background:url(https://i.gyazo.com/74f0fa6b9d9ed6652f3e220ceae113cf.png) no-repeat center;
width: 100%;
}
.wrapper:before, .wrapper:after {
content:'';
width: calc((100% - 900px) / 2); /*setting the width to the 100% minus your desired header's width / 2 so it will occupy the rest of your content*/
height:185px;
position: absolute;
top: 0;
background: rgba(0, 0, 0, 0.3); /*set the desired opacity*/
}
.wrapper:before {
left: 0;
}
.wrapper:after {
right: 0;
}
.banner {
width: 900px;
height:185px;
margin: 0 auto;
}
<div class="wrapper" style="">
<div class="banner"></div>
</div>
So the idea is your pseudo elements occupy the rest of the content and setting them your desired transparency, notice that in this way you also can set them blur or whatever filter that you want.
Here a working jsfiddle to play with
You can't control opacity of a single background like that, you need another element. For example:
.banner, .bannert {
background:url(https://i.gyazo.com/74f0fa6b9d9ed6652f3e220ceae113cf.png) no-repeat;
background-position: center;
margin: 0 auto;
height:185px;
}
.banner {
max-width: 800px;
position: absolute;
left: 0;
right: 0;
top: 0;
}
.bannert {
background-repeat: repeat-x;
opacity: 0.5;
}
<div style="width: 100%; background: black; padding: 1px;position: relative;">
<div class="bannert"></div>
<div class="banner"></div>
</div>
https://jsfiddle.net/h3w89t9y/6/
Try this; add two divs first, one for the left side, and one for the right,hence you can apply your desired opacity to them and make the banner sides filtered, look at the snippets below;
HTML
<div style="width: 100%; padding: 1px;">
<div class="banner">
<div class="trans_right"></div>
<div class="trans_left"></div>
</div>
</div>
CSS
.trans_right {
padding: 2rem;
width: 13%;
float: right;
background: rgba(71,67,255,0.9);
height: 65%;
}
.trans_left {
padding: 2rem;
width: 13%;
float: left;
background: rgba(71,67,255,0.9);
height: 65%;
}
I'm really not sure if there is a better way to do this, but it gives you what you're looking for, checkout the link:
Transparent Sides

CSS crop image to certain dimensions

I have looked around a lot for a solution to this but I can't seem to find one.
I have an image that I need to display within a certain set of dimensions. It must be no more than 100% of the width of the container: fine. But when I try to faux crop it to 50% of the container; it is scaled.
An example of the 100% width: http://i.stack.imgur.com/WTisJ.png
And an example of the problem when it is set to only 50% of the container: http://i.stack.imgur.com/J01sF.png
The code:
CSS:
.shopcontent{
margin-top: 120px;
}
.product{
margin: 2px;
display: block;
height: 250px;
width: 300px;
border: 2px solid #7f8c8d;
border-radius: 10px;
overflow: hidden;
}
.prodimg{
max-width: 100%;
max-height: 50%; (The problem line!)
border: 0px solid white;
border-radius: 10px;
}
.prodimgcont{
overflow: hidden;
}
HTML:
<div class="shopcontent">
<div class="product">
<span class="prodimgcont">
<img src="http://u.danmiz.net/xqz" class="prodimg"></img>
</span>
<p>This is a test</p>
</div>
</div>
Thanks for any help: I really have tried to find a way of doing this but nothing seems to work!
If I understand your problem correctly you could achieve the desired cropping effect like so:
HTML
<div class="img_container">
<div class="cropper">
<img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQQWvNeCn17lshW3c9Z4PLXlTZe6GPf2oiNrLZQft1nPgld1WYb" />
</div>
</div>
CSS
.img_container {
width:300px;
height:250px;
}
.img_container .cropper {
width:50%;
height:50%;
overflow:hidden;
}
.img_container .cropper img {
width:200%;
height:200%;
}
You use the .cropper div to set the desired 50% width and add overflow:hidden, then set the child img tag to width:200% (100% of grandparent width)
Fiddle: http://jsfiddle.net/6hjL0pat/3/
EDIT:
Updated fiddle with your use case
First of all, your img tag should be self closing. so replace
<img src="http://u.danmiz.net/xqz" class="prodimg"></img>
with
<img src="http://u.danmiz.net/xqz" class="prodimg" />
To your problem. I'd advice you to give the dimensions to the container (change that spanto div by the way) and then assign your image as a background-image, because it is more useful for scaling images, especially with background-size: cover.
HTML
<div class="shopcontent">
<div class="product">
<div class="prodimgcont"></div>
<p>This is a test</p>
</div>
</div>
CSS
.shopcontent{
margin-top: 120px;
}
.product{
margin: 2px;
display: block;
height: 250px;
width: 300px;
border: 2px solid #7f8c8d;
border-radius: 10px;
overflow: hidden;
}
.prodimgcont{
display: block;
max-width: 100%;
height: 50%;
max-height: 50%;
border: 0px solid white;
border-radius: 10px;
background-image: url(http://u.danmiz.net/xqz);
background-size: cover;
overflow: hidden;
}
I created a JSfiddle to show you how to do it.
This is one way to do it.
Let me know if you absolutely need to use an img tag. There is a solution for that too. In any case: you need to assign the dimensions you want to the container of the image, not the image itself - because the image needs to be cut off.
Please note, that background-size: cover won't work in IE8 and lower, unless you use a polyfill.

Insert image between two divs

I need to insert photo in between divs (blue and light-blue) as in the following example.
I did it with absolute positioning:
HTML:
#*Blue and light-blue sections with photo*#
<div style="width: 100%; height: 120px; background-color: #0052a4"></div>
<div style="width: 100%; height: 120px; background-color: #c2dffd">
<div class="image">
<img src="/Content/pictures/MainPhoto.png" />
</div>
</div>
CSS:
.image {
position: absolute;
bottom: -100px; /* bottom space */
right: 100px; /* right space */
}
.image img {
display: block;
}
But this way doesn't work correctly when you change the screen resolution of the device.
I create JSFiddle with this example.
Could you please to suggest another way to resolve this problem?
I need to create responsive design without hardcoded values.
Thanks! :)
you could just use a background and a padding to keep image from sides : DEMO
HTML
<div class="imaged">
<img src="http://lorempixel.com/800/180/food/7"/>
</div>
CSS
.imaged {
padding:20px;
text-align:center;
background:url(my-blue-light-blue-bg.jpg)repeat-x center ;
}
img {
border:solid white 4px;
vertical-align:top;/* or display:block; + margin:auto;*/
}
from your fiddle: inset box-shadow in a single div works too : DEMO 2 / DEMO 3
<div style="
padding:0 20px;
display:table;
min-width: 100%;
box-shadow:
inset 0 120px 0 #0052a4,
inset 0 -120px 0 #c2dffd;
height:244px;
line-height:244px;
text-align:center;
">
<img src="http://lorempixel.com/842/176/city" style="border:solid white 4px;"/>
</div>
HTML
<div class="container-box">
<div style="width: 100%; height: 120px; background-color: #0052a4"></div>
<div style="width: 100%; height: 120px; background-color: #c2dffd"></div>
<div class="image">
<img src="http://media1.santabanta.com/full1/Outdoors/Landscapes/landscapes-267a.jpg" />
</div>
</div>
CSS
.container-box {
position:relative;
}
.container-box img {
height:200px;
width:90%;
position:absolute;
top:20px;
left:5%;
border:3px solid #fff
}
Of course I'd never use inline CSS, but there you go. See fiddle here
I think this is what you are trying to do:
http://jsfiddle.net/dc6r1bny/
.image {
position: absolute;
bottom: 30px; /* bottom height */
left: 50%; /* position element 50% from left side */
margin-left: -421px; /* bring it back left half the image size */
}
Then for mobile, you will just need to use media queries to adjust the image size to be 100%, remove the margin, etc. when you hit roughly 842px.
add this css code to the image. it should work.
margin-right:auto;
margin-left:auto;

Repeat background image around centered header

I have a header with a fixed height and width centered on my page. From each edge of the header image I have taken small 1 px images that I wish to repeat on the remeining space of that width.
Anyway, my problem is that I can not get the small images to repeat around the header. I started by trying to repeat the background image right of the header, but instead that background jumps down to the next "row" and repeats itself there. When I added the left header bg image that one repeated on the entire "row" and the right header image goes under it.
Edit-
Here it is at jsfiddle. hope it helps you understand my problem.
http://jsfiddle.net/kEdGb/
Now here's the html:
<div id="HeaderContainer">
<div id="HeaderLeft"></div>
<div id="Header">
<div id="Menu">
<ul class="tab">
<li>Link</li>
</ul>
</div>
</div>
<div id="HeaderRight"></div>
</div>
And the css:
#HeaderContainer {
width: 100%;
}
#Header
{
background-image: url(images/header_image.png);
background-repeat: no-repeat;
background-color: #707173;
height: 210px;
width: 990px;
max-width: 990px;
margin: 0px auto;
-webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box; /* Firefox, other Gecko */
box-sizing: border-box; /* Opera/IE 8+ */
position: relative;
border-top-style: solid;
border-top-color: #707173;
border-top-width: 1px;
border-left-style: solid;
border-left-color: #707173;
border-left-width: 1px;
border-right-style: solid;
border-right-color: #707173;
border-right-width: 1px;
}
#HeaderLeft {
background-image: url(images/headerLeft.png);
background-repeat: repeat-x;
position: absolute;
height: 210px;
width: 100%;
}
#HeaderRight {
background-image: url(images/headerRight.png);
background-repeat: repeat-x;
position: absolute;
height: 210px;
width: 100%;
}
#Menu {
max-width: 95%;
position: absolute;
bottom: 0px;
left: 5%;
}
Doesnt work in any browser. What am I missing? :)
You don't need extra div's for left and right. You can simply apply the background tile to the #HeaderContainer, and the overlapping #Header will cover it with its background =)
And for different images left and right, you just need to overlap the background from one side - say left, with another div.
Here is a DEMO.
Have you tried to wrap the image URLs inside some quotation marks? Like background-image: url('images/headerLeft.png');
What I understood from your question is, you want to implement background-image on header corners only (similar to border-image).
If so,
you can simply following steps:
take element: parent and child
Repeat background image in parent element and given padding according to image you want to visible.
Assign different background color or image for inner element.
In this way background image of parent element works as border-image of header
Cheers,
Vikram
Just for curiosity sake, I have worked on your issue.
Here is the fiddle.
The HTML:
<div id="HeaderContainer">
<div id="HeaderLeft"> </div>
<div id="HeaderRight"> </div>
<div id="header">
<div id="Menu">
<ul class="tab">
<li>Link</li>
</ul>
</div>
</div>
</div>
The CSS:
#HeaderContainer {width: 100%; position:relative;}
#HeaderLeft {background: url("images/headerLeft.png") repeat-x top left; height: 210px; position:absolute; top:0%; left:0%; width:50%;}
#header {background: url("images/header_image.png") no-repeat 0 0 #707173; height: 210px; width: 990px; margin:0 auto; position:relative;}
#HeaderRight {background: url("images/headerRight.png") repeat-x top right; height: 210px; position:absolute; top:0%; right:0%; width:50%;}
I have taken a reference for some points from #Martin Turjak code.