0

HTML

<canvas id="canvas" width="800" height="600">
</canvas>

<div class="content"></div>

CSS

div.content {
  :root {
    --xPos: canvas[id="canvas"].width/2;
}   
  position: absolute;
  left: var(--xPos);
  bottom: 100px;
}

I tried using an attribute selector to get the canvas.id.width value so I can set the left margin edge property for div.content to the canvas' width/2. However, it seems like attribute selectors are used to style elements with specific attributes/values rather than to return an attribute value.

is there a way(function/method) to use a HTML attribute value as a property value within CSS? and then use it in expressions like in JS such as attrValue*2 or attrValue/2.

Thanks.

1 Answer 1

1

This is not yet widely supported in most browsers, see attr()

If its just a layout issue in different viewports, try to use CSS calc() and/or Media Queries

If its some behavior you want, try using JavaScript for that. With javascript, you could actually create the styles dynamically with whatever values you want, see Set CSS styles with javascript

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.