Right div on float:right appears a line below the left one - css

Here's my problem.
I have a wrapper div(width: 800px and height: 250px) which contains two divs occupying all the space in height and dividing their width in half.
I set up my css, float the right div to float: right and this one appears where it should but "below" the other one, exceeding the wrapper div space(which shouldn't even be possibile).
I'm posting both the jdfiddle and the code.
JS Fiddle http://jsfiddle.net/FV9yC/
HTML
<div id="wrapper">
<!-- left div -->
<div id="leftDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<!-- right div -->
<div id="rightDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
</div>
CSS
#wrapper {
background-color: grey;
height: 200px;
width: 500px; }
#leftDiv {
background-color: purple;
height: 200px;
width: 250px; }
#rightDiv {
background-color: green;
float: right;
height: 250px;
width: 250px; }

Just shift the div with ID rightDiv above the div with ID leftDiv. That's it.
Here is the WORKING SOLUTION
The Code:
<div id="wrapper">
<!-- right div -->
<div id="rightDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<!-- left div -->
<div id="leftDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
</div>

http://jsfiddle.net/FV9yC/1/
You should add float: left to your left div.

Add float: left to the other div. Also you can use float: left for both; unless you have other reasons than just positioning there to use it.

Used to this code
#leftDiv{float:left;}
#wrapper:after{
content:"";
clear:both;
display:table;
}
#wrapper {
height:200px; // remove this line
}
Demo

try this
http://jsfiddle.net/FV9yC/5/
#wrapper {
background-color: grey;
height: 200px;
width: 500px;
}
#leftDiv {
background-color: purple;
height: 200px;
width: 250px;
float:left;
}
#rightDiv {
background-color: green;
float: right;
height: 250px;
width: 250px; }

You don't need to float your div to the right — you just need to align each block beside the other, and you can do this using float: left;.
I made a consistent solution for you. See below:
Using a class to remove the DRY of your code, I grouped your blocks into a common class with common behaviors.
See your new CSS:
#wrapper {
background-color: grey;
height: 200px;
width: 500px; }
.block {
float: left;
width: 250px;
}
#leftDiv {
background-color: purple;
height: 200px; }
#rightDiv {
background-color: green;
height: 250px; }
And your new HTML:
<div id="wrapper">
<!-- left div -->
<div class="block" id="leftDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
<!-- right div -->
<div class="block" id="rightDiv">
<h1>This is my heading</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
</div>
</div>
Using classes to execute common behaviors it is a good practice to avoid redundancy and future problems.
To see your code working on jsFiddle, just click here.

Related

Can I have a responsive image with a set height?

I working in a page builder
For a shop, I am creating. I can change the CSS which is great.
I’m struggling to get a responsive resize of the images in this 4 column row. Since the images are different heights I have to have to set a height and have responsive width. Is there any way to get it to scale correctly?
The width is auto and the height is a set height based on the size of the screen.
You can see that when I resize it separates from the box and then sometimes get squished.
object-fit property
I did your design by using display : flex; and object-fit : cover; properties. I think that this object-fit property directly on the image is the only lacking property to make your images still looking good despite the screen resolution.
Notice the use of object-position : center; which makes the resizing always axed on the center of the image.
index.html
<div class="foo">
<div class="bar">
<img src="https://picsum.photos/200/300" alt="">
<div>
<h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
<p>$42</p>
</div>
</div>
<div class="bar">
<img src="https://picsum.photos/500/200" alt="">
<div>
<h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
<p>$42</p>
</div>
</div>
<div class="bar">
<img src="https://picsum.photos/200/700" alt="">
<div>
<h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
<p>$42</p>
</div>
</div>
<div class="bar">
<img src="https://picsum.photos/500/400" alt="">
<div>
<h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
<p>$42</p>
</div>
</div>
</div>
style.css
body {
background-color: #f7f7f7;
font-family: Arial, Helvetica, sans-serif;
}
.foo {
width: 100%;
display: flex;
gap: 20px;
}
.bar {
background-color: #ffffff;
display: flex;
flex-direction: column;
width: 100%;
border-radius: 15px;
overflow: hidden;
box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
}
.bar > img {
width: 100%;
height: 200px;
object-fit: cover;
object-position: center;
}
h4 {
color:#9ccf74;
}
.bar > div {
padding: 10px;
height: 100px;
}

