Css div absolute surrounded by text [duplicate] - html

This question already has answers here:
How can I wrap text around a bottom-right div?
(9 answers)
Closed 8 years ago.
I am trying to surround div with position: absolute by text.
Always text going under div.
CSS which I am using:
.all {
display: block;
width: 250px;
min-height: 180px;
height: auto;
position: relative;
background: #fa65fc;
}
.abs {
position: absolute;
top: 80px;
left: 200px;
float: right;
width: 50px;
height: 100px;
background: #4542df;
}
Two divs:
<div class="all">
<div class="abs">ABS</div>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
</div>
Here is link to: JSFiddle
I would like to do something like this:
Thanks in advance.

You can achieve the result you are looking for by using css to insert a dummy element. This method means you do not need to position your <div class="abs"> within the middle of the content of that div. This may be of use if you are not able to control what the content is (in the case of content being entered in a cms).
HTML:
<div class="all">
<div class="abs">ABS</div>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
</div>
CSS:
.all {
display: block;
width: 250px;
min-height: 180px;
height: auto;
position: relative;
background: #fa65fc;
}
.all:before {
content: "";
float: right;
height: 80px;
width: 0;
}
.abs {
clear: both;
float: right;
width: 50px;
height: 100px;
background: #4542df;
}
Link to JSFiddle.
.all:before inserts a dummy element which is floated right, no width and 80px high at the very beginning of <div class="all">.
Because .abs is floated right (but not positioned absolute), it will now try and stay floated right at the top of the div. Adding clear: both forces it drop below any other floated elements, so it ends up moving 80px down to clear the dummy float before it.

You cannot do that with position absolute. However you can achieve what you show on your image using static position and float: right; with some margins.
Here is an updated jsFiddle: http://jsfiddle.net/U5Pg5/2/
HTML:
<div class="all">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
<div class="abs">ABS</div>
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
</div>
CSS:
.all {
display: block;
width: 250px;
min-height: 180px;
height: auto;
position: relative;
background: #fa65fc;
}
.abs {
float: right;
width: 50px;
height: 100px;
background: #4542df;
margin-left: 15px;
margin-top: 10px;
margin-bottom: 10px;
}

Related

How to fill the width of a background image about half way with content overlaying on top?

I'm trying to make the background-image fill the entire width of the parent div positioned in the top left corner relative to the image size (in this case 600x375) while keeping it at 100% width across the parent div as well. In addition to doing this, I have content overlaying on top of the background-image which is the intended effect. I tried background-size but that covers the whole div which I don't want. I want the background-color:red to remain the full background color of the parent div as it is now but have the background-image about half way depending on the image size. I also tried width and height attributes on the :after property but it seems to shifts the background-image around and not change the actual size of the image. What is the appropriate approach to making the background-image size fit and stretched across it but not covering the entire parent div.
Here's the jsfiddle: https://jsfiddle.net/TheAmazingKnight/6xgrftLj/4/
.container {
z-index: 10;
position: relative;
}
#parent {
padding-top: 70px;
padding-bottom: 35px;
content: '';
background-color: red;
background-repeat: no-repeat;
width: 100%;
height: 500px;
background-position-y: 50%;
background-position-x: 50%;
position: relative;
}
#parent:after {
content: '';
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-repeat: no-repeat;
background-position-y: 50%;
background-position-x: 50%;
background-image: url(https://via.placeholder.com/600x375);
height: 675px;
width: 900px;
}
<div id="parent">
<div class="container" style="
z-index: 10;
position: relative;
">
<h1>Heading 1</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</span>
<h3>Lorem ipsum dolor</h3>
<p>Download</p>
</div>
</div>
I've written a solution for you. In my solution i didn't use the image as background. I used the image as a child element of the div(#parent) and positioned it as absolute. I made some changes in both of your html and css file. Hope it will help you.
.container {
z-index: 10;
position: relative;
}
#parent {
position: relative;
content: '';
background-color: red;
background-repeat: no-repeat;
width: 100%;
height: 500px;
position: relative;
}
#parent img {
position: absolute;
z-index: -1;
top: 0;
left: 0;
width: 100%;
height: 250px;
object-fit: cover;
}
<div id="parent">
<div class="container"
>
<img src="https://via.placeholder.com/600x375" alt="">
<h1>Heading 1</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</span>
<h3>Lorem ipsum dolor</h3>
<p>Download</p>
</div>
</div>

