Alright so I am trying to a basic overlay over an image but it seems that I am doing something wrong, instead of being width and height 100% of the IMG, it is width and height 100% of the entire page
HTML
<div id="main_BodyNews">
<img src="img/main.png" alt="mainNews" />
<div class="overflow-box"></div>
</div>
And the CSS
#main_BodyNews {
width: 50%;
height: 300px;
background-color: #F2C68C;
margin-top: 50px;
margin-left: 20px;
float: left;
border-radius: 5px;
border: 1px solid #F2C68C;
}
#main_BodyNews img {
width: 100%;
height: 100%;
border-radius: 5px;
background-color: 1px solid #F2C68C;
position: relative;
}
.overflow-box {
position:absolute;
top: 0px;
left: 0px;
bottom: 0px;
right: 0px;
background-color:rgba(255,255,0,0.5);
width: 100%;
height: 100%;
}
JS fiddle: https://jsfiddle.net/0utbjwo0/
you should add position: relative; to your absolute parent div
#main_BodyNews{
position: relative;
}
#main_BodyNews {
width: 50%;
height: 300px;
background-color: #F2C68C;
margin-top: 50px;
margin-left: 20px;
float: left;
border-radius: 5px;
border: 1px solid #F2C68C;
position: relative;
}
#main_BodyNews img {
width: 100%;
height: 100%;
border-radius: 5px;
background-color: 1px solid #F2C68C;
position: relative;
}
.overflow-box {
position:absolute;
top: 0px;
left: 0px;
bottom: 0px;
right: 0px;
background-color:rgba(255,255,0,0.5);
}
<div id="main_BodyNews">
<img src="img/main.png" alt="mainNews" />
<div class="overflow-box"></div>
</div>
You can use absolute. It's just that you are setting
width: 100%;
height: 100%;
Remove that and set your margin-top and margin left. You can set your width and height for the actually dimensions of your image. If you do this, you wont have to exactly keep your overlay div within your image div.
Here is an example of one I have made for my site.
#overlay {
margin-top: 60px;
margin-left: 88px;
height: 30px;
width: 85px;
position: absolute;
}
You can temporarily set a background-color for it so that you can get a good idea of where it is placed on your page. Then adjust your margins accordingly.
It's because the position: absolute has top, right, bottom, left value of 0. You don't need to specify the height and width. To make it resize on it's parent size. You need position: relative on parent element.
#main_BodyNews {
width: 50%;
height: 300px;
background-color: #F2C68C;
margin-top: 50px;
margin-left: 20px;
float: left;
border-radius: 5px;
border: 1px solid #F2C68C;
position: relative;
}
#main_BodyNews img {
width: 100%;
height: 100%;
border-radius: 5px;
background-color: 1px solid #F2C68C;
position: relative;
}
.overflow-box {
position: absolute;
top: 0px;
left: 0px;
bottom: 0px;
right: 0px;
background-color: rgba(255, 255, 0, 0.5);
}
<div id="main_BodyNews">
<img src="img/main.png" alt="mainNews" />
<div class="overflow-box"></div>
</div>
Related
I'm designing a logo in CSS3.
I have made a class .logo with particular height and width. Rest of the div inside .logo class will resize its position relative to parent div.
This is my CSS code.
.logo {
width: 200px;
height: 200px;
position: fixed;
}
.logo .vertical-left {
width: 25px;
height: 60%;
position: absolute;
bottom: 0;
background-color: #09aaba;
}
.logo .vertical-right {
width: 25px;
height: 65%;
background-color: #09aaba;
margin-left: 60%;
top: 0;
position: absolute;
}
.logo .vertical-right2 {
width: 25px;
height: 60%;
background-color: #ba1dd4;
margin-left: 60%;
top: 0;
position: absolute;
}
.logo .horizontal-top {
width: 100%;
height: 25px;
background-color: #09aaba;
position: absolute;
top: 30%;
border-radius: 10px 0;
}
.logo .horizontal-top2 {
width: 60%;
height: 25px;
background-color: #ba1dd4;
position: absolute;
top: 30%;
right: 0;
}
.logo .horizontal-bottom {
width: 72.5%;
height: 25px;
background-color: #09aaba;
position: absolute;
top: 65%;
border-radius: 10px 0;
}
/* triangle */
.logo .arrow-left {
width: 0;
height: 0;
border-top: 12px solid transparent;
border-bottom: 12px solid transparent;
position: absolute;
top: 30%;
left: 35%;
border-right:10px solid #ba1dd4;
}
.logo .arrow-down {
width: 0;
height: 0;
border-left: 12px solid transparent;
border-right: 13px solid transparent;
position: absolute;
top: 60%;
right: 27%;
border-top: 10px solid #ba1dd4;
}
<div class="logo">
<div class="vertical-left"></div>
<div class="vertical-right"></div>
<div class="vertical-right2"></div>
<div class="horizontal-top"></div>
<div class="horizontal-top2"></div>
<div class="horizontal-bottom"></div>
<div class="arrow-left"></div>
<div class="arrow-down"></div>
</div>
Here .logo div size is 200px X 200px. When I change it to 300px X 300px the inside div are messed up as in following snippet.
.logo {
width: 300px;
height: 300px;
position: fixed;
}
.logo .vertical-left {
width: 25px;
height: 60%;
position: absolute;
bottom: 0;
background-color: #09aaba;
}
.logo .vertical-right {
width: 25px;
height: 65%;
background-color: #09aaba;
margin-left: 60%;
top: 0;
position: absolute;
}
.logo .vertical-right2 {
width: 25px;
height: 60%;
background-color: #ba1dd4;
margin-left: 60%;
top: 0;
position: absolute;
}
.logo .horizontal-top {
width: 100%;
height: 25px;
background-color: #09aaba;
position: absolute;
top: 30%;
border-radius: 10px 0;
}
.logo .horizontal-top2 {
width: 60%;
height: 25px;
background-color: #ba1dd4;
position: absolute;
top: 30%;
right: 0;
}
.logo .horizontal-bottom {
width: 72.5%;
height: 25px;
background-color: #09aaba;
position: absolute;
top: 65%;
border-radius: 10px 0;
}
/* triangle */
.logo .arrow-left {
width: 0;
height: 0;
border-top: 12px solid transparent;
border-bottom: 12px solid transparent;
position: absolute;
top: 30%;
left: 35%;
border-right: 10px solid #ba1dd4;
}
.logo .arrow-down {
width: 0;
height: 0;
border-left: 12px solid transparent;
border-right: 13px solid transparent;
position: absolute;
top: 60%;
right: 27%;
border-top: 10px solid #ba1dd4;
}
<div class="logo">
<div class="vertical-left"></div>
<div class="vertical-right"></div>
<div class="vertical-right2"></div>
<div class="horizontal-top"></div>
<div class="horizontal-top2"></div>
<div class="horizontal-bottom"></div>
<div class="arrow-left"></div>
<div class="arrow-down"></div>
</div>
How can I have a responsive logo which will adjust according to parent height and width?
Setting the dimensions to percentages should make it resize. But the pointed arrow tips won't because they can't be set to percentage. The code below makes the logo resize but you will see that the arrow head cuts off at some point. Hope this puts you in the right direction
.logo {
width: 300px;
height: 300px;
position: fixed;
}
.logo:nth-child(2) {
width: 100px;
height: 100px;
position: absolute;
right: 0;
}
.logo .vertical-left {
width: 12.5%;
height: 60%;
position: absolute;
bottom: 0;
background-color: #09aaba;
}
.logo .vertical-right {
width: 12.5%;
height: 65%;
background-color: #09aaba;
margin-left: 60%;
top: 0;
position: absolute;
}
.logo .vertical-right2 {
width: 12.5%;
height: 60%;
background-color: #ba1dd4;
margin-left: 60%;
top: 0;
position: absolute;
display: flex;
justify-content: center ;
align-items: flex-end;
}
.logo .horizontal-top {
width: 100%;
height: 12.5%;
background-color: #09aaba;
position: absolute;
top: 30%;
border-radius: 10px 0;
}
.logo .horizontal-top2 {
width: 60%;
height: 12.5%;
background-color: #ba1dd4;
position: absolute;
top: 30%;
right: 0;
display: flex;
justify-content: center;
flex-direction: column;
}
.logo .horizontal-bottom {
width: 72.5%;
height: 12.5%;
background-color: #09aaba;
position: absolute;
top: 65%;
border-radius: 10px 0;
}
/* triangle */
.logo .arrow-left {
}
.logo .arrow-down {
}
.vertical-right2:after {
content: '';
width: 100%;
height: 0;
border-left: 12px solid transparent;
border-right: 13px solid transparent;
position: relative;
bottom: -10px;
border-top: 10px solid #ba1dd4;
z-index: 100;
}
.horizontal-top2:before {
content: '';
width: 0;
border-top: 12px solid transparent;
border-bottom: 12px solid transparent;
position: relative;
border-right: 10px solid #ba1dd4;
left: -10px;
flex-grow: 1;
z-index: 100;
}
<div class="logo">
<div class="vertical-left"></div>
<div class="vertical-right"></div>
<div class="vertical-right2"></div>
<div class="horizontal-top"></div>
<div class="horizontal-top2"></div>
<div class="horizontal-bottom"></div>
<div class="arrow-left"></div>
<div class="arrow-down"></div>
</div>
<div class="logo">
<div class="vertical-left"></div>
<div class="vertical-right"></div>
<div class="vertical-right2"></div>
<div class="horizontal-top"></div>
<div class="horizontal-top2"></div>
<div class="horizontal-bottom"></div>
<div class="arrow-left"></div>
<div class="arrow-down"></div>
</div>
I've tweaked your CSS a bit:
.logo .vertical-right {
width: 25px;
height: calc(65% - 25px);
background-color: #09aaba;
margin-left: 60%;
bottom: calc(27% + 25px);
position: absolute;
}
.logo .vertical-right2 {
width: 25px;
height: 60%;
background-color: #ba1dd4;
margin-left: 60%;
bottom: 40%;
position: absolute;
}
.logo .horizontal-top {
width: 100%;
height: 25px;
background-color: #09aaba;
position: absolute;
bottom: 60%;
border-radius: 10px 0;
}
.logo .horizontal-top2 {
width: 60%;
height: 25px;
background-color: #ba1dd4;
position: absolute;
bottom: 60%;
right: 0;
}
.logo .horizontal-bottom {
width: calc(60% + 25px);
height: 25px;
background-color: #09aaba;
position: absolute;
bottom: 27%;
border-radius: 10px 0;
}
/* triangle */
.logo .arrow-left {
width: 0;
height: 0;
border-top: 12px solid transparent;
border-bottom: 12px solid transparent;
position: absolute;
bottom: 60%;
right: 60%;
border-right: 10px solid #ba1dd4;
}
.logo .arrow-down {
width: 0;
height: 0;
border-left: 12px solid transparent;
border-right: 13px solid transparent;
position: absolute;
top: 60%;
margin-left: 60%;
border-top: 10px solid #ba1dd4;
}
First off I agree with other posters here that an image sounds more like what should be getting used in this case. However that doesn't really answer the question; it's just handy advice.
With the sort of positioning you're attempting try to make your elements use a common "point of origin" within your container. In other words always try to align them from the same edges. You had a bit of a mixture before of top, right, left, and bottom. I've made elements that respect each other use the same edge for calculating distance. I've also added a couple of CSS calc functions like this one height: calc(65% - 25px);, since you're mixing mostly percentage elements with a couple of static pixel based measurements.
A perfect way to achieve what you want to do is to go for a SVG logo.
SVGs can be resized without breaking, and are quite powerful.
This tutorial could help you get started.
I have 2 DIVs, that I want to center and overlap. The smaller one is to lay on top of the bigger one.
It works great at full-screen, but if I decrease the browser size, the top/smaller one moves to the left.
<div style="position: relative; top: 160px; border: thin solid gray; border-radius: 10px; width: 300px; height: 64px; margin-left: auto; margin-right: auto; z-index: 1; background: url(...); background-repeat:no-repeat; background-position:top; background-color: #4b2f84"> </div>
<div style="position: absolute; top: 200px; left: 15%; width: 70%; background: white; border: thin solid gray; border-radius: 10px; height: 500px; padding: 50px 30px; margin: auto">something
</div>
I like to use left: 50%; combined with transform: translateX(-50%); when trying to center and overlap content.
The content is offset 50% to the left, and then -50% of its width to the left.. or (this.left == parent.x + parent.width* 0.5 - this.width*0.5)
#div1 {
position: relative;
top: 160px;
border: thin solid gray;
border-radius: 10px;
width: 300px;
height: 64px;
margin-left: auto;
margin-right: auto;
z-index: 1;
background: url(...);
background-repeat: no-repeat;
background-position: top;
background-color: #4b2f84
}
#div2 {
position: absolute;
top: 200px;
left: 50%;
transform: translateX(-50%);
width: 70%;
background: white;
border: thin solid gray;
border-radius: 10px;
height: 500px;
padding: 50px 30px;
margin: auto
}
<div id="div1"> </div>
<div id="div2">something</div>
How to create the curve that you see in picture with CSS and HTML?
Can I use CSS border radius or use other solution?
You could do it with two divs and psuedo elements :before and :after. Working code below
.top-bar{
height: 100px;
width: 100%;
background-color: #55c3ff;
}
.curved-bottom{
width: 80%;
margin: 0 auto;
height: 50px;
background-color: #55c3ff;
border-radius: 0 0 20px 20px;
position: relative;
}
.curved-bottom:before {
height: 50px;
width: 16%;
background-color: white;
border-top-right-radius: 10px;
left: -16%;
content: '';
position: absolute;
top: -8px;
}
.curved-bottom:after {
height: 50px;
width: 16%;
background-color: white;
border-top-left-radius: 10px;
right: -16%;
content: '';
position: absolute;
top: -8px;
}
<div class="top-bar"></div>
<div class="curved-bottom"></div>
If your main horizontal blue bar is a div, and the box sticking down is a separate div, you can use the pseudo elements :before and :after to create those inner radius.
See the following as an example:
html,
body {
padding: 0;
margin: 0;
}
.header {
position: relative;
background-color: #5DC4FD;
width: 100%;
height: 160px;
}
.tab {
position: relative;
top: 130px;
background-color: #5DC4FD;
width: 80%;
height: 100px;
margin: 0 auto;
border-radius: 0 0 30px 30px;
}
.tab:before {
position: absolute;
content: "";
left: -50%;
width: 50%;
height: 100px;
background-color: white;
border-radius: 0 30px 0 0;
}
.tab:after {
position: absolute;
content: "";
right: -50%;
width: 50%;
height: 100px;
background-color: white;
border-radius: 30px 0 0 0;
}
<div class="header">
<div class="tab">
</div>
</div>
Well, you could use overlapping divs like this:
#top {
background: #00BFFF;
width: 100%;
height: 150px;
}
#container{
display: flex;
}
#mid{
background: #00BFFF;
width: 70%;
height: 50px;
border-bottom-left-radius: 25px;
border-bottom-right-radius: 25px;
}
#left{
background: #FFFFFF;
margin-top: -50px;
width: 15%;
height: 50px;
border-top-right-radius: 25px;
}
#right{
background: #FFFFFF;
margin-top: -50px;
width: 15%;
height: 50px;
border-top-left-radius: 25px;
}
<div id="top"></div>
<div id="container">
<div id="left"></div>
<div id="mid"></div>
<div id="right"></div>
</div>
but I'd recommend using a background image with the desired shape
I want to draw an inner div over an outer div for scrolling purposes. How can I change my CSS to fix this?
HTML code:
<div class="sliderPath">
<div class="slider"></div>
</div>
CSS code:
.sliderPath {
border-radius: 25px;
background: #73AD21;
padding: 20px;
width: 80%;
height: 20%;
margin: auto;
}
.slider {
border-radius: 4px;
background:#50c2de;
position: absolute;
width: 50px;
height: 50px;
left: 50%;
}
https://jsfiddle.net/ImSonuGupta/0bx6uwyn/1/
Try setting top position to small value:
.slider {
border-radius: 4px;
background:#50c2de;
position: absolute;
width: 50px;
height: 50px;
left: 50%;
top: 3px; //added this
}
And also set position: relative on parent:
.sliderPath {
border-radius: 25px;
background: #73AD21;
padding: 20px;
width: 80%;
height: 20%;
margin: auto;
position: relative; //added this
}
updated jsfiddle
Simply make .sliderPath the base position for its childs, with position:relative, and make .slider top 100% off its parent height.
.sliderPath {
border-radius: 25px;
background: #73AD21;
padding: 20px;
width: 80%;
height: 20%;
margin: auto;
position:relative;
}
.slider {
border-radius: 4px;
background:#50c2de;
position: absolute;
width: 50px;
height: 50px;
left: 50%;
top:100%
}
EDIT
if you need multiple slides, just add margin-bottom to .sliderPath equal to .slider height. So it would be:
.sliderPath {
border-radius: 25px;
background: #73AD21;
padding: 20px;
width: 80%;
height: 20%;
margin: auto;
position:relative;
margin-bottom:50px;
}
How could I center the blue box inside the red one ?
I see that the left side of the blue box is exactly in the middle of the red box, but I would like to center the whole blue box, not its left side. The dimensions of the boxes are not constant. I want to align regardless of boxes dimensions. Example to play with here. Thanks !
HTML:
<div id="rel">
<span id="abs">Why I'm not centered ?</span>
</div>
CSS:
#rel {
position: relative;
top: 10px;
left: 20px;
width: 400px;
height: 300px;
border: 1px solid red;
text-align: center;
}
#abs {
position: absolute;
bottom: 15px;
width: 300px;
height: 200px;
border: 1px solid blue;
}
If you're able to change the <span> tag to a <div>
<div id="rel">
<div id="abs">Why I'm not centered ?</div>
</div>
Then this piece of CSS should work.
#rel {
position: absolute;
top: 10px;
left: 20px;
width: 400px;
height: 300px;
border: 1px solid red;
text-align: center; }
#abs {
width: 300px;
height: 200px;
border: 1px solid blue;
margin: auto;
margin-top: 50px; }
I think it's better to use more automation for the enclosed box as less changes would be needed should you change the size of the container box.
You could add left:50px to #abs if that's all you want...
#abs {
position: absolute;
bottom: 15px;
width: 300px;
height: 200px;
border: 1px solid blue;
left:50px;
}
If you are going to define dimensions like that (200px x 300px and 300px x 400px), here's how it can be centered:
#rel {
position: relative;
top: 10px;
left: 20px;
width: 400px;
height: 300px;
border: 1px solid red;
text-align: center;
}
#abs {
position: absolute;
width: 300px;
height: 200px;
border: 1px solid blue;
margin: 49px 0 0 49px;
}
You can check at my solution here at http://jsfiddle.net/NN68Z/96/
I did the following to the css
#rel {
position: relative;
top: 10px;
left: 20px;
right: 20px;
width: 400px;
height: 300px;
border: 1px solid red;
text-align: center;
display: table-cell;
vertical-align: middle;
}
#abs {
display: block;
bottom: 15px;
width: 300px;
height: 200px;
border: 1px solid blue;
margin: 0 auto;
}
This should work
#abs {
position: absolute;
left: auto;
right: auto;
bottom: 15px;
width: 300px;
height: 200px;
border: 1px solid blue;
}