How can I display 2 items in a flex container and keep set width and height of a flex item?

*I have added padding inside the divs so you're able to see the boxes
I need to display these 2 items next to each other on desktop screen sizes.
I the purple bordered box which contains the 2 elements is set to display flex.
The circle div sqaushes up.
I have set is to a height: 200px width: 200px - this is fine before I set the parent to display flex.
How can I make sure that the circle stays at the set width and height and the rest of the content in the red box resizes - rather than the other way around?
If you could also please explain like I'm 5 why this is happening that would be really appreciated.
.card__inner {
display: flex;
}
.news__feature-image {
border-radius: 100%;
width: 200px;
height: 200px;
background: red;
}
<article class="card">
<div class="card__inner">
<div class="news__feature-image"></div>
<div class="card__content">
<header class="news__header">
<span class="new__post-date">
18 Sep
</span>
<a class="card__link" href="/">
Read More
</a>
</header>
<h2 class="news__title">Lorem ipsum title this is a title etc Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed turpis est, eleifend.</h2>
</div>
</div>
</article>
I have tried using flex grow and flex shrink on the feature image and the content.
I have added settings for the flex items that contains the circle. By default the setting for flex-shrink is 1, which allows it to shrink if necessary. Setting it to zero ensures the circle is displayed as desired.
* {
box-sizing: border-box;
}
.container {
border: thin solid green;
padding: 1rem;
}
.inner {
border: thin solid purple;
display: flex;
padding: 1rem;
}
.circle {
width: 200px;
height: 200px;
background-color: red;
border-radius: 100%;
flex-shrink: 0;
}
<div class="container">
<div class="inner">
<div class="circle"></div>
<div class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
</div>
</div>

How to make an entire div clickable (table style divs)

Yes, I know, this question have been asked many times and a possible solution is to add style="display:block;" to the link.
For some reason this solution does not work with table style DIVs:
https://jsfiddle.net/exyv8jmw/1/
HTML:
<div class="table">
<div class="tablerow">
<div class="left">
<a href="/something.html" style="display:block">
This is a link</a>
</div>
<div class="right">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
</div>
</div>
CSS:
.table{
width:500px;
display:table;
}
.tablerow{
display:table-row;
}
.left{
width:50%;
background:green;
display:table-cell;
padding:5px;
}
.right{
width:50%;
display:table-cell;
background:red;
padding:5px;
}
As you can see, the empty green space is clickable only horizontally, but not vertically. I also tried:
<div class="left">This is a link</div>
but it does not help.
You need to add height: 100%; to the link, .left, and .tablerow elements.
.table{
width:500px;
display:table;
}
.tablerow{
display:table-row;
height: 100%;
}
.left{
width:50%;
background:green;
display:table-cell;
padding:5px;
height: 100%;
}
.right{
width:50%;
display:table-cell;
background:red;
padding:5px;
}
<div class="table">
<div class="tablerow">
<div class="left">
<a href="/something.html" style="display:block;height:100%;">
This is a link</a>
</div>
<div class="right">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
</div>
</div>
First set .left position to relative, Then set a tag position to absolute and width and height to 100%.
.table{
width:500px;
display:table;
}
.tablerow{
display:table-row;
}
.left{
width:50%;
background:green;
display:table-cell;
padding:5px;
position: relative;
}
.left a {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.right{
width:50%;
display:table-cell;
background:red;
padding:5px;
}
<div class="table">
<div class="tablerow">
<div class="left">
<a href="/something.html" style="display:block">
This is a link</a>
</div>
<div class="right">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
</div>
</div>
Can't you put the div inside the 'a' element?
<a href="/something.html" >
<div class="left">
This is a link </div>
</a>
Apply the 'left' class to the a element directly, i.e.:
this is a link
Although this does not allow for additional content, it does make the anchor fill the available space.
Part of the problem is that the anchor tag is, by default, a "span" type tag, which only fills a space as big as its content, disregarding internal divs.
You have to make the anchor tag act like a "block" style element, not its surrounding element, or an internal element.
You could add listener to the div id.
<div class="table" id="clik">
document.getElementById("clik").addEventListener("click", function(){
// document.location = "/something.html";
alert("hello");
});

Align middle 2 elements in Boostrap 3?

This is what I want to do with BS3:
My beautiful schema :)
And this is my start code:
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="well well-sm">
<i class="glyphicon glyphicon-th-large pull-left"></i>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Atque molestias nihil quia!</p>
</div>
</div>
</div>
</div>
JsFiddle URL - http://jsfiddle.net/AJAhR/54/
I tried with line-height but it must be responsive so it doesn't work.
Someone can explain me how to do that with CSS/Boostrap3.
Try something with position absolute
.well.well-sm p { margin-left: 60px; /* with of the icons + icon's padding right */ }
.well.well-sm { position: relative; }
.well.well-sm i { position: absolute; top: 50%; left: 0; margin-top: -50px; /* 50% of the icon's height */ }
Live: http://jsfiddle.net/AJAhR/55/

