I have two div elements as below:
<div class="div1">
This is div 1
</div>
<div class="div2">
This is div 2
</div>
With css as:
.div1 {
border-style: solid;
float: left;
width: 50%;
}
.div2 {
border-style: solid;
margin-left: 50%;
padding-left: 10px;
}
I want to add a third div which has nothing but a symbol through css.
<div class="div3">
</div>
with css:
.div3:after {
font-family: "FontAwesome";
content: "\f100";
color: blue;
font-size: 1.5em;
}
For this to nicely position between the two original div's, I changed div 2's css to:
.div2 {
border-style: solid;
margin-left: 51%;
padding-left: 6px;
}
This looks ok on FullScreen. However, when I minimize the browser window to a smaller size, div2 starts overlapping div3.
How can I nicely position div3 between div1 and div2 with even space at both sides of div2?
Here's the JSFiddle for my try: https://jsfiddle.net/d8nuw2m3/5/
Edit:
The flex solution provided below by Roy works fine. However, assume that the content of left and right div's grows a lot. In which case, scroll bar will appear. What if in this case, I want to keep the position of the middle div fixed as I scroll down ?
Adding position: fixed to div3 css doesn't work.
Here's the fiddle: https://jsfiddle.net/zL8sc2a9/
Solution #1
You could use flex for this.
Wrap all div's in a container with display: flex:
<div class="flex-container">
<div class="div1">
This is div 1
</div>
<div class="div3">
</div>
<div class="div2">
This is div 2
</div>
</div>
.flex-container {
display: flex;
}
Let the div1 and div2 grow equally (remove widths, margins, paddings):
.div1 {
border-style: solid;
flex-grow: 1;
}
.div2 {
border-style: solid;
flex-grow: 1;
}
Set a fixed width to the div3:
.div3 {
width: 18px;
}
That's it! Flex will do the trick for you: https://jsfiddle.net/1amuwfj5/
Solution #2
Use calc() to subtract div3 width from div1 and div2. General idea:
.div1 {
border-style: solid;
float: left;
width: calc(50% - 15px); /* we subtract 9x (half of div3) and 6px (border width) */
}
.div2 {
border-style: solid;
float: right;
width: calc(50% - 15px);
}
.div3 {
width: 18px;
}
Example: https://jsfiddle.net/1amuwfj5/1/
You can achieve this adding a div around it and using display: flex;. Floating elements is really the old way to do this.
Edit
I updated the snippet to have the .div3:after fixed with scrolling.
.page {
background-color: #eee;
height: 150vh;
}
.wrapper {
display: flex;
justify-content: space-between;
}
.div1 {
border-style: solid;
width: 50%;
margin-right: .5em;
}
.div2 {
border-style: solid;
width: 50%;
margin-left: 1.5em;
}
.div3:after {
position: fixed;
font-family: "FontAwesome";
content: "\f100";
color: blue;
font-size: 1.5em;
}
<div class="page">
<div class="wrapper">
<div class="div1">
This is div 1
</div>
<div class="div3">
</div>
<div class="div2">
This is div 2
</div>
</div>
</div>
Related
I have this table-style DIV code for a used vehicle sales platform:
.mainwrapper {
border: 2px solid;
display: table;
}
.itemwrapper {
display: table-row;
width: 706px;
}
.mainwrapper {
margin-bottom: 20px;
}
.item {
width: 700px;
border: 1px solid;
padding: 1em;
background-color: blue;
color: white;
}
.item:nth-child(2) {
float: left;
margin: -2px;
}
.item1 {
display: table-cell;
text-align: left;
margin-left: -30px;
}
.item1 p {
margin-top: -30px;
}
.item-price {
width: 300px;
background-color: blue;
padding: 1em;
color: white;
text-align: center;
}
.picture, .item {
display: table-cell;
border: 1px solid;
}
.picture {
width: 90px;
margin: 1px;
border: 2px solid;
}
.picture img {
height: 185px;
}
<div class="mainwrapper">
<div class="itemwrapper">
<div class="item">1992 ELDDIS PAMPEROS XLi</div>
<div class="item-price">£1,000</div>
</div>
<div class="itemwrapper">
<div class="picture"><img src="https://complianz.io/wp-content/uploads/2019/03/placeholder-300x202.jpg.webp"></div>
<div class="item1"><p>2 berth, good condition</p></div>
</div>
</div>
<div class="mainwrapper">
<div class="itemwrapper">
<div class="item">2008 SWIFT CHALLENGER 540</div>
<div class="item-price">£13,000</div>
</div>
<div class="itemwrapper">
<div class="picture"><img src="https://complianz.io/wp-content/uploads/2019/03/placeholder-300x202.jpg.webp"></div>
<div class="item1"><p>4 berth end bedroom</p></div>
</div>
</div>
What I am trying to do is ensure the class item1 is opposite the image, with the text like this if you didn't have the £ per month div and list as table:
Basically, what I am trying to fix is the text that's in class item1 opposite the image (not with all the description or colored DIV there); see the image below.
I tried margin-left and margin-top, but it won't quite put the image opposite.
This is the result of my code:
I can't quite get it to work as I'd expected, text opposite image and size of DIV in the CSS; if anyone can help, I'd much welcome this.
It works OK - no major coding errors, but isn't quite esthetically working out, and that's the basic problem.
I'm trying this as basic HTML first before attempting anything with Javascript, just to ensure it works as a standalone design.
Edit: I tried vertical-align for text, that worked, but it's fixing the gap between image div and text that's the issue. There's a large amount of space I don't know how to fix.
As the answer for the text is solved. You can change the column width by changing the css property of item. you can do it as follows. The width was 700px in your code you can reduce to get a smaller width. I changed it to 400px.
.item {
width: 400px;
border: 1px solid;
padding: 1em;
background-color: blue;
color: white;
}
I have a questions about centering multiple absolute div horizontally. I have a parent div that i s relative that contain four child divs. I want them to center evenly. Is there a way to center the four divs then add once? Or must I position them one by one? What I have now is when I position the divs absolute. They stack on top of each other, when they are centered.
Thank you for your time.
You can define a div for your sub divs and use display: flex.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
boxes {
margin-top: 25px;
width: 100%;
position: relative;
text-align: center;
}
.eq {
display: flex;
}
.box {
width: 300px;
max-height: 400px;
min-height: 233px;
padding: 15px;
border: 1px solid #e6e6e6;
overflow-wrap: break-word;
background-color: #fff;
margin-right: 20px;
;
}
<div class="boxes">
<div class="box">
<h3>MyBMW Login</h3>
<form class="" action="index.html" method="post">
<input type="text" name="email" placeholder="Emailadres">
<input type="password" name="password" placeholder="Wachtwoord">
</form>
</div>
<div class="eq">
<div class="box">
1
</div>
<div class="box">
2
</div>
<div class="box">
3
</div>
</div>
</div>
However I had to delete position: absolute. If it's not what you want, please give us more details.
Change your css to this, worked for me.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.boxes {
position: absolute;
background-color: #000;
display: flex;
margin-top: 25px;
width: 100%;
text-align: center;
}
.box {
position: relative;
width: 300px;
max-height: 400px;
min-height: 233px;
padding: 15px;
border: 1px solid #e6e6e6;
overflow-wrap: break-word;
background-color: #fff;
margin-right: 20px;
}
i wouldn't make this as absolute, these .box divs need to be relative, you can make the .boxes a "position absolute".
https://jsfiddle.net/DamianToczek/3w50gj28/4/
Make the .boxes absolute, the ID .box cant be absolute you always make a container that is absolute. If you want to make .box absolute, you need to give them another id like .box1 .box2 .box3, the best option is always a container that holds the "centered" items, the container is .boxes for you:
.boxes {
width:100%;
position:absolute;
}
I'm trying to have 3 left aligned floats. Here's the following JFiddle that does it.
div { height: 45px; }
div.side {
background-color: skyblue;
width: 72px;
float: left;
}
div#range {
background-color: tomato;
width: 216px;
float: left;
}
span {
width:100%;
text-align: center;
display: block;
margin: 3px auto;
/* background-color: gold; */
}
<div id="start" class="side">
<button type="button">Click Me!</button>
</div>
<div id="range"></div>
<div id="end" class="side">
<button type="button">Click Me!</button>
</div>
What I'm trying to do now is center align the buttons within the div called side. I've tried using another div with it's text-align set to center, but that doesn't work.
The text-align: center applied to .side divs should do the trick, because button elements are inlines by default. Note, I changed .side's widths to make the centering effect noticeable: https://jsfiddle.net/021gu79c/1/.
CSS:
div.side {
background-color: skyblue;
width: 120px;
float: left;
text-align: center;
line-height: 45px;
}
I guess he means horizontal, based on the text-align property.
Hav you tried to add a wrapper div around the Buttons with
.btn-wrapper{
position: absolute;
left:40%;
right:40%;
Maybee that's working for you
I'm trying to align three divs horizontally but facing some problems.
I'm doing this to build some modals.
Thanks!
html code
<div id="modal-wrapper">
<div class="modal-body"></div>
<div class="modal-body-2"></div>
<div class="modal-body-3"></div>
</div>
css code
.modal-body{
float:left;
width: 100%;
}
.modal-body-2{
margin-left: 100%;
width:100%;
padding: 15px;
}
.modal-body-3{
margin-left: 200%;
width:100%;
padding: 15px;
}
#modal-wrapper{
display: inline-block;
width: 100%;
margin: 0 auto;
}
Here is a JSFiddle I made: http://jsfiddle.net/5m1n8p0q/1/. You're spacing each element the entire width of a page. With a 3 div layout, you want just 1/3 or 33% of the width. I placed a padding-right property of 0.5px to mitigate the gap between a 33% and 33.33...% width.
HTML:
<div class="modal-body">asdf</div>
<div class="modal-body-2">asdf</div>
<div class="modal-body-3">asdf</div>
CSS
.modal-body{
float:left;
width: 33.3%;
background: red;
}
.modal-body-2{
display: inline-block;
width: 33.3%;
background: blue;
}
.modal-body-3{
float: right;
width: 33.3%;
background: green;
padding-right: 0.5px;
}
I am battling with the positioned element that I want to be able to resize.
I have an element that has a position absolute.
Inside I am making three divs, .left, .center, .right.
I want to be able to resize element dragging on .left, .right, which will shrink the element down.
(I do not include JS to dragging, it is irrelevant so far - you can just increase/decrease width of an element in a console).
Question:
My problem is that elements don't seem to align inside the div - content of .center and .right are pushed down. If I make them float, on resizing the element same behavior appears.
And because I am going to resize the .holder element width, I am not in control of the width of .center element, so technically it should just shrink to fit to width minus width of .left and .right.
I made this jsfiddle for the question.
Edit: updated, merged .holder and .second into .second
Edit: I didn't make that clear, but height cannot grow above 60 pixels.
Edit: The text in center can be hidden, if element is super small in width, I don't care for the text.
Edit: thank you everyone who participated! everyone was close with the table positioning. i didn't use it before.
Selecting this: jsfiddle.net/abhitalks/c5L6tLt0/25 and http://jsfiddle.net/abhitalks/1o3vbhhp/ fiddles as what I needed, I am still further to find how to shrink it to just .left and .right and keep height to 60pixels, but these 2 are very close to what I was looking for.
Thank you everyone again!
html:
<body>
<div class="first">
<div class="second">
<div class="left"></div>
<div class="center">Should be center</div>
<div class="right"></div>
</div>
</div>
</body>
css:
.first {
position: relative;
border: solid 1px green;
width: 600px;
height: 600px;
}
.second {
position: absolute;
border: solid 1px red;
left: 100px;
top: 10px;
}
.left, .right, .center {
height: 60px;
}
.left, .right {
min-width: 1px;
width: 5px;
max-width: 5px;
background-color: skyblue;
}
You could make use of display: table on container and display: table-cell on .left, .center, .right divs to fit.
Giving a width:100% to .center will cause to stretch to available space. Changing widths is then easy.
In the example snippet below, you can try changing width by clicking .left (to reduce) or .right (to increase).
Your updated fiddle: http://jsfiddle.net/abhitalks/c5L6tLt0/22/
Code Snippet:
$(".right").on("click", function () {
$(this).parent().css("width", "+=10px");
});
$(".left").on("click", function () {
$(this).parent().css("width", "-=10px");
});
.first {
position: relative;
border: solid 1px green;
width: 400px; height: 240px;
}
.second {
position: absolute;
border: solid 1px red;
left: 100px; top: 10px;
width: 120px; height: 60px;
display: table; transition: all 250ms;
}
.left, .right, .center {
display: table-cell;
height: 100%;
overflow: hidden; text-align: center; vertical-align: middle;
word-wrap: break-word; word-break: break-all;
}
.left, .right {
min-width: 5px; width: 5px; max-width: 5px;
background-color: skyblue; cursor: pointer;
}
.center { width: 100%; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div class="first">
<div class="second holder">
<div class="left" title="Click to shrink"></div>
<div class="center">Center</div>
<div class="right" title="Click to grow"></div>
</div>
</div>
</body>
If you want to remove height restrictions and make it grow or shrink as per contents, then simply remove the height from all divs.
See this fiddle: http://jsfiddle.net/abhitalks/1o3vbhhp/
If you want contents to be hidden or scrolled when the div width is changed, then table-cell won't allow that. You will have to wrap an inner div to do that.
Fiddle with inner div scroll: http://jsfiddle.net/abhitalks/c5L6tLt0/23/
Fiddle with contents hidden and height fixed: http://jsfiddle.net/abhitalks/c5L6tLt0/24/
Or this, if you are happy with word breaks to allow smaller size upto 1 character:
Fiddle with word break: http://jsfiddle.net/abhitalks/c5L6tLt0/25/
.
Remove the following section from your CSS
.holder {
width: 100px;
height: 60px;
}
Why are you applying two classes on same div anyway ?
UPDATE
here is the updated code that might work for you. Treat your Div as Table. Its very lightweight and powerful.
.first {
display: table;
border: solid 1px green;
}
.second {
display: table-row;
border: solid 1px red;
left: 100px;
top: 10px;
}
.left, .right, .center {
display:table-cell;
}
.left, .right {
min-width: 1px;
width: 5px;
max-width: 5px;
background-color: skyblue;
}
Do you want them displayed as table cells?
.holder {
width: 300px;
height: 90px;
display: table;
}
.left, .right, .center {
display: table-cell;
vertical-align: middle;
text-align: center;
}
http://jsfiddle.net/c5L6tLt0/20/
Remove the following .holder CSS as #Adnan said
and add the following
.left .right .center {
max-width: 200px;
overflow: auto;
}
overflow property will add scrollbar when your content exceeds 200 x 60 dimension.