I'm trying to force a div to fit the text inside. No matter what I have tried, there seems to be extra white space. Here is my code:
body {
background-color: #000;
}
.holding {
width: 500px;
background-color: red;
height: 500px;
padding: 40px 100px;
max-width: 400px;
width: fit-content;
}
.childone {
background-color: white;
display: inline;
padding: 20px;
float: left;
clear: both;
}
.clear {
clear: both;
}
.childtwo {
background-color: white;
display: inline-block;
float: left;
padding: 10px;
}
<body>
<div class="holding">
<div class="childone">This is the Title and I really the div to end here with.no.extra.white.space </div>
<div class="clear"></div>
<div class="childtwo">This is the next child div with a bund of stuff.</div>
</div>
</body>
Use word-break: break-all, like:
.childone {
word-break: break-all;
}
Have a look at the snippet below:
body {
background-color: #000;
}
.holding {
width: 500px;
background-color: red;
height: 500px;
padding: 40px 100px;
max-width: 400px;
width: fit-content;
}
.childone {
background-color: white;
display: inline;
padding: 20px;
float: left;
clear: both;
word-break: break-all;
}
.clear {
clear: both;
}
.childtwo {
background-color: white;
display: inline-block;
float: left;
padding: 10px;
}
<body>
<div class="holding">
<div class="childone">This is the Title and I really the div to end here with.no.extra.white.space </div>
<div class="clear"></div>
<div class="childtwo">This is the next child div with a bund of stuff.</div>
</div>
</body>
Hope this helps!
div { width: max-content; }
Support browsers: https://caniuse.com/#search=max-content
Reference: https://developer.mozilla.org/en-US/docs/Web/CSS/max-width
Or you can use this way (change display attribute):
div { display: table; }
Related
I have two boxes each one is an inline-block i want to put two inline-block boxes underneath each other , like the following image
here's the code i used
.box1
{
background-color: red;
display: inline-block;
width: 300px;
float: right;
height: 31%;
}
.box2
{
background-color: green;
display: inline-block;
width: 324px;
float: right;
height: 31%;
}
<body>
<div class="box1">Box1</div>
<div class="box2">Box2</div>
</body>
In the second box clear your float. Just add clear: both.
Code Snippet:
.box1 {
background-color: red;
display: inline-block;
width: 300px;
float: right;
height: 31%;
}
.box2 {
clear: both;
background-color: green;
display: inline-block;
width: 324px;
float: right;
height: 31%;
}
<body>
<div class="box1">Box1</div>
<div class="box2">Box2</div>
</body>
You could wrap both your divs in a container and float that instead.
example
.container {
float: right;
}
.box1 {
background-color: red;
width: 300px;
height: 31%;
}
.box2 {
background-color: green;
width: 324px;
height: 31%;
}
<body>
<div class="container">
<div class="box1">Box1</div>
<div class="box2">Box2</div>
</div>
</body>
Seems like you could just add a "clear: both;" to your second div and otherwise leave your code as-is.
.box1
{
background-color: red;
display: inline-block;
width: 300px;
float: right;
height: 31%;
}
.box2
{
background-color: green;
display: inline-block;
width: 324px;
float: right;
height: 31%;
clear: both;
}
<body>
<div class="box1">Box1</div>
<div class="box2">Box2</div>
</body>
I have two floated divs, after them I have a block element with margin-top. Unfortunately the margin-top doesn't work because of the float. Is it possible to add margin-top without extra markup in the code?
I tried with :after, but didn't help.
div {
background-color: red;
}
#left {
float: left;
height: 100px;
background-color: yellow;
}
#right {
float: right;
height: 100px;
background-color: yellow;
}
#content {
margin-top: 50px;
height: 100px;
clear: both;
}
#content:before {
clear: both;
content: "";
display: table;
}
<div id="left">left</div>
<div id="right">left</div>
<div id="content">left</div>
div {
background-color: red;
}
#left {
float: left;
height: 100px;
background-color: yellow;
}
#right {
float: right;
height: 100px;
background-color: yellow;
}
#content {
margin-top: 50px;
height: 100px;
clear: both;
}
#content:before {
clear: both;
content: "";
display: table;
}
<div id="left">left</div>
<div id="right">left</div>
<div id="content">left</div>
Oh, those collapsing margins...
If you change the top margin on the last div to a bottom margin on the top two divs, it works as expected.
div {
background-color: red;
}
#left {
float: left;
height: 100px;
background-color: yellow;
margin-bottom: 50px;
}
#right {
float: right;
height: 100px;
background-color: yellow;
margin-bottom: 50px;
}
#content {
height: 100px;
clear: both;
}
<div id="left">left</div>
<div id="right">right</div>
<div id="content">content</div>
I need little help for my CSS.
I am trying to make a comment system but it has something went wrong.
This is my DEMO page from codepen.io
You can see there is a user avatar and textarea. The container max-width:650px; when you reduced width the browser the it is automatically changing.
anyone can help me in this regard?
HTML
<div class="container">
<div class="comment">
<div class="commenter">
<img src="https://igcdn-photos-c-a.akamaihd.net/hphotos-ak-xta1/t51.2885-19/11084950_1591056347778266_1536322251_a.jpg">
</div>
<div class="comment-text-area">
<textarea class="textinput" placeholder="Comment"></textarea>
</div>
</div>
</div>
CSS
body {
background-color: #dddddd;
}
.container {
position: relative;
max-width: 650px;
margin: 0px auto;
margin-top: 50px;
}
.comment {
background-color: blue;
float: left;
width: 100%;
height: auto;
}
.commenter {
float: left;
}
.commenter img {
width: 35px;
height: 35px;
}
.comment-text-area {
float: left;
width:100%;
height: auto;
background-color: red;
}
.textinput {
float:left;
width: 100%;
min-height: 35px;
outline: none;
resize: none;
border: 1px solid #f0f0f0;
}
I want to make it like this:
You could try using calc(); to perform the calculation for you... baring in mind you would need to add the vendor prefixes to this.
body {
background-color: #dddddd;
}
.container {
position: relative;
max-width: 650px;
margin: 0px auto;
margin-top: 50px;
}
.comment {
background-color: blue;
float: left;
width: 100%;
height: auto;
}
.commenter {
float: left;
}
.commenter img {
width: 35px;
height: 35px;
}
.comment-text-area {
float: right;
width: calc(100% - 45px);
height: auto;
background-color: red;
}
.textinput {
float:left;
width: 100%;
min-height: 35px;
outline: none;
resize: none;
border: 1px solid #f0f0f0;
}
<div class="container">
<div class="comment">
<div class="commenter">
<img src="https://igcdn-photos-c-a.akamaihd.net/hphotos-ak-xta1/t51.2885-19/11084950_1591056347778266_1536322251_a.jpg">
</div>
<div class="comment-text-area">
<textarea class="textinput" placeholder="Comment"></textarea>
</div>
</div>
</div>
as an option instead of float use display: table
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
background-color: #dddddd;
}
.container {
position: relative;
max-width: 650px;
margin: 0px auto;
margin-top: 50px;
}
.comment {
background-color: #00f;
height: auto;
display: table;
width: 100%;
padding: 5px;
}
.commenter,
.comment-text-area{
display: table-cell;
vertical-align: top;
}
.commenter{
width: 35px;
padding-right: 5px;
}
.commenter img {
width: 35px;
height: 35px;
}
.comment-text-area {
width:100%;
height: 100%;
/*background-color: red;*/
}
.textinput {
width: 100%;
min-height: 35px;
outline: none;
resize: none;
border: 1px solid #f0f0f0;
}
<div class="container">
<div class="comment">
<div class="commenter">
<img src="https://igcdn-photos-c-a.akamaihd.net/hphotos-ak-xta1/t51.2885-19/11084950_1591056347778266_1536322251_a.jpg">
</div>
<div class="comment-text-area">
<textarea class="textinput" placeholder="Comment"></textarea>
</div>
</div>
</div>
For scenarios like this I combine float:left;and float:none; The avatar wrapper div gets the float:left; and the comment wrapper div gets the float:none;.
The trick here is to put padding-left on the float:none; div equal to the width of the float:left; div.
.comment {
background-color: blue;
float: left;
width: 100%;
height: auto;
}
.commenter {
float: left;
width:35px;
}
.commenter img {
width: 35px;
height: 35px;
}
.comment-text-area {
float: none;
height: auto;
background-color: red;
padding-left:35px;
}
Here is a working demo
See this fiddle
I have changed your CSS a little bit. See the changes below. The problem with your CSS was that you used no width for .commenter. Thus it took default 100% width.
CSS
.commenter {
float: left;
width: 6%;
}
.comment-text-area {
float: left;
width: 94%;
height: auto;
background-color: red;
}
EDIT
use width for .commenter as width: 35px;..I chose 35px because it is the width of the avatar image.
only change .comment-text-area height:94%
body {
background-color: #dddddd;
}
.container {
position: relative;
max-width: 650px;
margin: 0px auto;
margin-top: 50px;
}
.comment {
background-color: blue;
float: left;
width: 100%;
height: auto;
}
.commenter {
float: left;
}
.commenter img {
width: 35px;
height: 35px;
}
.comment-text-area {
float: left;
width: 94%;
height: auto;
background-color: red;
}
.textinput {
float:left;
width: 100%;
min-height: 35px;
outline: none;
resize: none;
border: 1px solid #f0f0f0;
}
<div class="container">
<div class="comment">
<div class="commenter">
<img src="https://igcdn-photos-c-a.akamaihd.net/hphotos-ak-xta1/t51.2885-19/11084950_1591056347778266_1536322251_a.jpg">
</div>
<div class="comment-text-area">
<textarea class="textinput" placeholder="Comment"></textarea>
</div>
</div>
</div>
you can give the class name form-control to the <textarea> like this:
<textarea class="form-control" rows="3" cols="90" ></textarea>
Reference: https://www.w3schools.com/bootstrap/bootstrap_forms_inputs.asp
How can I make this html structure
<div id="1"></div>
<div id="2"></div>
<div id="3"></div>
be displayed like this while div#1 and #2 have css float:left
( id names are integers only for demonstration purposes )
First of all, you will need to change the id's of your <div>'s to start with an alphabet rather than just one single digit since you won't be able to style your <div>'s using CSS then. Moreover, to achieve the sort of a layout which you're trying to create, you will need to wrap your two floated <div>'s inside a <div> and set the display property of that <div> to inline-block.
Here's a demo:
#one,
#two {
float: left;
}
#one {
display: block;
width: 200px;
height: 200px;
text-align: center;
color: white;
}
#two {
display: block;
width: 200px;
height: 200px;
text-align: center;
color: white;
}
#three {
display: block;
width: 200px;
height: 200px;
text-align: center;
color: white;
}
#one {
background: pink;
}
#two {
background: brown;
}
#three {
background: gray;
}
div#row-left {
display: inline-block;
width: 200px;
overflow: hidden;
vertical-align: top;
}
div#row-right {
display: inline-block;
width: 200px;
vertical-align: top;
}
<div id="row-left">
<div id="one">One</div>
<div id="two">Two</div>
</div>
<div id="row-right">
<div id="three">Three</div>
</div>
Edit: If you want to align the three boxes to the right side of the page then you will need to wrap your HTML inside another <div> and set the text-align property of that <div> to right, like this:
#wrapper {
text-align: right;
}
#one,
#two {
float: left;
}
#one {
display: block;
width: 200px;
height: 200px;
text-align: center;
color: white;
}
#two {
display: block;
width: 200px;
height: 200px;
text-align: center;
color: white;
}
#three {
display: block;
width: 200px;
height: 200px;
text-align: center;
color: white;
}
#one {
background: pink;
}
#two {
background: brown;
}
#three {
background: gray;
}
div#row-left {
display: inline-block;
width: 200px;
overflow: hidden;
vertical-align: top;
}
div#row-right {
display: inline-block;
width: 200px;
vertical-align: top;
}
<div id="wrapper">
<div id="row-left">
<div id="one">One</div>
<div id="two">Two</div>
</div>
<div id="row-right">
<div id="three">Three</div>
</div>
</div>
If you want to keep the given HTML structure, here's two different methods. One is working around the floats, the other is simply using absolute or relative positioning to force the third div into place.
HTML
<div id="d1">One</div>
<div id="d2">Two</div>
<div id="d3">Three</div>
CSS using inline-block (jsfiddle):
div {
width: 200px;
height: 200px;
margin: 10px;
}
#d1 {
float: left;
background-color: rgba(255,0,0,0.3);
}
#d2 {
float: left;
clear: left;
background-color: rgba(0,255,0,0.3);
}
#d3 {
background-color: rgba(0,0,255,0.3);
display: inline-block;
}
CSS using relative positioning (jsfiddle):
div {
width: 200px;
height: 200px;
margin: 10px;
}
#d1 {
float: left;
background-color: rgba(255,0,0,0.3);
}
#d2 {
float: left;
clear: left;
background-color: rgba(0,255,0,0.3);
}
#d3 {
background-color: rgba(0,0,255,0.3);
clear: both;
position: relative;
left: 220px;
top: -430px;
}
Fixed here - http://jsfiddle.net/3147og96/1/
html:
<div class="parent">
<div id="one">1</div>
<div id="two">2</div>
<div id="three">3</div>
</div>
css:
.parent {
height: auto;
width: 120px;
padding: 5px;
padding-left: 110px;
border: 1px solid red;
}
.parent div {
height: 50px;
width: 50px;
border: 1px solid red;
display: inline-block;
}
#one, #two {
float: left;
}
How to stretch parent div to fit children div?
I tried to add element with clear: both; after content but it didn't work for me.
HTML:
<div id="wrapper">
<div class="left-menu">
</div>
<div class="right-bar">
<div class="right-content">
<div class="content">
<div class="content-wrapper">
<div class="content-body">
Here is content
</div
</div>
</div>
</div>
</div>
</div>
CSS:
.left-menu {
background-color: #0B0C0E;
width: 20%;
height: 100%;
float: left;
}
.right-bar {
background-color: #F0F0F0;
height: 100%;
width: 100%;
}
.right-content {
float: left;
width: 80%;
}
.right-content > .content {
padding: 21px 0 0 42px;
}
.right-content > .content > .content-wrapper {
width: 98%;
height: 70%;
}
.right-content > .content .content-body {
background-color: #FAFAFA;
padding: 20px;
font-size: 24px;
border: 1px solid #D0D0D0;
}
sandbox for test: http://roonce.com/en/room/SwZuEJYB
Thanks in advance.
Use "clear-fix" technique. http://css-tricks.com/snippets/css/clear-fix/
This will allow the parent div to be the appropriate size of the floated elements within. Note this works specifically on #wrapper. (http://jsbin.com/huqehuta/1/edit)
.clear-fix:after {
content: "";
display: table;
clear: both;
}