Two inline blocks: place text next to an image

I have problem with 2 inline blocks. I want create something like this:
<img>
<span>some long text next to the img</span>
I have following structure (which I have to use):
<div class="mainContainer">
<div class="additional">
<div class="description">
<img></img>
</div>
<div class="description">
Lorem ipsum dolor sit amet, consectetur adipisicing elit...
</div>
</div>
</div>
Styles:
.mainContainer {
height: 15px; } //doesn't matter in this case
.additional {
line-height: 15px; } //doesn't matter in this case
.description {
float: left;
display: inline;
}
The problem is when I want add long text, then image is above the text, but it should be next to it.
It should be something like this (I add on code but it has to be removed):
http://jsfiddle.net/476fm/
Any suggestions?
EDIT:
Ok actually the text above was only example and to be more specific what I want achieve:
I have one main container which contains 2 inline elements:
- first element have image
- second have image too and the text
and what I wanna do is: when the text is long and need to be in second line it shouldn't be underneath the first image
http://jsfiddle.net/z2t7b/ - it should be fixed
(I hope that somebody understood what I wanna do )
Is this what you are trying to accomplish?
<div class="mainContainer">
<div class="additional">
<div class="description">
<img></img> <div class="description">Lorem ipsum dolor sit amet, consectetur adipisicing elit,</br> sed do eiusmod tempor incididunt ut labore</div>
<div class="breaker"></div>
<img></img><div class="description">Lorem ipsum dolor sit amet, consectetur adipisicing elit,</br> sed do eiusmod tempor incididunt ut labore</div>
</div>
</div>
</div>
.mainContainer {
height: 23px;
}
.additional{
line-height: 23px;
}
.description {
float:left;
display: inline;
}
.breaker {clear: both;}
img {
background-color:#FFF;
width:20px;
height:20px;
display:inline-block;
border:solid black 1px;
float:left;
}
Here is the fiddle
Well, for INLINE elements, just remove the 'float:left' .. Natural inline flow will apply, THEN, your text will pass under normally.
OK, now i understook your quest (i hobe)
just make your container behave has a block, float it left and add overflow: hidden;
http://jsfiddle.net/NgfSF/
.description {
padding-right:10px;
float:left;
display: inline;
}
.description1 {
display: block;
overflow: hidden;
}

Resources