Right panel with div at bottom

To make it simple: I have a page with a div as right panel
.rightPanel{
position: fixed;
right: 0px;
}
This panel has a a few div inside (header, titles, etc.) and a div with the body. I need an extra div at the bottom where I will place the action bar.
I have tried
.actionBar{
position: absolute;
bottom: 20px;
}
The problem with this approach is that when the body is too big, the action bar will be on top of it. I would like a scroll bar on the body, if needed, with the action bar always fixed at the bottom.
<div class="rightPanel">
<header> .. </header>
<div class="body"> .. </div>
<div class="actionBar"> .. </div>
</div>
I don't want to give a fixed height to the body as it is dynamically crated.
Use flexbox to have a dynamic middle section. Here's a working demo:
body {
padding: 0;
margin: 0;
}
.rightPanel {
display: flex;
flex-direction: column;
position: fixed;
right: 0px;
width: 200px;
height: 100%;
border: 1px solid blue;
}
header {
width: 100%;
height: 30px;
border: 1px solid red;
}
.body {
flex-grow: 1;
width: 100%;
height: 100%;
overflow: auto;
}
.actionBar {
width: 100%;
height: 30px;
border: 1px solid red;
}
<div class="rightPanel">
<header> this is the header </header>
<div class="body"> "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." "Lorem ipsum dolor sit amet, consectetur adipiscing
elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum
dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
</div>
<div class="actionBar"> this is the action bar </div>
</div>
JSFiddle: https://jsfiddle.net/x52rq6du/1/
What you'll want to do is make .rightPanel a flexbox element, and give it display: flex and flex-direction: column. Then simply give all children flex-grow: 1, apart from .actionBar, which you want to keep fixed to the bottom. Note that .rightPanel will need a height for this top work, and this height should also accommodate the offset.
This can be seen in the following:
.rightPanel {
position: fixed;
right: 0px;
top: 20px;
display: flex;
flex-direction: column;
height: calc(100vh - (20px * 2));
}
.rightPanel > * {
flex-grow: 1;
}
.actionBar {
flex-grow: 0;
}
<div class="rightPanel">
<header>Header</header>
<div class="body">Body</div>
<div class="actionBar">Action Bar</div>
</div>
You can use clear property.
The clear property tells on which sides of an element floating elements cannot float.
By using both value for clear. You can specify no element can float neither on right nor left side of the element. like below:
.actionBar{
position: absolute;
bottom: 20px;
clear: both; // I think this should solve the problem
}
Maybe you will need to get rid of position: absolute; as well

Which approach is better for responsive design with fixed width sidebar?

so which approach is better for responsive design with fixed width sidebar ?
both are working normally, and now some people says that the second approach is better, some says first...
or it is all the same ?
approach 1: http://jsfiddle.net/56erp1my/33/
<div id="wrap">
<div id="header">Header</div>
<div id="sidebar">Static LEFT sidebar</div>
<div id="content">Main content: fluid div.<br/>Width is automatically adjusted between 300px and 700px</div>
<div id="footer">Footer</div>
</div>
#wrap { padding: 10px; max-width:1000px; margin: 0 auto;}
#header {background: #0f0;}
#sidebar {width: 200px; float: right; height: 200px; background: #ddd;}
#content {margin-right: 210px; min-height: 100px; background: #ddd;}
#footer {clear:both; background: #0f0;}
approach 2: http://jsfiddle.net/56erp1my/35/
<h2>With Content:</h2>
<div class="wrap">
<div class="right">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
<div class="left">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
.wrap {
background: #eee;
padding: 10px;
max-width: 960px;
overflow: hidden;
margin: 0 auto;
}
.left, .right {
padding: 5px;
}
.left {
background: tomato;
display: table-cell;
width: 9999px;
}
.right {
background: green;
width: 300px;
float: right;
}
Thank you
The second approach seems better in terms of maintenance. This is why:
If you want to change the width of the right sidebar in the first approach, you will also have to change the margin-right of the element with the class content.
While in the second approach, if you change the width of the right side bar, the content on the left will resize and re-position itself automatically.

