I have to create a page with this structure:
Where the RED part has width = 100% and BLUE (and GREEN) part has width 885px.
I thought to create different width, some with width = 885px and the others with width 980px... but I think this is not the right approach... in fact if I have to change the width for example from 885px to 980 px
Another solution I think could be to have to div... the first one has width 100%; the second one, inside the first one, has width 885px. But I think could be difficult to place the green div at the same height/top of the red one on the back.
Which approach would you used to reach the goal?
Thank you
You need to manage simple html like below:
<div class="wrapper">
<div class="wrap"></div>
</div>
.wrapper{
width: 100%;
}
.wrap{
width: 885px;
margin: 0 auto;
}
When you only need full width div don't include .wrap in your html. And when you only need 885px width div exclude .wrapper in your html.
I made a quick example of how you could do this right here. I just made two classes, one that has a width of 100% (red div), and one that has a fixed width (blue div, I used 450px in my example). The green div is just a blue div inside a red div. I hope my example answers all the questions you have. Good luck!
I guess you can manage the red with green band and sencond red band positioning absolutely in a main container, that also contains the blue one with a width. To explain it better i've created a JsFiddle please follow the link to see it working:
Working example
I've use the approach suggested #C-link Nepal, but I think to put red bars this isn't enough.
HTML:
<div class="top"></div>
<div class="main">
<div class="redgreen">
<div class="green"></div>
</div>
<div class="red"></div>
<div class="blue"></div>
</div>
<div class="foot">
</div>
CSS:
.top {
background-color: red;
height: 50px;
}
.main {
text-align: center;
position: relative;
}
.green, .blue {
width: 600px;
margin: 0 auto;
}
.redgreen {
width: 100%;
background-color: red;
position: absolute;
top: 30px;
}
.red {
height: 30px;
position: absolute;
top: 150px;
background-color: red;
width: 100%;
}
.green {
background-color: green;
height: 100px;
}
Please note that most of CSS classes have height and background color to the pattern be drawn...
Related
Is it possible with only CSS to have the following effect:
I have two divs. One follows the other.
Now, if the user starts scrolling down the page (to see other content, more divs if you want..) the second div should "go up" (could also stay fixed and the first div goes down, I mean it would look the same) and overlap the first.
But only overlap for let's say 50px. After that, the behaviour is normal again, meaning that if you scroll further, those divs move out of the browser window eventually.
Have I made myself clear? I can add two coloured boxed to showcase if that helps. I played around a bit and tried parallex/position fixed/sticky mixes, but none seem to work with a given height restriction. I just wonder if this is possible without javascript.
You can get this effect by using position: sticky on both elements. There are a few things that can stop this from taking place, like having overflow: hidden or not having a height set on the parent element.
HTML
<div class="container">
<div class="red-box">This is the red box</div>
<div class="blue-box">this is the blue box</div>
</div>
<!-- needs space to be able to actually scroll on the page -->
<div class="container">
<div class=""></div>
<div class=""></div>
</div>
CSS
/* set the height of the container so that the sticky elements know how far they are meant to scroll */
.container{
min-height: 400px;
}
/* set your position sticky and a attribute that tells it when it should become sticky, in this case right at the top */
.red-box{
height: 400px;
background-color: red;
position: sticky;
top: 0px;
}
.blue-box{
height: 400px;
background-color: blue;
position: sticky;
top: 0px;
}
I have done a quick codepen example so that you can see this working. hope that helps.
https://codepen.io/Domnewmarch/pen/NWzqBde
Solution: I used a combination of negative margin, z-index and position: sticky.
Added margin to the 2nd container to make it more visible.
.sticky-wrapper {
height: 310px;
margin-bottom: -60px;
}
.content {
z-index: -1;
position: sticky;
top: 0;
padding: 0 3%;
height: 250px;
background-color: green;
}
.foo {
margin: 0 50px;
background-color: red;
height: 200px;
}
.next-content {
height: 1000px;
background-color: khaki;
}
<div class="sticky-wrapper">
<div class="content"></div>
</div>
<div class="foo"></div>
<div class="next-content"></div>
I have this <div> with a radial-gradient on it, however I don't want it to be over all the <div> but rather I'd like it to go some amount down and then repeat.
So to be clear I don't want the gradient to stretch over the div like this
But rather repeat across the div at a certain height like this
You need to add a selector, class or id to your div.
.container{
height: 500px;
background: red;
background: radial-gradient(red, yellow);
width: 100%px;
}
.content{
position:absolute;
top: 20%;
left: 35%;
width: 300px;
height: 150px;
background:
}
<div class="container">
<div class="content">
</div>
</div>
Please Ignore the positions, and etc...
Only focus in the class container and content. That the css of gradient background only affect the div that has the class.
Check this: JSFiddle
I've already asked this question, but in the wrong way. I should have told you guys what I wanted, and not how to fix my way of getting to it...
Basically I want a dynamic, fixed-height (150px) header that consists of an image (PNG with alpha channel) and a box on either side that stretch to the edge of the screen. The whole thing needs to be 50% transparent too.
I've tried this with divs to create the stretching boxes, having them and the image in a parent div, but never got them to adjust their width to reach the edge of the screen.
Here's a diagram:
[---div---][---image---][---div---]
Anyone got any ideas?
#header
{
position: absolute;
top: 0px;
left: 0px;
height: 150px;
width: 100%;
opacity: 0.5;
background-color: gray;
display: table;
}
#l
{
background-color: red;
}
#r
{
background-color: blue;
}
.headDiv
{
height: 100%;
width: 50%;
vertical-align: middle;
display: table-cell;
}
<head>
<div id="header">
<div class="headDiv" id="l"></div>
<img src="https://upload.wikimedia.org/wikipedia/en/thumb/9/9e/Flag_of_Japan.svg/1280px-Flag_of_Japan.svg.png" width=auto height=150px style="display:block">
<div class="headDiv" id="r"></div>
</div>
</head>
<body style="background-color:green; margin-top:160px;">
Things in the body go here.
</body>
This is the working bar, with color-coded divs and a Japanese flag to represent the center. All credit goes to Marc Audet for his great display: table strategy for filling space.
I got two divs. The second div should be on first div, so... When clicking at menu buttons in first div, there should appear second div on first div (the second div covers the first one). I created the second div under first one, gave to it relative position, and took it up to first one. But there is a problem. There is an overflow, cause the div is long, and div's height saved at bottom. How to do this thing without any problems?
HTML
<div class='wrapper'>
<div class='firstDiv'></div>
<div class='secondDiv'></div>
</div>
CSS
.wrapper{
position: relative;
}
.firstDiv, .secondDiv{
position: absolute;
}
HTML
<div class="one">
<div class="two"></div>
</div>
Css :-
.one
{
width: 170px;
height: 170px;
position: relative;
background: red;
}
.two
{
width: 70px;
height: 70px;
position: absolute;
background: black;
}
jsfiddle demo
http://jsfiddle.net/xnqsF/
Real answer available via css-grid,
setting the parent to display:grid
and the children to grid-row/column-start:1 as shown in the answer below
https://stackoverflow.com/a/50086485/3810321
Hi I am trying to create a responsive layout where I have three different size elements and I want an equal margin on both sides of middle element for all screens.
Here is my HTML and CSS
<section class="container">
<div href="/" class="pull-left logo"><img src="/images/logo.jpg"></a>
<div class="slogan pull-left"><img src="/images/slogan.jpg"></div>
<div class="pull-left support"></div>
</section>
<style>
.pull-left
{
float-left;
}
.slogan
{
margin: 0 17%;
}
</style>
Since logo and support sections are of fixed size. Above Css works fine for one resolution but as screen size goes down the margin doesn't remain same.
Any ideas how to achieve that?
EDIT: Here is the fiddle. http://jsfiddle.net/VdYua/22/
Initially there is an equal margin on both side of .slogan div. But on re size last div goes to next line. I want the margin to be decreased without braking layout.
Are you looking for something like this?
HTML:
<div class="centered">This is some content in the centered DIV</div>
CSS:
.centered { background: #888; margin: 0 auto; width: 50%; }
Using margin: 0 auto will center the elements horizontally, meaning that it will have "Equal margin on both sides"
You do have to set a width on elements when using the above method, but as shown you can use percentage widths (as I image you may well be for a responsive layout)
You cannot however use this technique on floated elements, so you may be looking to add something like this to your CSS:
.container { margin: 0 auto; width: 50%; }
If I have misunderstood your question please let me know.
EDIT: In response to the comment below I think I have managed to achieve what you're looking for, see this fiddle
HTML:
<section class="header">
<div href="/" class="logo"><img src="/images/logo.jpg" /></div>
<div class="slogan"><img src="/images/slogan.jpg" /></div>
<div class="support"></div>
</section>
CSS:
.header { padding: 0 50px 0 300px; position: relative; }
.logo, .support { background: red; height: 50px; position: absolute; top: 0; }
.support { background: blue; right: 0; width: 50px; }
.logo { left: 0; width: 300px; }
.slogan { background: black; height: 50px; margin: 0 auto; width: 50px; }
The positioning/padding aspect of things isn't particularly pretty (if the width of .support or .logo change, you have to change that in the css) however I think this is the only safe way of doing it with pure HTML/CSS in a cross browser way (I'd be interested to see anyone elses take on it though), the important this is that it works and is completely valid.