Keep div from going under image with absolute positioning - html

I want put one image with a caption and after the image a description of the image. When I use absolute positioning for the image, the div goes under the image. How can i fix it so the div goes after the image: http://jsfiddle.net/qhcb0616/
.slide-square {
width: 148px;
height: 348px;
border:solid 1px black;
background: red;
}
.slide-square img {
position: absolute;
}
.slide-square h1 {
position: relative;
font-size:11px;
background :blue;
text-align:right;
float:right;
color:white;
padding:5px;
margin-top:5px;
margin-right:5px;
}
.slide-square div {
display: inline-block;
font-size: 50px;
}
<div class="slide-square">
<img src="http://www.flowers-online.com.au/flower-pictures/spring-surprise-flower-bouquet-b10l.jpg" alt="" width="148" height="148">
<h1>Scientific</h1>
<div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Velit debitis error magnam sunt numquam ab quasi omnis eaque dolore, perferendis quisquam, minus qui repudiandae possimus esse tempore ipsum adipisci soluta!</div>
</div>

What you need is make absolute the h1 element. Try this:
Make the parent relative:
.slide-square {
position: relative;
}
Then position the h1 on the parent
.slide-square h1 {
position: absolute;
top: 5px;
right: 5px;
}
Remove also the absolute for the img
Check the Snippet Below
.slide-square {
width: 148px;
height: 348px;
border: solid 1px black;
background: red;
position: relative;
}
.slide-square h1 {
position: absolute;
font-size: 11px;
background: blue;
text-align: right;
color: white;
margin:0;
padding: 5px;
top: 5px;
right: 5px;
}
.slide-square div {
display: inline-block;
font-size: 50px;
}
<div class="slide-square">
<img src="http://www.flowers-online.com.au/flower-pictures/spring-surprise-flower-bouquet-b10l.jpg" alt="" width="148" height="148">
<h1>Scientific</h1>
<div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Velit debitis error magnam sunt numquam ab quasi omnis eaque dolore, perferendis quisquam, minus qui repudiandae possimus esse tempore ipsum adipisci soluta!</div>
</div>

Related

How can I style a pseudo-element as the title of a paragraph?

