I have tried float, display inline-block. Flex does work but I cant use flex as it will inline every single child div and I dont want that. I basically want few boxes in the left and one box on the right that auto size the height accordingly with the height size of the child divs from the left. I have tried floating boxChildRight to the right/left vice verse and its not working.
HTML
#sp {
position: absolute;
left: 400px;
top: 0;
}
#box {
width: 100%;
height: auto;
margin-top: 20px;
}
.boxChildRight {
right: 0;
width: 20%;
height: auto;
border: 1px solid;
position: relative;
float: right;
}
.boxChildLeft {
left: 0;
width: 80%;
height: 100px;
border: 1px solid;
margin-bottom: 2px;
position: relative;
float: left;
}
.prodInfo {
float: left;
margin-left: 10px;
}
.img {
margin-left: 0;
float: left;
}
<div id="box"> PARENT DIV
<div class="boxChildLeft"> CHILD
<div class="img">
<img src="../shopImages/candles.jpg" width="100px" height="100px">
</div>
<div class="prodInfo">
<p1>Test</p1><br>
<span id="sp"><p1>Test</p1></span>
</div>
</div>
<div class="boxChildLeft">
<div class="img">
<img src="../shopImages/candles.jpg" width="100px" height="100px">
</div>
<div class="prodInfo">
<p1>Test</p1><br>
<span id="sp"><p1>Test</p1></span>
</div>
</div>
<div class="boxChildLeft">
<div class="img">
<img src="../shopImages/candles.jpg" width="100px" height="100px">
</div>
<div class="prodInfo">
<p1>Test</p1><br>
<span id="sp"><p1>Test</p1></span>
</div>
</div>
<div class="boxChildLeft">
<div class="img">
<img src="../shopImages/candles.jpg" width="100px" height="100px">
</div>
<div class="prodInfo">
<p1>Test</p1><br>
<span id="sp"><p1>Test</p1></span>
</div>
</div>
<div class="boxChildRight"> THIS CHILD DIV SHOULD BE ON RIGHT SIDE OF ALL OTHER DIVS
</div>
</div>
You can use position:absolute;
*{
box-sizing:border-box; /* Added */
}
#sp {
position: absolute;
left: 400px;
top: 0;
}
#box {
width: 100%;
height: auto;
margin-top: 20px;
position:relative; /* Added */
padding-right:20%; /* Added */
float:left; /* Added */
}
.boxChildRight {
width: 20%;
height: 100%; /* Updated */
border: 1px solid;
position:absolute; /* Added */
right:0; /* Added */
top:0; /* Added */
}
.boxChildLeft {
left: 0;
width: 80%;
height: 100px;
border: 1px solid;
margin-bottom: 2px;
position: relative;
float: left;
}
.prodInfo {
float: left;
margin-left: 10px;
}
.img {
margin-left: 0;
float: left;
}
<div id="box"> PARENT DIV
<div class="boxChildLeft"> CHILD
<div class="img">
<img src="../shopImages/candles.jpg" width="100px" height="100px">
</div>
<div class="prodInfo">
<p1>Test</p1><br>
<span id="sp"><p1>Test</p1></span>
</div>
</div>
<div class="boxChildLeft">
<div class="img">
<img src="../shopImages/candles.jpg" width="100px" height="100px">
</div>
<div class="prodInfo">
<p1>Test</p1><br>
<span id="sp"><p1>Test</p1></span>
</div>
</div>
<div class="boxChildLeft">
<div class="img">
<img src="../shopImages/candles.jpg" width="100px" height="100px">
</div>
<div class="prodInfo">
<p1>Test</p1><br>
<span id="sp"><p1>Test</p1></span>
</div>
</div>
<div class="boxChildLeft">
<div class="img">
<img src="../shopImages/candles.jpg" width="100px" height="100px">
</div>
<div class="prodInfo">
<p1>Test</p1><br>
<span id="sp"><p1>Test</p1></span>
</div>
</div>
<div class="boxChildRight"> THIS CHILD DIV SHOULD BE ON RIGHT SIDE OF ALL OTHER DIVS
</div>
</div>
Updated on your comment
box-sizing
In CSS, by default, the width and height you assign to an element is
applied only to the element's content box. If the element has any
border or padding, this is then added to the width and height to
arrive at the size of the box that's rendered on the screen. This
means that when you set width and height you have to adjust the value
you give to allow for any border or padding that may be added. This is
especially tricky when implementing a responsive design.
MDN
Your problem is that although the divs are set to 80% and 20% - that does not include the border.
In order to include the border width within the overall width calculated for your elements, simply add:
* {
box-sizing: border-box;
}
for your CSS. This way, each element will take the border property in concideration when calculating the element's width.
I think using position is not a good solution we can do without using position absolute.
Agree with
* {
box-sizing: border-box;
}
This code is without using position absolute property:-
https://jsfiddle.net/oceh7f9q/
Related
I want to align the image to left, then its title then the text below it.
Here is the screenshot of what I want to make.
I have made DIV for each content. I dont know if its okay to do that.
I made it, because I ll have more control for individual content.
But I havent ben able to do so.
.howtocontainer {
height: 1985px;
width: 1121px;
background-image: url("//azlily.bex.jp/eccube_1/html/template/default/img/howto/background.png");
}
.firstsection {
/*background: rgb(255,255,255,0.3);*/
background: grey;
height: 200px;
position: relative;
top: 300px;
margin: 0 40px 0 40px ;
}
.firstpic {
padding-top: 20px;
}
.firstsecbanner {
float: right;
margin-right: 500px;
margin-top: -15px;
}
<div class ="howtocontainer">
<div class="firstsection">
<div class="firstpic">
<img src="https://images.pexels.com/photos/462118/pexels-photo-462118.jpeg?auto=compress&cs=tinysrgb&h=350">
</div>
<div class="firstsecbanner">
<img src="https://images.pexels.com/photos/462118/pexels-photo-462118.jpeg?auto=compress&cs=tinysrgb&h=350">
</div>
<div class="firstsectext">
お好みの量(目安はピンポン玉大です)を手に取って、パートナーの性感帯を指の腹や手のひらで優しくマッサージ<br>
してください。<br>
最初は背中や首筋、そして胸などと、段々と敏感な部分へ伸ばしていくと、ヌルヌルと滑る感覚が気持ちよく、エロ<br>
ティックな気分を高めることができます。<br><br>
性感帯は塗った部分が敏感になり、ただ触れるだけでも極上の気持ち良さ。<br>
シュチュエーションに合わせてラブローションの香りを変えたりしながら楽しみ方を<br>
見つけてください。
</div>
</div>
<div class="secondsection"></div>
<div class="thirdsection"></div>
</div>
All I did was Included image and text in one DIV
But gave a class to image by <img class="class" src"path" >
Then I did float:left to .img class.
There are 2 key points that you should notice about using float:
Float container should be set a specific width (absolute or relative width)
clear all floating items
You should change your HTML structure a little bit, and add some CSS styles:
.firstpic {
float: left;
width: 300px; /*this width is equal with its image's width */
}
.description {
float: left;
width: calc(100% - 300px);
}
/* Clear floating item */
.firstsection::after {
display: table;
content: "";
clear: both;
}
<div class="firstsection">
<div class="firstpic">
<img src="the-image-on-left-side">
</div>
<div class="description">
<div class="firstsecbanner">
<img src="the-title-image-on-top">
</div>
<div class="firstsectext">
お好みの量(目安はピンポン玉大です)を手に取って、パートナーの性感帯を指の腹や手のひらで優しくマッサージ<br>
してください。<br>
最初は背中や首筋、そして胸などと、段々と敏感な部分へ伸ばしていくと、ヌルヌルと滑る感覚が気持ちよく、エロ<br>
ティックな気分を高めることができます。<br><br>
性感帯は塗った部分が敏感になり、ただ触れるだけでも極上の気持ち良さ。<br>
シュチュエーションに合わせてラブローションの香りを変えたりしながら楽しみ方を<br>
見つけてください。
</div>
</div>
</div>
Please add absolute URL instead of relative URL to see your pictures.
Hope this helps.
A disadvantage of using floats is that it disturbs the natural document flow. You may want to consider an alternative using flexbox.
.firstsection {
display: flex;
}
.firstpic {
width: 300px;
/*this width is equal with its image's width */
}
.description {
width: calc(100% - 300px);
}
<div class="howtocontainer">
<div class="firstsection">
<div class="firstpic">
<img src="//azlily.bex.jp/eccube_1/html/template/default/img/howto/01.jpg">
</div>
<div class="description">
<div class="firstsecbanner">
<img src="//azlily.bex.jp/eccube_1/html/template/default/img/howto/firstsecbanner.png">
</div>
<div class="firstsectext">
お好みの量(目安はピンポン玉大です)を手に取って、パートナーの性感帯を指の腹や手のひらで優しくマッサージ<br> してください。
<br> 最初は背中や首筋、そして胸などと、段々と敏感な部分へ伸ばしていくと、ヌルヌルと滑る感覚が気持ちよく、エロ
<br> ティックな気分を高めることができます。
<br><br> 性感帯は塗った部分が敏感になり、ただ触れるだけでも極上の気持ち良さ。
<br> シュチュエーションに合わせてラブローションの香りを変えたりしながら楽しみ方を
<br> 見つけてください。
</div>
</div>
</div>
<div class="secondsection"></div>
<div class="thirdsection"></div>
</div>
I am trying to achieve facebook alike image cover as follow, but unable to achieve desire effect. This is the css that i have try. As you all can see, the div content does not position correctly.
.profile {
position: relative;
}
.profile-cover img{
width: 100%;
}
.profile-photo {
position: absolute;
left: 20px;
bottom: -60px;
}
.profile-info {
padding-right: 120px;
}
<div class="profile">
<div class="profile-cover">
<img src="http://dummyimage.com/1200x300/000/fff" />
</div>
<div class="profile-details">
<div class="profile-photo">
<img src="http://dummyimage.com/100x100/eee/000" />
</div>
<div class="profile-info">
Profile info here
</div>
</div>
</div>
<div>
Remaining content here
</div>
I am not exactly sure if you wanted this because you just say the "the div content does not position correctly", but I'm quiet sure it's that.
So, if not, tell me.
Using the absolute positioning gets out of the flow the element. So, the next ones continues as if this element didn't exist. That's why the others were showing under this absolute element.
For next time, please precise which DIV by using its id, a class name or anything that we can know for sure the one you are talking about.
.profile {
position: relative;
}
.profile-cover img{
width: 100%;
}
.profile-photo {
display: inline-block;
margin-top: -60px;
margin-left: 20px;
}
.profile-info {
display: inline-block;
padding-right: 120px;
}
<div class="profile">
<div class="profile-cover">
<img src="http://dummyimage.com/1200x300/000/fff" />
</div>
<div class="profile-details">
<div class="profile-photo">
<img src="http://dummyimage.com/100x100/eee/000" />
</div>
<div class="profile-info">
Profile info here
</div>
</div>
</div>
<div>
Remaining content here
</div>
This is how I would do it ...
.profile {
margin-bottom: 15px;
}
.profile-cover img {
width: 100%;
}
.profile-photo {
display: inline-block;
margin-left: 20px;
margin-top: -24px;
}
.profile-info {
display: inline-block;
position: relative;
top: -25px;
left: 10px;
}
<div class="profile">
<div class="profile-cover">
<img src="http://dummyimage.com/1200x300/000/fff" />
</div>
<div class="profile-details">
<div class="profile-photo">
<img src="http://dummyimage.com/100x100/eee/000" />
</div>
<div class="profile-info">
Profile info here<br>
More info here
</div>
</div>
</div>
<div>
Remaining content here
</div>
I have the following html code:
<div class="text-center">
<div class="row">
<div class="col-md-6">gdshdhfdhfdhfd</div>
<div class="col-md-6"><img src="http://image.com/img.png" alt="cat"></div>
</div>
<h4>header.</h4>
<p>hey there</p>
<div class="paddown">
<a class="btn btn-default" name="close" id="close" >Close</a>
</div>
</div>
And I would like to add any text (e.g. <span id="txt">lorem ipsum</span>)on the picture, let's say around 30-40px from the bottom, so it's visible in front of the cat's picture. Is it possible in bootstrap without using absolute positioning?
Here is my fiddle: http://jsfiddle.net/2wujw71x/3/
The general idea to this approach is to wrap the image in a containing element that will take on the size of the image itself. You also include the overlay element inside the wrapper element.
Apply position: relative; to the wrapper element and apply position: absolute; to the overlay element. The relative positioning on the wrapper element prevents the absolutely positioned element from being positioned outside of the wrapper element which happens to be the same size as our image. We now have a "viewport" for the overlay element that is the same size as the image and positioned over the image.
<div class="container">
<div class="row">
<div class="col-md-6">
<p>
Lorem Ipsum
</p>
</div>
<div class="col-md-6">
<span class="overlay-wrapper">
<img class="img-responsive" src="http://maxcdn.thedesigninspiration.com/wp-content/uploads/2012/01/Just-A-Cat-Playing-The-Violin-l.jpg" alt="Cat"/>
<div class="text-overlay">
Caption
</div>
</span>
</div>
</div>
</div>
.overlay-wrapper {
position: relative;
display: inline-block;
}
.text-overlay {
position: absolute;
width: 80%;
background: rgba( 0, 0, 0, .5 );
top: 75%;
left: 10%;
color: #fff;
text-align: center;
}
#media( min-width: 300px ) {
.text-overlay {
padding: 1em 0;
top: 65%;
}
}
#media( min-width: 550px ) {
.text-overlay {
padding: 2em 0;
}
}
http://jsfiddle.net/o2cjkyow/
I've included some media queries in my example so you can see a demonstration on how to adjust various properties at various viewport sizes for fine-tuning.
Here's my JSFiddle from the comments.
You can use the img-thumbnail class as a base with some minor adjustments, then set your text overlay rules in another class.
See working example in Snippet at Full Page.
html,
body {
margin-top: 50px;
}
.img-thumbnail.overlay {
position: relative;
border: none;
margin: 0;
padding: 0;
}
.img-thumbnail.overlay .caption {
position: absolute;
top: 45%;
left: 0;
margin: 0;
padding: 10px 15px 0 15px;
width: 100%;
color: #fff;
background: rgba(0, 0, 0, 0.3);
}
.img-thumbnail.overlay .caption2 {
position: absolute;
top: 45%;
left: 0;
margin: 0;
padding: 10px 15px 0 15px;
width: 100%;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div class="container text-center">
<div class="row">
<div class="col-md-6">
<div class="img-thumbnail overlay">
<img src="http://maxcdn.thedesigninspiration.com/wp-content/uploads/2012/01/Just-A-Cat-Playing-The-Violin-l.jpg" alt="" class="img-responsive">
<div class="caption">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
</div>
</div>
<div class="col-md-6">
<div class="img-thumbnail overlay">
<img src="http://maxcdn.thedesigninspiration.com/wp-content/uploads/2012/01/Just-A-Cat-Playing-The-Violin-l.jpg" alt="" class="img-responsive">
<div class="caption2">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
</div>
</div>
</div>
</div>
I suggest using relative positioning instead of absolute, that way your position is always relative to the image and its container.
.my-floating-text{
position: relative;
top: 110px;
left: 0px;
}
And in your html:
<div class="text-center">
<div class="row">
<div class="col-md-6"><div class="my-floating-text">gdshdhfdhfdhfd</div><img src="http://maxcdn.thedesigninspiration.com/wp-content/uploads/2012/01/Just-A-Cat-Playing-The-Violin-l.jpg" alt="cat" />
</div>
</div>
<h4>header.</h4>
<p>hey there</p>
<div class="paddown">
<a class="btn btn-default" name="close" id="close" >Close</a>
</div>
</div>
http://jsfiddle.net/2wujw71x/5/
You can try with position:absolute;
HTML:
<div class="text-center">
<div class="row">
<div class="col-md-6 aclass">
<div class="auto">
gdshdhfdhfdhfd
</div>
</div>
<div class="col-md-6">
<img src="http://maxcdn.thedesigninspiration.com/wp-content/uploads/2012/01/Just-A-Cat-Playing-The-Violin-l.jpg" alt="cat"></div>
</div>
<h4>header.</h4>
<p>hey there</p>
<div class="paddown">
<a class="btn btn-default" name="close" id="close" >Close</a>
</div>
</div>
CSS:
.aclass {
position: absolute;
left: 50%;
}
.auto {
position:relative;
top:50px; /*position to top*/
left:-50%;
z-index:9999;
color:#fff;
}
DEMO
The text always is center! :)
Just want to post an alternative approach, with figure and figcaption. The two elements aren't neccessary, but they do make it nice and semantic. The solution itself still exists in CSS. Using padding and an absolute bottom allows the container to scale with multiple lines of text.
HTML
<figure>
<img src="http://maxcdn.thedesigninspiration.com/wp-content/uploads/2012/01/Just-A-Cat-Playing-The-Violin-l.jpg" alt="cat">
<figcaption>This is cat, playing violin</figcaption>
</figure>
CSS
figure {
position: relative;
display: inline-block;
}
figcaption {
background: rgba(255,255,255,0.3);
box-sizing: border-box;
bottom: 0;
font-weight: bold;
width: 100%;
padding: 10px;
position: absolute;
z-index: 10;
}
http://jsfiddle.net/2wujw71x/10/
Description of Problem:
I'm attempting to arrange the kittens in a star-like pattern with 3 DIV "rows." I would like for the first top row's kitten to be centered on the page (easy enough); the second (or '#middle') row to have their cats left-aligned and right-aligned, respectively; and the third ('#bottom') row to have its cats aligned similar to the second row, but slightly indented on both sides. Again, like a star.
I know the float property essentially makes the element(s) absolutely positioned, which collapses the bottom two rows' height, so that's probably not the right answer. But I've also tried text-align and futzing with margins. My brain is fried. What am I doing wrong?
Fiddle:
http://jsfiddle.net/k97CG/1/
HTML Structure:
<div id="top">
<div id="container1" class="containers">
<div id="cat1">
<img src="http://placekitten.com/g/125/125" />
</div>
</div>
</div>
<div id="middle">
<div id="container2" class="containers">
<div id="cat2">
<img src="http://placekitten.com/g/125/125" />
</div>
</div>
<div id="container3" class="containers">
<div id="cat3">
<img src="http://placekitten.com/g/125/125" />
</div>
</div>
</div>
<div id="bottom">
<div id="container4" class="containers">
<div id="cat4">
<img src="http://placekitten.com/g/125/125" />
</div>
</div>
<div id="container5" class="containers">
<div id="cat5">
<img src="http://placekitten.com/g/125/125" />
</div>
</div>
</div>
CSS Structure:
.containers {
position: relative;
width: 125px;
height: 125px;
}
#top, #middle, #bottom {
position: relative;
width: 100%;
border: 1px solid red;
}
#container1 {
margin: 0 auto;
}
#container2 {
float: left;
}
#container3 {
float: right;
}
#container4 {
float: left;
}
#container5 {
float: right;
}
Is there a reason you can't just place them all in one div, then position them with CSS?
<div>
<img id="img01" src="img1">
<img id="img02" src="img1">
<img id="img03" src="img1">
<img id="img04" src="img1">
<img id="img05" src="img1">
</div>
then
div {
position:relative;
width:100%;
height:100%;
}
div img {
position:absolute;
}
#img01 {
top:x;
left:y;
} etc
As a rule, you shouldn't rely on HTML for visually styling content unless you have no other option. That's what CSS is for.
Is this the one you are looking for:
#top, #middle, #bottom {
position: relative;
width: 100%;
border: 1px solid red;
clear:both;
}
DEMO
I'm tring to create two horizontal bars like the below example using only html and css. I'm using the following code:
<div style="height: 150px;">
<div style="width: 55px;float:left;">
<div>340.8</div>
<div style="background-color:#95111d; height: 75px;">
</div>
</div>
<div style="width:55px;float:left">
<div>340.8</div>
<div style="background-color:#9e9e9e; height: 115px;">
</div>
</div>
<br style="clear: both" />
</div>
The problem with this is both bars are sitting at the top of their containing div and not at the bottom so you get the 2nd below images effect.
I have some code that will be generating the height of these bars so I can't just add top padding/margin to push them into position. Is there any way to do this without resorting to javascript to calculate a margin and bottom align them?
Here is the CSS and markup that should do the work (no absolute positioning used)-
DEMO
HTML:
<div id="wraper">
<div id="c1">
<div id="h1">340.8</div>
<div id="b1">
</div>
</div>
<div id="c2">
<div id="h2">340.8</div>
<div id="b2">
</div>
</div>
<br style="clear: both" />
</div>
CSS:
#wraper {
height: 150px;
}
#c1 {
width: 55px;
vertical-align: bottom;
display: inline-block;
}
#b1 {
background-color: #95111d;
height: 75px;
}
#c2 {
width: 55px;
margin-left: -4px;
display: inline-block;
}
#b2 {
background-color: #9e9e9e;
height: 115px;
}
DEMO
You can use absolute positioning to fix an element to the bottom of its container.
HTML:
<div class="container">
<div class="bar">
<div>
<div>340.8</div>
<div style="background-color:#95111d; height: 75px;"> </div>
</div>
</div>
<div class="bar">
<div>
<div>340.8</div>
<div style="background-color:#9e9e9e; height: 115px;"> </div>
</div>
</div>
<br style="clear: both" />
</div>
CSS:
.container {
height: 150px;
}
.bar {
width: 55px;
float: left;
position: relative;
height: 100%;
}
.bar > div {
position: absolute;
left: 0;
right: 0;
bottom: 0;
text-align: center;
}
Example: http://jsfiddle.net/grc4/pAnqe/1/
<html>
<body>
<div style="height: 150px;position:relative;" >
<div style="width: 55px;float:left;position:absolute;bottom:0;left:0px;">
<div style="background-color:#95111d; height: 75px;"> </div>
<div>340.8</div>
</div>
<div style="width:55px;float:left;position:absolute;bottom:0;left:55px;">
<div style="background-color:#9e9e9e; height: 115px;"> </div>
<div>340.8</div>
</div>
<br style="clear: both" />
</div>
</body>
</html>
jsFiddle
You can get the desired effect by using inline block elements and giving them a vertical-align value of bottom. here is some simple html and css.
html
<span class="bar" style="height:75px;background-color:#95111d;">
<div class="label">340.8</div>
</span>
<span class="bar" style="height:115px;background-color:#9e9e9e;">
<div class="label">350.1</div>
</span>
css
.bar {
display:inline-block;
width: 55px;
vertical-align:bottom;
}
.label {
position:relative;
top:-1em;
}