This question already has answers here:
Make a perfect circle around a div of variable height
(4 answers)
How can I ensure that text is inside rounded div?
(7 answers)
Closed 1 year ago.
Hi guys i have created circle using CSS shape.
I have used contentEditable="true" so the content of the circle can be edited.
The problem here is now when I edit the text inisde the circle it is going outside of the circle. What I am trying to do is when I add extra text the text should automatically move to next line inside the circle itself.
I don't want to increase the width and height of the circle.
Can anyone help me where I did mistake.
Here is my code :
.circle {
width: 50px;
height: 50px;
background-color: #40a977;
border-radius: 50%;
float: left;
shape-outside: circle();
}
<div contentEditable="true" class="circle">circle</div>
Can anyone help me where i did mistake.Thanks in advance.
You've created a circle and defined the height and width. As a result, where the circle would expand to fit all the text in, it instead stays at 50px by 50px. You can fix this by adding CSS
min-width: 50px;
min-height: 50px;
to the class. This says that the circle has to be at least 50px by 50px, but may be larger depending on the text inside. I hope this answers what you were asking!
You can't affect the width of an element to the height in pure CSS.
With JS you can create an event on modifications and adjust the height or padding (for the text to keep centered) depending on width value.
With a DOM modification, you can access on the parent width in CSS. If not defined, the parent will take the with of the child.
JS, same DOM
function updateCircle(circle) {
let width = Number.parseInt(getComputedStyle(circle).width);
let lineHeight = Number.parseInt(getComputedStyle(circle).lineHeight);
circle.style.paddingTop = (width - lineHeight) / 2 + 'px';
circle.style.paddingBottom = (width - lineHeight) / 2 + 'px';
};
// Modify on edit
document.getElementById('circle').addEventListener('input', function(){
updateCircle(this);
});
// On page load
updateCircle(document.getElementById('circle'));
#circle {
padding: 0;
background-color: #40a977;
border-radius: 50%;
float: left;
font-size: 16px;
line-height: 16px;
}
<div contentEditable="true" id="circle">circle</div>
CSS, different DOM
#circle {
padding: 0;
float: left;
}
#circle > div{
height: 16px;
background-color: #40a977;
border-radius: 50%;
padding: calc(50% - 16px / 2) 0;
font-size: 16px;
line-height: 16px;
}
<div id="circle">
<div contentEditable="true">circle</div>
</div>
Related
This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
How can I vertically align elements in a div?
(28 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 2 years ago.
My issue
Usually when using percentages inside a margin property the browser uses the width of the nearest positioned ancestor as reference length. However I'd like to set the margins of my element relative to its parent's height. Is that somehow possible? Or am I stuck with a JavaScript solution?
An example
Specifically I'd like the following:
.child {
height: 1em;
width: 1em;
background: red;
display: inline-block;
margin: calc((100% - 1em) / 2);
/* margin: calc((5em - 1em) / 2); In absolute units */
/* margin-sizing: height; If only there was some magical property like this... */
}
.container {
background: green;
height: 5em;
width: 15em;
position: relative;
}
<div class="container">
<div class="child"></div>
Something
</div>
To look like this:
I know that I could use absolute units in the example above. However I may not always know the exact height of the parent in a more complex or dynamic example. And I'd like to avoid JavaScript for this.
Edit
I should clarify something. My end goal is that the red div in the example above is centered vertically but also has the same distance to the left as it has to the top/bottom. Obviously I'm not bound to specifically use the margin properties. But the outcome of your solution should be the same.
If you can just use margin or padding then you are stuck because the top and bottom of these properties are relative to the parent's width when used as percentages (as you mentioned already).
The only solution I see is keep the parent element in position: relative and use top & bottom to the child element (which is in position: absolute) to use in percentages.
.child {
height: 1em;
width: 1em;
background: red;
display: inline-block;
--calc: calc((100% - 1em) / 2);
top: var(--calc);
left: var(--calc);
right: var(--calc);
bottom: var(--calc);
position: absolute;
/* margin: calc((5em - 1em) / 2); In absolute units */
/* margin-sizing: height; If only there was some magical property like this... */
}
.container {
background: green;
height: 5em;
width: 15em;
position: relative;
}
<div class="container">
<div class="child"></div>
Something
</div>
I'm attempting to understand why in this codepen the two boxes aren't perfectly aligned.
https://codepen.io/mburke05/pen/BYXOGP
html
<div class="div_one">pixel</div>
<div class="div_two">percent</div>
css
.div_one {
border: solid red;
transform: translate(70px, 20%) ;
width: 140px;
height: 60px;
}
.div_two {
border: solid blue;
transform: translate(50%, 30%) ;
width: 140px;
height: 60px;
}
I thought I understood that, when using %'s rather than pixel or other values, that the % value was based on the height of the element itself rather than the % of the parent (which in this case would be the viewport.)
However, to achieve what I believe is alignment, I would need to set translate(48%, 30%) as the value. Why is this? Isn't 70 50% of 140, or is there more to it than I'm understanding.
As a follow-up, can anybody explain why this is the preferred way of centering an object vertically mathematically?
div {
box-sizing : border-box
}
By default in the CSS box model, 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.
Read More here
.div_one {
border: solid red;
width: 140px;
height: 60px;
}
.div_two {
border: solid blue;
width: 140px;
height: 60px;
}
remove CSS property "transform", Both Div will align perfectly and if you want to move the position of the box means use padding or margin and if you want to fix in box position then use Position property
I am new with css and im stuck
I have a parent div containing 3 imgs. When I shrink the imgs the parrent div keeps original size. It shrink does shrink with the imgs. How can I make sure that the div-element shrinks with img? Because i have a lot extra space after the last img.
I tried al the: block, inline-block, inline selectors.
I hope that one you can help me. Thank you for your time.
https://jsfiddle.net/7p479cfq/
<div class="logos">
<div class="dealer">Ekris</div>
<img src="assets/img/bmw-logo.png" alt="" class="bmw-logo">
<img src="assets/img/m-logo.png" alt="" class="m-logo">
<img src="assets/img/i-logo.png" alt="" class="i-logo">
</div>
div.logos {
display: block;
background: blue;
position: absolute;
top: 0;
right: 0;
}
.dealer {
display: inline-block;
font-size: 1.3em;
font-family: Arial, serif;
font-weight: bold;
}
img {
width: 20%;
height: 20%;
}
The problem is that you are setting the widths of the images but those percentages are calculated based on the parent. So, if you have your images set to 15% of the parent, then they are going to only take up 15%. For example, let's say the div is 300px, 15% of 300 is 45. You have three images. 3 * 45 = 135. Then, you have another div of an unspecified width that takes up some more space, but only as much as the text of it is. Let's say that for a short name, like you have, it only takes up 15px. So now you have 135 + 15 = 150. But your div is 300, so that leaves you with 150px of un-filled space.
It's not clear what you are looking to happen here, but, if you are looking to set your divs to take up an equal amount of whatever the parent is, you can just divide that equally. It's a little bit of a gamble if you don't set a width on the name div as well, but for arguments sake, let's say you want the four items to take up the same width: 100% / 4 = 25%.
I also added borders around the divs so you can see what is happening and left a commented out 40% so you could uncomment that to play around and get an idea of what is happening.
And because nothing is ever easy, because the images are inline there is some default spacing that comes with them because of font size. In the example I set the font-size of the parent to 0 to remove that as an unknown quantity and then just set the font-size on the div that actually has the text.
Again, it depends on what exactly you are looking for, but understanding what is going on might help you figure out how to achieve something else if this isn't what you had in mind.
https://jsfiddle.net/7p479cfq/1/
* {
box-sizing: border-box;
}
div.logos {
display: block;
background: blue;
position: absolute;
top: 0;
right: 0;
font-size: 0;
/* width: 40%; */
}
.dealer {
display: inline-block;
font-size: 1.3em;
font-family: Arial, serif;
font-weight: bold;
width: 25%;
font-size: 14px;
border: 1px solid red;
}
img {
width: 25%;
border: 1px solid red;
}
I think the problem here is an understanding of what's happening. Because you are specifying percentages on the width of the images, that percentage is a percentage of the parent element, not a percentage of the natural img size.
To solve this use pixel widths on the images and the space within their parent div disappears. Otherwise, if you want to fill the space then use percentages that add up to 100% - eg. 5 x images with 20% width each. Also, consider what width the text element will take up.
I tested this in Chrome. When I deleted width: 20%; from img, extra space disappeared.I'm not sure why it happpened. I hope this solves your problem
This question already has answers here:
How to draw circle in html page?
(19 answers)
Closed 6 years ago.
In CSS it is allowed to write something like this.
#div-with-border {
width: 100%; // scales with parent wrapper
height: 30%; // scales with parent wrapper
border: 1px solid black;
border-radius: 10%;
}
If #div-width-border isn't a perfect square the border won't be a circle, since this means, that 10% of the width and 10% of the height are used for the border-radius (which differ). I would like to get perfect circles... I can't use px, since the border-radius depends on the height/width.
I'm sure that the width of #div-width-border is always greater than the height, of the element. I would need a border radius of the size 100% of element height to get a perfect circle, but just 100% won't do it, since it'll use the element width for one part of the radius calculation.
If you know the ratio between the width and the height, you may use the Slash-Annotation to specify different %-values for horizontal and vertical border-radius. An Example is below:
.wrapper {
width: 300px;
height: 300px;
margin: 0 auto;
}
.div-with-border {
width: 100%;
height: 25%;
background-color: blue;
border: 1px solid black;
border-radius: 10% / 40%;
}
<div class="wrapper">
<div class="div-with-border"></div>
</div>
Have you tried border-radius: 50%; ? Since you're saying that the div is a perfect square, setting border radius to 50% should work
Here is the HTML code:
<div id="homepage_boxes_holder">
<div class="homepage_boxes">
<h3 class="box_heading">Test</h3>
</div>
<div class="homepage_boxes">
<h3 class="box_heading">Test</h3>
</div>
<div class="homepage_boxes">
<h3 class="box_heading">Test</h3>
</div>
</div>
Here is the CSS:
.homepage_boxes{
width: 300px;
height: 200px;
float: left;
margin-top: 100px;
margin-right: 80px;
margin-left: 150px;
line-height: 10;
}
.box_heading{
text-align:center;
font-family: BebasNeue;
font-size: 30px;
padding: 0;
margin: 0;
border: 1px solid black;
}
For some reason, the h3 is occupying a huge amount of space in the div (it looks as though the padding is huge to me but that can't be since I have set it to 0). I have put a border on the .box_heading for demonstrative purposes. Image is here:
Link to image: http://imgur.com/vDs1KYs -The blue is the div border, and the black is the H3 border.
EDIT: If possible, I would also like the heading to be centred on the div, rather than pushing outside the boundaries.
That's because you added a line-height: 10; to the parent element, which increases the height of each line 10 times.
Just remove that And it works.
Working fiddle
Update
To keep the h3 element at the middle of its parent, you could set a line-height as the parent's height to that element (in this case you could apply this CSS declaration to both parent and h3 element).
.homepage_boxes {
/* ... */
line-height: 200px; /* as the same of the parent's height */
}
Udpated Fiddle
Another option is setting the same padding to the top and bottom of the parent element without setting a fixed height, which makes the children at the middle.
JSFiddle Demo
just add line-height: 1em; to the .box-heading class description.
The line-height attribute alters the 'ascent' or 'ascender height' of the font, making it that high.
Just add this to the h3:
line-height: 1em;
http://jsfiddle.net/B7ZTw/
To center the text both ways please add the following to h3:
position: relative;
top: 50%;
margin-top: -0.5em;
What you do here is: set the h3 to be 50% from the top (so the middle), but then substract half of the element height so it doesn't start at 50% but is centered there.
See here: http://jsfiddle.net/B7ZTw/2/
You can put static height for if it possible