We are using Markdown (Kramdown) to generate a static website. For infoboxes, we can annotade paragraphs and get the following results:
{:.note .icon-check title="This is a title"}
Lorem, ipsum dolor sit amet consectetur adipisicing elit.
And this is the converted HTML:
<p class="note icon-check" title="This is a title">
Lorem, ipsum dolor sit amet consectetur adipisicing elit.
</p>
The style is (SCSS):
p.note {
&::before {
float: right;
}
position: relative;
&[title]::after {
content: attr(title);
position: relative;
top: 0;
left: 0;
}
}
.icon::before {
content: "#";
}
As we are using Icomoon icon fonts, where the content is set in :before, the title must be in :after.
we won't change the Iconfont, so the icon must stay in :before
no additional distracting markup in the markdown, so no HTML wrapper
no Javascript
It is possible to set an absolute positioning to the title, but this would be too narrow to the paragraph-text itself, as no margin can be set.
Here is a JSFiddle
Now, how does one style a box with :after as a title on top, that also looks good when no title is set?
main {
width: 50%;
display: block;
place-items: center;
margin: auto;
}
p.note {
border: 3px solid red;
border-radius: 3px;
padding: 1em;
position: relative;
}
p.note::before {
margin: auto 0 0.5em 0.5em;
float: right;
}
p.note[title]::after {
content: attr(title);
position: relative;
font-weight: bold;
top: 0;
left: 1em;
}
.icon::before {
font-size: 2em;
content: "#";
}
<html>
<body>
<main>
<p class="note icon" title="This is a title">
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Dolorem quidem non placeat quibusdam velit, dicta adipisci saepe magnam fuga debitis qui, minima, eius error laboriosam distinctio eos natus et!
</p>
</main>
</body>
</html>
What it looks like:
How it should look like:
A crazy idea using shape-outside. The trick is to use the before for the title where you apply float and after for the icon having position:absolute then the shape-outside will create a particular shape in order to simulate the float behaviour around the icon.
main {
width: 50%;
display: block;
margin: auto;
text-align:justify;
}
p.note {
border: 3px solid red;
border-radius: 3px;
padding: 1em;
position: relative;
}
p.note::after {
margin: auto 0 0.5em 0.5em;
position:absolute;
top: 1em;
right: 0.5em;
}
p.note[title]::before {
content: attr(title);
display:block;
height:3.5em;
float:right;
width:100%;
text-align:center;
font-weight: bold;
shape-outside:polygon(0 0,100% 0,100% 100%,calc(100% - 3em) 100%,calc(100% - 3em) calc(100% - 2em),0 calc(100% - 2em));
/* To illustrate the shape */
background:
linear-gradient(rgba(255,255,0,0.3) 0 0) top/100% calc(100% - 2em) no-repeat,
linear-gradient(rgba(255,255,0,0.3) 0 0) bottom right/3em 3em no-repeat;
border:1px solid rgba(0,0,0,0.2);
/**/
}
.icon::after {
font-size: 2em;
content: "#";
}
.icon:not([title])::after {
display:none;
}
.icon:not([title])::before {
font-size: 2em;
content: "#";
margin: auto 0 0.5em 0.5em;
float:right;
}
<main>
<p class="note icon" title="This is a title">
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Dolorem quidem non placeat quibusdam velit, dicta adipisci saepe magnam fuga debitis qui, minima, eius error laboriosam distinctio eos natus et!
</p>
</main>
<main>
<p class="note icon" >
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Dolorem quidem non placeat quibusdam velit, dicta adipisci saepe magnam fuga debitis qui, minima, eius error laboriosam distinctio eos natus et!
</p>
</main>
Or a basic idea like below:
main {
width: 50%;
display: block;
margin: auto;
text-align:justify;
}
p.note {
border: 3px solid red;
border-radius: 3px;
padding: 1em;
position: relative;
}
p.note::before {
margin: auto 0 0.5em 0.5em;
float: right;
}
p.note[title]::after {
content: attr(title);
position: absolute;
font-weight: bold;
top: 0.5em;
left: 0;
right:2em;
text-align:center;
}
p.note[title] {
padding-top:2em;
}
.icon::before {
font-size: 2em;
content: "#";
}
<main>
<p class="note icon" title="This is a title">
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Dolorem quidem non placeat quibusdam velit, dicta adipisci saepe magnam fuga debitis qui, minima, eius error laboriosam distinctio eos natus et!
</p>
</main>
<main>
<p class="note icon" >
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Dolorem quidem non placeat quibusdam velit, dicta adipisci saepe magnam fuga debitis qui, minima, eius error laboriosam distinctio eos natus et!
</p>
</main>
since grid doesn't help enough, let's go back in time with display :table and display:table-caption to add another answer and see if that could work too for you ;)
* {
box-sizing: border-box;
}
main {
width: 50%;
min-width:450px;
display: block;
place-items: center;
margin: auto;
}
p.note {
font-size:clamp(12px, 4vw, 30px);
border: 3px solid red;
border-radius: 3px;
padding: 1em;
display: table;
padding-top: 1.5em;
}
p.note::before {
margin: auto 0 0.5em 0.5em;
float: right;
font-weight:bold;
color:tomato;
}
p.note[title]::after {
content: attr(title);
font-weight: bold;
display: table-caption;
margin-bottom: -1.6em;
text-align: center;
padding-right: 3em;
}
.icon::before {
font-size: 2em;
content: "#";
line-height: 0.25;
}
<main>
<p class="note icon" title="This is a title">
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Dolorem quidem non placeat quibusdam velit, dicta adipisci saepe magnam fuga debitis qui, minima, eius error laboriosam distinctio eos natus et!
</p>
</main>

How to remove space between div elements? [duplicate]

