Effectively, I'm trying to move/position the inner paragraph element (p) from within the .box div (blue box) to be positioned above the .box div (blue box) in the fiddle below:
html, body {
margin: 0;
}
.box {
height: 200px;
width: 400px;
background: aqua;
color: black;
}
<div class="container">
<h4>Box heading</h4>
<div class="box">
<div class="contents">
<p>This is some awesome text, which is inside the box</p>
<p>This is also another great paragraph that is better than any book ever written</p>
</div>
</div>
</div>
My expected output is:
html,
body {
margin: 0;
}
.box {
height: 200px;
width: 400px;
background: aqua;
color: black;
}
.contents {
position: relative;
top: -50px;
}
<div class="container">
<h4>Box heading</h4>
<p>This is some awesome text, which is inside the box</p>
<p>This is also another great paragraph that is better than any book ever written</p>
<div class="box">
<div class="contents"></div>
</div>
</div>
However, in the above snippet, I have physically placed the paragraph elements above the blue box (.box div). Is there a way to essentially move the paragraph element from within the blue box above it using css.
I've tried using position attribute or giving the .contents a negative margin, however, this doesn't add space for the text to go above and causes overlapping issue with the heading and/or the blue box.
Note: The paragraphs within the box can be of any length, so I don't know the height and thus the offset needed for the paragraph.
You can consider position:relative for the content element and its container and use the same amount of pixel:
body {
margin: 0;
}
.box {
height: 200px;
width: 400px;
background: aqua;
color: black;
position: relative;
top: 110px;
}
.contents {
position: relative;
top: -110px;
}
<div class="container">
<h4>Box heading</h4>
<div class="box">
<div class="contents">
<p>This is some awesome text, which is inside the box</p>
<p>This is also another great paragraph that is better than any book ever written</p>
</div>
</div>
</div>
Or consider translate for a more dynamic way:
body {
margin: 0;
}
.box {
height: 200px;
width: 400px;
background: aqua;
color: black;
transform:translateY(40%); /*use 100% if height auto*/
}
.contents {
transform:translateY(-100%);
}
<div class="container">
<h4>Box heading</h4>
<div class="box">
<div class="contents">
<p>This is some awesome text, which is inside the box</p>
<p>This is also another great paragraph that is better than any book ever written</p>
</div>
</div>
</div>
You could remove the styles from .box on mobile and add them to .content::after. This will allow for any amount of content in the paragraphs and grow accordingly.
html, body {
margin: 0;
}
.box,
.contents::after {
height: 200px;
width: 400px;
background: aqua;
color: black;
}
/* Mobile only styles */
.box {
height: auto;
width: auto;
background: none;
}
.contents::after {
content: '';
display: block;
}
<div class="container">
<h4>Box heading</h4>
<div class="box">
<div class="contents">
<p>This is some awesome text, which is inside the box</p>
<p>This is also another great paragraph that is better than any book ever written</p>
</div>
</div>
</div>
Related
I need your help!
The designer did that:
But I can't display the list like that !
I need to have a max width on my container!
How my horizontal list can be align on left side and not affected by the max width on the right side ? :'(
That is a conundrum. I would use position:absolute to get out of the edge of container. If the container is position:relative you wouldn't be able to do that. Play around with the width of the cards container to see what fits your needs because you can't really snap it to the right side exactly, you'd need to overflow it (so make body {overflow-x:hidden}).
body {
overflow-x: hidden;
}
.container {
margin: auto;
max-width: 400px;
border: 1px solid red;
}
.strip-right {
position: absolute;
height: 50px;
display: flex;
width: 100%;
justify-content: space-between;
}
.strip-placeholder {
height: 50px;
}
.card {
width: 50px;
height: 50px;
background: purple;
}
<body>
<div class="container">
<h2>hello world</h2>
<div class="strip-right">
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
</div>
<div class="strip-placeholder"></div>
<p>
other content 1
</p>
<p>
other content 2
</p>
<p>
other content 3
</p>
<p>
other content 4
</p>
</div>
</body>
I have the following code
<body>
<div style="height: 35%; background-color: black;"></div>
<div id="header">
<div>
<h1>Title</h1>
<h3>Subtitle</h3>
</div>
</div>
<div id="content" class="card">
<div>
<p>
One
</p>
<p>
Two
</p>
</div>
</div>
</body>
Ideally, I would like the top portion of the page to be a certain color (black in the example), and I want the header area (which contains the <h1> and <h3> elements) to be inside the black box. Then I would like the first paragraph of the content to also be included inside the black box. Very similar to this picture:
What is the best way to go about this?
The simplest way is to use an absolute positioned pseudo element on the header
Stack snippet
body {
background-color: #ddd;
margin: 0;
}
#header {
position: relative;
color: white;
background-color: black;
}
#header::before {
content: '';
position: absolute;
top: 100%;
left: 0;
height: 60px;
width: 100%;
background-color: inherit;
}
#header div {
width: 80%;
margin: 0 auto;
padding: 10px;
}
#content {
position: relative;
width: 80%;
margin: 0 auto;
background-color: white;
}
<div id="header">
<div>
<h1>Title</h1>
<h3>Subtitle</h3>
</div>
</div>
<div id="content" class="card">
<div>
<p>
One
</p>
<p>
Two
</p>
<p>
Thre
</p>
<p>
Fou
</p>
</div>
</div>
Three steps:
Apply a gradient background to the <body>.
Create two sectioning elements: <header> and <section>
Ensure all the relevant elements in <header> and at the top of <section> have an explicitly declared height in pixels which, combined, match the height of the first part of the gradient.
Make sure that the html and body have height: 100% or min-height: 100% otherwise height 35% is not going to be 35% of the viewport height.
You can make the black background with an absolute positioned element. I suggest to look into css position(relative, absolute, fixed, static).
Here's a demo and the code:
https://jsfiddle.net/n617L6rh/
<div id="bg"></div>
<div id="header">
<div>
<h1>Title</h1>
<h3>Subtitle</h3>
</div>
</div>
<div id="content" class="card">
<div>
<p>One</p>
<p>Two</p>
</div>
</div>
html,
body {
height: 100%;
}
#bg {
height: 35%;
background: black;
position: absolute;
top: 0;
left: 0;
right: 0;
}
#header {
height: 35%;
color: #fff;
position: relative;
}
I have two divs that stack horizontally on each other.
Each div occupies 100% of the view-port.
.section{
width:100%;
height:100%;
}
In each div there is an anchor to the other div.
<div class="first section ">
<a name="first-section"> </a>
<p>Click here to go to the second section</p>
</div>
<div class="second section">
<p>Click here to go to the first section</p>
</div>
<a name="second-section"> </a>
My goal is to show the content of only one div each time. But when one is on the second section div and resize the page, the two divs are showned when the page is resized to its initial size.
How can I avoid scrolling up when the page is resize?
I have body{overflow:hidden;}. Thanks.
You can set the div to position:fixed, so it's always relative to the viewport. Then hide all the divs except the first one by default, and show the one when it's on :target.
jsFiddle example
html, body { height: 100%; }
body { margin: 0; }
.section {
width: 100%;
height: 100%;
position: fixed;
}
.section:target {
visibility: visible;
}
.first {
background: lightblue;
}
.second {
background: lightgreen;
visibility: hidden;
}
<div class="first section" id="first-section">
<p>Click here to go to the second section</p>
</div>
<div class="second section" id="second-section">
<p>Click here to go to the first section</p>
</div>
Or, use z-index with different values, and move the div on top when it's on :target.
jsFiddle example
html, body { height: 100%; }
body { margin: 0; }
.section {
width: 100%;
height: 100%;
position: fixed;
}
.section:target {
z-index: 3;
}
.first {
background: lightblue;
z-index: 2;
}
.second {
background: lightgreen;
z-index: 1;
}
<div class="first section" id="first-section">
<p>Click here to go to the second section</p>
</div>
<div class="second section" id="second-section">
<p>Click here to go to the first section</p>
</div>
Note, both approaches require each div to have a solid background defined.
I currently have some text that is left aligned but centered as follows:
<div class="center-text">There is some text here</div>
<div class="center-text">There is also some other text here</div>
<div class="center-text">Finally there is some text here</div>
.center-text {
width: 50%;
margin: auto;
}
How can I float an image to the left of the text that is in the bottom right corner? What I'm looking for is the following:
The black frame is the surrounding div, the red is the image and the text is as described above. My text is currently behaving as expected but I can't seem to add the image without impacting the text.
I made it a little bit different:
.container{
height: 300px;
position:relative;
border: 30px solid black;
}
.center-text {
width: 100%;
}
p {
margin: 0;
}
img{
position: absolute;
bottom: 0;
left: 0;
width:30%;
}
.text{
float: right;
width: 65%;
margin-top: 100px;
}
<div class="container">
<div class="center-text">
<img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcRLwIM4lLwdmXvvYio988Z70kxTBtoNudITd9_72iGRjlusoVey" />
<div class="text">
<p>There is some text here</p>
<p>There is also some other text here</p>
<p>Finally there is some text here</p>
<p>Finally there is some text here</p>
<p>Finally there is some text here</p>
</div>
</div>
</div>
I hope it also helps you :)
I'm redesigning a site and the different sections (header, banner image, main, etc.) have a background that stretches all the way across, however the content is contained to a certain width and that box is centered.
However, in the design the "banner image" (which is a image below the header but above the main content) will extend beyond the width of the rest of the content. At first this was easy until a need arose to have text on top of the banner image, and that text would need to line up with the rest of the text.
I cannot use CSS background image because on some pages the banner image area will be a slider, which requires tags.
I have a working solution, but it seems clunky and I was hoping to find a better method: http://jsfiddle.net/PkStg/10/
HTML:
<div class="header">
<div class="content-wrapper">
header text
</div>
</div>
<div class="banner">
<div class="content-wrapper">
<div class="banner-text-outer">
<div class="banner-text-inner">
<h2>banner text header</h2>
<p>banner text paragraph</p>
</div>
</div>
</div>
<div class="banner-image-wrapper">
<img src="http://www.brokenbowlakeguide.com/rainbow-trout-1.jpg" />
</div>
</div>
<div class="main-content">
<div class="content-wrapper">
main content text
</div>
</div>
CSS:
.header, .banner, .main-content { width: 100%; }
.header { background: red;}
.banner { background: green; }
.main-content { background: yellow; }
.content-wrapper {
margin: 0 auto;
max-width: 300px;
}
.banner-text-outer {
position: relative;
}
.banner-text-inner {
position: absolute;
top: 10px;
}
.banner-image-wrapper {
margin: 0 auto;
max-width: 400px;
min-width: 300px;
font-size: 0;
}
.banner-image-wrapper img {
width: 100%;
}
I know that you wanted to not use background-image, but here is a solution which uses that for anyone else who sees the page.
Perhaps your slider could make use of the background-image?
This should do it:
jsFiddle
HTML
<body>
<div class="header">
<div class="content-wrapper">
header text
</div>
</div>
<div class="banner">
<div class="content-wrapper">
<div class="banner-text-outer">
<div class="banner-text-inner">
<h2>banner text header</h2>
<p>banner text paragraph</p>
</div>
</div>
</div>
</div>
<div class="main-content">
<div class="content-wrapper">
main content text
</div>
</div>
</body>
CSS
.header, .banner, .main-content { width: 100%; }
.header { background: red;}
.banner { background: green; }
.main-content { background: yellow; }
.content-wrapper {
margin: 0 auto;
max-width: 300px;
}
.banner {
background: green url("http://www.brokenbowlakeguide.com/rainbow-trout-1.jpg") no-repeat;
background-size: contain;
background-position: center;
min-height: 150px;
}