how to make div with auto height overflow?

I got stuck. I have a wrapping div on my page with height set to some value. In this div, I have another div with set height (the yellow one). Under it, there is a blue div, which height automatically grows with the content. I want that div to have a scrollbar when the content exceeds all available height.
here is an example you can play with:
.container {
width: 200px;
height: 300px;
padding: 10px;
border: 1px solid #888891;
}
.header {
height: 40px;
background-color: #FEEC63;
margin-bottom: 10px;
}
.body {
color: #fff;
background-color: #63A4FE;
overflow: hidden; /* why is that not hiding the excess text? */
}
<div class="container">
<div class="header">
Hi there!
</div>
<div class="body">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
https://jsfiddle.net/674w4a09/
add height: calc(100% - 50px); to .body and it will work
the overflow didn't working on div.body because the height wasn't fixed
and to make it fit the rest of the container you use calc to substruct the height of the header plus 10px of the margin-bottom
jsfiddle
From MDN:
In order for the overflow property to have an effect, the block
level container must either have a bounding height (height or
max-height) or have white-space set to nowrap.
However, when you switch from a block formatting context to a flex formatting context, the requirement above doesn't apply and you can keep things simple:
.container {
display: flex;
flex-direction: column;
width: 200px;
height: 300px;
padding: 10px;
border: 1px solid #888891;
}
.header {
height: 40px;
background-color: #FEEC63;
margin-bottom: 10px;
}
.body {
overflow: hidden; /* switch to 'auto' for scrollbar */
color: #fff;
background-color: #63A4FE;
}
<div class="container">
<div class="header">Hi there!</div>
<div class="body">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
add height: calc(100% - 50px) to .body
50px = 40px (of header height) + 10px (of header bottom margin)
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.container {
width: 200px;
height: 300px;
padding: 10px;
border: 1px solid #888891;
overflow: hidden;
}
.header {
height: 40px;
background-color: #FEEC63;
margin-bottom: 10px;
}
.body {
color: #fff;
background-color: #63A4FE;
height: calc(100% - 50px);
}
</style>
</head>
<body>
<div class="container">
<div class="header">
Hi there!
</div>
<div class="body">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
</body>
</html>

Centring div content when they have max-width

I've got this container with 2 elements inside: http://jsfiddle.net/scQa2/1/ (JSFiddle doesn't seem to center properly so it's best to copy and paste the code)
test.html
<div id="main">
<img src="http://images.fanpop.com/images/image_uploads/Flower-Wallpaper-flowers-249402_1024_768.jpg" id="image"/>
<div id="text">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>​
test.css
#main {
width: 410px;
margin: auto;
}
#image {
max-width: 200px;
width: 100%;
float: left;
border: 1px solid blue;
}
#text {
max-width: 200px;
width: 100%;
float: left;
border: 1px solid red;
}​
What I am to do is have the contents align in the centre of the container, rather than have the container centred as since the two elements are both using max-width.
If I set the margin of the container to auto and set it to a specific width (say 410px, just enough for the 2 max-widths of 200px) , I get this:
But if the child elements shrink below the max-width they do not align as the container has not changed width:
Is there a way I can ensure that the two child elements are centred horizontally at all times, preferably without JavaScript and with just pure CSS/HTML?
Try this, hope its what you're after...
#main {
border: 1px solid red;
display: block;
width: 90%;
margin: 0 auto;
text-align: center;
}
.image{
vertical-align: top;
border: 1px solid blue;
display: inline-block;
}
.image img {
max-width: 200px;
}
#text {
vertical-align: top;
max-width: 200px;
border: 1px solid red;
display: inline-block;
}
html
<div id="main">
<p class="image">
<img src="http://images.fanpop.com/images/image_uploads/Flower-Wallpaper-flowers-249402_1024_768.jpg"/>
</p>
<p id="text">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</div>