This question already has answers here:
CSS margin terror; Margin adds space outside parent element [duplicate]
(7 answers)
Closed 2 years ago.
I have two div elements and I want to remove the space between them, which is filled with the background color at the moment. This is what it looks like. I want to remove the space between the green section and where the background image ends for the first div element. Here is the HTML for the page:
<body style="background-color: #c5ffff">
<div class="main-search hero-image">
Log Out
<div class="welcome-text">
<div class="title">Welcome to Ripple.</div>
<div class="content">Lorem ipsum dolor sit amet, consectetur adipisicing elit. A aliquam commodi doloremque esse itaque iusto, labore laborum maxime odio, quidem quos, repellat rerum? Ab, asperiores aspernatur assumenda atque distinctio dolor dolore eveniet facilis, id illo ipsa ipsam minus nemo nobis porro quam quia quibusdam quis ratione rerum soluta suscipit temporibus vel vitae voluptate. Accusamus dignissimos ea esse expedita itaque mollitia nobis, numquam odio, quaerat qui vel voluptatibus?</div>
<button class="search_button" >Search for a location</button>
<button class="search_button">Search for a song</button>
</div>
</div>
<div class="main-left">
<div class="title">Collections Near Me</div>
<div>
</div>
</body>
And the CSS:
.main-left {
vertical-align: top;
margin-top: 0;
margin-left: 0;
position: relative;
background-color: #85dcba;
text-align: left;
width: 100%;
float: top;
left: 50%;
transform: translate(-50%);
}
.main-search {
vertical-align: top;
background-color: #d2fdff;
text-align: left;
margin: 0;
padding-top: 2em;
position: relative;
top: 0;
width: 100%;
left: 50%;
transform: translate(-50%);
}
.hero-image {
background-image: url("main_background.jpg");
background-size: cover;
background-repeat: no-repeat;
}
.title {
color: black;
font-family: Roboto, sans-serif;
font-size: 3em;
margin-top: 0.5em;
margin-left: 10px;
margin-bottom: 0.5em;
}
.content {
color: black;
font-family: Roboto, sans-serif;
font-size: 1em;
margin: 1em;
}
.welcome-text {
top: 5%;
left: 5%;
max-width: 35%;
}
There is a gap because div is block element, if you want to remove the gap between div use display: inline-block to remove it.
body > div { display: inline-block; }
This will display an element as an inline-level block container. Please refer to CSS Display
You could try setting the margin values of the elements manually. I see you've set the padding- Which refers to the internal distance of contents to edge, but not the margin- which refers to the distance between seperate elements.
Also, nice looking design so far!
Display flex has a nifty way of removing undesirable whitespace from the html
So place flex on the wrapper of these elements, in this case the body tag
css
body {
display: flex;
flex-direction: column;
}
Set margin-top of the .title to zero and for space between the .main-search and .title, give padding-bottom to .main-search as well. Below is how it will look like:
body{
background-color: #c5ffff;
}
.main-left{
vertical-align: top;
margin-top: 0;
margin-left: 0;
position: relative;
background-color: #85dcba;
text-align: left;
width: 100%;
float: top;
left: 50%;
transform: translate(-50%);
}
.main-search{
vertical-align: top;
background-color: #d2fdff;
text-align: left;
margin: 0;
padding: 2em 0;
position: relative;
top: 0;
width: 100%;
left: 50%;
transform: translate(-50%);
}
.hero-image{
background-image: url("https://picsum.photos/500/500");
background-size: cover;
background-repeat: no-repeat;
}
.title{
color: black;
font-family: Roboto, sans-serif;
font-size: 3em;
margin-top: 0;
margin-left: 10px;
margin-bottom: 0.5em;
}
.content{
color: black;
font-family: Roboto, sans-serif;
font-size: 1em;
margin: 1em;
}
.welcome-text{
top: 5%;
left: 5%;
max-width: 35%;
}
<div class="main-search hero-image">
Log Out
<div class="welcome-text">
<div class="title">Welcome to Ripple.</div>
<div class="content">Lorem ipsum dolor sit amet, consectetur adipisicing elit. A aliquam commodi doloremque esse itaque iusto, labore laborum maxime odio, quidem quos, repellat rerum? Ab, asperiores aspernatur assumenda atque distinctio dolor dolore eveniet facilis, id illo ipsa ipsam minus nemo nobis porro quam quia quibusdam quis ratione rerum soluta suscipit temporibus vel vitae voluptate. Accusamus dignissimos ea esse expedita itaque mollitia nobis, numquam odio, quaerat qui vel voluptatibus?</div>
<button class="search_button" >Search for a location</button>
<button class="search_button">Search for a song</button>
</div>
</div>
<div class="main-left">
<div class="title">Collections Near Me</div>
<div>
remove margin-top:0.5em; on your title class.
.title {
color: black;
font-family: Roboto, sans-serif;
font-size: 3em;
margin-top: 0.5em; <-- REMOVE
margin-left: 10px;
margin-bottom: 0.5em;

Inside border of image

I have some problem with creating inside border of image. Tried to do it with border, outline and box-shadow but didn't get the result.
HTML:
<div class="item">
<img src="https://i.ytimg.com/vi/vsZDSXlHqI4/maxresdefault.jpg" alt="">
<h3>Title</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo blanditiis, distinctio. Odio eveniet vel nobis, consequuntur atque, dolorum debitis quae nesciunt esse quasi beatae, odit repudiandae dolore animi delectus ad nostrum, quas maiores hic labore?
Nisi, expedita sint, qui ullam itaque natus optio error accusantium placeat, culpa reiciendis, quos tempora.</p>
<button>Some action</button>
</div>
CSS:
div.item:hover {
//some code
img {
border-bottom: 5px solid #8cc34b;
margin-bottom: -5px;
}
}
My fiddle: JSFiddle
Here I got the outside border but I want to get inside border of image.
Thanks for help.
Wrap your img in div, and on hover use :after pseudo-element:
div.item {
min-height: 300px;
overflow: auto;
width: 300px;
background-color: white;
border: 2px solid #8cc34b;
border-radius: 5px;
text-align: center;
box-sizing: border-box;
-webkit-box-sizing: border-box;
}
div.item img {
width: 100%;
display: block;
}
div.item p {
text-align: left;
padding-left: 10px;
padding-right: 10px;
}
div.item button {
height: 35px;
width: 120px;
margin: 0 auto;
margin-bottom: 20px;
}
div.item .img-container {
position: relative;
}
div.item:hover h3 {
color: #8cc34b;
}
div.item:hover .img-container:after {
content: '';
position: absolute;
left: 0;
bottom: 0;
height: 5px;
width: 100%;
background-color: #8cc34b;
}
<div class="item">
<div class="img-container">
<img src="https://i.ytimg.com/vi/vsZDSXlHqI4/maxresdefault.jpg" alt="">
</div>
<h3>Title</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo blanditiis, distinctio. Odio eveniet vel nobis, consequuntur atque, dolorum debitis quae nesciunt esse quasi beatae, odit repudiandae dolore animi delectus ad nostrum, quas maiores hic labore?
Nisi, expedita sint, qui ullam itaque natus optio error accusantium placeat, culpa reiciendis, quos tempora.</p>
<button>Some action</button>
</div>
I also added display: block on img, because inline img creates extra whitespace below.
Another answer:
To create a color bar like in the image you liked to, I wrapped your image in a DIV with class x and added the following CSS (including a pseudo element):
img {
width: 100%;
}
.x {
display: block;
position: relative;
}
.x:before {
content: '';
position: absolute;
z-index: -1;
bottom: 7px;
left: 0;
right: 0;
height: 5px;
background-color: #8cc34b;
}
Instead of a hover rule for the image, I just make the x DIV visible by changing its z-index:
div.item:hover {
h3 {
color: #8cc34b;
}
.x:before {
z-index: 1;
}
}
Adjust the bottom setting as desired to move it up or down:
https://jsfiddle.net/9wLx9p0f/5/
We can simply do this with outline-offset property:
outline: 1px solid #fff;
outline-offset: -10px;
Try this in the div you wants, if it works for you.

Div with text and 3 divs inside it filling it

I would like to create a div which has auto height based on the length of the text inside it (the text should be in the parent element so that it has 100% width. But also there should be 3 divs in the background each taking 1/3rd of the parent width and being 100% of the parent height.
This is an example of what I expect.
Any ideas how to create this?
I usually don't post questions like this one. But I just got stuck on how to do that and I needed help
You can use Flexbox for this
.content {
display: flex;
position: relative;
}
h1 {
text-transform: uppercase;
font-size: 45px;
margin: 0;
font-family: sans-serif;
word-spacing: 15px;
}
.divs {
position: absolute;
z-index: -1;
width: 100%;
top: 0;
left: 0;
height: 100%;
display: flex;
}
.el {
flex: 1;
border: 3px solid lightgreen;
}
.el:nth-child(1) {background: #DBEBD4;}
.el:nth-child(2) {background: #CEE2F4;}
.el:nth-child(3) {background: #D9D2EA;}
<div class="content">
<h1>Lorem ipsum dolor sit.</h1>
<div class="divs">
<div class="el"></div>
<div class="el"></div>
<div class="el"></div>
</div>
</div>
Have a container div absolutely positioned under the text and then have three child divs inside that.
The reason for the container div is that it avoids having to position each child separately.
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.parent {
position: relative;
}
.underlay {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
display: flex;
border: 1px solid grey;
}
.child {
flex:1;
border:1px solid red;
background: rgba(255,0,255,0.25);
z-index:-1;
}
<div class="parent">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dicta sunt natus architecto deserunt eligendi repellat corporis doloribus fugit ipsam fugiat, eius vitae, magni? Quasi consequatur voluptatem eius excepturi eum qui dolores placeat maxime corporis
laborum hic, magnam ipsa voluptatibus doloremque. Dignissimos dolorum corporis sunt amet unde repellat, dolor consectetur earum.</p>
<div class="underlay">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
</div>

When I resize browsers window, my div gets down to bottom and a big space comes in top

When I resize browsers window, my div gets down to bottom and a big space comes in top. I want that my div should remain in center of page even if resize the size of window lesser than size of div. In my school they haven't teach about this but I want to align this div in center of page
<!DOCTYPE html>
<html>
<head>
<title>CSS centering Done Right.</title>
<style>
* {
margin: 0;
padding: 0;
}
html, body, .m {
height: 100%;
width: 100%;
}
.m {
opacity:1;
font-size: 0;
overflow: auto;
text-align: center;
/*styling>>*/
background-color: #8F1C10;
}
.m::before {
content:'';
display: inline-block;
height: 100%;
vertical-align: middle;
}
.m>div {
display: inline-block;
font-size: 19px;
margin: 20px;
max-width: 320px;
min-height: 20px;
min-width: 300px;
padding: 14px;
vertical-align: middle;
/*styling*/
color:white;
background-color: #140A08;
outline:none;
}
</style>
</head>
<body>
<title>CSS centering Done Right.</title>
<div class="m">
<div contenteditable="true">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Corporis, enim, possimus, voluptates, quia voluptatibus voluptas eum quaerat iure aperiam asperiores debitis fuga itaque quibusdam a ad odio assumenda iusto voluptatem.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quidem, ex quia consequatur quae quasi amet veniam est quas error quos perferendis ducimus non similique in soluta magnam dolore molestias veritatis.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptate, doloremque iste magni in eveniet ipsa odio mollitia eligendi magnam placeat aliquam hic reprehenderit reiciendis itaque assumenda ratione delectus. Alias, quis.</div>
</div>
</body>
</html>
margin: auto; is what you need. Try this:
<style>
* {
margin: 0;
padding: 0;
}
/*html, body, .m {
height: 100%;
width: 100%;
}*/
.m {
opacity:1;
font-size: 0;
overflow: auto;
text-align: center;
margin: auto;
/*styling>>*/
background-color: #8F1C10;
}
.m::before {
content:'';
display: inline-block;
height: 100%;
vertical-align: middle;
}
.m>div {
display: inline-block;
font-size: 19px;
margin: 20px;
max-width: 320px;
min-height: 20px;
min-width: 300px;
padding: 14px;
vertical-align: middle;
/*styling*/
color:white;
background-color: #140A08;
outline:none;
}
</style>