I'm trying to prevent an image and text from overlapping with each other in html. Currently this is what I have:
But when the screen size gets smaller, the text and image intercept. I'm looking to make it so the text conform to the boarders of the image. This is what's happening now:
Lastly, this is my CSS
.image {
width: 500px;
margin-left: auto;
display: block;
padding-top: 10%;
padding-right: 10%;
}
.text {
font-size: 22px;
padding-top: 10%;
max-width: 700px;
position: absolute;
}
Your issue is with absolute positioning. Whenever you use position:absolute you remove that element from the source flow. You can try to remove the absolute positioning and then try something like the following:
Codepen example
<div class="parent">
<div class="text">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem
Ipsum has been the industry's standard dummy text ever since the 1500s, when an
unknown printer took a galley of type and scrambled it to make a type specimen
book. It has survived not only five centuries, but also the leap into electronic
typesetting, remaining essentially unchanged. It was popularised in the 1960s with
the release of Letraset sheets containing Lorem Ipsum passages, and more recently
with desktop publishing software like Aldus PageMaker including versions of Lorem
Ipsum.
</p>
</div>
<div class="image">
Place your image here.
</div>
And CSS:
.parent {
display: flex;
max-width: 1000px;
margin: 0 auto;
}
.text, .image {
padding: 15px;
}
.image {
width: 500px;
}
.text {
width: calc(100% - 500px);
}
Related
I would like to accomplish something like what's pictured above, using pure CSS. It must have the following features:
image-container will contain a 3x4 aspect ratio image, which should fill the available viewport height. Consequently, image-container will have a variable width, depending on the height of the viewport.
image-container should be fixed in place as the window scrolls.
content-container should be scrollable.
content-container should fill all the available space between the right edge of image-container and the right edge of the viewport.
In the past, I might have accomplished it with something like this (using jquery):
// style.css
.image-container {
position:fixed;
top: 0px;
left:0px;
bottom: 0px;
}
.image-container img {
height: 100%;
}
// script.js
$(window).load(function() {
var width = $('.image-container').width();
$('.content-container').css({'margin-left': width});
});
Is this possible using only CSS? Perhaps using flex?
css solution
use flex
html,
body {
height: 100%;
overflow: hidden;
}
html {
background-color: #000;
}
.body {
margin: 0;
width: 100%;
height: 100%;
background-color: #333;
color: #999;
}
.container-wrapper {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: flex-start;
height: 100%;
}
.image-container {
background-size: contain;
text-align: center;
height: 100%;
}
.image-container img {
height: 100%;
width: auto;
}
.content-container {
height: 100%;
display: flex;
flex-direction: column;
}
.content-inner-container {
flex-grow: 1;
overflow-y: auto;
padding: 20px 20px 40px 20px;
}
<div class="body">
<div class="container-wrapper">
<div class="image-container">
<img src="http://placehold.it/750x1334">
</div>
<div class="content-container">
<div class="content-inner-container">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<p>
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>
</div>
</div>
</div>
</div>
js solution
$(document).ready(function(){
var width = $('.image-container').width();
$('.content-container').css({'width': 'calc(100% - '+width+')'});
});
html,
body {
height: 100%;
overflow: hidden;
}
html {
background-color: #000;
}
.body {
margin: 0;
width: 100%;
height: 100%;
background-color: #333;
color: #999;
}
.container-wrapper {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: flex-start;
height: 100%;
}
.image-container {
background-size: contain;
text-align: center;
height: 100%;
}
.image-container img {
height: 100%;
width: auto;
}
.content-container {
height: 100%;
display: flex;
flex-direction: column;
}
.content-inner-container {
flex-grow: 1;
overflow-y: auto;
padding: 20px 20px 40px 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="body">
<div class="container-wrapper">
<div class="image-container">
<img src="https://dummyimage.com/480x640/6b676b/fff">
</div>
<div class="content-container">
<div class="content-inner-container">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<p>
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>
</div>
</div>
</div>
</div>
I believe I've accomplished what you're attempting. Check out this fiddle.
.image-container{
height:100vh;
width:100vh - 25%;
border:2px solid red;
display:inline-block;
margin:0;
position:fixed;
}
.content-container{
border:2px solid green;
margin-left: 100vh - 25%;
display:inline-block;
width:100%;
height:2000px;
}
<div class="image-container"></div>
<div class="content-container">
<p>I would like to accomplish something like what's pictured above,
using pure CSS. It must have the following features. </p>
</div>
After following a n amount of sticky footers tutorials i got stuck.
Can anyone explain where my sticky footer is going wrong?
The main idea is that the footer gets on the bottom of the page.
If the page is larger then the window then the footer should be viewable after scrolling down.
The code works on the homepage as it should except for that i get a little bit of space below the footer.
As soon as the content is bigger than the window the footer stops working.
css:
html, body {
min-height:100%;
height:100%;
margin: 0;
padding:0;
font-family: 'Open Sans', sans-serif;
background-color: #2b2d2f;
color: #d9edf7;
}
#wrap {
min-height: 100%;
/* equal to footer height */
margin-bottom: -30px;
}
#wrap:after {
content: "";
display: block;
}
#footer, #wrap:after {
height: 30px;
}
#footer{
background-color: #2b542c;
text-align:center;
}
html:
<div id="wrap">
... content...
</div>
<div id="footer">
... content ...
</div>
You can use min-width on #wrap.
Look at this Codepen
Just like:
#wrap {
min-height: calc(100vh - 30px); /* '30px' - Height of the footer */
}
Hope this helps!
html {
position: relative;
min-height: 100%;
}
body {
margin: 0 0 100px; /* bottom = footer height */
}
footer {
position: absolute;
left: 0;
bottom: 0;
height: 100px;
width: 100%;
background-color:red;
}
<body>
<nav></nav>
<article>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</article>
<footer></footer>
</body>
#footer {
background-color: #2b542c;
text-align: center;
position: fixed;
width: 100vw;
top: calc(100vh - 30px);
}
If you change your footer style to this what you will get is a Sticky Footer that is responsive and will work no matter how its parent element sizes are set, since it's screen dependent.
I have 2 divs:
div.main-info {
background: yellow;
width: 50%;
float: left;
}
div.main-cta {
background: red;
width: 20px;
}
<div id="wrapper">
<div class="main-info">
<h1>Lorem Ipsum?</h1>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
<div class="main-cta">
<div class="window-cta">
<p>Compare Now!</p>
</div>
</div>
</div>
but the div with class main-cta seems to be still at the 2nd line? Why is that? I thought that when you float a div, it will pop out of the doc's flow and the next element will occupy the first row where the floated element was and get hidden by it?
I'm kinda confused.
Thanks!
Try this
<style>
div.main-info {
background: yellow;
width: 50%;
float: left;
}
div.main-cta {
background: red none repeat scroll 0 0;
float: left;
width: auto;
}
</style>
I think it will help for you..
div.main-info {
background: yellow;
width: 50%;
float: left;
}
div.main-cta {
background: red;
background: red none repeat scroll 0 0;
float:right;
}
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<div id="wrapper">
<div class="main-info">
<h1>Lorem Ipsum?</h1>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<div class="main-cta">
<div class="window-cta">
<p>Compare Now!</p>
</div>
</div>
</div>
</div>
Just add float:left to div.main-cta .
div.main-info {
background: yellow;
width: 50%;
float: left;
}
div.main-cta {
background: red;
width: 20px;
float: left; /*new line*/
}
Demo fiddle: https://jsfiddle.net/nikhilvkd/f6stjstk/
Or you dont like to add float:left , you can use display:inline-block;
div.main-info {
background: yellow;
width: 50%;
display: inline-block; /*new line*/
}
div.main-cta {
background: red;
width: 20px;
display: inline-block; /*new line*/
vertical-align: top;
}
Demo fiddle: https://jsfiddle.net/nikhilvkd/f6stjstk/2/
In order to pop the div out of the flow, you need to make the position of the div absolute.
float asks the div to move toward the left, in the same row.
This question already has answers here:
How to add border to a container with transparent gaps in between
(4 answers)
Closed 7 years ago.
For a client we are building a new website. They want a effect where the title is in the border of a text-area. How can this be done with CSS/HTML.
The effect I want to create looks like this:
In this image the background is green but in some cases this is a image. So a overlay with a background color in the text won't work.
Any ideas?
Here is an example I made for you. You just need to apply position: absolute; to the contact div and then set up where you want it to overlap the next div. http://jsfiddle.net/tzrhcmb4/
<div class="main">
<div class="container">
<div class="text-header">CONTACT</div>
<div class="text"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
</div>
</div>
css:
.main { background: green; width: 500px; height: 300px;}
.container { width: 100%; height: 100%; padding-top: 20px;}
.text-header { color: white; text-align: center; top: 30px; width: 110px; left: 35%; position: absolute; background: green;}
.text { margin: 10px; text-align: left; padding: 20px 10px 20px 10px; border: 2px dashed lightgreen;}
This question already has an answer here:
How to make gutter between columns in a CSS grid system
(1 answer)
Closed 8 years ago.
I'm creating a webpage that should have 3 columns with whitespace between them.
Here's how it should look:
Here's how it actually looks:
How can I fix my code so that the webpage has the gaps between the columns?
I would like a 3 column design that is responsive (and covers all webpage wide when below than 600px wide screen). I don't want 3 absolute measured columns.
Note that I have 1 pixel at the end of the third div: that's intentional, since that's how it looks when viewed with a browser.
Here's my HTML:
<div id="container">
<div class="squares"></div>
<div class="squares"></div>
<div class="squares"></div>
</div>
And my CSS:
#container {
width: 960px;
}
.squares {
width: 33.33%;
height: 250px;
background: red;
}
What you are trying to achieve is already build with bootstrap, but still here is a working JSFIDDLE.
CSS:
#container{
width: 960px;
}
.squares{
width: calc(98% / 3);
margin-right: 1%;
height: 250px;
float:left;
background: red;
}
.squares:last-child{
margin-right: 0%;
}
Add a margin to your Square.
.squares{
width: 33.33%;
height: 250px;
background: red;
margin: 10px;
}
I'm assuming since your divs are touching that you're using a framework that has border-box. Here's a fiddle:
.squares{
width: 33.33%;
padding: 5px;
float: left;
}
.squares div {
background: red;
padding: 5px;
}
Basically what you did was right. The issue is that the grid needs padding and an interior object. Always use padding for grids, not margins, so the math is much easier. It is much easier to write 33% with whatever padding you want vs. 30% with 3% margin-right. Imagine if you decided to change the margin and you would have to do the math again.
You want to create two new classes called first and last.
Add the class first to the first div and the class last to the last div.
Apply a general left and right padding to the divs and give them a box-sizing of border-box
Then remove the padding using the classes first and last.
Here is the jsfiddle of it all in play: http://jsfiddle.net/b20n6rvt/4/
<div id="container">
<div class="squares first">
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
</div>
<div class="squares">
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
</div>
<div class="squares last">
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
</div>
</div>
CSS:
#container{
width: 960px;
}
.squares{
width: 33.33%;
padding-left: 10px;
padding-right: 10px;
box-sizing: border-box;
float: left;
}
.squares div {
background: red;
}
.first {
padding-left: 0px;
}
.last {
padding-right: 0px;
}
Two ways to achieve this:
1) Using display: table:
#container {
width: 960px;
display: table;
}
.squares {
width: 33%;
height: 250px;
background: red;
display: table-cell;
border-right: solid 25px #fff;
}
.squares:nth-child(3) {
border: 0;
}
<div id="container">
<div class="squares"></div>
<div class="squares"></div>
<div class="squares"></div>
</div>
2) Using floats:
#container {
width: 960px;
}
.squares {
width: 29%;
height: 250px;
background: red;
margin: 3%;
float: left;
}
.squares:nth-child(3) {
margin-right: 0;
}
.squares:first-child {
margin-left: 0;
}
<div id="container">
<div class="squares"></div>
<div class="squares"></div>
<div class="squares"></div>
</div>
You are well on your way. What you really want is to have these squares being containers as well. Then they can be a third width including padding, which will become your "gap", usually called gutters.
This might be a bit of an overkill, but it's a simple guide to the basics of a grid system. (This technique is used by bootstrap, though that's a tad more advanced.)
There are a couple of steps to make this grid system work.
Use the invaluable * { box-sixing: border-box; } to make a box model where padding and borders are included in your elements width and height.
Make a container/wrapper which will hold your content.
Use .row to hold your .column. The .row should have 100% width + your gutter witdh / 2 as negative margins. (They will be next to eachother.)
Clearfix your floats.
Demo
CSS
.squares {
width: 33.3333%;
display-:inline-block;
float;left
}
.squares + .squares {
padding-left:5px;
}
The create your red color block inside it
That square will act as grid.
Or you can replace padding by margin, but then you will need to have equal width squares where width of all squares + margin = width of parent container.